jag

typing_rust

public

A small typing test program in Rust

Star0
Watch0
002c547ac61d949dc882d687ba3abed5431dfa30
1
ssh://git@git.jaglekon.de:30/jag/typing_rust.git
src/text_combine.rsRust105 lines2.1 KB
1
use rand::prelude::*;
2
3
#[derive(Debug)]
4
pub struct TextCombine {
5
    text: String,
6
    len: usize,
7
    pos: usize,
8
    err_n: usize,
9
    err_s: String,
10
}
11
12
impl TextCombine {
13
    pub fn new() -> Self {
14
        let words: Vec<String> = include_str!("../assets/wordlist.txt")
15
            .lines()
16
            .map(str::to_owned)
17
            .collect();
18
19
20
        /*let text = words
21
                    .choose_multiple(&mut rand::rng(), 10)
22
                    .cloned()
23
                    .collect::<Vec<_>>()
24
                    .join(" ");*/
25
        let mut rng = rand::rng();
26
27
        let text = (0..60)
28
            .filter_map(|_| words.choose(&mut rng))
29
            .cloned()
30
            .collect::<Vec<_>>()
31
            .join(" ");
32
33
        let len = text.chars().count();
34
35
        Self {
36
            text,
37
            len,
38
            pos: 0,
39
            err_n: 0,
40
            err_s: String::new(),
41
        }
42
43
    }
44
45
    pub fn get_text(&self) -> &str {
46
        &self.text
47
    }
48
49
    pub fn get_pos(&self) -> usize {
50
        self.pos
51
    }
52
53
    pub fn pos_increment(&mut self) {
54
        if self.pos < self.len {
55
            self.pos += 1;
56
        }
57
    }
58
59
    pub fn err_n_increment(&mut self) {
60
        if self.real_pos() < self.len {
61
            self.err_n += 1;
62
        }
63
    }
64
65
    pub fn get_err_n(&self) -> usize {
66
        self.err_n
67
    }
68
69
    pub fn get_err_s(&self) -> &str {
70
        &self.err_s
71
    }
72
73
    pub fn push_err_s(&mut self, c: char) {
74
        self.err_s.push(c);
75
    }
76
77
    pub fn get_len(&self) -> usize {
78
        self.len
79
    }
80
81
    pub fn curr_char(&self) -> Option<char> {
82
        self.pos_char(self.real_pos())
83
    }
84
85
    pub fn pos_char(&self, pos: usize) -> Option<char> {
86
        self
87
            .text
88
            .chars()
89
            .nth(pos) 
90
    }
91
92
    pub fn real_pos(&self) -> usize {
93
        self.pos + self.err_n
94
    }
95
96
    pub fn back(&mut self) {
97
        if self.err_s.len() > 0 {
98
            self.err_s.pop() ;
99
        } else if self.err_n > 0 {
100
            self.err_n -= 1;
101
        } else if self.pos > 0 {
102
            self.pos -= 1;
103
        }
104
    }
105
}