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
|
mod threads;
use crate::window::{Area, Statusbar, Window};
use std::io::{Stdin, Write};
use termion::event::Key;
use termion::input::TermRead;
pub use threads::Threads;
pub struct Client {
window: Window,
buffers: Vec<Buffer>,
statusbar: Statusbar,
}
impl Client {
pub fn new<W: Write>(initial_buffer: Buffer, out: &mut W) -> Self {
let size = termion::terminal_size().unwrap();
let mut window = Window::new();
let statusbar = Statusbar::new(String::from("Hello world!"));
match &initial_buffer {
Buffer::Threads(t) => t.fill_window(&mut window),
}
window.draw(out, Area {
x: 1,
y: 1,
w: size.0,
h: size.1 - 2,
}).unwrap();
statusbar.draw(out, Area {
x: 1,
y: size.1,
w: size.0,
h: 1,
}).unwrap();
Self {
window,
buffers: vec![initial_buffer],
statusbar,
}
}
pub fn run<W: Write>(mut self, out: &mut W, stdin: Stdin) {
let mut buffer = Vec::new();
for c in stdin.keys() {
let c = c.unwrap();
// Global keybinds
match c {
Key::Char('q') => {
self.buffers.pop().unwrap();
if self.buffers.is_empty() {
break;
}
},
_ => ()
}
let next_buffer = match self.buffers.last_mut().unwrap() {
Buffer::Threads(s) => s.tick(&mut self.window, c),
};
if let Some(next_buffer) = next_buffer {
self.buffers.push(next_buffer);
}
write!(out, "{}", termion::clear::All).unwrap();
let size = termion::terminal_size().unwrap();
self.window.draw(&mut buffer, Area {
x: 1,
y: 1,
w: size.0,
h: size.1 - 2,
}).unwrap();
self.statusbar.draw(&mut buffer, Area {
x: 1,
y: size.1,
w: size.0,
h: 1,
}).unwrap();
out.write(&buffer).unwrap();
out.flush().unwrap();
buffer.clear();
}
}
}
pub enum Buffer {
Threads(Threads),
}
impl Buffer {
pub fn name(&self) -> String {
match self {
Buffer::Threads(t) => t.name(),
}
}
}
|