summaryrefslogtreecommitdiffstats
path: root/src/history.rs
blob: d20de809da409a144a9e8ae237cbf0516a3ac7f4 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use async_std::io::{ReadExt as _, WriteExt as _};
use futures_lite::future::FutureExt as _;
use pty_process::Command as _;
use std::os::unix::process::ExitStatusExt as _;
use textmode::Textmode as _;

pub struct History {
    size: (u16, u16),
    entries: Vec<crate::util::Mutex<HistoryEntry>>,
}

impl History {
    pub fn new() -> Self {
        Self {
            size: (24, 80),
            entries: vec![],
        }
    }

    pub async fn handle_key(&self, key: textmode::Key, idx: usize) {
        let entry = self.entries[idx].lock_arc().await;
        if entry.running() {
            entry.input.send(key.into_bytes()).await.unwrap();
        }
    }

    pub async fn render(
        &self,
        out: &mut textmode::Output,
        repl_lines: usize,
        focus: Option<usize>,
    ) -> anyhow::Result<()> {
        let mut used_lines = repl_lines;
        let mut pos = None;
        for (idx, entry) in self.entries.iter().enumerate().rev() {
            let mut entry = entry.lock_arc().await;
            let focused = focus.map_or(false, |focus| idx == focus);
            let last_row = entry.lines(self.size.1, focused);
            used_lines += 1 + std::cmp::min(6, last_row);
            if used_lines > self.size.0 as usize {
                break;
            }
            if focused && used_lines == 1 && entry.running() {
                used_lines = 2;
            }
            out.move_to(
                (self.size.0 as usize - used_lines).try_into().unwrap(),
                0,
            );
            entry.render(out, self.size.1, focused);
            if focused {
                pos = Some(out.screen().cursor_position());
            }
        }
        if let Some(pos) = pos {
            out.move_to(pos.0, pos.1);
        }
        Ok(())
    }

    pub async fn render_fullscreen(
        &self,
        out: &mut textmode::Output,
        idx: usize,
    ) {
        let mut entry = self.entries[idx].lock_arc().await;
        entry.render_fullscreen(out);
    }

    pub async fn resize(&mut self, size: (u16, u16)) {
        self.size = size;
        for entry in &self.entries {
            let entry = entry.lock_arc().await;
            if entry.running() {
                entry.resize.send(size).await.unwrap();
            }
        }
    }

    pub async fn run(
        &mut self,
        cmd: &str,
        action_w: async_std::channel::Sender<crate::action::Action>,
    ) -> anyhow::Result<usize> {
        let (exe, args) = crate::parse::cmd(cmd);
        let (input_w, input_r) = async_std::channel::unbounded();
        let (resize_w, resize_r) = async_std::channel::unbounded();
        let entry = crate::util::mutex(HistoryEntry::new(
            cmd, self.size, input_w, resize_w,
        ));
        if crate::builtins::is(&exe) {
            let code: i32 = crate::builtins::run(&exe, &args).into();
            entry.lock_arc().await.exit_info = Some(ExitInfo::new(
                async_std::process::ExitStatus::from_raw(code << 8),
            ));
            action_w
                .send(crate::action::Action::UpdateFocus(
                    crate::state::Focus::Readline,
                ))
                .await
                .unwrap();
        } else {
            let mut process = async_std::process::Command::new(&exe);
            process.args(&args);
            let child = process
                .spawn_pty(Some(&pty_process::Size::new(
                    self.size.0,
                    self.size.1,
                )))
                .unwrap();
            run_process(
                child,
                async_std::sync::Arc::clone(&entry),
                input_r,
                resize_r,
                action_w,
            );
        }
        self.entries.push(entry);
        Ok(self.entries.len() - 1)
    }

    pub async fn toggle_fullscreen(&mut self, idx: usize) {
        self.entries[idx].lock_arc().await.toggle_fullscreen();
    }

    pub async fn should_fullscreen(&self, idx: usize) -> bool {
        self.entries[idx].lock_arc().await.should_fullscreen()
    }

    pub fn entry_count(&self) -> usize {
        self.entries.len()
    }
}

