Skip to main content

agent_procs/tui/
app.rs

1use crate::protocol::{ProcessInfo, Stream};
2use std::collections::{HashMap, VecDeque};
3
4const MAX_BUFFER_LINES: usize = 10_000;
5
6#[derive(Debug, Clone, Copy, PartialEq)]
7pub enum StreamMode {
8    Stdout,
9    Stderr,
10    Both,
11}
12
13#[derive(Debug, Clone, Copy, PartialEq)]
14pub enum LineSource {
15    Stdout,
16    Stderr,
17}
18
19/// Single ring buffer storing all output with source tags.
20/// Stdout/stderr views are filtered from the same data — no duplication.
21pub struct OutputBuffer {
22    lines: VecDeque<(LineSource, String)>,
23    max_lines: usize,
24}
25
26impl OutputBuffer {
27    pub fn new(max_lines: usize) -> Self {
28        Self {
29            lines: VecDeque::with_capacity(max_lines),
30            max_lines,
31        }
32    }
33
34    pub fn push(&mut self, source: LineSource, line: String) {
35        if self.lines.len() == self.max_lines {
36            self.lines.pop_front();
37        }
38        self.lines.push_back((source, line));
39    }
40
41    pub fn stdout_lines(&self) -> Vec<&str> {
42        self.lines
43            .iter()
44            .filter(|(src, _)| *src == LineSource::Stdout)
45            .map(|(_, s)| s.as_str())
46            .collect()
47    }
48
49    pub fn stderr_lines(&self) -> Vec<&str> {
50        self.lines
51            .iter()
52            .filter(|(src, _)| *src == LineSource::Stderr)
53            .map(|(_, s)| s.as_str())
54            .collect()
55    }
56
57    pub fn all_lines(&self) -> Vec<(LineSource, &str)> {
58        self.lines
59            .iter()
60            .map(|(src, s)| (*src, s.as_str()))
61            .collect()
62    }
63}
64
65pub struct App {
66    pub processes: Vec<ProcessInfo>,
67    pub selected: usize,
68    pub buffers: HashMap<String, OutputBuffer>,
69    pub stream_mode: StreamMode,
70    pub paused: bool,
71    pub scroll_offsets: HashMap<String, usize>,
72    pub running: bool,
73    pub stop_all_on_quit: bool,
74}
75
76impl Default for App {
77    fn default() -> Self {
78        Self::new()
79    }
80}
81
82impl App {
83    pub fn new() -> Self {
84        Self {
85            processes: Vec::new(),
86            selected: 0,
87            buffers: HashMap::new(),
88            stream_mode: StreamMode::Stdout,
89            paused: false,
90            scroll_offsets: HashMap::new(),
91            running: true,
92            stop_all_on_quit: false,
93        }
94    }
95
96    pub fn update_processes(&mut self, processes: Vec<ProcessInfo>) {
97        self.processes = processes;
98        if self.selected >= self.processes.len() && !self.processes.is_empty() {
99            self.selected = self.processes.len() - 1;
100        }
101    }
102
103    pub fn selected_name(&self) -> Option<&str> {
104        self.processes.get(self.selected).map(|p| p.name.as_str())
105    }
106
107    pub fn select_next(&mut self) {
108        if !self.processes.is_empty() {
109            self.selected = (self.selected + 1) % self.processes.len();
110        }
111    }
112
113    pub fn select_prev(&mut self) {
114        if !self.processes.is_empty() {
115            self.selected = if self.selected == 0 {
116                self.processes.len() - 1
117            } else {
118                self.selected - 1
119            };
120        }
121    }
122
123    pub fn cycle_stream_mode(&mut self) {
124        self.stream_mode = match self.stream_mode {
125            StreamMode::Stdout => StreamMode::Stderr,
126            StreamMode::Stderr => StreamMode::Both,
127            StreamMode::Both => StreamMode::Stdout,
128        };
129    }
130
131    pub fn toggle_pause(&mut self) {
132        self.paused = !self.paused;
133        if !self.paused {
134            // Reset scroll offset to bottom on unpause
135            if let Some(name) = self.processes.get(self.selected).map(|p| p.name.clone()) {
136                self.scroll_offsets.remove(&name);
137            }
138        }
139    }
140
141    pub fn push_output(&mut self, process: &str, stream: Stream, line: &str) {
142        let buf = self
143            .buffers
144            .entry(process.to_string())
145            .or_insert_with(|| OutputBuffer::new(MAX_BUFFER_LINES));
146        let source = match stream {
147            Stream::Stdout => LineSource::Stdout,
148            Stream::Stderr => LineSource::Stderr,
149        };
150        buf.push(source, line.to_string());
151    }
152
153    pub fn quit(&mut self) {
154        self.running = false;
155    }
156
157    pub fn quit_and_stop(&mut self) {
158        self.stop_all_on_quit = true;
159        self.running = false;
160    }
161
162    pub fn running_count(&self) -> usize {
163        self.processes
164            .iter()
165            .filter(|p| p.state == crate::protocol::ProcessState::Running)
166            .count()
167    }
168
169    pub fn exited_count(&self) -> usize {
170        self.processes
171            .iter()
172            .filter(|p| p.state == crate::protocol::ProcessState::Exited)
173            .count()
174    }
175}