summaryrefslogtreecommitdiffstats
path: root/src/commands/command_line.rs
blob: 259828311f7a2f5c0bd1aa13021c58e5e7221015 (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
use crate::commands::KeyCommand;
use crate::context::JoshutoContext;
use crate::error::JoshutoResult;
use crate::ui::views::TuiTextField;
use crate::ui::TuiBackend;

use super::JoshutoRunnable;

pub fn readline(
    context: &mut JoshutoContext,
    backend: &mut TuiBackend,
    prefix: &str,
    suffix: &str,
) -> JoshutoResult<()> {
    let user_input: Option<String> = TuiTextField::default()
        .prompt(":")
        .prefix(prefix)
        .suffix(suffix)
        .get_input(backend, context);

    if let Some(s) = user_input {
        let trimmed = s.trim_start();
        let command = KeyCommand::parse_command(trimmed)?;
        command.execute(context, backend)
    } else {
        Ok(())
    }
}