-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.rs
140 lines (136 loc) · 3.2 KB
/
utils.rs
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
//! # Code containing expressions used in `crablit` regularly.
use owo_colors::OwoColorize;
use std::fmt;
/// space before any output
pub const SPCR: &str = " ";
/// Knew it text.
pub fn knew_msg() -> String {
format!(
"{SPCR}{} {}",
"$".bright_green().bold(),
"Yes, that's right!\n".bright_green()
)
}
/// Revising text.
pub fn revise_msg() -> String {
format!(
"{SPCR}{}",
"Going to the ones not guessed correctly...".bright_magenta()
)
}
/// Typo text.
pub fn typo_msg(s: &str) -> String {
format!(
"{}{} {s}",
SPCR.repeat(2),
"Corrected:".bright_magenta().italic(),
)
}
/// Exiting text.
pub fn exit_msg() -> String {
format!("\n{SPCR}{}", "Exiting...".bright_magenta().italic())
}
/// To go text.
pub fn togo_msg(sum: usize, i: usize) -> String {
format!(
"{}{} at {:.1}{}, {} more to go",
SPCR.repeat(2),
"!".bold().bright_purple(),
(i as f32 / sum as f32 * 100.),
"%".bold().bright_purple(),
(sum + 1 - i).to_string().italic()
)
}
#[derive(Debug, Clone, PartialEq)]
/// Level Of Knowledge
pub enum Lok {
/// has not seen it yet
Nothing,
/// got once right
Something,
/// got twice rigth
Almost,
/// got 3 times right: done
Done,
}
impl Lok {
/// Creates a new [`Lok`].
///
/// # Examples
///
/// ```
/// use crablit::Lok;
///
/// assert_eq!(Lok::new("1"), Lok::Something);
/// ```
pub fn new(s: &str) -> Self {
let s = s.trim();
if s == "Nothing" || s == "0" {
Self::Nothing
} else if s == "Something" || s == "1" {
Self::Something
} else if s == "Almost" || s == "2" {
Self::Almost
} else if s == "Done" || s == "3" {
Self::Done
} else {
Self::default()
}
}
/// Increment this [`Lok`].
///
/// # Examples
///
/// ```
/// use crablit::Lok;
///
/// let mut lok = Lok::default();
/// lok.incr();
/// assert_eq!(lok, Lok::Something);
/// ```
pub fn incr(&mut self) {
*self = match *self {
Self::Nothing => Self::Something,
Self::Something => Self::Almost,
Self::Almost | Self::Done => Self::Done,
}
}
/// Decrement this [`Lok`].
///
/// # Examples
///
/// ```
/// use crablit::utils::Lok;
///
/// let mut lok = Lok::Almost;
/// lok.decr();
/// assert_eq!(lok, Lok::Something);
/// ```
pub fn decr(&mut self) {
*self = match *self {
Self::Nothing | Self::Something => Self::Nothing,
Self::Almost => Self::Something,
Self::Done => Self::Almost,
}
}
}
impl Default for Lok {
fn default() -> Self {
Self::Nothing
}
}
impl fmt::Display for Lok {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Self::Nothing => "Nothing",
Self::Something => "Something",
Self::Almost => "Almost",
Self::Done => "Done",
}
)?;
Ok(())
}
}