From e7fa88dcd4b261af643a9aedfce0f0a1d9a97462 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 11 Nov 2021 15:19:31 -0500 Subject: handle dynamic terminal sizes --- src/state.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src/state.rs') diff --git a/src/state.rs b/src/state.rs index 854c306..2d5154a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -5,21 +5,23 @@ pub struct State { history: crate::history::History, focus: Focus, output: textmode::Output, + action: async_std::channel::Sender, } impl State { pub fn new( - actions: async_std::channel::Sender, + action: async_std::channel::Sender, output: textmode::Output, ) -> Self { - let readline = crate::readline::Readline::new(actions.clone()); - let history = crate::history::History::new(actions); + let readline = crate::readline::Readline::new(action.clone()); + let history = crate::history::History::new(action.clone()); let focus = Focus::Readline; Self { readline, history, focus, output, + action, } } @@ -52,6 +54,13 @@ impl State { self.focus = new_focus; self.render().await.unwrap(); } + crate::action::Action::Resize(new_size) => { + self.readline.resize(new_size).await; + self.history.resize(new_size).await; + self.output.set_size(new_size.0, new_size.1); + self.output.clear(); + self.render().await.unwrap(); + } } } @@ -61,6 +70,17 @@ impl State { Focus::History(idx) => self.history.handle_key(key, idx).await, } } + + pub async fn resize(&mut self) { + let size = terminal_size::terminal_size().map_or( + (24, 80), + |(terminal_size::Width(w), terminal_size::Height(h))| (h, w), + ); + self.action + .send(crate::action::Action::Resize(size)) + .await + .unwrap(); + } } #[derive(Copy, Clone, Debug)] -- cgit v1.2.3-54-g00ecf