ragout_assistant 0.1.1

Internal/backend crate for the ragout crate
Documentation
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
use std::io::{StdoutLock, Write};

use crossterm::terminal::enable_raw_mode;

// raw mode:
// you need to create exetrns for C functions from unistd.h
// Specifically to enable raw mode you need tcgetattr and tcsetattr functions.

///
/// Enables terminal raw mode and initializes the necessary variables for behaving in the raw mode.
///
/// Takes a [`&str`] for the shell prompt (give "" for no prompt) and a bool for the option of running in the terminal alternate screen (give true to run your cli program in alternate screen)
/// # Errors
/// Does NOT return errors and never panics
///
/// # Example
///
/// Basic usage
///
/// ```
/// use ragout::{init, run};
///
/// fn main() {
///     // enter raw mode and initialize necessary variables
///     // the string literal argument will be the value of the prompt
///     let (mut sol, mut i, mut h, mut ui) = init("some prompt 🐱 ", true);
///
///     'main: loop {
///         let input = run(&mut i, &mut h, &mut sol, &mut ui);
///         if !input.is_empty() {
///             // do some stuff with the user input
///         }
///     }
/// }
/// ```
pub fn init(
    prompt: &str,
    alt_screen: bool,
) -> (std::io::StdoutLock<'static>, Input, History, String) {
    _ = enable_raw_mode();

    let mut sol = std::io::stdout().lock();

    if alt_screen {
        _ = sol.write(b"\x1b[?1049h");
        _ = sol.write(b"\x1b[1;1f");
    }

    let i = Input::new(prompt, alt_screen);
    i.write_prompt(&mut sol);

    (sol, i, History::new(), String::new())
}

/// A struct that implements the user input movement and deletion logic inside the terminal raw
/// mode
#[derive(Debug)]
pub struct Input {
    pub values: Vec<char>,
    pub cursor: usize,
    #[cfg(any(debug_assertions, feature = "debug_logs"))]
    pub debug_log: std::fs::File,
    pub prompt: String,
    pub alt_screen: bool,
}

impl Input {
    /// Creates a new Input instance
    pub fn new(prompt: &str, alt_screen: bool) -> Self {
        Self {
            #[cfg(any(debug_assertions, feature = "debug_logs"))]
            debug_log: std::fs::File::create("resources/logs/terminal/input").unwrap_or_else(
                |_| {
                    std::fs::create_dir_all("resources/logs/terminal").unwrap();
                    std::fs::File::create("resources/logs/terminal/input").unwrap()
                },
            ),
            values: Vec::new(),
            cursor: 0,
            prompt: prompt.to_owned(),
            alt_screen,
        }
    }

    // NOTE: should input.values not be a byte vec instead of a char vec?
    /// Adds inputted char to Input values at cursor position then increments Input cursor
    pub fn put_char(&mut self, c: char) {
        match self.values.is_empty() {
            true => {
                self.values.push(c);
                self.cursor += 1;
            }
            false => match self.cursor == self.values.len() {
                true => {
                    self.values.push(c);
                    self.cursor += 1;
                }

                false => {
                    self.values.insert(self.cursor, c);
                    self.cursor += 1;
                }
            },
        }
    }

    // TODO: multiline input
    // WARN: do NOT touch this Input implementation
    // the fns other than write are not to be touched

    /// Pushs Input values to history, then binds a [`String`] of the Input values to user_input and resets both Input cursor and values
    pub fn cr_lf(&mut self, h: &mut History, user_input: &mut String) {
        h.push(self.values.to_vec());
        *user_input = self.values.drain(..).collect::<String>();
        self.cursor = 0;
    }

    /// Deletes the char behind the cursor position in the Input values
    pub fn backspace(&mut self) {
        if self.values.is_empty() || self.cursor == 0 {
            return;
        }
        if self.cursor > 0 {
            self.values.remove(self.cursor - 1);
            self.cursor -= 1;
        }
    }

