Skip to main content

waxterm/
app.rs

1use crate::input::{KeyPress, OscHandler};
2
3/// The trait that application authors implement.
4pub trait TerminalApp {
5    /// App-specific command type. Must accept framework KeyPress via From.
6    type Command: From<KeyPress>;
7
8    /// Render the current frame.
9    fn render(&self, frame: &mut ratatui::Frame);
10
11    /// Handle a parsed input command. Return true if display needs redrawing.
12    fn handle_input(&mut self, cmd: Self::Command) -> bool;
13
14    /// Advance state by delta_ms. Return true if redraw needed.
15    fn tick(&mut self, delta_ms: u64) -> bool;
16
17    /// Whether the application should exit.
18    fn should_quit(&self) -> bool;
19
20    /// Override to register custom OSC input handlers.
21    fn input_extensions(&self) -> Vec<OscHandler<Self::Command>> {
22        vec![]
23    }
24}
25
26/// Terminal configuration with builder pattern.
27pub struct TerminalConfig {
28    pub cols: u16,
29    pub rows: u16,
30    pub fps: u16,
31    pub heartbeat: bool,
32    pub buf_capacity: usize,
33}
34
35impl Default for TerminalConfig {
36    fn default() -> Self {
37        Self::new()
38    }
39}
40
41impl TerminalConfig {
42    pub fn new() -> Self {
43        TerminalConfig {
44            cols: 80,
45            rows: 24,
46            fps: 30,
47            heartbeat: cfg!(target_os = "wasi"),
48            buf_capacity: if cfg!(target_os = "wasi") { 1024 } else { 8192 },
49        }
50    }
51
52    pub fn cols(mut self, cols: u16) -> Self {
53        self.cols = cols;
54        self
55    }
56
57    pub fn rows(mut self, rows: u16) -> Self {
58        self.rows = rows;
59        self
60    }
61
62    pub fn fps(mut self, fps: u16) -> Self {
63        self.fps = fps;
64        self
65    }
66
67    pub fn heartbeat(mut self, heartbeat: bool) -> Self {
68        self.heartbeat = heartbeat;
69        self
70    }
71
72    pub fn buf_capacity(mut self, capacity: usize) -> Self {
73        self.buf_capacity = capacity;
74        self
75    }
76}