summaryrefslogtreecommitdiffstats
path: root/src/commands/command_line.rs
blob: 5f9d0ded015f4f6dd79630fb5c48fe164b64d774 (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
use std::str::FromStr;

use crate::config::clean::keymap::AppKeyMapping;
use crate::context::AppContext;
use crate::error::AppResult;
use crate::key_command::{AppExecute, Command};
use crate::ui::views::{DummyListener, TuiTextField};
use crate::ui::AppBackend;

pub fn read_and_execute(
    context: &mut AppContext,
    backend: &mut AppBackend,
    keymap_t: &AppKeyMapping,
    prefix: &str,
    suffix: &str,
) -> AppResult {
    context.flush_event();
    let mut listener = DummyListener {};
    let user_input: Option<String> = TuiTextField::default()
        .prompt(":")
        .prefix(prefix)
        .suffix(suffix)
        .get_input(backend, context, &mut listener);

    if let Some(mut s) = user_input {
        let mut trimmed = s.trim_start();
        let _ = context.commandline_context_mut().history_mut().add(trimmed);

        let (command, arg) = match trimmed.find(' ') {
            Some(i) => (&trimmed[..i], &trimmed[i..]),
            None => (trimmed, ""),
        };

        if let Some(alias) = context.config_ref().cmd_aliases.get(trimmed) {
            trimmed = alias;
        } else if let Some(alias) = context.config_ref().cmd_aliases.get(command) {
            s.replace_range(..s.len() - arg.len(), alias);
            trimmed = &s;
        }

        let command = Command::from_str(trimmed)?;
        command.execute(context, backend, keymap_t)
    } else {
        Ok(())
    }
}