    /// Moves the Input cursor one cell to the right
    pub fn to_the_right(&mut self) -> bool {
        if self.values.is_empty() || self.cursor == self.values.len() {
            return false;
        }
        self.cursor += 1;

        true
    }

    /// Moves the Input cursor one cell to the left
    pub fn to_the_left(&mut self) -> bool {
        if self.values.is_empty() || self.cursor == 0 {
            return false;
        }
        self.cursor -= 1;

        true
    }

    /// Moves Input cursor to the position after the last in Input values (which is values.len())
    pub fn to_end(&mut self) -> usize {
        let diff = self.values.len() - self.cursor;
        if diff > 0 {
            self.cursor = self.values.len();
        }

        diff
    }

    /// Moves Input cursor to the first position in Input values (which is 0)
    pub fn to_home(&mut self) -> bool {
        if self.cursor == 0 {
            return false;
        }
        self.cursor = 0;

        true
    }

    /// Clears all the Input values
    pub fn clear_line(&mut self) {
        self.cursor = 0;
        self.values.clear();
    }

    /// clears the values of Input to the right of Input cursor
    pub fn clear_right(&mut self) {
        for _ in self.cursor..self.values.len() {
            self.values.pop();
        }
    }

    /// clears the values of Input to the left of Input cursor
    pub fn clear_left(&mut self) {
        for _ in 0..self.cursor {
            self.values.remove(0);
        }
        self.cursor = 0;
    }

    const STOPPERS: [char; 11] = ['/', ' ', '-', '_', ',', '"', '\'', ';', ':', '.', ','];

    /// Syncs Input's internal state to a movement of the user input cursor to the right, stops at the first stopper char
    pub fn to_right_jump(&mut self) {
        if self.cursor == self.values.len() {
            return;
        }

        match self.values[if self.cursor + 1 < self.values.len() {
            self.cursor + 1
        } else {
            self.cursor
        }] == ' '
        {
            true => {
                while self.cursor + 1 < self.values.len() && self.values[self.cursor + 1] == ' ' {
                    self.cursor += 1;
                }
            }
            false => {
                while self.cursor + 1 < self.values.len()
                    && !Self::STOPPERS.contains(&self.values[self.cursor + 1])
                {
                    self.cursor += 1;
                }
                self.cursor += 1;
            }
        }
    }

    /// Syncs Input's internal state to a movement of the user input cursor to the left, stops at the first stopper char
    pub fn to_left_jump(&mut self) {
        if self.cursor == 0 {
            return;
        }

        match self.values[self.cursor - 1] == ' ' {
            true => {
                while self.cursor > 0 && self.values[self.cursor - 1] == ' ' {
                    self.cursor -= 1;
                }
            }
            false => {
                while self.cursor > 1 && !Self::STOPPERS.contains(&self.values[self.cursor - 1]) {
                    self.cursor -= 1;
                }
                self.cursor -= 1;
            }
        }
    }
}

// NOTE: the cursor in both input and history does not point to the item it's on,
// but is alawys pointing at the item to the left
// basically cursor = 0 points at nothing and cursor = 4 points at eg. input[3]
// this logic is implemented in the functionality

#[derive(Debug)]
pub struct History {
    #[cfg(any(debug_assertions, feature = "debug_logs"))]
    pub debug_log: std::fs::File,
    pub values: Vec<Vec<char>>,
    pub cursor: usize,
    pub temp: Option<Vec<char>>,
}

impl History {
    /// Creates a new History instance
    pub fn new() -> Self {
        Self {
            #[cfg(any(debug_assertions, feature = "debug_logs"))]
            debug_log: std::fs::File::create("resources/logs/terminal/history").unwrap_or_else(
                |_| {
                    std::fs::create_dir_all("resources/logs/terminal").unwrap();
                    std::fs::File::create("resources/logs/terminal/history").unwrap()
                },
            ),
            values: Vec::new(),
            cursor: 0,
            temp: None,
        }
    }

