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

View file

@ -24,7 +24,6 @@ fn into_raw_mode(pid: RawFd) {
Ok(_) => {},
Err(e) => panic!("error {:?}", e)
};
}
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) {
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) => {
let pid_primary = fork_pty_res.master;
let pid_secondary = match fork_pty_res.fork_result {

View file

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