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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/// Module containing terminal tools
pub mod terminal {
/// Creates a `term` as seen in the `console` crate
pub fn new_term() -> console::Term {
console::Term::stdout()
}
/// Clears the screen using the standard library's `std::process::Command`
pub fn clear() {
let _ = std::process::Command::new("clear").status();
}
/// Writes a displayable object to the screen.
///
/// # Arguments
///
/// * `term` - a reference to a `console::Term`, can be generated by `vaz_lib::terminal::new_term()`
/// * `item` - a reference to an object that has the `std::fmt::Display` trait
/// # Examples
/// ```rust
/// use vaz_lib::terminal;
/// let t = terminal::new_term();
/// let output = "Hello, World!\n";
/// termina::write(&t, &output);
/// ```
pub fn write(term: &console::Term, item: &dyn std::fmt::Display) {
let _ = term.write_str(format!("{}", item).as_str());
}
/// Gets a character from the console input without echo
///
/// # Arguments
///
/// * `term` - a reference to a `console::Term`, can be generated by `vaz_lib::terminal::new_term()`
///
pub fn getch(term: &console::Term) -> char {
term.read_char().unwrap()
}
/// Gets a character from the console input with echo
///
/// When `ch` is `None`, it echoes the character it read, when it is `Some(c)` it echoes `c`
/// # Arguments
///
/// * `term` - a reference to a `console::Term`, can be generated by `vaz_lib::terminal::new_term()`
/// * `ch` - a `Option<char>`
///
pub fn getche(term: &console::Term, ch: Option<char>) -> char {
let new_ch = term.read_char().unwrap();
let _ = match ch {
Some(c) => {
if new_ch != '\n' {
term.write_str(c.to_string().as_str())
} else {
term.write_str(" ".to_string().as_str())
}
}
None => term.write_str(new_ch.to_string().as_str()),
};
return new_ch;
}
/// Gets a vector of characters from the console input with echo
///
/// When `ch` is `None`, it echoes the character it read, when it is `Some(c)` it echoes `c`
/// The vector is of size `n`
/// # Arguments
///
/// * `term` - a reference to a `console::Term`, can be generated by `vaz_lib::terminal::new_term()`
/// * `ch` - a `Option<char>`
/// * `n` - a `u8` the determines how many characters are read
///
pub fn bulk_getche(term: &console::Term, ch: Option<char>, n: u8) -> Vec<char> {
let mut chars: Vec<char> = Vec::new();
for _ in 0..n {
let x = getche(term, ch);
if x == '\n' {
break;
} else {
chars.push(x);
}
}
chars
}
/// Gets a key input from the user, halts the rest of the program
///
///
/// # Arguments
///
/// * `term` - a reference to a `console::Term`, can be generated by `vaz_lib::terminal::new_term()`
///
pub fn getk(term: &console::Term) -> console::Key {
term.read_key().unwrap()
}
/// Gets a string of characters from the console input with echo
///
/// When `ch` is `None`, it echoes the character it read, when it is `Some(c)` it echoes `c`
/// The string is of size `n`
/// # Arguments
///
/// * `term` - a reference to a `console::Term`, can be generated by `vaz_lib::terminal::new_term()`
/// * `ch` - a `Option<char>`
/// * `n` - a `u8` the determines how many characters are read
///
pub fn get_string(term: &console::Term, ch: Option<char>, n: u8) -> String {
use console::Key;
let mut charlist: Vec<char> = Vec::new();
let mut index: u8 = 0;
while index < n + 1 {
let x = getk(term);
match x {
Key::Enter => break,
Key::Char(s) => {
if index == n {
} else if index == charlist.len() as u8 {
charlist.push(s);
let _ = term.write_str(
format!(
"{}",
match ch {
Some(c) => c,
None => s,
}
)
.as_str(),
);
index += 1;
} else {
charlist[index as usize] = s;
let _ = term.write_str(
format!(
"{}",
match ch {
Some(c) => c,
None => s,
}
)
.as_str(),
);
index += 1;
}
}
Key::ArrowLeft => {
if index != 0 {
let _ = term.write_str("\x1b[D");
index -= 1;
}
}
Key::ArrowRight => {
if index != charlist.len() as u8 {
let _ = term.write_str("\x1b[C");
index += 1;
}
}
Key::Backspace => {
if index == charlist.len() as u8 && index != 0 {
charlist.pop();
let _ = term.write_str("\x1b[D \x1b[D");
index -= 1;
}
}
_ => (),
}
}
charlist.iter().fold(String::new(), |x, y| {
let mut z = x;
z.push(*y);
z
})
}
///Creates a terminal menu that the user can scroll through using the arrow keys.
/// It outputs the index of the element the user chose
///
/// # Arguments
///
/// * `term` - a reference
/// * `items` - a vector of items that have the `std::fmt::Display` trait
pub fn menu(term: &console::Term, items: Vec<&dyn core::fmt::Display>) -> u32 {
use console::Key;
let mut i = 0;
let mut new_i = 0;
for item in &items {
let _ = term.write_str(format!("{}\n", item).as_str());
}
for _ in &items {
let _ = term.write_str("\x1b[A\r");
}
loop {
let _ = term.write_str(
format!(
"\r\x1b[2K\x1b[38;2;0;0;0m\x1b[48;2;255;255;255m\
{}\
\x1b[m\x1b[?25l\
",
items[i]
)
.as_str(),
);
let key = getk(term);
match key {
Key::Enter => {
break;
}
Key::ArrowUp => {
if i != 0 {
new_i -= 1;
}
}
Key::ArrowDown => {
if i < items.len() - 1 {
new_i += 1;
}
}
_ => (),
}
if new_i != i {
let _ = term.write_str("\x1b[2K\r");
let _ = term.write_str(format!("{}", items[i]).as_str());
for _ in i..items.len() {
let _ = term.write_str("\x1b[B");
}
for _ in new_i..items.len() {
let _ = term.write_str("\x1b[A");
}
}
i = new_i;
}
for _ in 0..(items.len() - i) {
let _ = term.write_str("\x1b[B\x1b[2K\r");
}
for _ in &items {
let _ = term.write_str("\x1b[A\x1b[2K\r");
}
let _ = term.write_str("\x1b[?25h");
i as u32
}
}