Skip to main content

hyprstream_tui/
cast_app.rs

1//! `CastPlayerApp` — a `TerminalApp` implementation for asciicast v2 playback.
2
3use waxterm::input::KeyPress;
4use waxterm::TerminalApp;
5
6use crate::cast_player::{CastFile, CastPlayer};
7
8/// Default idle time limit: cap gaps between events (ms).
9/// Set to None for real-time playback, or Some(ms) to compress long pauses.
10const DEFAULT_IDLE_LIMIT_MS: Option<u64> = None;
11
12/// A log entry with a name and its cast file content.
13pub struct LogEntry {
14    pub name: String,
15    pub cast_content: String,
16}
17
18/// Cast player application state.
19pub struct CastPlayerApp {
20    pub logs: Vec<LogEntry>,
21    pub selected: usize,
22    pub player: CastPlayer,
23    quit: bool,
24    loop_playback: bool,
25}
26
27/// Commands for the cast player.
28pub enum CastCommand {
29    Key(KeyPress),
30}
31
32impl From<KeyPress> for CastCommand {
33    fn from(k: KeyPress) -> Self {
34        CastCommand::Key(k)
35    }
36}
37
38fn parse_cast(content: &str) -> CastFile {
39    CastFile::parse_with_idle_limit(content, DEFAULT_IDLE_LIMIT_MS).unwrap_or_else(|e| {
40        eprintln!("Failed to parse cast: {}", e);
41        CastFile {
42            header: crate::cast_player::CastHeader {
43                version: 2,
44                width: 80,
45                height: 24,
46                title: Some("Error".to_string()),
47            },
48            events: vec![],
49            duration_ms: 0,
50        }
51    })
52}
53
54fn empty_cast() -> CastFile {
55    CastFile {
56        header: crate::cast_player::CastHeader {
57            version: 2,
58            width: 80,
59            height: 24,
60            title: Some("No logs".to_string()),
61        },
62        events: vec![],
63        duration_ms: 0,
64    }
65}
66
67impl CastPlayerApp {
68    /// Create a new cast player with the given log entries.
69    pub fn new(logs: Vec<LogEntry>) -> Self {
70        let cast = if !logs.is_empty() {
71            parse_cast(&logs[0].cast_content)
72        } else {
73            empty_cast()
74        };
75
76        let player = CastPlayer::new(cast);
77
78        CastPlayerApp {
79            logs,
80            selected: 0,
81            player,
82            quit: false,
83            loop_playback: false,
84        }
85    }
86
87    /// Create a cast player from a single cast file content string.
88    pub fn from_content(name: String, content: String) -> Self {
89        Self::new(vec![LogEntry {
90            name,
91            cast_content: content,
92        }])
93    }
94
95    /// Enable or disable loop playback.
96    pub fn set_loop(&mut self, loop_playback: bool) {
97        self.loop_playback = loop_playback;
98    }
99
100    /// Switch to a different log entry.
101    pub fn select_log(&mut self, index: usize) {
102        if index >= self.logs.len() || index == self.selected {
103            return;
104        }
105        self.selected = index;
106        let cast = parse_cast(&self.logs[index].cast_content);
107        self.player = CastPlayer::new(cast);
108    }
109
110    /// Cycle to next log. If there is only one log, restart it.
111    pub fn next_log(&mut self) {
112        if self.logs.is_empty() {
113            return;
114        }
115        let next = (self.selected + 1) % self.logs.len();
116        if next == self.selected {
117            let cast = parse_cast(&self.logs[self.selected].cast_content);
118            self.player = CastPlayer::new(cast);
119        } else {
120            self.select_log(next);
121        }
122    }
123}
124
125impl TerminalApp for CastPlayerApp {
126    type Command = CastCommand;
127
128    fn render(&self, frame: &mut ratatui::Frame) {
129        crate::cast_ui::draw(frame, self);
130    }
131
132    fn handle_input(&mut self, cmd: CastCommand) -> bool {
133        match cmd {
134            CastCommand::Key(key) => match key {
135                KeyPress::Char(b'q' | b'Q') => {
136                    self.quit = true;
137                    false
138                }
139                KeyPress::Tab => {
140                    self.next_log();
141                    true
142                }
143                KeyPress::Char(b' ') => {
144                    self.player.toggle_pause();
145                    true
146                }
147                KeyPress::ArrowRight => {
148                    self.player.seek_forward(3000);
149                    true
150                }
151                KeyPress::ArrowLeft => {
152                    self.player.seek_backward(3000);
153                    true
154                }
155                KeyPress::Char(b'1') => {
156                    self.select_log(0);
157                    true
158                }
159                KeyPress::Char(b'2') => {
160                    self.select_log(1);
161                    true
162                }
163                KeyPress::Char(b'3') => {
164                    self.select_log(2);
165                    true
166                }
167                _ => false,
168            },
169        }
170    }
171
172    fn tick(&mut self, delta_ms: u64) -> bool {
173        let changed = self.player.tick(delta_ms);
174        if self.player.is_finished() {
175            if self.loop_playback {
176                self.next_log();
177                return true;
178            }
179            self.quit = true;
180        }
181        changed
182    }
183
184    fn should_quit(&self) -> bool {
185        self.quit
186    }
187}