struct HistoryEntry {
    cmd: String,
    vt: vt100::Parser,
    audible_bell_state: usize,
    visual_bell_state: usize,
    fullscreen: Option<bool>,
    input: async_std::channel::Sender<Vec<u8>>,
    resize: async_std::channel::Sender<(u16, u16)>,
    start_time: chrono::DateTime<chrono::Local>,
    start_instant: std::time::Instant,
    exit_info: Option<ExitInfo>,
}

impl HistoryEntry {
    fn new(
        cmd: &str,
        size: (u16, u16),
        input: async_std::channel::Sender<Vec<u8>>,
        resize: async_std::channel::Sender<(u16, u16)>,
    ) -> Self {
        Self {
            cmd: cmd.into(),
            vt: vt100::Parser::new(size.0, size.1, 0),
            audible_bell_state: 0,
            visual_bell_state: 0,
            input,
            resize,
            fullscreen: None,
            start_time: chrono::Local::now(),
            start_instant: std::time::Instant::now(),
            exit_info: None,
        }
    }

    fn render(
        &mut self,
        out: &mut textmode::Output,
        width: u16,
        focused: bool,
    ) {
        out.set_bgcolor(textmode::Color::Rgb(32, 32, 32));
        if let Some(info) = self.exit_info {
            if info.status.signal().is_some() {
                out.set_fgcolor(textmode::color::MAGENTA);
            } else if info.status.success() {
                out.set_fgcolor(textmode::color::DARKGREY);
            } else {
                out.set_fgcolor(textmode::color::RED);
            }
            out.write_str(&crate::format::exit_status(info.status));
        } else {
            out.write_str("     ");
        }
        out.reset_attributes();
        if focused {
            out.set_fgcolor(textmode::color::BLACK);
            out.set_bgcolor(textmode::color::CYAN);
        } else {
            out.set_bgcolor(textmode::Color::Rgb(32, 32, 32));
        }
        out.write_str("$ ");
        out.reset_attributes();
        out.set_bgcolor(textmode::Color::Rgb(32, 32, 32));
        if self.running() {
            out.set_bgcolor(textmode::Color::Rgb(16, 64, 16));
        }
        out.write_str(&self.cmd);
        out.reset_attributes();
        out.set_bgcolor(textmode::Color::Rgb(32, 32, 32));
        let time = if let Some(info) = self.exit_info {
            format!(
                "[{} ({:6})]",
                self.start_time.time().format("%H:%M:%S"),
                crate::format::duration(info.instant - self.start_instant)
            )
        } else {
            format!("[{}]", self.start_time.time().format("%H:%M:%S"))
        };
        let cur_pos = out.screen().cursor_position();
        out.write_str(
            &" ".repeat(width as usize - time.len() - 1 - cur_pos.1 as usize),
        );
        out.write_str(&time);
        out.write_str(" ");
        out.reset_attributes();
        let last_row = self.lines(width, focused);
        if last_row > 5 {
            out.write(b"\r\n");
            out.set_fgcolor(textmode::color::BLUE);
            out.write(b"...");
            out.reset_attributes();
        }
        let mut out_row = out.screen().cursor_position().0 + 1;
        let screen = self.vt.screen();
        let pos = screen.cursor_position();
        let mut wrapped = false;
        let mut cursor_found = None;
        for (idx, row) in screen
            .rows_formatted(0, width)
            .enumerate()
            .take(last_row)
            .skip(last_row.saturating_sub(5))
        {
            let idx: u16 = idx.try_into().unwrap();
            out.write(b"\x1b[m");
            if !wrapped {
                out.write(format!("\x1b[{}H", out_row + 1).as_bytes());
            }
            out.write(&row);
            wrapped = screen.row_wrapped(idx);
            if pos.0 == idx {
                cursor_found = Some(out_row);
            }
            out_row += 1;
        }
        if focused {
            if let Some(row) = cursor_found {
                if screen.hide_cursor() {
                    out.write(b"\x1b[?25l");
                } else {
                    out.write(b"\x1b[?25h");
                    out.move_to(row, pos.1);
                }
            } else {
                out.write(b"\x1b[?25l");
            }
        }
        out.reset_attributes();
    }

    fn render_fullscreen(&mut self, out: &mut textmode::Output) {
        let screen = self.vt.screen();
        let new_audible_bell_state = screen.audible_bell_count();
        let new_visual_bell_state = screen.visual_bell_count();

        out.write(&screen.state_formatted());

        if self.audible_bell_state != new_audible_bell_state {
            out.write(b"\x07");
            self.audible_bell_state = new_audible_bell_state;
        }

        if self.visual_bell_state != new_visual_bell_state {
            out.write(b"\x1bg");
            self.visual_bell_state = new_visual_bell_state;
        }

        out.reset_attributes();
    }

