fix(compatibility): better stdin reading to fix various issues (#28)

This commit is contained in:
Aram Drevekenin 2020-11-07 19:20:13 +01:00 committed by GitHub
parent 13af16b336
commit 393bca0d39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 41 deletions

View file

@ -151,8 +151,8 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
ScreenInstruction::VerticalSplit(pid) => { ScreenInstruction::VerticalSplit(pid) => {
screen.vertical_split(pid); screen.vertical_split(pid);
} }
ScreenInstruction::WriteCharacter(byte) => { ScreenInstruction::WriteCharacter(bytes) => {
screen.write_to_active_terminal(byte); screen.write_to_active_terminal(bytes);
} }
ScreenInstruction::ResizeLeft => { ScreenInstruction::ResizeLeft => {
screen.resize_left(); screen.resize_left();
@ -240,41 +240,58 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: Opt) {
}).unwrap(); }).unwrap();
let mut stdin = os_input.get_stdin_reader(); let mut stdin = os_input.get_stdin_reader();
loop { loop {
let mut buffer = [0; 1]; let mut buffer = [0; 10]; // TODO: more accurately
stdin.read(&mut buffer).expect("failed to read stdin"); stdin.read(&mut buffer).expect("failed to read stdin");
if buffer[0] == 10 { // ctrl-j // uncomment this to print the entered character to a log file (/tmp/mosaic-log.txt) for debugging
send_screen_instructions.send(ScreenInstruction::ResizeDown).unwrap(); // _debug_log_to_file(format!("buffer {:?}", buffer));
} else if buffer[0] == 11 { // ctrl-k match buffer {
send_screen_instructions.send(ScreenInstruction::ResizeUp).unwrap(); [10, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-j
} else if buffer[0] == 16 { // ctrl-p send_screen_instructions.send(ScreenInstruction::ResizeDown).unwrap();
send_screen_instructions.send(ScreenInstruction::MoveFocus).unwrap(); },
} else if buffer[0] == 8 { // ctrl-h [11, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-k
send_screen_instructions.send(ScreenInstruction::ResizeLeft).unwrap(); send_screen_instructions.send(ScreenInstruction::ResizeUp).unwrap();
} else if buffer[0] == 12 { // ctrl-l },
send_screen_instructions.send(ScreenInstruction::ResizeRight).unwrap(); [16, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-p
} else if buffer[0] == 26 { // ctrl-z send_screen_instructions.send(ScreenInstruction::MoveFocus).unwrap();
send_pty_instructions.send(PtyInstruction::SpawnTerminal(None)).unwrap(); },
} else if buffer[0] == 14 { // ctrl-n [8, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-h
send_pty_instructions.send(PtyInstruction::SpawnTerminalVertically(None)).unwrap(); send_screen_instructions.send(ScreenInstruction::ResizeLeft).unwrap();
} else if buffer[0] == 2 { // ctrl-b },
send_pty_instructions.send(PtyInstruction::SpawnTerminalHorizontally(None)).unwrap(); [12, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-l
} else if buffer[0] == 17 { // ctrl-q send_screen_instructions.send(ScreenInstruction::ResizeRight).unwrap();
send_screen_instructions.send(ScreenInstruction::Quit).unwrap(); },
send_pty_instructions.send(PtyInstruction::Quit).unwrap(); [26, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-z
break; send_pty_instructions.send(PtyInstruction::SpawnTerminal(None)).unwrap();
} else if buffer[0] == 27 { // ctrl-[ },
send_screen_instructions.send(ScreenInstruction::ScrollUp).unwrap(); [14, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-n
} else if buffer[0] == 29 { // ctrl-] send_pty_instructions.send(PtyInstruction::SpawnTerminalVertically(None)).unwrap();
send_screen_instructions.send(ScreenInstruction::ScrollDown).unwrap(); },
} else if buffer[0] == 24 { // ctrl-x [2, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-b
send_screen_instructions.send(ScreenInstruction::CloseFocusedPane).unwrap(); send_pty_instructions.send(PtyInstruction::SpawnTerminalHorizontally(None)).unwrap();
} else if buffer[0] == 5 { // ctrl-e },
send_screen_instructions.send(ScreenInstruction::ToggleActiveTerminalFullscreen).unwrap(); [17, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-q
} else { send_screen_instructions.send(ScreenInstruction::Quit).unwrap();
// println!("\r buffer {:?} ", buffer[0]); send_pty_instructions.send(PtyInstruction::Quit).unwrap();
send_screen_instructions.send(ScreenInstruction::ClearScroll).unwrap(); break;
send_screen_instructions.send(ScreenInstruction::WriteCharacter(buffer[0])).unwrap(); },
[27, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-[
send_screen_instructions.send(ScreenInstruction::ScrollUp).unwrap();
},
[29, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-]
send_screen_instructions.send(ScreenInstruction::ScrollDown).unwrap();
},
[24, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-x
send_screen_instructions.send(ScreenInstruction::CloseFocusedPane).unwrap();
},
[5, 0, 0, 0, 0, 0, 0, 0, 0, 0] => { // ctrl-e
send_screen_instructions.send(ScreenInstruction::ToggleActiveTerminalFullscreen).unwrap();
},
_ => {
send_screen_instructions.send(ScreenInstruction::ClearScroll).unwrap();
send_screen_instructions.send(ScreenInstruction::WriteCharacter(buffer)).unwrap();
}
} }
}; };

