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
extern crate libc;
extern crate log;
extern crate nix;
extern crate unsegen;
extern crate vte;
#[allow(dead_code)]
mod ansi;
#[allow(dead_code)]
mod index;
mod pty;
mod terminalwindow;
use ansi::Processor;
use pty::{PTYInput, PTYOutput, PTY};
use std::ffi::{OsStr, OsString};
use unsegen::base::basic_types::*;
use unsegen::base::Window;
use unsegen::container::Container;
use unsegen::input::{Behavior, Input, Key, OperationResult, ScrollBehavior, Scrollable, Writable};
use unsegen::widget::{Demand2D, RenderingHints, Widget};
use terminalwindow::DualWindow;
use std::cell::RefCell;
use std::fs::File;
use std::thread;
fn read_slave_input_loop<S: SlaveInputSink>(mut sink: S, mut reader: PTYOutput) {
use std::io::Read;
let mut buffer = [0; 1024];
while let Ok(n) = reader.read(&mut buffer) {
let mut bytes = vec![0; n];
bytes.copy_from_slice(&mut buffer[..n]);
sink.receive_bytes_from_pty(bytes.into_boxed_slice());
}
}
pub trait SlaveInputSink: std::marker::Send {
fn receive_bytes_from_pty(&mut self, data: Box<[u8]>);
}
pub struct PassthroughBehavior<'a> {
term: &'a mut Terminal,
}
impl<'a> PassthroughBehavior<'a> {
pub fn new(term: &'a mut Terminal) -> Self {
PassthroughBehavior { term: term }
}
}
impl<'a> Behavior for PassthroughBehavior<'a> {
fn input(self, i: Input) -> Option<Input> {
self.term.process_input(i);
None
}
}
pub struct Terminal {
terminal_window: RefCell<DualWindow>,
master_input_sink: RefCell<PTYInput>,
_slave_handle: File,
slave_name: OsString,
ansi_processor: Processor,
}
impl Terminal {
pub fn new<S: SlaveInputSink + 'static>(input_sink: S) -> std::io::Result<Self> {
let process_pty = PTY::open().expect("Could not create pty.");
let ptsname = process_pty.name().to_owned();
let (pty_input, pty_output) = process_pty.split_io();
thread::Builder::new()
.name("slave input thread".to_owned())
.spawn(move || {
read_slave_input_loop(input_sink, pty_output);
})?;
let mut pts = std::fs::OpenOptions::new()
.write(true)
.read(true)
.open(&ptsname)?;
use std::io::Write;
write!(pts, "")?;
Ok(Terminal {
terminal_window: RefCell::new(DualWindow::new()),
master_input_sink: RefCell::new(pty_input),
_slave_handle: pts,
slave_name: ptsname,
ansi_processor: Processor::new(),
})
}
pub fn add_byte_input(&mut self, bytes: &[u8]) {
use std::ops::DerefMut;
let mut window_ref = self.terminal_window.borrow_mut();
let mut sink_ref = self.master_input_sink.borrow_mut();
for byte in bytes.iter() {
self.ansi_processor
.advance(window_ref.deref_mut(), *byte, sink_ref.deref_mut());
}
}
pub fn slave_name(&self) -> &OsStr {
self.slave_name.as_ref()
}
fn process_input(&mut self, i: Input) {
use std::io::Write;
self.master_input_sink
.borrow_mut()
.write_all(i.raw.as_slice())
.expect("Write to terminal");
}
fn ensure_size(&self, w: Width, h: Height) {
let mut window = self.terminal_window.borrow_mut();
if w != window.get_width() || h != window.get_height() {
window.set_width(w);
window.set_height(h);
let w16 = w.raw_value() as u16;
let h16 = h.raw_value() as u16;
self.master_input_sink
.borrow_mut()
.resize(w16, h16, w16 , h16 )
.expect("Resize pty");
}
}
}
impl Writable for Terminal {
fn write(&mut self, c: char) -> OperationResult {
use std::io::Write;
write!(self.master_input_sink.borrow_mut(), "{}", c).expect("Write key to terminal");
Ok(())
}
}
impl Scrollable for Terminal {
fn scroll_forwards(&mut self) -> OperationResult {
self.terminal_window.borrow_mut().scroll_forwards()
}
fn scroll_backwards(&mut self) -> OperationResult {
self.terminal_window.borrow_mut().scroll_backwards()
}
fn scroll_to_beginning(&mut self) -> OperationResult {
self.terminal_window.borrow_mut().scroll_to_beginning()
}
fn scroll_to_end(&mut self) -> OperationResult {
self.terminal_window.borrow_mut().scroll_to_end()
}
}
impl Widget for Terminal {
fn space_demand(&self) -> Demand2D {
self.terminal_window.borrow().space_demand()
}
fn draw(&self, window: Window, hints: RenderingHints) {
self.ensure_size(window.get_width(), window.get_height());
self.terminal_window.borrow_mut().draw(window, hints);
}
}
impl<P: ?Sized> Container<P> for Terminal {
fn input(&mut self, input: Input, _: &mut P) -> Option<Input> {
input
.chain(
ScrollBehavior::new(self)
.forwards_on(Key::PageDown)
.backwards_on(Key::PageUp)
.to_beginning_on(Key::Home)
.to_end_on(Key::End),
)
.chain(PassthroughBehavior::new(self))
.finish()
}
}
#[cfg(test)]
impl Terminal {
fn write(&mut self, s: &str) {
self.add_byte_input(s.as_bytes());
}
}
#[cfg(test)]
mod test {
use super::*;
use unsegen::base::terminal::test::FakeTerminal;
use unsegen::base::GraphemeCluster;
struct FakeSlaveInputSink;
impl SlaveInputSink for FakeSlaveInputSink {
fn receive_bytes_from_pty(&mut self, _: Box<[u8]>) {}
}
fn test_terminal<F: Fn(&mut Terminal)>(window_dim: (u32, u32), after: &str, action: F) {
let mut term = FakeTerminal::with_size(window_dim);
{
let mut window = term.create_root_window();
window.fill(GraphemeCluster::try_from('_').unwrap());
let mut tw = Terminal::new(FakeSlaveInputSink).unwrap();
tw.terminal_window.get_mut().set_show_cursor(false);
action(&mut tw);
tw.draw(window, RenderingHints::default());
}
term.assert_looks_like(after);
}
#[test]
fn test_terminal_window_simple() {
test_terminal((5, 1), "_____", |w| w.write(""));
test_terminal((5, 1), "t____", |w| w.write("t"));
test_terminal((5, 1), "te___", |w| w.write("te"));
test_terminal((5, 1), "tes__", |w| w.write("tes"));
test_terminal((5, 1), "test_", |w| w.write("test"));
test_terminal((5, 1), "testy", |w| w.write("testy"));
test_terminal((5, 1), "o____", |w| w.write("testyo"));
test_terminal((2, 2), "te|st", |w| w.write("te\nst"));
}
}