    /// Binds the value of the previous history entry to the value variable and moves back the
    /// History cursor by one
    pub fn prev(&mut self, value: &mut Vec<char>) -> bool {
        if self.cursor == 0 {
            return false;
        }

        if self.temp.is_none() || self.cursor == self.values.len() {
            self.temp = Some(value.clone()); // temporarily keep input val
        }

        *value = self.values[self.cursor - 1].clone();
        self.cursor -= 1;

        true
    }

    /// Binds the value of the next history entry to the value variable and moves forward the
    /// History cursor by one
    pub fn next(&mut self, value: &mut Vec<char>) -> bool {
        if self.cursor == self.values.len() {
            return false;
        }

        if self.cursor + 1 == self.values.len() {
            *value = self.temp.as_ref().unwrap().clone();
        } else {
            *value = self.values[self.cursor + 1].clone();
        }
        self.cursor += 1;

        true
    }

    /// Pushs a new history entry into the History.values
    pub fn push(&mut self, value: Vec<char>) {
        if value.iter().filter(|c| **c != ' ').count() > 0 && !self.values.contains(&value) {
            self.values.push(value);
        }
        self.temp = None;
        self.cursor = self.values.len();
    }
}

#[cfg(test)]
mod test_input {
    use super::{History, Input};

    #[test]
    fn test_put_char() {
        let mut i = Input::new("testing input> ", false);

        let mut idx = 0;
        ['p', 'i', 'k', 'a'].into_iter().for_each(|c| {
            i.put_char(c);
            idx += 1;

            assert_eq!(i.values[i.cursor - 1], c);
            assert_eq!(idx, i.cursor);
        })
    }

    #[test]
    fn test_backspace() {
        let mut i = Input::new("testing input> ", false);

        let input = "pikatchino";
        input.chars().into_iter().for_each(|c| i.put_char(c));

        i.backspace();

        assert!({ i.cursor == input.len() - 1 && i.values[i.cursor - 1] == 'n' });
    }

    #[test]
    fn test_to_end() {
        let mut i = Input::new("testing input> ", false);

        "pikatchaa".chars().into_iter().for_each(|c| i.put_char(c));
        // cursor is by default at end, but we still move it to end
        i.to_end();

        assert!({ i.cursor == 9 && i.values[i.cursor - 1] == 'a' });

        // now we test moving to end from somewhere else
        i.to_the_left();
        i.to_the_left();
        i.to_end();

        assert!({ i.cursor == 9 && i.values[i.cursor - 1] == 'a' });

        // and finally, moving to end from home (first cell in line)
        i.to_home();
        i.to_end();

        assert!({ i.cursor == 9 && i.values[i.cursor - 1] == 'a' });
    }

    #[test]
    fn test_to_home() {
        let mut i = Input::new("testing input> ", false);

        "pikatchuu".chars().into_iter().for_each(|c| i.put_char(c));
        i.to_home();

        assert!({ i.cursor == 0 && i.values[i.cursor] == 'p' });
    }

    #[test]
    fn test_to_the_right() {
        let mut i = Input::new("testing input> ", false);

        "pikatchau".chars().into_iter().for_each(|c| i.put_char(c));
        i.to_the_left();
        i.to_the_left();

        assert_eq!(i.values[i.cursor - 1], 'h');
        assert_eq!(i.cursor, "pikatchau".len() - 2);
    }

    #[test]
    fn test_to_the_left() {
        let mut i = Input::new("testing input> ", false);

        "pikatchau".chars().into_iter().for_each(|c| i.put_char(c));
        i.to_home();
        i.to_the_right();
        i.to_the_right();

        assert_eq!(i.values[i.cursor], 'k');
        assert_eq!(i.cursor, 2);
    }