    fn toggle_fullscreen(&mut self) {
        if let Some(fullscreen) = self.fullscreen {
            self.fullscreen = Some(!fullscreen);
        } else {
            self.fullscreen = Some(!self.vt.screen().alternate_screen());
        }
    }

    fn running(&self) -> bool {
        self.exit_info.is_none()
    }

    fn lines(&self, width: u16, focused: bool) -> usize {
        let screen = self.vt.screen();
        let mut last_row = 0;
        for (idx, row) in screen.rows(0, width).enumerate() {
            if !row.is_empty() {
                last_row = idx + 1;
            }
        }
        if focused && self.running() {
            last_row = std::cmp::max(
                last_row,
                screen.cursor_position().0 as usize + 1,
            );
        }
        last_row
    }

    fn should_fullscreen(&self) -> bool {
        self.fullscreen
            .unwrap_or_else(|| self.vt.screen().alternate_screen())
    }
}

#[derive(Copy, Clone)]
struct ExitInfo {
    status: async_std::process::ExitStatus,
    instant: std::time::Instant,
}

impl ExitInfo {
    fn new(status: async_std::process::ExitStatus) -> Self {
        Self {
            status,
            instant: std::time::Instant::now(),
        }
    }
}

fn run_process(
    mut child: pty_process::async_std::Child,
    entry: crate::util::Mutex<HistoryEntry>,
    input_r: async_std::channel::Receiver<Vec<u8>>,
    resize_r: async_std::channel::Receiver<(u16, u16)>,
    action_w: async_std::channel::Sender<crate::action::Action>,
) {
    async_std::task::spawn(async move {
        loop {
            enum Res {
                Read(Result<usize, std::io::Error>),
                Write(Result<Vec<u8>, async_std::channel::RecvError>),
                Resize(Result<(u16, u16), async_std::channel::RecvError>),
            }
            let mut buf = [0_u8; 4096];
            let mut pty = child.pty();
            let read = async { Res::Read(pty.read(&mut buf).await) };
            let write = async { Res::Write(input_r.recv().await) };
            let resize = async { Res::Resize(resize_r.recv().await) };
            match read.race(write).race(resize).await {
                Res::Read(res) => match res {
                    Ok(bytes) => {
                        let mut entry = entry.lock_arc().await;
                        let pre_alternate_screen =
                            entry.vt.screen().alternate_screen();
                        entry.vt.process(&buf[..bytes]);
                        let post_alternate_screen =
                            entry.vt.screen().alternate_screen();
                        if entry.fullscreen.is_none()
                            && pre_alternate_screen != post_alternate_screen
                        {
                            action_w
                                .send(crate::action::Action::CheckUpdateScene)
                                .await
                                .unwrap();
                        }
                        action_w
                            .send(crate::action::Action::Render)
                            .await
                            .unwrap();
                    }
                    Err(e) => {
                        if e.raw_os_error() != Some(libc::EIO) {
                            eprintln!("pty read failed: {:?}", e);
                        }
                        // XXX not sure if this is safe - are we sure
                        // the child exited?
                        entry.lock_arc().await.exit_info = Some(
                            ExitInfo::new(child.status().await.unwrap()),
                        );
                        action_w
                            .send(crate::action::Action::UpdateFocus(
                                crate::state::Focus::Readline,
                            ))
                            .await
                            .unwrap();
                        break;
                    }
                },
                Res::Write(res) => match res {
                    Ok(bytes) => {
                        pty.write(&bytes).await.unwrap();
                    }
                    Err(e) => {
                        panic!("failed to read from input channel: {}", e);
                    }
                },
                Res::Resize(res) => match res {
                    Ok(size) => {
                        child
                            .resize_pty(&pty_process::Size::new(
                                size.0, size.1,
                            ))
                            .unwrap();
                        entry.lock_arc().await.vt.set_size(size.0, size.1);
                    }
                    Err(e) => {
                        panic!("failed to read from resize channel: {}", e);
                    }
                },
            }
        }
    });
}