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
use core::prelude::*;
use alloc::rc::Rc;
use alloc::boxed::Box;
use collections::string::*;
use collections::Vec;
use collections::slice::SliceConcatExt;
use cli::*;
use utils::*;
pub struct CliCommandKeyword<Fo> where Fo: Fn(&str, &mut CliTerminal) -> () {
pub keyword: String,
pub action: Fo
}
impl<Fo> CliCommand for CliCommandKeyword<Fo> where Fo: Fn(&str, &mut CliTerminal) -> () {
fn execute(&mut self, cli: &mut CliTerminal, line: &str) {
self.action.call((line, cli));
}
fn is_match(&self, line: &str) -> bool {
self.keyword == line
}
fn autocomplete(&self, line_start: &str) -> Option<Vec<AutocompleteOption>> {
if self.keyword.starts_with(line_start) {
Some(vec! [
AutocompleteOption::FullCommand { line: self.keyword.clone() },
AutocompleteOption::Hint { hint: self.keyword.clone() },
])
} else {
None
}
}
}
pub struct CliPropertyVar<T, Fo, Fi> where Fo: Fn(&T) -> String, Fi: Fn(&str) -> Option<T>
{
pub var_name: String,
pub var_value: T,
pub var_output: Fo,
pub var_input: Fi,
pub val_hint: String
}
pub struct CliPropertyFn<Fo, Fi> where Fo: Fn() -> String, Fi: Fn(&str, &mut CliTerminal) -> ()
{
pub var_name: String,
pub var_output: Fo,
pub var_input: Fi,
pub val_hint: String
}
trait CliProperty {
fn get_var_name(&self) -> &str;
fn get_val_hint(&self) -> &str;
fn set_prefix(&self) -> String {
format!("set {}", self.get_var_name())
}
fn get_prefix(&self) -> String {
format!("get {}", self.get_var_name())
}
fn _autocomplete(&self, line_start: &str) -> Option<Vec<AutocompleteOption>> {
let mut ret = Vec::new();
let l_get = self.get_prefix();
let l_set = self.set_prefix();
if l_get.starts_with(line_start) {
ret.push(AutocompleteOption::FullCommand { line: l_get.clone() });
ret.push(AutocompleteOption::Hint { hint: l_get.clone() });
}
if l_set.starts_with(line_start) {
ret.push(AutocompleteOption::FullCommand { line: format!("{} ", l_set) });
ret.push(AutocompleteOption::Hint { hint: format!("{} <{}>", l_set, self.get_val_hint()) });
}
if ret.len() == 0 { return None; }
Some(ret)
}
fn _is_match(&self, line: &str) -> bool {
if line.starts_with(self.set_prefix().as_str()) ||
line.starts_with(self.get_prefix().as_str()) {
true
} else {
false
}
}
}
impl<T, Fo, Fi> CliProperty for CliPropertyVar<T, Fo, Fi> where Fo: Fn(&T) -> String, Fi: Fn(&str) -> Option<T> {
fn get_var_name(&self) -> &str {
self.var_name.as_str()
}
fn get_val_hint(&self) -> &str {
self.val_hint.as_str()
}
}
impl<Fo, Fi> CliProperty for CliPropertyFn<Fo, Fi> where Fo: Fn() -> String, Fi: Fn(&str, &mut CliTerminal) -> () {
fn get_var_name(&self) -> &str {
self.var_name.as_str()
}
fn get_val_hint(&self) -> &str {
self.val_hint.as_str()
}
}
impl<T, Fo, Fi> CliCommand for CliPropertyVar<T, Fo, Fi>
where Fo: Fn(&T) -> String, Fi: Fn(&str) -> Option<T>
{
fn execute(&mut self, cli: &mut CliTerminal, line: &str) {
if line.starts_with(self.get_prefix().as_str()) {
let d = self.var_output.call((&self.var_value,));
cli.output_line(format!("{} = {}", self.var_name, d).as_str());
}
let set_pref = self.set_prefix();
let set_pref = set_pref.as_str();
if line.starts_with(&set_pref) {
let v = {
if line.len() > set_pref.len() {
let l = &line[(set_pref.len() + 1)..];
self.var_input.call((l,))
} else {
None
}
};
match v {
Some(v) => { self.var_value = v; }
None => { cli.output_line("Couldn't parse the value."); }
}
}
}
fn is_match(&self, line: &str) -> bool {
self._is_match(line)
}
fn autocomplete(&self, line_start: &str) -> Option<Vec<AutocompleteOption>> {
self._autocomplete(line_start)
}
}
impl<Fo, Fi> CliCommand for CliPropertyFn<Fo, Fi> where Fo: Fn() -> String, Fi: Fn(&str, &mut CliTerminal) {
fn execute(&mut self, cli: &mut CliTerminal, line: &str) {
if line.starts_with(self.get_prefix().as_str()) {
let d = self.var_output.call(());
cli.output_line(format!("{} = {}", self.var_name, d).as_str());
}
let set_pref = self.set_prefix();
let set_pref = set_pref.as_str();
if line.starts_with(&set_pref) {
if line.len() > set_pref.len() {
let l = &line[(set_pref.len() + 1)..];
self.var_input.call((l, cli));
}
}
}
fn is_match(&self, line: &str) -> bool {
self._is_match(line)
}
fn autocomplete(&self, line_start: &str) -> Option<Vec<AutocompleteOption>> {
self._autocomplete(line_start)
}
}