    #[test]
    fn test_cr_lf() {
        let mut i = Input::new("testing input> ", false);
        let mut h = History::new();
        let mut user_input = String::new();

        "pikatcharu".chars().into_iter().for_each(|c| i.put_char(c));

        i.cr_lf(&mut h, &mut user_input);

        assert_eq!(
            h.values[0],
            "pikatcharu".chars().into_iter().collect::<Vec<char>>()
        );
        assert!(i.values.is_empty());
        assert_eq!(i.cursor, 0);
    }

    #[test]
    fn test_clear_line() {
        let mut i = Input::new("testing input> ", false);

        "pikauchi".chars().into_iter().for_each(|c| i.put_char(c));

        assert!({ i.cursor == "pikauchi".len() && i.values[i.cursor - 1] == 'i' });

        i.clear_line();
        assert!(i.values.is_empty());
        assert_eq!(i.cursor, 0);
    }

    #[test]
    fn test_clear_right() {
        let mut i = Input::new("testing input> ", false);

        "pikatchiatto"
            .chars()
            .into_iter()
            .for_each(|c| i.put_char(c));
        (0..4).for_each(|_| {
            i.to_the_left();
        });

        i.clear_right();
        assert_eq!(i.values.iter().map(|c| *c).collect::<String>(), "pikatchi");
    }

    #[test]
    fn test_clear_left() {
        let mut i = Input::new("testing input> ", false);

        "pikatchiatto"
            .chars()
            .into_iter()
            .for_each(|c| i.put_char(c));
        (0..4).for_each(|_| {
            i.to_the_left();
        });

        i.clear_left();
        assert_eq!(i.values.iter().map(|c| *c).collect::<String>(), "atto");
    }
}

impl Input {
    /// Changes the Input prompt value to the provided string
    pub fn overwrite_prompt(&mut self, new_prompt: &str) {
        self.prompt.clear();
        self.prompt.push_str(new_prompt);
    }

    /// Renders the Input prompt followed by the Input values on a clean line
    pub fn write_prompt(&self, sol: &mut StdoutLock) {
        _ = sol.write(b"\x1b[2K");
        _ = sol.write(&[13]);
        _ = sol.write(&str_to_bytes(&self.prompt));
        _ = sol.write(&str_to_bytes(&self.as_str(&mut "".to_string())));
        _ = sol.flush();
    }

    /// Syncs the user input cursor displayed in the terminal to the cursor of Input
    pub fn sync_cursor(&self, sol: &mut StdoutLock) {
        _ = sol.write(&[13]);
        // BUG: at every first inputted char of an input line, the cursor was moving forward
        // by the sum of the byte lengths of all non-ascii chars in the prompt
        // this is because prompt(String).len() was counting the byte lengths of the chars not the
        // number of the chars
        // FIX: switch to prompt.chars.count() from prompt.len()
        for _idx in 0..self.prompt.chars().count() + 1 + self.cursor {
            _ = sol.write(b"\x1b[C");
        }
    }

    // pub fn toggle_alt_screen(&mut self, sol: &mut StdoutLock) {
    //     match self.alt_screen {
    //         true => {
    //             _ = sol.write(b"\x1b[?1049l");
    //         }
    //         false => {
    //             _ = sol.write(b"\x1b[?1049h");
    //         }
    //     }
    //
    //     self.alt_screen = !self.alt_screen;
    // }
    fn as_str<'a>(&self, s: &'a mut String) -> &'a str {
        *s = self.values.iter().map(|c| c).collect::<String>();

        s.as_str()
    }
}

fn encode_char(c: char, bytes: &mut Vec<u8>) {
    match c.is_ascii() {
        false => bytes.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes()),
        true => bytes.push(c as u8),
    }
}

fn str_to_bytes(s: &str) -> Vec<u8> {
    let mut bytes = Vec::new();
    s.chars()
        .into_iter()
        .for_each(|c| encode_char(c, &mut bytes));

    bytes
}