forked from skim-rs/skim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.rs
314 lines (263 loc) · 8.39 KB
/
query.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
use model::ClosureType;
use getopts;
use curses::*;
#[derive(Clone, Copy)]
enum QueryMode {
CMD,
QUERY,
}
pub struct Query {
cmd_before: Vec<char>,
cmd_after: Vec<char>,
query_before: Vec<char>,
query_after: Vec<char>,
mode: QueryMode,
base_cmd: String,
replstr: String,
query_prompt: String,
cmd_prompt: String,
}
impl Query {
pub fn builder() -> Self {
Query {
cmd_before: Vec::new(),
cmd_after: Vec::new(),
query_before: Vec::new(),
query_after: Vec::new(),
mode: QueryMode::QUERY,
base_cmd: String::new(),
replstr: "{}".to_string(),
query_prompt: "> ".to_string(),
cmd_prompt: "c> ".to_string(),
}
}
pub fn base_cmd(mut self, base_cmd: &str) -> Self {
self.base_cmd = base_cmd.to_owned();
self
}
// currently they are not used, but will in the future
#[cfg(test)]
pub fn query(mut self, query: &str) -> Self {
self.query_before = query.chars().collect();
self
}
//pub fn cmd(mut self, cmd: &str) -> Self {
//self.cmd_before = cmd.chars().collect();
//self
//}
pub fn build(self) -> Self {
self
}
pub fn parse_options(&mut self, options: &getopts::Matches) {
if let Some(base_cmd) = options.opt_str("c") {
self.base_cmd = base_cmd.clone();
}
if let Some(query) = options.opt_str("q") {
self.query_before = query.chars().collect();
}
if let Some(replstr) = options.opt_str("I") {
self.replstr = replstr.clone();
}
if options.opt_present("i") {
self.mode = QueryMode::CMD;
}
if let Some(query_prompt) = options.opt_str("prompt") {
self.query_prompt = query_prompt;
}
if let Some(cmd_prompt) = options.opt_str("cmd-prompt") {
self.cmd_prompt = cmd_prompt;
}
}
pub fn get_query(&self) -> String {
self.query_before.iter().cloned().chain(self.query_after.iter().cloned().rev()).collect()
}
pub fn get_cmd(&self) -> String {
let arg: String = self.cmd_before.iter().cloned().chain(self.cmd_after.iter().cloned().rev()).collect();
self.base_cmd.replace(&self.replstr, &arg)
}
fn get_before(&self) -> String {
match self.mode {
QueryMode::CMD => self.cmd_before.iter().cloned().collect(),
QueryMode::QUERY => self.query_before.iter().cloned().collect(),
}
}
fn get_after(&self) -> String {
match self.mode {
QueryMode::CMD => self.cmd_after.iter().cloned().rev().collect(),
QueryMode::QUERY => self.query_after.iter().cloned().rev().collect(),
}
}
pub fn get_print_func(&self) -> ClosureType {
let before = self.get_before();
let after = self.get_after();
let mode = self.mode;
let cmd_prompt = self.cmd_prompt.clone();
let query_prompt = self.query_prompt.clone();
Box::new(move |curses| {
match mode {
QueryMode::CMD => {
curses.cprint(&cmd_prompt, COLOR_PROMPT, false);
}
QueryMode::QUERY => {
curses.cprint(&query_prompt, COLOR_PROMPT, false);
}
}
curses.printw(&before);
let (y, x) = curses.getyx();
curses.printw(&after);
curses.mv(y, x);
})
}
fn get_ref(&mut self) -> (&mut Vec<char>, &mut Vec<char>) {
match self.mode {
QueryMode::QUERY => (&mut self.query_before, &mut self.query_after),
QueryMode::CMD => (&mut self.cmd_before, &mut self.cmd_after)
}
}
//------------------------------------------------------------------------------
// Actions
//
pub fn act_query_toggle_interactive(&mut self) {
self.mode = match self.mode {
QueryMode::QUERY => QueryMode::CMD,
QueryMode::CMD => QueryMode::QUERY,
}
}
pub fn act_add_char(&mut self, ch: char) {
let (before, _) = self.get_ref();
before.push(ch);
}
pub fn act_backward_delete_char(&mut self) {
let (before, _) = self.get_ref();
let _ = before.pop();
}
// delete char foraward
pub fn act_delete_char(&mut self) {
let (_, after) = self.get_ref();
let _ = after.pop();
}
pub fn act_backward_char(&mut self) {
let (before, after) = self.get_ref();
before.pop().map(|ch| {
after.push(ch);
});
}
pub fn act_forward_char(&mut self) {
let (before, after) = self.get_ref();
after.pop().map(|ch| {
before.push(ch);
});
}
pub fn act_backward_kill_word(&mut self) {
let (before, _) = self.get_ref();
// skip whitespace
while !before.is_empty() && before[before.len()-1].is_whitespace() {
before.pop();
}
// kill word until whitespace
while !before.is_empty() && !before[before.len()-1].is_whitespace() {
before.pop();
}
}
pub fn act_kill_word(&mut self) {
let (_, after) = self.get_ref();
// kill word until whitespace
while !after.is_empty() && !after[after.len()-1].is_whitespace() {
after.pop();
}
// skip whitespace
while !after.is_empty() && after[after.len()-1].is_whitespace() {
after.pop();
}
}
pub fn act_backward_word(&mut self) {
let (before, after) = self.get_ref();
// skip whitespace
while !before.is_empty() && before[before.len()-1].is_whitespace() {
before.pop().map(|ch| {
after.push(ch);
});
}
// backword char until whitespace
while !before.is_empty() && !before[before.len()-1].is_whitespace() {
before.pop().map(|ch| {
after.push(ch);
});
}
}
pub fn act_forward_word(&mut self) {
let (before, after) = self.get_ref();
// backword char until whitespace
while !after.is_empty() && !after[after.len()-1].is_whitespace() {
after.pop().map(|ch| {
before.push(ch);
});
}
// skip whitespace
while !after.is_empty() && after[after.len()-1].is_whitespace() {
after.pop().map(|ch| {
before.push(ch);
});
}
}
pub fn act_beginning_of_line(&mut self) {
let (before, after) = self.get_ref();
while !before.is_empty() {
before.pop().map(|ch| {
after.push(ch);
});
}
}
pub fn act_end_of_line(&mut self) {
let (before, after) = self.get_ref();
while !after.is_empty() {
after.pop().map(|ch| {
before.push(ch);
});
}
}
pub fn act_kill_line(&mut self) {
let (_, after) = self.get_ref();
after.clear();
}
pub fn act_line_discard(&mut self) {
let (before, _) = self.get_ref();
before.clear();
}
}
#[cfg(test)]
mod test {
use super::Query;
#[test]
fn test_new_query() {
let query1 = Query::builder().query("").build();
assert_eq!(query1.get_query(), "");
let query2 = Query::builder().query("abc").build();
assert_eq!(query2.get_query(), "abc");
}
#[test]
fn test_add_char() {
let mut query1 = Query::builder().query("").build();
query1.act_add_char('a');
assert_eq!(query1.get_query(), "a");
query1.act_add_char('b');
assert_eq!(query1.get_query(), "ab");
query1.act_add_char('中');
assert_eq!(query1.get_query(), "ab中");
}
#[test]
fn test_backward_delete_char() {
let mut query = Query::builder().query("AB中c").build();
assert_eq!(query.get_query(), "AB中c");
query.act_backward_delete_char();
assert_eq!(query.get_query(), "AB中");
query.act_backward_delete_char();
assert_eq!(query.get_query(), "AB");
query.act_backward_delete_char();
assert_eq!(query.get_query(), "A");
query.act_backward_delete_char();
assert_eq!(query.get_query(), "");
query.act_backward_delete_char();
assert_eq!(query.get_query(), "");
}
}