summaryrefslogtreecommitdiffstats
path: root/src/readline.rs
blob: e13c00f5928d2753b9a4d1b911fc668e3c12af3a (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use textmode::Textmode as _;
use unicode_width::{UnicodeWidthChar as _, UnicodeWidthStr as _};

pub struct Readline {
    size: (u16, u16),
    input_line: String,
    pos: usize,
}

impl Readline {
    pub fn new() -> Self {
        Self {
            size: (24, 80),
            input_line: "".into(),
            pos: 0,
        }
    }

    pub async fn render(
        &self,
        out: &mut textmode::Output,
        entry_count: usize,
        focus: bool,
        offset: time::UtcOffset,
    ) -> anyhow::Result<()> {
        let pwd = crate::env::pwd()?;
        let user = crate::env::user()?;
        let hostname = crate::env::hostname()?;
        let time = crate::env::time(offset)?;
        let prompt_char = crate::env::prompt_char()?;

        let id = format!("{}@{}", user, hostname);
        let idlen: u16 = id.len().try_into().unwrap();
        let timelen: u16 = time.len().try_into().unwrap();

        out.move_to(self.size.0 - 2, 0);
        if focus {
            out.set_bgcolor(textmode::Color::Rgb(32, 32, 64));
        } else {
            out.set_bgcolor(textmode::Color::Rgb(32, 32, 32));
        }
        out.write(b"\x1b[K");
        out.set_fgcolor(textmode::color::YELLOW);
        out.write_str(&format!("{}", entry_count + 1));
        out.reset_attributes();
        if focus {
            out.set_bgcolor(textmode::Color::Rgb(32, 32, 64));
        } else {
            out.set_bgcolor(textmode::Color::Rgb(32, 32, 32));
        }
        out.write_str(" (");
        out.write_str(&pwd);
        out.write_str(")");
        out.move_to(self.size.0 - 2, self.size.1 - 4 - idlen - timelen);
        out.write_str(&id);
        out.write_str(" [");
        out.write_str(&time);
        out.write_str("]");

        out.move_to(self.size.0 - 1, 0);
        out.reset_attributes();
        out.write_str(&prompt_char);
        out.write_str(" ");
        out.reset_attributes();
        out.write(b"\x1b[K");
        out.write_str(&self.input_line);
        out.reset_attributes();
        out.move_to(self.size.0 - 1, 2 + self.pos_width());
        if focus {
            out.hide_cursor(false);
        }
        Ok(())
    }

    pub async fn resize(&mut self, size: (u16, u16)) {
        self.size = size;
    }

    pub fn lines(&self) -> usize {
        2 // XXX handle wrapping
    }

    pub fn input(&self) -> String {
        self.input_line.clone()
    }

    pub fn add_input(&mut self, s: &str) {
        self.input_line.insert_str(self.byte_pos(), s);
        self.pos += s.chars().count();
    }

    pub fn set_input(&mut self, s: &str) {
        self.input_line = s.to_string();
        self.pos = s.chars().count();
    }

    pub fn backspace(&mut self) {
        while self.pos > 0 {
            self.pos -= 1;
            let width =
                self.input_line.remove(self.byte_pos()).width().unwrap_or(0);
            if width > 0 {
                break;
            }
        }
    }

    pub fn clear_input(&mut self) {
        self.input_line.clear();
        self.pos = 0;
    }

    pub fn clear_backwards(&mut self) {
        self.input_line = self.input_line.chars().skip(self.pos).collect();
        self.pos = 0;
    }

    pub fn cursor_left(&mut self) {
        if self.pos == 0 {
            return;
        }
        self.pos -= 1;
        while let Some(c) = self.input_line.chars().nth(self.pos) {
            if c.width().unwrap_or(0) == 0 {
                self.pos -= 1;
            } else {
                break;
            }
        }
    }

    pub fn cursor_right(&mut self) {
        if self.pos == self.input_line.chars().count() {
            return;
        }
        self.pos += 1;
        while let Some(c) = self.input_line.chars().nth(self.pos) {
            if c.width().unwrap_or(0) == 0 {
                self.pos += 1;
            } else {
                break;
            }
        }
    }

    fn pos_width(&self) -> u16 {
        self.input_line
            .chars()
            .take(self.pos)
            .collect::<String>()
            .width()
            .try_into()
            .unwrap()
    }

    fn byte_pos(&self) -> usize {
        self.input_line
            .char_indices()
            .nth(self.pos)
            .map_or(self.input_line.len(), |(i, _)| i)
    }
}