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
|
use termion::event::{Event, Key};
use crate::commands::cursor_move;
use crate::config::AppKeyMapping;
use crate::context::AppContext;
use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult};
use crate::event::process_event;
use crate::event::AppEvent;
use crate::key_command::{CommandKeybind, NumberedExecute};
use crate::ui::views::TuiView;
use crate::ui::TuiBackend;
pub fn numbered_command(
first_char: char,
context: &mut AppContext,
backend: &mut TuiBackend,
keymap: &AppKeyMapping,
) -> JoshutoResult {
context.flush_event();
let mut prefix = String::from(first_char);
loop {
context.message_queue_mut().push_info(prefix.clone());
backend.render(TuiView::new(context));
context.message_queue_mut().pop_front();
let event = match context.poll_event() {
Ok(event) => event,
Err(_) => return Ok(()),
};
let num_prefix = match prefix.parse::<usize>() {
Ok(n) => n,
Err(_) => {
context.message_queue_mut().pop_front();
return Err(JoshutoError::new(
JoshutoErrorKind::ParseError,
"Number is too big".to_string(),
));
}
};
match event {
AppEvent::Termion(event) => {
match event {
Event::Key(Key::Esc) => return Ok(()),
Event::Key(Key::Char('g')) => {
cursor_move::cursor_move(context, num_prefix - 1);
return Ok(());
}
Event::Key(Key::Char(c)) if c.is_numeric() => {
prefix.push(c);
}
key => match keymap.default_view.get(&key) {
Some(CommandKeybind::SimpleKeybind(command)) => {
return command.numbered_execute(num_prefix, context, backend, keymap);
}
_ => {
return Err(JoshutoError::new(
JoshutoErrorKind::UnrecognizedCommand,
"Command cannot be prefixed by a number or does not exist"
.to_string(),
));
}
},
}
context.flush_event();
}
event => process_event::process_noninteractive(event, context),
}
}
}
|