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
use anyhow::Result;
use panorama_tui::crossterm::event::{KeyCode, KeyEvent};

use super::input::{HandlesInput, InputResult};
use super::TermType;

#[derive(Clone, Default, Debug)]
pub struct ColonPrompt {
    pub value: String,
}

impl ColonPrompt {
    pub fn init(term: TermType) -> Result<Self> {
        let s = term.size()?;
        term.set_cursor(1, s.height - 1)?;
        term.show_cursor()?;
        Ok(ColonPrompt::default())
    }
}

impl Drop for ColonPrompt {
    fn drop(&mut self) {}
}

impl HandlesInput for ColonPrompt {
    fn handle_key(&mut self, term: TermType, evt: KeyEvent) -> Result<InputResult> {
        let KeyEvent { code, .. } = evt;
        match code {
            KeyCode::Esc => return Ok(InputResult::Pop),
            KeyCode::Char(c) => {
                let mut b = [0; 2];
                self.value += c.encode_utf8(&mut b);
            }
            KeyCode::Enter => {
                let cmd = self.value.clone();
                self.value.clear();
                debug!("executing colon command: {:?}", cmd);
                return Ok(InputResult::Pop);
            }
            KeyCode::Backspace => {
                let mut new_len = self.value.len();
                if new_len > 0 {
                    new_len -= 1;
                    self.value.truncate(new_len);
                } else {
                    return Ok(InputResult::Pop);
                }
            }
            _ => {}
        }

        Ok(InputResult::Ok)
    }
}