Skip to content

Commit

Permalink
Fix broken SIGWINCH detection / window resizing caused by clashing si…
Browse files Browse the repository at this point in the history
…gnal handler registered by rustyline.
  • Loading branch information
PaulJuliusMartinez committed Mar 3, 2022
1 parent b681708 commit 880ef2d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Bug Fixes:
status bar to be highlighted and copied.
- [Issue #61]: Display error message for unrecognized CSI escape
sequences and other IO errors instead of panicking.
- [Issue #62]: Fix broken window resizing / SIGWINCH detection caused
by clashing signal handler registered by rustyline.


v0.7.2 (2022-02-20)
Expand Down
8 changes: 7 additions & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const BUFFER_SIZE: usize = 1024;

const ESCAPE: u8 = 0o33;

pub fn get_input() -> impl Iterator<Item = io::Result<TuiEvent>> {
pub fn remap_dev_tty_to_stdin() {
// The readline library we use, rustyline, always gets its input from STDIN.
// If jless accepts its input from STDIN, then rustyline can't accept input.
// To fix this, we open up /dev/tty, and remap it to STDIN, as suggested in
Expand All @@ -30,8 +30,14 @@ pub fn get_input() -> impl Iterator<Item = io::Result<TuiEvent>> {
let path = std::ffi::CString::new("r").unwrap();
let _ = libc::freopen(filename.as_ptr(), path.as_ptr(), libc_stdhandle::stdin());
}
}

pub fn get_input() -> impl Iterator<Item = io::Result<TuiEvent>> {
let (sigwinch_read, sigwinch_write) = UnixStream::pair().unwrap();
// NOTE: This overrides the SIGWINCH handler registered by rustyline.
// We should maybe get a reference to the existing signal handler
// and call it when appropriate, but it seems to only be used to handle
// line wrapping, and it seems to work fine without it.
pipe::register(SIGWINCH, sigwinch_write).unwrap();
TuiInput::new(stdin(), sigwinch_read)
}
Expand Down
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ fn main() {
std::process::exit(0);
}

// Create our input *before* constructing the App. When we get the input,
// we use freopen to remap /dev/tty to STDIN so that rustyline works when
// We use freopen to remap /dev/tty to STDIN so that rustyline works when
// JSON input is provided via STDIN. rustyline gets initialized when we
// create the App, so by putting this before, we make sure rustyline gets
// the /dev/tty input.
let input = Box::new(input::get_input());
// create the App, so by putting this before creating the app, we make
// sure rustyline gets the /dev/tty input.
input::remap_dev_tty_to_stdin();

let stdout = MouseTerminal::from(HideCursor::from(AlternateScreen::from(
io::stdout().into_raw_mode().unwrap(),
)));
Expand All @@ -77,7 +77,7 @@ fn main() {
}
};

app.run(input);
app.run(Box::new(input::get_input()));
}

fn print_pretty_printed_input(input: String, data_format: DataFormat) {
Expand Down

0 comments on commit 880ef2d

Please sign in to comment.