diff options
| author | Aram Drevekenin <aram@poor.dev> | 2020-11-29 19:12:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-29 19:12:19 +0100 |
| commit | ea549f151e4e17d07177c4fb12af19f9c4905a16 (patch) | |
| tree | df8d4c2540150b49b2029679362cd7385f5e3f2a /src/input.rs | |
| parent | chore(infra): added clippy check. (#65) (diff) | |
| download | sl0ppij-ea549f151e4e17d07177c4fb12af19f9c4905a16.tar.gz sl0ppij-ea549f151e4e17d07177c4fb12af19f9c4905a16.tar.xz sl0ppij-ea549f151e4e17d07177c4fb12af19f9c4905a16.zip | |
fix(input): read stdin with lock (#72)
* fix(input): lock stdin when reading and use its own buffer
* style(format): make rustfmt happy
* fix(ipc): add accidentally removed trait
Diffstat (limited to 'src/input.rs')
| -rw-r--r-- | src/input.rs | 64 |
1 files changed, 25 insertions, 39 deletions
diff --git a/src/input.rs b/src/input.rs index 49092430..00ed0725 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,5 +1,4 @@ /// Module for handling input -use std::io::Read; use std::sync::mpsc::Sender; use crate::os_input_output::OsApi; @@ -9,9 +8,8 @@ use crate::AppInstruction; use crate::CommandIsExecuting; struct InputHandler { - buffer: [u8; 10], // TODO: more accurately mode: InputMode, - stdin: Box<dyn Read>, + os_input: Box<dyn OsApi>, command_is_executing: CommandIsExecuting, send_screen_instructions: Sender<ScreenInstruction>, send_pty_instructions: Sender<PtyInstruction>, @@ -27,9 +25,8 @@ impl InputHandler { send_app_instructions: Sender<AppInstruction>, ) -> Self { InputHandler { - buffer: [0; 10], // TODO: more accurately mode: InputMode::Normal, - stdin: os_input.get_stdin_reader(), + os_input, command_is_executing, send_screen_instructions, send_pty_instructions, @@ -57,16 +54,10 @@ impl InputHandler { assert_eq!(self.mode, InputMode::Normal); loop { - self.buffer = [0; 10]; - let _ = self - .stdin - .read(&mut self.buffer) - .expect("failed to read stdin"); - - match self.buffer { - [7, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { + let stdin_buffer = self.os_input.read_from_stdin(); + match stdin_buffer.as_slice() { + [7] => { // ctrl-g - // debug_log_to_file(format!("switched to command mode")); self.mode = InputMode::Command; return; } @@ -75,7 +66,7 @@ impl InputHandler { .send(ScreenInstruction::ClearScroll) .unwrap(); self.send_screen_instructions - .send(ScreenInstruction::WriteCharacter(self.buffer)) + .send(ScreenInstruction::WriteCharacter(stdin_buffer)) .unwrap(); } } @@ -92,15 +83,12 @@ impl InputHandler { } loop { - self.buffer = [0; 10]; - let _ = self - .stdin - .read(&mut self.buffer) - .expect("failed to read stdin"); + let stdin_buffer = self.os_input.read_from_stdin(); // uncomment this to print the entered character to a log file (/tmp/mosaic/mosaic-log.txt) for debugging - // debug_log_to_file(format!("buffer {:?}", self.buffer)); - match self.buffer { - [7, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { + // debug_log_to_file(format!("buffer {:?}", stdin_buffer)); + + match stdin_buffer.as_slice() { + [7] => { // Ctrl-g // If we're in command mode, this will let us switch to persistent command mode, to execute // multiple commands. If we're already in persistent mode, it'll return us to normal mode. @@ -108,49 +96,47 @@ impl InputHandler { InputMode::Command => self.mode = InputMode::CommandPersistent, InputMode::CommandPersistent => { self.mode = InputMode::Normal; - // debug_log_to_file(format!("switched to normal mode")); return; } _ => panic!(), } } - [27, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { + [27] => { // Esc self.mode = InputMode::Normal; - // _debug_log_to_file(format!("switched to normal mode")); return; } - [106, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { + [106] => { // j self.send_screen_instructions .send(ScreenInstruction::ResizeDown) .unwrap(); } - [107, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { + [107] => { // k self.send_screen_instructions .send(ScreenInstruction::ResizeUp) .unwrap(); } - [112, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { + [112] => { // p self.send_screen_instructions .send(ScreenInstruction::MoveFocus) .unwrap(); } - [104, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { + [104] => { // h self.send_screen_instructions .send(ScreenInstruction::ResizeLeft) .unwrap(); } - [108, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { + [108] => { // l self.send_screen_instructions .send(ScreenInstruction::ResizeRight) .unwrap(); } - [122, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { + [122] => { // z self.command_is_executing.opening_new_pane(); self.send_pty_instructions @@ -158,7 +144,7 @@ impl InputHandler { .unwrap(); self.command_is_executing.wait_until_new_pane_is_opened(); } - [110, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { + [110] => { // n self.command_is_executing.opening_new_pane(); self.send_pty_instructions @@ -166,7 +152,7 @@ impl InputHandler { .unwrap(); self.command_is_executing.wait_until_new_pane_is_opened(); } - [98, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { + [98] => { // b self.command_is_executing.opening_new_pane(); self.send_pty_instructions @@ -174,24 +160,24 @@ impl InputHandler { .unwrap(); self.command_is_executing.wait_until_new_pane_is_opened(); } - [113, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { + [113] => { // q self.mode = InputMode::Exiting; return; } - [27, 91, 53, 126, 0, 0, 0, 0, 0, 0] => { + [27, 91, 53, 126] => { // PgUp self.send_screen_instructions .send(ScreenInstruction::ScrollUp) .unwrap(); } - [27, 91, 54, 126, 0, 0, 0, 0, 0, 0] => { + [27, 91, 54, 126] => { // PgDown self.send_screen_instructions .send(ScreenInstruction::ScrollDown) .unwrap(); } - [120, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { + [120] => { // x self.command_is_executing.closing_pane(); self.send_screen_instructions @@ -199,7 +185,7 @@ impl InputHandler { .unwrap(); self.command_is_executing.wait_until_pane_is_closed(); } - [101, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { + [101] => { // e self.send_screen_instructions .send(ScreenInstruction::ToggleActiveTerminalFullscreen) |
