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
|
use super::actions::Action;
use super::keybinds::get_default_keybinds;
use crate::common::{update_state, AppInstruction, AppState, SenderWithContext, OPENCALLS};
/// Module for handling input
use crate::errors::ContextType;
use crate::os_input_output::OsApi;
use crate::pty_bus::PtyInstruction;
use crate::screen::ScreenInstruction;
use crate::wasm_vm::PluginInstruction;
use crate::CommandIsExecuting;
use serde::{Deserialize, Serialize};
use strum_macros::EnumIter;
use termion::input::TermReadEventsAndRaw;
use super::keybinds::key_to_action;
struct InputHandler {
mode: InputMode,
mode_is_persistent: bool,
os_input: Box<dyn OsApi>,
command_is_executing: CommandIsExecuting,
send_screen_instructions: SenderWithContext<ScreenInstruction>,
send_pty_instructions: SenderWithContext<PtyInstruction>,
send_plugin_instructions: SenderWithContext<PluginInstruction>,
send_app_instructions: SenderWithContext<AppInstruction>,
}
impl InputHandler {
fn new(
os_input: Box<dyn OsApi>,
command_is_executing: CommandIsExecuting,
send_screen_instructions: SenderWithContext<ScreenInstruction>,
send_pty_instructions: SenderWithContext<PtyInstruction>,
send_plugin_instructions: SenderWithContext<PluginInstruction>,
send_app_instructions: SenderWithContext<AppInstruction>,
) -> Self {
InputHandler {
mode: InputMode::Normal,
mode_is_persistent: false,
os_input,
command_is_executing,
send_screen_instructions,
send_pty_instructions,
send_plugin_instructions,
send_app_instructions,
}
}
/// Main event loop
fn get_input(&mut self) {
let mut err_ctx = OPENCALLS.with(|ctx| *ctx.borrow());
err_ctx.add_call(ContextType::StdinHandler);
self.send_pty_instructions.update(err_ctx);
self.send_app_instructions.update(err_ctx);
self.send_screen_instructions.update(err_ctx);
if let Ok(keybinds) = get_default_keybinds() {
'input_loop: loop {
let entry_mode = self.mode;
//@@@ I think this should actually just iterate over stdin directly
let stdin_buffer = self.os_input.read_from_stdin();
drop(
self.send_plugin_instructions
.send(PluginInstruction::GlobalInput(stdin_buffer.clone())),
);
for key_result in stdin_buffer.events_and_raw() {
match key_result {
Ok((event, raw_bytes)) => match event {
termion::event::Event::Key(key) => {
let should_break = self.dispatch_action(key_to_action(
&key, raw_bytes, &self.mode, &keybinds,
));
//@@@ This is a hack until we dispatch more than one action per key stroke
if entry_mode == self.mode && !self.mode_is_persistent {
self.mode = InputMode::Normal;
update_state(&self.send_app_instructions, |_| AppState {
input_state: InputState {
mode: self.mode,
persistent: self.mode_is_persistent,
},
});
}
if should_break {
break 'input_loop;
}
}
termion::event::Event::Mouse(_)
| termion::event::Event::Unsupported(_) => {
unimplemented!("Mouse and unsupported events aren't supported!");
}
},
Err(err) => panic!("Encountered read error: {:?}", err),
}
}
}
} else {
//@@@ Error handling?
self.exit();
}
}
fn dispatch_action(&mut self, action: Action) -> bool {
let mut interrupt_loop = false;
match action {
Action::Write(val) => {
self.send_screen_instructions
.send(ScreenInstruction::ClearScroll)
.unwrap();
self.send_screen_instructions
.send(ScreenInstruction::WriteCharacter(val))
.unwrap();
}
Action::Quit => {
self.exit();
interrupt_loop = true;
}
Action::SwitchToMode(mode) => {
self.mode = mode;
if mode == InputMode::Normal {
self.mode_is_persistent = false;
}
update_state(&self.send_app_instructions, |_| AppState {
input_state: InputState {
mode: self.mode,
persistent: self.mode_is_persistent,
},
});
self.send_screen_instructions
.send(ScreenInstruction::Render)
.unwrap();
}
Action::TogglePersistentMode => {
self.mode_is_persistent = !self.mode_is_persistent;
update_state(&self.send_app_instructions, |_| AppState {
input_state: InputState {
mode: self.mode,
persistent: self.mode_is_persistent,
},
});
}
Action::Resize(direction) => {
let screen_instr = match direction {
super::actions::Direction::Left => ScreenInstruction::ResizeLeft,
super::actions::Direction::Right => ScreenInstruction::ResizeRight,
super::actions::Direction::Up => ScreenInstruction::ResizeUp,
super::actions::Direction::Down => ScreenInstruction::ResizeDown,
};
self.send_screen_instructions.send(screen_instr).unwrap();
}
Action::SwitchFocus(_) => {
self.send_screen_instructions
.send(ScreenInstruction::MoveFocus)
.unwrap();
}
Action::MoveFocus(direction) => {
let screen_instr = match direction {
super::actions::Direction::Left => ScreenInstruction::MoveFocusLeft,
super::actions::Direction::Right => ScreenInstruction::MoveFocusRight,
super::actions::Direction::Up => ScreenInstruction::MoveFocusUp,
super::actions::Direction::Down => ScreenInstruction::MoveFocusDown,
};
self.send_screen_instructions.send(screen_instr).unwrap();
}
Action::ScrollUp => {
self.send_screen_instructions
.send(ScreenInstruction::ScrollUp)
.unwrap();
}
Action::ScrollDown => {
self.send_screen_instructions
.send(ScreenInstruction::ScrollDown)
.unwrap();
}
Action::ToggleFocusFullscreen => {
self.send_screen_instructions
.send(ScreenInstruction::ToggleActiveTerminalFullscreen)
.unwrap();
}
Action::NewPane(direction) => {
let pty_instr = match direction {
Some(super::actions::Direction::Left) => {
PtyInstruction::SpawnTerminalVertically(None)
}
Some(super::actions::Direction::Right) => {
PtyInstruction::SpawnTerminalVertically(None)
}
Some(super::actions::Direction::Up) => {
PtyInstruction::SpawnTerminalHorizontally(None)
}
Some(super::actions::Direction::Down) => {
PtyInstruction::SpawnTerminalHorizontally(None)
}
// No direction specified - try to put it in the biggest available spot
None => PtyInstruction::SpawnTerminal(None),
};
self.command_is_executing.opening_new_pane();
self.send_pty_instructions.send(pty_instr).unwrap();
self.command_is_executing.wait_until_new_pane_is_opened();
}
Action::CloseFocus => {
self.command_is_executing.closing_pane();
self.send_screen_instructions
.send(ScreenInstruction::CloseFocusedPane)
.unwrap();
self.command_is_executing.wait_until_pane_is_closed();
}
Action::NewTab => {
self.command_is_executing.opening_new_pane();
self.send_pty_instructions
.send(PtyInstruction::NewTab)
.unwrap();
self.command_is_executing.wait_until_new_pane_is_opened();
}
Action::GoToNextTab => {
self.send_screen_instructions
.send(ScreenInstruction::SwitchTabNext)
.unwrap();
}
Action::GoToPreviousTab => {
self.send_screen_instructions
.send(ScreenInstruction::SwitchTabPrev)
.unwrap();
}
Action::CloseTab => {
self.command_is_executing.closing_pane();
self.send_screen_instructions
.send(ScreenInstruction::CloseTab)
.unwrap();
self.command_is_executing.wait_until_pane_is_closed();
}
}
interrupt_loop
}
/// Routine to be called when the input handler exits (at the moment this is the
/// same as quitting mosaic)
fn exit(&mut self) {
self.send_app_instructions
.send(AppInstruction::Exit)
.unwrap();
}
}
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize)]
pub struct InputState {
mode: InputMode,
persistent: bool,
}
impl Default for InputState {
fn default() -> InputState {
InputState {
mode: InputMode::Normal,
persistent: false,
}
}
}
/// Dictates whether we're in command mode, persistent command mode, normal mode or exiting:
/// - Normal mode either writes characters to the terminal, or switches to command mode
/// using a particular key control
/// - Command mode intercepts characters to control mosaic itself, before switching immediately
/// back to normal mode
/// - Persistent command mode is the same as command mode, but doesn't return automatically to
/// normal mode
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, EnumIter, Serialize, Deserialize)]
pub enum InputMode {
Normal,
Command,
Resize,
Pane,
Tab,
Scroll,
Exiting,
}
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct Help {
pub mode: InputMode,
pub mode_is_persistent: bool,
pub keybinds: Vec<(String, String)>, // <shortcut> => <shortcut description>
}
impl Default for InputMode {
fn default() -> InputMode {
InputMode::Normal
}
}
pub fn get_help(input_state: &InputState) -> Help {
let mut keybinds: Vec<(String, String)> = vec![];
match input_state.mode {
InputMode::Normal | InputMode::Command | InputMode::Exiting => {
keybinds.push((format!("p"), format!("Pane mode")));
keybinds.push((format!("t"), format!("Tab mode")));
keybinds.push((format!("r"), format!("Resize mode")));
}
InputMode::Resize => {
keybinds.push((format!("←↓↑→"), format!("resize pane")));
}
InputMode::Pane => {
keybinds.push((format!("←↓↑→"), format!("move focus")));
keybinds.push((format!("p"), format!("next pane")));
keybinds.push((format!("n"), format!("new pane")));
keybinds.push((format!("d"), format!("down split")));
keybinds.push((format!("r"), format!("right split")));
keybinds.push((format!("x"), format!("exit pane")));
keybinds.push((format!("f"), format!("fullscreen pane")));
}
InputMode::Tab => {
keybinds.push((format!("←↓↑→"), format!("move tab focus")));
keybinds.push((format!("n"), format!("new tab")));
keybinds.push((format!("x"), format!("exit tab")));
}
InputMode::Scroll => {
keybinds.push((format!("↓↑"), format!("scroll up/down")));
}
}
keybinds.push((format!("ESC"), format!("Back")));
keybinds.push((format!("q"), format!("Quit")));
Help {
mode: input_state.mode,
mode_is_persistent: input_state.persistent,
keybinds,
}
}
/// Entry point to the module that instantiates a new InputHandler and calls its
/// reading loop
pub fn input_loop(
os_input: Box<dyn OsApi>,
command_is_executing: CommandIsExecuting,
send_screen_instructions: SenderWithContext<ScreenInstruction>,
send_pty_instructions: SenderWithContext<PtyInstruction>,
send_plugin_instructions: SenderWithContext<PluginInstruction>,
send_app_instructions: SenderWithContext<AppInstruction>,
) {
let _handler = InputHandler::new(
os_input,
command_is_executing,
send_screen_instructions,
send_pty_instructions,
send_plugin_instructions,
send_app_instructions,
)
.get_input();
}
|