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
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
use std::mem;
use cvars::SetGet;
#[derive(Debug, Clone, Default)]
pub struct Console {
pub prompt: String,
prompt_saved: Option<String>,
prompt_history_index: Option<usize>,
pub history: Vec<HistoryLine>,
pub history_view_end: usize,
}
impl Console {
pub fn new() -> Self {
Console {
prompt: String::new(),
prompt_saved: None,
prompt_history_index: None,
history: Vec::new(),
history_view_end: 0,
}
}
pub fn history_back(&mut self) {
let search_slice = if let Some(hi) = self.prompt_history_index {
&self.history[0..hi]
} else {
&self.history[..]
};
if let Some(new_index) = search_slice
.iter()
.rposition(|hist_line| hist_line.is_input)
{
self.prompt_history_index = Some(new_index);
if self.prompt_saved.is_none() {
self.prompt_saved = Some(self.prompt.clone());
}
self.prompt = self.history[new_index].text.clone();
}
}
pub fn history_forward(&mut self) {
if let Some(index) = self.prompt_history_index {
let begin = index + 1;
let search_slice = &self.history[begin..];
if let Some(local_index) = search_slice.iter().position(|hist_line| hist_line.is_input)
{
let new_index = begin + local_index;
self.prompt_history_index = Some(new_index);
self.prompt = self.history[new_index].text.clone();
} else {
self.prompt_history_index = None;
self.prompt = self.prompt_saved.take().unwrap();
}
}
}
pub fn history_scroll_up(&mut self, count: usize) {
self.history_view_end = self.history_view_end.saturating_sub(count);
if self.history_view_end == 0 && !self.history.is_empty() {
self.history_view_end = 1;
}
}
pub fn history_scroll_down(&mut self, count: usize) {
self.history_view_end = (self.history_view_end + count).min(self.history.len());
}
pub fn enter(&mut self, cvars: &mut dyn SetGet) {
let cmd = mem::take(&mut self.prompt);
self.print_input(&cmd);
let res = self.execute_command(cvars, &cmd);
if let Err(msg) = res {
self.print(msg);
}
self.prompt_history_index = None;
}
fn execute_command(&mut self, cvars: &mut dyn SetGet, cmd: &str) -> Result<(), String> {
let mut parts = cmd.split_whitespace();
let cvar_name = match parts.next() {
Some(name) => name,
None => return Ok(()),
};
if cvar_name == "help" || cvar_name == "?" {
self.print("Available actions:");
self.print(" help Print this message");
self.print(" <cvar name> Print the cvar's value");
self.print(" <cvar name> <value> Set the cvar's value");
return Ok(());
}
let cvar_value = match parts.next() {
Some(val) => val,
None => {
let val = cvars.get_string(cvar_name)?;
self.print(val);
return Ok(());
}
};
if let Some(rest) = parts.next() {
return Err(format!("expected only cvar name and value, found {rest}"));
}
cvars.set_str(cvar_name, cvar_value)
}
pub fn print<S: Into<String>>(&mut self, text: S) {
self.push_history_line(text.into(), false);
}
fn print_input<S: Into<String>>(&mut self, text: S) {
self.push_history_line(text.into(), true);
}
fn push_history_line(&mut self, text: String, is_input: bool) {
let hist_line = HistoryLine::new(text, is_input);
self.history.push(hist_line);
self.history_view_end += 1;
}
}
#[derive(Debug, Clone)]
pub struct HistoryLine {
pub text: String,
pub is_input: bool,
}
impl HistoryLine {
pub fn new(text: String, is_input: bool) -> Self {
Self { text, is_input }
}
}