-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscramble.hpp
More file actions
134 lines (114 loc) · 3.48 KB
/
scramble.hpp
File metadata and controls
134 lines (114 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
#include <string>
#include <thread>
#include <vector>
namespace Scramble {
namespace {
class TransitionData {
public:
std::string from;
std::string to;
int start;
int end;
std::string cr;
TransitionData(std::string &&from, std::string &&to, int &&start, int &&end,
std::string &&cr)
: from(from), to(to), start(start), end(end), cr(cr) {}
};
const std::string CLEARLINE = "\33[2K\r";
const std::string chars = "!<>-_\\/[]{}|=+*^?#()$%@&;:~";
std::string curText = "";
std::vector<TransitionData> queue;
int frame = 0;
uint printDelay = 100;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distrrc(0, chars.length());
std::uniform_int_distribution<> distr40(0, 40 + 1);
std::uniform_int_distribution<> distr99(0, 100);
inline std::string randomChar() { return chars.substr(distrrc(gen), 1); }
inline void displayString(const std::string &newText) {
if (newText.length() == 0) {
return;
}
std::string oldText = curText;
std::cout << CLEARLINE << newText
<< std::flush; // clears current line and prints new text
std::this_thread::sleep_for(std::chrono::milliseconds(printDelay));
curText = newText;
}
inline void update() {
std::string output = "";
int complete = 0;
for (int i = 0, n = queue.size(); i < n; i++) {
auto transitionData = queue[i];
if (frame >= transitionData.end) {
complete++;
output += transitionData.to;
} else if (frame >= transitionData.start) {
if (transitionData.cr == "" || distr99(gen) < chars.length()) {
std::string ranChar = randomChar();
queue[i].cr = ranChar;
}
output += queue[i].cr;
} else {
output += transitionData.from;
}
}
displayString(output);
if (complete == queue.size()) {
return;
} else {
frame++;
update();
}
}
inline void setText(const std::string &newText) {
std::string oldText = curText;
int length = std::max(oldText.length(), newText.length());
queue.clear();
for (int i = 0; i < length; i++) {
queue.emplace_back(i < oldText.length() ? oldText.substr(i, 1) : "",
i < newText.length() ? newText.substr(i, 1) : "",
distr40(gen), distr40(gen), "");
}
frame = 0;
update();
}
inline bool hasWhitespace(const std::string &str) {
for (char ch : str) {
if (ch != ' ' && std::isspace(ch)) {
return true;
}
}
return false;
}
} // namespace
inline void scramble(const std::vector<std::string> &texts,
bool clearAll = true, uint transitionDelay = 100,
uint textDelay = 1000) {
printDelay = transitionDelay;
for (auto text : texts) {
if (hasWhitespace(text)) {
std::cerr
<< "String: \"" << text
<< "\" contains a white space. No white spaces other than "
"space ' ' are allowed."
<< std::endl;
std::exit(1);
}
}
for (auto text : texts) {
setText(text);
std::fflush(stdout);
std::this_thread::sleep_for(std::chrono::milliseconds(textDelay));
if (clearAll)
std::cout << CLEARLINE;
}
}
} // namespace Scramble