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
#[macro_use]
extern crate log;
extern crate pseudoterm;
extern crate termit_ui;
extern crate vte;

use pseudoterm::*;
use std::fs::File;
use std::io::{self, Read, Write};
use std::iter::once;
use std::process::{Child, Command};
use std::thread::sleep;
use std::time::Duration;
use termit_ui::geometry::*;
use termit_ui::graphics::Drawing;
use termit_ui::input::{GrinInput, Key, KeyEvent};
use termit_ui::screen::Screen;
use termit_ui::widget::Painter;
use termit_ui::widget::Widget;

pub struct Temu {
    master: File,
    child: Child,
    size: Point,
    sizer: WinsizeSetter,
    parser: vte::Parser,
    buffer: VTBuffer,
    cursor_x: X,
    cursor_y: Y,
    screen: Screen,
    errors: Vec<io::Error>,
}

impl Drop for Temu {
    fn drop(&mut self) {
        self.kill()
    }
}

impl Widget for Temu {
    fn render(&self, painter: &mut Painter) -> io::Result<()> {
        for d in self.screen.into_iter() {
            painter.paint(d.clone())?
        }

        self.errors
            .first()
            .map(|e| Err(io::Error::new(e.kind(), format!("{}", e))))
            .unwrap_or(Ok(()))
    }
    fn arrange(&mut self, window: Window) {
        if window.size == self.size {
            return;
        }

        self.size = window.size;

        self.sizer
            .set(Winsize {
                cols: self.size.x.to_primitive(),
                rows: self.size.y.to_primitive(),
            })
            .unwrap_or_else(|e| self.errors.push(e));

        self.cursor_x = X::min(self.cursor_x, self.size.x);
        self.cursor_y = Y::min(self.cursor_y, self.size.y);

        self.screen
            .resize(self.size)
            .unwrap_or_else(|e| self.errors.push(e));
    }
    fn consume(&mut self, input: Vec<GrinInput>) -> Vec<GrinInput> {
        self.errors.clear();

        let mut utf8 = [0u8; 4];
        for i in input {
            match i {
                GrinInput::Key(KeyEvent {
                    key: Key::Char(c), ..
                }) => {
                    self.master
                        .write(c.encode_utf8(&mut utf8).as_bytes())
                        .unwrap_or_else(|e| (0, self.errors.push(e)).0);
                }
                _ => {}
            }
        }
        self.master.flush().unwrap_or_else(|e| self.errors.push(e));

        self.react().unwrap_or_else(|e| self.errors.push(e));

        for vt in self.buffer.buffer.drain(0..) {
            match vt {
                VTOut::Print(txt) => {
                    let d = Drawing::new()
                        .with_text(txt)
                        .left(self.cursor_x.to_primitive())
                        .top(self.cursor_y.to_primitive());
                    let len = d.actual_width();
                    self.screen.draw(d);
                    self.cursor_x = self.cursor_x + len;
                }
                VTOut::Execute(13) => {
                    self.cursor_y = self.cursor_y + y(1);
                }
                VTOut::Execute(10) => {
                    self.cursor_x = x(0);
                }
                _ => {}
            }
        }
        vec![]
    }
}

impl Temu {
    pub fn open(mut cmd: &mut Command) -> io::Result<Temu> {
        let size = point(x(80), y(20));

        // Step 1: Create PTY
        let (master, slave) = pseudoterm::openpty(
            &OpenptyOptions::new()
                .with_size(Winsize {
                    cols: size.x.to_primitive(),
                    rows: size.y.to_primitive(),
                })
                .with_nonblocking(true),
        )?;

        // Step 2: Launch command
        let child = pseudoterm::prepare_cmd(slave, &mut cmd)?.spawn()?;

        // window size setter
        let sizer = WinsizeSetter::new(&master)?;
        // parse VT into a buffer
        let parser = vte::Parser::new();
        let buffer = VTBuffer { buffer: vec![] };

        Ok(Temu {
            master,
            size,
            sizer,
            child,
            parser,
            buffer,
            cursor_x: x(0),
            cursor_y: y(0),
            screen: Screen::new(size),
            errors: vec![],
        })
    }
    pub fn react(&mut self) -> io::Result<()> {
        let Temu {
            ref mut master,
            ref mut sizer,
            ref mut parser,
            ref mut buffer,
            ref mut child,
            ..
        } = self;

        sizer.set(Winsize { cols: 70, rows: 10 })?;

        sleep(Duration::from_millis(100));

        for b in master.bytes() {
            match b {
                Ok(b) => {
                    parser.advance(buffer, b);
                }
                Err(e) => match e.kind() {
                    io::ErrorKind::WouldBlock => {
                        break;
                    }
                    _ => {
                        let status = child.try_wait();
                        warn!("Error on child output. Child status: {:?}", status);
                        break;
                    }
                },
            }
        }

        for o in buffer.buffer.iter() {
            trace!("VTOut::{:?}", o);
        }

        Ok(())
    }
    fn kill(&mut self) {
        self.child
            .kill()
            .unwrap_or_else(|e| warn!("Couldn't kill the child, unfortunately! {}", e));
    }
}

/// A type implementing Perform that just logs actions
struct VTBuffer {
    buffer: Vec<VTOut>,
}

#[derive(Debug)]
enum VTOut {
    Print(String),
    Execute(u8),
    OSC(Vec<Vec<u8>>),
    DCS {
        params: Vec<i64>,
        intermediates: Vec<u8>,
        ignore: bool,
        data: Vec<u8>,
    },
    CSI {
        params: Vec<i64>,
        intermediates: Vec<u8>,
        ignore: bool,
        c: char,
    },
    ESC {
        params: Vec<i64>,
        intermediates: Vec<u8>,
        ignore: bool,
        b: u8,
    },
}

impl vte::Perform for VTBuffer {
    fn print(&mut self, c: char) {
        if let Some(VTOut::Print(ref mut s)) = self.buffer.last_mut() {
            s.push(c);
            return;
        }
        self.buffer.push(VTOut::Print(once(c).collect()))
    }

    fn execute(&mut self, byte: u8) {
        self.buffer.push(VTOut::Execute(byte))
    }

    fn hook(&mut self, params: &[i64], intermediates: &[u8], ignore: bool) {
        self.buffer.push(VTOut::DCS {
            params: params.into(),
            intermediates: intermediates.into(),
            ignore,
            data: vec![],
        })
    }

    fn put(&mut self, byte: u8) {
        if let Some(VTOut::DCS { ref mut data, .. }) = self.buffer.last_mut() {
            data.push(byte);
        } else {
            warn!("put() without hook() or after unhook() or on ignored DCS")
        }
    }

    fn unhook(&mut self) {
        if let Some(VTOut::DCS { .. }) = self.buffer.last_mut() {
            // TODO: might want to handle complete/incomplete state
        } else {
            warn!("unhook() without hook()")
        }
    }

    fn osc_dispatch(&mut self, params: &[&[u8]]) {
        self.buffer
            .push(VTOut::OSC(params.iter().map(|v| Vec::from(*v)).collect()))
    }

    fn csi_dispatch(&mut self, params: &[i64], intermediates: &[u8], ignore: bool, c: char) {
        self.buffer.push(VTOut::CSI {
            params: params.into(),
            intermediates: intermediates.into(),
            ignore,
            c,
        })
    }

    fn esc_dispatch(&mut self, params: &[i64], intermediates: &[u8], ignore: bool, b: u8) {
        self.buffer.push(VTOut::ESC {
            params: params.into(),
            intermediates: intermediates.into(),
            ignore,
            b,
        })
    }
}