/
main.rs
128 lines (109 loc) · 3.48 KB
/
main.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
mod literals;
mod macros;
use colored::Colorize;
use literals::*;
use std::collections::{HashSet, VecDeque};
use std::io::{self, BufRead, Write};
use std::{process, fs, env};
const WORD_HISTORY_BUFFER_LENGTH: usize = 10;
fn main() {
let stdin = io::stdin();
let mut words: HashSet<String> = HashSet::new();
let mut last_words_buffer: VecDeque<String> = VecDeque::new();
let mut results_filname: Option<String> = None;
let mut args = env::args().skip(1);
while let Some(token) = args.next() {
if token == "--save" {
let filename = args.next().unwrap_or_else(|| {
err!(ERR_MISSING_FILENAME);
process::exit(1);
});
results_filname = Some(filename);
} else if token == "--help" {
println!("{}", HELP_STRING);
return;
} else {
err!(ERR_INVALID_CLI_ARGUMENT, token);
process::exit(1);
}
}
info!(INF_WELCOME);
let mut stdin = stdin.lock().lines();
loop {
print!("{}", PROMPT);
let _ = io::stdout().flush();
let line = if let Some(res) = stdin.next() {
res
} else {
break;
};
let query = line.unwrap().to_lowercase();
if query.is_empty() {
continue;
}
if query == COMMAND_UNDO {
if let Some(last_word) = last_words_buffer.iter().last() {
words.remove(last_word);
info!(INF_UNDO_WORD, &last_word);
last_words_buffer.pop_back();
} else {
err!(ERR_END_OF_UNDO_BUFFER);
}
continue;
}
if query == COMMAND_EXIT {
info!(INF_EXIT, words.len());
if let Some(filename) = results_filname {
let content = words
.iter()
.map(|w| &**w)
.collect::<Vec<&str>>()
.join("\n");
fs::write(filename, content).unwrap_or_else(|e| {
err!(ERR_FAILED_WRITE, e);
process::exit(1);
});
} else {
for item in words {
println!("{}", item);
}
}
break;
}
if query.starts_with('!') {
err!(ERR_INVALID_COMMAND);
continue;
}
if query.starts_with('?') {
println!("{}", HELP_STRING);
continue;
}
if !query.chars().all(|c| c.is_alphabetic() || c == '-') {
err!(ERR_INVALID_CHARACTER);
continue;
}
if !query.chars().any(|c| !ILLEGAL_STARTING_CHARACTERS.contains(&c)) {
err!(ERR_INVALID_WORD);
continue;
}
if let Some(last_word) = last_words_buffer.iter().last() {
let target_char = last_word
.chars()
.rev()
.find(|c| !ILLEGAL_STARTING_CHARACTERS.contains(c))
.unwrap();
if query.chars().next().unwrap() != target_char {
err!(ERR_ILLEGAL_FIRST_CHARACTER, target_char);
continue;
}
}
if !words.insert(query.clone()) {
err!(ERR_WORD_REPEAT);
} else {
last_words_buffer.push_back(query);
if last_words_buffer.len() > WORD_HISTORY_BUFFER_LENGTH {
last_words_buffer.pop_front();
}
}
}
}