From b96b4a50d7f38ac0d884f677d4e1c4172c3f19ae Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 11 Dec 2021 02:14:03 -0500 Subject: simplify also avoid releasing and reacquiring the entry lock multiple times within a single handler, to avoid time of check to time of use issues --- src/history.rs | 44 ++++++++++++++++---------------------------- src/state.rs | 15 +++++++-------- 2 files changed, 23 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/history.rs b/src/history.rs index fbcd2d8..4c77aad 100644 --- a/src/history.rs +++ b/src/history.rs @@ -126,27 +126,11 @@ impl History { Ok(self.entries.len() - 1) } - pub async fn toggle_fullscreen(&mut self, idx: usize) { - self.entries[idx].lock_arc().await.toggle_fullscreen(); - } - - pub async fn set_fullscreen(&mut self, idx: usize, fullscreen: bool) { - self.entries[idx] - .lock_arc() - .await - .set_fullscreen(fullscreen); - } - - pub async fn should_fullscreen(&self, idx: usize) -> bool { - self.entries[idx].lock_arc().await.should_fullscreen() - } - - pub async fn running(&self, idx: usize) -> bool { - self.entries[idx].lock_arc().await.running() - } - - pub async fn history_cmd(&self, idx: usize) -> String { - self.entries[idx].lock_arc().await.cmd.clone() + pub async fn entry( + &self, + idx: usize, + ) -> async_std::sync::MutexGuardArc { + self.entries[idx].lock_arc().await } pub fn entry_count(&self) -> usize { @@ -154,7 +138,7 @@ impl History { } } -struct HistoryEntry { +pub struct HistoryEntry { cmd: String, vt: vt100::Parser, audible_bell_state: usize, @@ -320,7 +304,11 @@ impl HistoryEntry { } } - fn toggle_fullscreen(&mut self) { + pub fn cmd(&self) -> String { + self.cmd.clone() + } + + pub fn toggle_fullscreen(&mut self) { if let Some(fullscreen) = self.fullscreen { self.fullscreen = Some(!fullscreen); } else { @@ -328,19 +316,19 @@ impl HistoryEntry { } } - fn set_fullscreen(&mut self, fullscreen: bool) { + pub fn set_fullscreen(&mut self, fullscreen: bool) { self.fullscreen = Some(fullscreen); } - fn running(&self) -> bool { + pub fn running(&self) -> bool { self.exit_info.is_none() } - fn binary(&self) -> bool { + pub fn binary(&self) -> bool { self.vt.screen().errors() > 5 } - fn lines(&self, width: u16, focused: bool) -> usize { + pub fn lines(&self, width: u16, focused: bool) -> usize { if self.binary() { return 1; } @@ -361,7 +349,7 @@ impl HistoryEntry { last_row } - fn should_fullscreen(&self) -> bool { + pub fn should_fullscreen(&self) -> bool { self.fullscreen .unwrap_or_else(|| self.vt.screen().alternate_screen()) } diff --git a/src/state.rs b/src/state.rs index c030e2a..28f76ac 100644 --- a/src/state.rs +++ b/src/state.rs @@ -71,7 +71,7 @@ impl State { textmode::Key::Ctrl(b'm') => { let idx = self.focus_idx(); let focus = if let Some(idx) = idx { - self.history.running(idx).await + self.history.entry(idx).await.running() } else { true }; @@ -87,7 +87,7 @@ impl State { textmode::Key::Char(' ') => { if let Some(idx) = self.focus_idx() { self.readline - .set_input(&self.history.history_cmd(idx).await); + .set_input(&self.history.entry(idx).await.cmd()); self.set_focus(crate::action::Focus::Readline).await; } } @@ -100,14 +100,13 @@ impl State { } textmode::Key::Char('f') => { if let Some(idx) = self.focus_idx() { + let mut entry = self.history.entry(idx).await; let mut focus = crate::action::Focus::History(idx); if let crate::action::Focus::Scrolling(_) = self.focus { - self.history.set_fullscreen(idx, true).await; + entry.set_fullscreen(true); } else { - self.history.toggle_fullscreen(idx).await; - if !self.history.should_fullscreen(idx).await - && !self.history.running(idx).await - { + entry.toggle_fullscreen(); + if !entry.should_fullscreen() && !entry.running() { focus = crate::action::Focus::Scrolling(Some(idx)); } @@ -259,7 +258,7 @@ impl State { crate::action::Scene::Readline } crate::action::Focus::History(idx) => { - if self.history.should_fullscreen(idx).await { + if self.history.entry(idx).await.should_fullscreen() { crate::action::Scene::Fullscreen } else { crate::action::Scene::Readline -- cgit v1.2.3-54-g00ecf