aboutsummaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
authorAram Drevekenin <aram@poor.dev>2021-04-01 17:38:13 +0200
committerGitHub <noreply@github.com>2021-04-01 17:38:13 +0200
commit1739f370f98923d8955e1478bcda564220294829 (patch)
tree1f8f4b2c7f5696787a5be65057ef04c1c5101436 /src/common
parentMerge pull request #244 from a-kenji/main (diff)
downloadsl0ppij-1739f370f98923d8955e1478bcda564220294829.tar.gz
sl0ppij-1739f370f98923d8955e1478bcda564220294829.tar.xz
sl0ppij-1739f370f98923d8955e1478bcda564220294829.zip
* fix(ui): react to terminal window size changes (SIGWINCH) (#245)
* fix(ui): react to terminal window size changes (SIGWINCH) * style(fmt): rustfmt * style(format): remove some warnings * style(fmt): rustfmt
Diffstat (limited to 'src/common')
-rw-r--r--src/common/errors.rs4
-rw-r--r--src/common/mod.rs16
-rw-r--r--src/common/os_input_output.rs32
-rw-r--r--src/common/screen.rs10
-rw-r--r--src/common/utils/shared.rs13
5 files changed, 67 insertions, 8 deletions
diff --git a/src/common/errors.rs b/src/common/errors.rs
index 56bf27f4..00284cd4 100644
--- a/src/common/errors.rs
+++ b/src/common/errors.rs
@@ -42,7 +42,7 @@ pub fn handle_panic(
msg,
location.file(),
location.line(),
- backtrace
+ backtrace,
),
(Some(location), None) => format!(
"{}\n\u{1b}[0;0mError: \u{1b}[0;31mthread '{}' panicked: {}:{}\n\u{1b}[0;0m{:?}",
@@ -200,6 +200,7 @@ pub enum ScreenContext {
CloseTab,
GoToTab,
UpdateTabName,
+ TerminalResize,
ChangeMode,
}
@@ -241,6 +242,7 @@ impl From<&ScreenInstruction> for ScreenContext {
ScreenInstruction::CloseTab => ScreenContext::CloseTab,
ScreenInstruction::GoToTab(_) => ScreenContext::GoToTab,
ScreenInstruction::UpdateTabName(_) => ScreenContext::UpdateTabName,
+ ScreenInstruction::TerminalResize => ScreenContext::TerminalResize,
ScreenInstruction::ChangeMode(_) => ScreenContext::ChangeMode,
}
}
diff --git a/src/common/mod.rs b/src/common/mod.rs
index a566d655..87d8cd94 100644
--- a/src/common/mod.rs
+++ b/src/common/mod.rs
@@ -389,6 +389,9 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
ScreenInstruction::UpdateTabName(c) => {
screen.update_active_tab_name(c);
}
+ ScreenInstruction::TerminalResize => {
+ screen.resize_to_screen();
+ }
ScreenInstruction::ChangeMode(mode_info) => {
screen.change_mode(mode_info);
}
@@ -506,6 +509,19 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs) {
})
.unwrap();
+ let _signal_thread = thread::Builder::new()
+ .name("signal_listener".to_string())
+ .spawn({
+ let os_input = os_input.clone();
+ let send_screen_instructions = send_screen_instructions.clone();
+ move || {
+ os_input.receive_sigwinch(Box::new(move || {
+ let _ = send_screen_instructions.send(ScreenInstruction::TerminalResize);
+ }));
+ }
+ })
+ .unwrap();
+
// TODO: currently we don't wait for this to quit
// because otherwise the app will hang. Need to fix this so it both
// listens to the ipc-bus and is able to quit cleanly
diff --git a/src/common/os_input_output.rs b/src/common/os_input_output.rs
index 043a9aea..de0c6a3b 100644
--- a/src/common/os_input_output.rs
+++ b/src/common/os_input_output.rs
@@ -13,6 +13,8 @@ use std::path::PathBuf;
use std::process::{Child, Command};
use std::sync::{Arc, Mutex};
+use signal_hook::{consts::signal::*, iterator::Signals};
+
use std::env;
fn into_raw_mode(pid: RawFd) {
@@ -65,7 +67,7 @@ pub fn set_terminal_size_using_fd(fd: RawFd, columns: u16, rows: u16) {
/// process exits.
fn handle_command_exit(mut child: Child) {
// register the SIGINT signal (TODO handle more signals)
- let signals = ::signal_hook::iterator::Signals::new(&[::signal_hook::SIGINT]).unwrap();
+ let mut signals = ::signal_hook::iterator::Signals::new(&[SIGINT]).unwrap();
'handle_exit: loop {
// test whether the child process has exited
match child.try_wait() {
@@ -82,10 +84,15 @@ fn handle_command_exit(mut child: Child) {
}
for signal in signals.pending() {
- if signal == signal_hook::SIGINT {
- child.kill().unwrap();
- child.wait().unwrap();
- break 'handle_exit;
+ // FIXME: We need to handle more signals here!
+ #[allow(clippy::single_match)]
+ match signal {
+ SIGINT => {
+ child.kill().unwrap();
+ child.wait().unwrap();
+ break 'handle_exit;
+ }
+ _ => {}
}
}
}
@@ -188,6 +195,7 @@ pub trait OsApi: Send + Sync {
fn get_stdout_writer(&self) -> Box<dyn io::Write>;
/// Returns a [`Box`] pointer to this [`OsApi`] struct.
fn box_clone(&self) -> Box<dyn OsApi>;
+ fn receive_sigwinch(&self, cb: Box<dyn Fn()>);
}
impl OsApi for OsInputOutput {
@@ -238,6 +246,20 @@ impl OsApi for OsInputOutput {
waitpid(Pid::from_raw(pid), None).unwrap();
Ok(())
}
+ fn receive_sigwinch(&self, cb: Box<dyn Fn()>) {
+ let mut signals = Signals::new(&[SIGWINCH, SIGTERM, SIGINT, SIGQUIT]).unwrap();
+ for signal in signals.forever() {
+ match signal {
+ SIGWINCH => {
+ cb();
+ }
+ SIGTERM | SIGINT | SIGQUIT => {
+ break;
+ }
+ _ => unreachable!(),
+ }
+ }
+ }
}
impl Clone for Box<dyn OsApi> {
diff --git a/src/common/screen.rs b/src/common/screen.rs
index 178e8be9..121f21bd 100644
--- a/src/common/screen.rs
+++ b/src/common/screen.rs
@@ -50,6 +50,7 @@ pub enum ScreenInstruction {
CloseTab,
GoToTab(u32),
UpdateTabName(Vec<u8>),
+ TerminalResize,
ChangeMode(ModeInfo),
}
@@ -213,6 +214,15 @@ impl Screen {
}
}
+ pub fn resize_to_screen(&mut self) {
+ let new_screen_size = self.os_api.get_terminal_size_using_fd(0);
+ self.full_screen_ws = new_screen_size;
+ for (_, tab) in self.tabs.iter_mut() {
+ tab.resize_whole_tab(new_screen_size);
+ }
+ self.render();
+ }
+
/// Renders this [`Screen`], which amounts to rendering its active [`Tab`].
pub fn render(&mut self) {
if let Some(active_tab) = self.get_active_tab_mut() {
diff --git a/src/common/utils/shared.rs b/src/common/utils/shared.rs
index 9717837a..93892d4f 100644
--- a/src/common/utils/shared.rs
+++ b/src/common/utils/shared.rs
@@ -11,9 +11,18 @@ fn ansi_len(s: &str) -> usize {
.count()
}
-pub fn pad_to_size(s: &str, rows: usize, columns: usize) -> String {
+pub fn adjust_to_size(s: &str, rows: usize, columns: usize) -> String {
s.lines()
- .map(|l| [l, &str::repeat(" ", columns - ansi_len(l))].concat())
+ .map(|l| {
+ let actual_len = ansi_len(l);
+ if actual_len > columns {
+ let mut line = String::from(l);
+ line.truncate(columns);
+ return line;
+ } else {
+ return [l, &str::repeat(" ", columns - ansi_len(l))].concat();
+ }
+ })
.chain(iter::repeat(str::repeat(" ", columns)))
.take(rows)
.collect::<Vec<_>>()