View file

@ -24,7 +24,6 @@ fn into_raw_mode(pid: RawFd) {
Ok(_) => {}, Ok(_) => {},
Err(e) => panic!("error {:?}", e) Err(e) => panic!("error {:?}", e)
}; };
} }
pub fn get_terminal_size_using_fd(fd: RawFd) -> Winsize { pub fn get_terminal_size_using_fd(fd: RawFd) -> Winsize {
@ -94,7 +93,8 @@ fn handle_command_exit(mut child: Child) {
fn spawn_terminal (file_to_open: Option<PathBuf>) -> (RawFd, RawFd) { fn spawn_terminal (file_to_open: Option<PathBuf>) -> (RawFd, RawFd) {
let (pid_primary, pid_secondary): (RawFd, RawFd) = { let (pid_primary, pid_secondary): (RawFd, RawFd) = {
match forkpty(None, None) { let current_termios = tcgetattr(0).unwrap();
match forkpty(None, Some(&current_termios)) {
Ok(fork_pty_res) => { Ok(fork_pty_res) => {
let pid_primary = fork_pty_res.master; let pid_primary = fork_pty_res.master;
let pid_secondary = match fork_pty_res.fork_result { let pid_secondary = match fork_pty_res.fork_result {

View file

@ -62,7 +62,7 @@ pub enum ScreenInstruction {
NewPane(RawFd), NewPane(RawFd),
HorizontalSplit(RawFd), HorizontalSplit(RawFd),
VerticalSplit(RawFd), VerticalSplit(RawFd),
WriteCharacter(u8), WriteCharacter([u8; 10]),
ResizeLeft, ResizeLeft,
ResizeRight, ResizeRight,
ResizeDown, ResizeDown,
@ -257,10 +257,9 @@ impl Screen {
let terminal_output = self.terminals.get_mut(&pid).unwrap(); let terminal_output = self.terminals.get_mut(&pid).unwrap();
terminal_output.handle_event(event); terminal_output.handle_event(event);
} }
pub fn write_to_active_terminal(&mut self, byte: u8) { pub fn write_to_active_terminal(&mut self, mut bytes: [u8; 10]) {
if let Some(active_terminal_id) = &self.get_active_terminal_id() { if let Some(active_terminal_id) = &self.get_active_terminal_id() {
let mut buffer = [byte]; self.os_api.write_to_tty_stdin(*active_terminal_id, &mut bytes).expect("failed to write to terminal");
self.os_api.write_to_tty_stdin(*active_terminal_id, &mut buffer).expect("failed to write to terminal");
self.os_api.tcdrain(*active_terminal_id).expect("failed to drain terminal"); self.os_api.tcdrain(*active_terminal_id).expect("failed to drain terminal");
} }
} }