summaryrefslogtreecommitdiffstats
path: root/src/commands/numbered_command.rs
blob: a7f9f0c0dba52207fac1ba0e3999778884f049a1 (plain)
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
use termion::event::{Event, Key};

use crate::commands::cursor_move;
use crate::config::clean::keymap::AppKeyMapping;
use crate::context::AppContext;
use crate::error::{AppError, AppErrorKind, AppResult};
use crate::event::process_event;
use crate::event::AppEvent;
use crate::key_command::{CommandKeybind, NumberedExecute};
use crate::ui::views::TuiView;
use crate::ui::AppBackend;

pub fn numbered_command(
    context: &mut AppContext,
    backend: &mut AppBackend,
    keymap: &AppKeyMapping,
    first_char: char,
) -> AppResult {
    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(AppError::new(
                    AppErrorKind::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 { commands, .. }) => {
                            for command in commands {
                                let _ =
                                    command.numbered_execute(num_prefix, context, backend, keymap);
                            }
                            return Ok(());
                        }
                        _ => {
                            return Err(AppError::new(
                                AppErrorKind::UnrecognizedCommand,
                                "Command cannot be prefixed by a number or does not exist"
                                    .to_string(),
                            ));
                        }
                    },
                }
                context.flush_event();
            }
            event => process_event::process_noninteractive(event, context),
        }
    }
}