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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/// 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
}
}
/// A Linear algebra module, dealing with vectors/matrices
/// Only square matrices allowed currently
/// A Linear algebra module, dealing with vectors/matrices
pub mod linear_algebra {
/// Deal with two dimensional vectors stored as `[f32;2]`
pub mod two_dimensions {
pub fn add([x1, y1]: [f32; 2], [x2, y2]: [f32; 2]) -> [f32; 2] {
[x1 + x2, y1 + y2]
}
pub fn sub([x1, y1]: [f32; 2], [x2, y2]: [f32; 2]) -> [f32; 2] {
[x1 - x2, y1 - y2]
}
pub fn mul([x, y]: [f32; 2], scalar: f32) -> [f32; 2] {
[x * scalar, y * scalar]
}
pub fn len([x, y]: [f32; 2]) -> f32 {
(x * x + y * y).sqrt()
}
/// Dot Product
pub fn dot([x1, y1]: [f32; 2], [x2, y2]: [f32; 2]) -> f32 {
x1 * x2 + y1 * y2
}
/// Limited 2d cross product
pub fn cross([x1, y1]: [f32; 2], [x2, y2]: [f32; 2]) -> f32 {
x1 * y2 - y1 * x2
}
/// Normalize the vector
pub fn norm([x, y]: [f32; 2]) -> [f32; 2] {
let len = len([x, y]);
[x / len, y / len]
}
/// Deals with matrices that are `[[f32;2];2]`
/// Where they look like `[[a,b],[c,d]]` when used in computations, where the matrix representation is
/// ```
/// | a b |
/// | c d |
pub mod matrices {
/// Multiplies the vector `v` with the matrix `m`
pub fn transform([x, y]: [f32; 2], [[a, b], [c, d]]: [[f32; 2]; 2]) -> [f32; 2] {
let x = x * a + y * b;
let y = x * c + y * d;
[x, y]
}
/// Multiplies two matrices
pub fn mul(
[[a1, b1], [c1, d1]]: [[f32; 2]; 2],
[[a2, b2], [c2, d2]]: [[f32; 2]; 2],
) -> [[f32; 2]; 2] {
let m1 = [[a1, b1], [c1, d1]];
let m2 = [[a2, b2], [c2, d2]];
let mut result = [[0.0; 2]; 2];
for i in 0..2 {
for j in 0..2 {
for k in 0..2 {
result[i][j] += m1[i][k] * m2[k][j];
}
}
}
result
}
/// Determinant of a matrix
pub fn det([[a, b], [c, d]]: [[f32; 2]; 2]) -> f32 {
a * d - c * b
}
}
}
/// 3d vectors stored as `[f32;3]`
pub mod three_dimensions {
pub fn add([x1, y1, z1]: [f32; 3], [x2, y2, z2]: [f32; 3]) -> [f32; 3] {
return [x1 + x2, y1 + y2, z1 + z2];
}
pub fn sub([x1, y1, z1]: [f32; 3], [x2, y2, z2]: [f32; 3]) -> [f32; 3] {
return [x1 - x2, y1 - y2, z1 - z2];
}
pub fn mul([x, y, z]: [f32; 3], scalar: f32) -> [f32; 3] {
[x * scalar, y * scalar, z * scalar]
}
pub fn len([x, y, z]: [f32; 3]) -> f32 {
(x * x + y * y + z * z).sqrt()
}
/// Normalize the vector
pub fn norm([x, y, z]: [f32; 3]) -> [f32; 3] {
let len = len([x, y, z]);
[x / len, y / len, z / len]
}
/// Dot Product
pub fn dot([x1, y1, z1]: [f32; 3], [x2, y2, z2]: [f32; 3]) -> f32 {
x1 * x2 + y1 * y2 + z1 * z2
}
/// Full 3D cross product
///
/// | i x1 x2 |
/// | j y1 y2 |
/// | k z1 z2 |
pub fn cross([x1, y1, z1]: [f32; 3], [x2, y2, z2]: [f32; 3]) -> [f32; 3] {
[y1 * z2 - y2 * z1, z1 * x2 - z2 * x1, x1 * y2 - x2 * y1]
}
}
}