From c1ea723f8277470e2bbaff7cd679e50d23c427f2 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 5 Dec 2021 01:24:40 -0500 Subject: simplify --- src/bin/ttyplay/event.rs | 50 +++++++++++++----------------------------------- src/bin/ttyplay/input.rs | 20 ++++++++++++++----- src/bin/ttyplay/main.rs | 47 +++++++++++---------------------------------- 3 files changed, 39 insertions(+), 78 deletions(-) diff --git a/src/bin/ttyplay/event.rs b/src/bin/ttyplay/event.rs index d1d03e9..4effba4 100644 --- a/src/bin/ttyplay/event.rs +++ b/src/bin/ttyplay/event.rs @@ -2,23 +2,21 @@ pub enum Event { FrameTransition((usize, vt100::Screen)), Key(textmode::Key), FrameLoaded(Option), - Pause, Paused(bool), + TimerAction(TimerAction), + ToggleUi, + Quit, +} + +pub enum TimerAction { + Pause, FirstFrame, LastFrame, NextFrame, PreviousFrame, - ToggleUi, Quit, } -enum MoveFrame { - First, - Last, - Next, - Previous, -} - pub struct Reader { pending: async_std::sync::Mutex, cvar: async_std::sync::Condvar, @@ -68,9 +66,8 @@ struct Pending { key: std::collections::VecDeque, frame_loaded: Option, done_loading: bool, - pause: bool, paused: Option, - frame_controls: std::collections::VecDeque, + timer_actions: std::collections::VecDeque, toggle_ui: bool, quit: bool, } @@ -95,23 +92,11 @@ impl Pending { self.done_loading = true; } } - Event::Pause => { - self.pause = !self.pause; - } Event::Paused(paused) => { self.paused = Some(paused); } - Event::FirstFrame => { - self.frame_controls.push_back(MoveFrame::First); - } - Event::LastFrame => { - self.frame_controls.push_back(MoveFrame::Last); - } - Event::NextFrame => { - self.frame_controls.push_back(MoveFrame::Next); - } - Event::PreviousFrame => { - self.frame_controls.push_back(MoveFrame::Previous); + Event::TimerAction(action) => { + self.timer_actions.push_back(action); } Event::ToggleUi => { self.toggle_ui = !self.toggle_ui; @@ -127,9 +112,8 @@ impl Pending { || !self.key.is_empty() || self.frame_loaded.is_some() || self.done_loading - || self.pause || self.paused.is_some() - || !self.frame_controls.is_empty() + || !self.timer_actions.is_empty() || self.toggle_ui || self.quit } @@ -140,16 +124,8 @@ impl Pending { Some(Event::Quit) } else if let Some(key) = self.key.pop_front() { Some(Event::Key(key)) - } else if self.pause { - self.pause = false; - Some(Event::Pause) - } else if let Some(dir) = self.frame_controls.pop_front() { - match dir { - MoveFrame::First => Some(Event::FirstFrame), - MoveFrame::Last => Some(Event::LastFrame), - MoveFrame::Next => Some(Event::NextFrame), - MoveFrame::Previous => Some(Event::PreviousFrame), - } + } else if let Some(action) = self.timer_actions.pop_front() { + Some(Event::TimerAction(action)) } else if self.toggle_ui { self.toggle_ui = false; Some(Event::ToggleUi) diff --git a/src/bin/ttyplay/input.rs b/src/bin/ttyplay/input.rs index 0da0c8b..a922a55 100644 --- a/src/bin/ttyplay/input.rs +++ b/src/bin/ttyplay/input.rs @@ -1,13 +1,23 @@ pub fn to_event(key: &textmode::Key) -> Option { Some(match key { textmode::Key::Char('g' | '0' | ')') => { - crate::event::Event::FirstFrame + crate::event::Event::TimerAction( + crate::event::TimerAction::FirstFrame, + ) } - textmode::Key::Char('G' | '$') => crate::event::Event::LastFrame, - textmode::Key::Char('l' | 'n') => crate::event::Event::NextFrame, - textmode::Key::Char('h' | 'p') => crate::event::Event::PreviousFrame, + textmode::Key::Char('G' | '$') => crate::event::Event::TimerAction( + crate::event::TimerAction::LastFrame, + ), + textmode::Key::Char('l' | 'n') => crate::event::Event::TimerAction( + crate::event::TimerAction::NextFrame, + ), + textmode::Key::Char('h' | 'p') => crate::event::Event::TimerAction( + crate::event::TimerAction::PreviousFrame, + ), textmode::Key::Char('q') => crate::event::Event::Quit, - textmode::Key::Char(' ') => crate::event::Event::Pause, + textmode::Key::Char(' ') => { + crate::event::Event::TimerAction(crate::event::TimerAction::Pause) + } textmode::Key::Ctrl(b'i') => crate::event::Event::ToggleUi, _ => return None, }) diff --git a/src/bin/ttyplay/main.rs b/src/bin/ttyplay/main.rs index 3300047..0e0fe16 100644 --- a/src/bin/ttyplay/main.rs +++ b/src/bin/ttyplay/main.rs @@ -17,15 +17,6 @@ struct Opt { file: std::ffi::OsString, } -enum TimerAction { - Pause, - FirstFrame, - LastFrame, - NextFrame, - PreviousFrame, - Quit, -} - fn spawn_frame_reader_task( event_w: async_std::channel::Sender, frames: async_std::sync::Arc>, @@ -61,7 +52,7 @@ fn spawn_frame_reader_task( fn spawn_timer_task( event_w: async_std::channel::Sender, frames: async_std::sync::Arc>, - timer_r: async_std::channel::Receiver, + timer_r: async_std::channel::Receiver, ) -> async_std::task::JoinHandle<()> { async_std::task::spawn(async move { let mut idx = 0; @@ -72,7 +63,7 @@ fn spawn_timer_task( enum Res { Wait(Option), TimerAction( - Result, + Result, ), } let wait = async { @@ -123,7 +114,7 @@ fn spawn_timer_task( event_w.send(event::Event::Paused(true)).await.unwrap(); } Res::TimerAction(Ok(action)) => match action { - TimerAction::Pause => { + event::TimerAction::Pause => { let now = std::time::Instant::now(); if let Some(time) = paused_time.take() { start_time += now - time; @@ -135,24 +126,24 @@ fn spawn_timer_task( .await .unwrap(); } - TimerAction::FirstFrame => { + event::TimerAction::FirstFrame => { idx = 0; force_update_time = true; } - TimerAction::LastFrame => { + event::TimerAction::LastFrame => { idx = frames.lock_arc().await.count() - 1; force_update_time = true; } // force_update_time will immediately transition to the // next frame and do idx += 1 on its own - TimerAction::NextFrame => { + event::TimerAction::NextFrame => { force_update_time = true; } - TimerAction::PreviousFrame => { + event::TimerAction::PreviousFrame => { idx = idx.saturating_sub(2); force_update_time = true; } - TimerAction::Quit => break, + event::TimerAction::Quit => break, }, Res::TimerAction(Err(e)) => panic!("{}", e), } @@ -203,24 +194,8 @@ async fn async_main(opt: Opt) -> anyhow::Result<()> { } continue; } - event::Event::Pause => { - timer_w.send(TimerAction::Pause).await?; - continue; - } - event::Event::FirstFrame => { - timer_w.send(TimerAction::FirstFrame).await?; - continue; - } - event::Event::LastFrame => { - timer_w.send(TimerAction::LastFrame).await?; - continue; - } - event::Event::NextFrame => { - timer_w.send(TimerAction::NextFrame).await?; - continue; - } - event::Event::PreviousFrame => { - timer_w.send(TimerAction::PreviousFrame).await?; + event::Event::TimerAction(action) => { + timer_w.send(action).await?; continue; } event::Event::FrameTransition((idx, screen)) => { @@ -247,7 +222,7 @@ async fn async_main(opt: Opt) -> anyhow::Result<()> { display.render(¤t_screen, &mut output).await?; } - timer_w.send(TimerAction::Quit).await?; + timer_w.send(event::TimerAction::Quit).await?; timer_task.await; Ok(()) -- cgit v1.2.3-54-g00ecf