oy-tui 0.2.0

Terminal UI for oy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use std::env;

use oy_agent::format_token_count;
use ratatui::{
    buffer::Buffer,
    layout::{Alignment, Constraint, Layout, Rect},
    style::Style,
    text::{Line, Span, Text},
    widgets::{Block, BorderType, Paragraph, Widget, Wrap},
};

use crate::{
    app::{App, AppMode, visual_cursor_pos},
    command::CommandInfo,
    message::Status,
};

impl Widget for &App {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let t = self.theme;

        let input_text_width = area.width.saturating_sub(2) as usize;
        let visual_lines = self.total_visual_lines(input_text_width.max(1));
        let input_text_height = visual_lines.clamp(2, 7);
        let input_height = input_text_height + 2;

        // Reserve space for popups when in selector/menu mode
        let has_popup = matches!(
            self.app_mode,
            AppMode::CommandSelector { .. } | AppMode::SubMenu { .. }
        );
        let popup_rows: u16 = if has_popup { 7 } else { 0 };

        let chunks = Layout::vertical([
            Constraint::Min(5),
            Constraint::Length(input_height),
            Constraint::Length(popup_rows),
            Constraint::Length(3),
        ])
        .split(area);

        // ── Message Area (per-message paragraphs with full-line backgrounds) ──
        let content_width = chunks[0].width.saturating_sub(2) as usize;
        let visible_height = (chunks[0].height.saturating_sub(2)) as usize;

        // Compute per-message heights (content only, no trailing blanks)
        let msg_heights: Vec<usize> = self
            .messages
            .iter()
            .map(|msg| msg.visual_line_count(content_width, t))
            .collect();
        // Add 1 spacer line between messages
        let spacer_count = self.messages.len().saturating_sub(1);
        let total_visual: usize = msg_heights.iter().sum::<usize>() + spacer_count;

        // Clamp scroll offset
        if total_visual > visible_height {
            let max_offset = (total_visual - visible_height) as u16;
            if self.scroll_offset.get() > max_offset {
                self.scroll_offset.set(max_offset);
            }
            if self.scroll_offset.get() >= max_offset {
                self.auto_scroll.set(true);
            }
        } else {
            if self.scroll_offset.get() > 0 {
                self.scroll_offset.set(0);
            }
            self.auto_scroll.set(true);
        }

        // Determine starting message and line offset from scroll
        // (spacers are counted as 1 line each in the scroll space)
        let mut scroll_rem = self.scroll_offset.get() as usize;
        let mut msg_idx = 0usize;
        let mut line_offset = 0usize;
        for (i, &h) in msg_heights.iter().enumerate() {
            let entry_height = h + if i < msg_heights.len() - 1 { 1 } else { 0 };
            if scroll_rem < entry_height {
                msg_idx = i;
                if scroll_rem < h {
                    line_offset = scroll_rem;
                } else {
                    line_offset = 0;
                }
                break;
            }
            scroll_rem -= entry_height;
        }

        // Render visible messages as individual paragraphs
        let inner_x = chunks[0].x + 1;
        let inner_w = content_width as u16;
        let mut used_lines = 0usize;

        for (i, _) in msg_heights
            .iter()
            .enumerate()
            .take(self.messages.len())
            .skip(msg_idx)
        {
            if used_lines >= visible_height {
                break;
            }
            let h = msg_heights[i];
            let available = visible_height - used_lines;
            let remaining = h.saturating_sub(line_offset);
            let render_lines = remaining.min(available);
            if render_lines == 0 {
                break;
            }

            let msg_area = Rect {
                x: inner_x,
                y: chunks[0].y + 1 + used_lines as u16,
                width: inner_w,
                height: render_lines as u16,
            };

            let lines = self.messages[i].to_lines(t);
            let mut text = Text::default();
            text.extend(lines);

            let bg = self.messages[i].message_bg(t);
            Paragraph::new(text)
                .scroll((line_offset as u16, 0))
                .wrap(Wrap { trim: false })
                .style(Style::default().bg(bg))
                .render(msg_area, buf);

            used_lines += render_lines;
            line_offset = 0;

            // Add spacer line (with surface_bg) between messages
            if i + 1 < self.messages.len() && used_lines < visible_height {
                let spacer_y = chunks[0].y + 1 + used_lines as u16;
                let spacer_area = Rect {
                    x: inner_x,
                    y: spacer_y,
                    width: inner_w,
                    height: 1,
                };
                // Spacer row with surface_bg (text must be non-empty to fill the row)
                let spacer_text = Text::from(Line::from(Span::raw(" ")));
                let spacer = Paragraph::new(spacer_text)
                    .style(Style::default().fg(t.surface_bg).bg(t.surface_bg));
                spacer.render(spacer_area, buf);
                used_lines += 1;
            }
        }

        // Draw the outer border on top
        let border_block = Block::bordered()
            .title("Chat History")
            .title_alignment(Alignment::Center)
            .border_type(BorderType::Rounded)
            .border_style(Style::default().fg(t.border));
        border_block.render(chunks[0], buf);

        // ── Input Area ──
        let input_display = self.input.to_string();
        let input_text_width = chunks[1].width.saturating_sub(2) as usize;

        let (cursor_visual_row, cursor_visual_col) =
            visual_cursor_pos(&self.input, self.cursor_pos, input_text_width);

        let input_visible_height = chunks[1].height.saturating_sub(2);
        let input_scroll = if cursor_visual_row >= input_visible_height {
            cursor_visual_row - input_visible_height + 1
        } else {
            0
        };

        self.cursor_x.set(chunks[1].x + 1 + cursor_visual_col);
        self.cursor_y
            .set(chunks[1].y + 1 + cursor_visual_row - input_scroll);
        self.input_width.set(chunks[1].width.saturating_sub(2));

        let input_title =
            if matches!(self.app_mode, AppMode::ModelForm { .. }) && !self.input_title.is_empty() {
                self.input_title.clone()
            } else {
                "Input".to_string()
            };

        let input_paragraph = Paragraph::new(input_display)
            .block(
                Block::bordered()
                    .title(input_title)
                    .title_alignment(Alignment::Left)
                    .border_type(BorderType::Double)
                    .border_style(Style::default().fg(t.input_border)),
            )
            .wrap(Wrap { trim: false })
            .scroll((input_scroll, 0))
            .style(Style::default().fg(t.surface_fg).bg(t.surface_bg));

        input_paragraph.render(chunks[1], buf);

        // ── Status Area ──
        const SPINNER: &[&str] = &["", "", "", "", "", "", "", "", "", ""];
        let spinner_char = match self.agent_status.get() {
            Status::Running => {
                let idx = (self.tick_counter.get() as usize / 2) % SPINNER.len();
                SPINNER[idx]
            }
            Status::Pause => "",
        };

        // Build token stats string: [↑input ↓output]
        let token_stats = {
            let input = format_token_count(self.token_usage.input_tokens);
            let output = format_token_count(self.token_usage.output_tokens);
            format!(" [↑{}{}]", input, output)
        };

        // Build context usage string: current_context/max_context (percentage)
        let context_display = {
            let total_used = self.token_usage.context_tokens;
            let capacity = self
                .global_toml_config
                .as_ref()
                .and_then(|c| c.context_capacity)
                .unwrap_or(200_000);
            let used_str = format_token_count(total_used);
            let cap_str = if capacity >= 1_000_000 {
                format!("{}M", capacity / 1_000_000)
            } else {
                format_token_count(capacity)
            };
            let pct = if capacity > 0 {
                (total_used as f64 / capacity as f64 * 100.0).round() as u64
            } else {
                0
            };
            format!(" {}/{} ({}%)", used_str, cap_str, pct)
        };

        let mut agent_label = "<Current Agent>".to_string();
        if let Some(main_agent) = &self.main_agent {
            agent_label = format!("<{}>", &main_agent.name);
        }

        let status_text = format!(
            " {}{}{} {} (Cycle with shift+tab)\n Messages: {} | ↑/↓/←/→ move cursor | Enter send | Ctrl+O expand | Ctrl+C/Esc/q quit",
            agent_label,
            token_stats,
            context_display,
            spinner_char,
            self.messages.len()
        );

        let status_paragraph = Paragraph::new(status_text)
            .alignment(Alignment::Left)
            .style(Style::default().fg(t.status_fg).bg(t.status_bg));

        status_paragraph.render(chunks[3], buf);

        let mut status_right = "Use the /model command to set up one model 
            Unknown directory "
            .to_string();
        if let Some(config) = &self.global_toml_config {
            if let Some(model_name) = &config.model {
                let effort = config.reasoning_effort.as_deref().unwrap_or("high");
                status_right = status_right.replace(
                    "Use the /model command to set up one model ",
                    &format!("{} · {} ", model_name, effort),
                );
            }
            if let Ok(path) = env::current_dir() {
                status_right = status_right.replace(
                    "Unknown directory ",
                    &format!("{} ", &path.to_string_lossy()),
                );
            }
        }
        let status_right_para = Paragraph::new(status_right)
            .alignment(Alignment::Right)
            .style(Style::default().fg(t.status_fg).bg(t.status_bg));

        status_right_para.render(chunks[3], buf);

        // ── SubMenu / Command Selector Popup ──
        // Render into chunks[2] (reserved popup area)
        if let AppMode::SubMenu {
            title,
            items,
            selected,
            scroll_offset,
        } = &self.app_mode
            && !items.is_empty()
        {
            let sel = *selected;
            let scroll = *scroll_offset;
            let total = items.len();
            let max_content_rows = (chunks[2].height.saturating_sub(2)) as usize;
            let has_more_up = scroll > 0;
            let has_more_down = scroll + max_content_rows < total;

            // Account for "↑/↓ more..." indicator lines
            let indicator_lines = (has_more_up as usize) + (has_more_down as usize);
            let max_items = max_content_rows.saturating_sub(indicator_lines);
            let visible: Vec<&(String, String)> =
                items.iter().skip(scroll).take(max_items).collect();

            // Recalculate has_more_down based on actual items taken
            let has_more_down = scroll + visible.len() < total;

            let mut popup_text = Text::default();
            if has_more_up {
                popup_text.push_line(Line::from(Span::styled(
                    "  \u{2191} more...",
                    Style::default().fg(t.subtle),
                )));
            }
            for (i, (name, desc)) in visible.iter().enumerate() {
                let abs_idx = scroll + i;
                let style = if abs_idx == sel {
                    Style::default().fg(t.surface_bg).bg(t.accent)
                } else {
                    Style::default().fg(t.surface_fg)
                };
                popup_text.push_line(Line::from(vec![
                    Span::styled(if abs_idx == sel { "\u{25b8} " } else { "  " }, style),
                    Span::styled(format!("{}  - {}", name, desc), style),
                ]));
            }
            if has_more_down {
                popup_text.push_line(Line::from(Span::styled(
                    "  \u{2193} more...",
                    Style::default().fg(t.subtle),
                )));
            }

            let popup = Paragraph::new(popup_text)
                .block(
                    Block::bordered()
                        .title(title.as_str())
                        .title_alignment(Alignment::Left)
                        .border_type(BorderType::Rounded)
                        .border_style(Style::default().fg(t.accent)),
                )
                .style(Style::default().bg(t.surface_bg));
            popup.render(chunks[2], buf);
        }

        // ── Command Selector Popup ──
        if let AppMode::CommandSelector {
            selected,
            scroll_offset,
        } = &self.app_mode
        {
            let matches = self.command_registry.search(&self.input);
            if !matches.is_empty() {
                let sel = *selected;
                let scroll = *scroll_offset;
                let total = matches.len();
                let max_content_rows = (chunks[2].height.saturating_sub(2)) as usize;
                let has_more_up = scroll > 0;
                let has_more_down = scroll + max_content_rows < total;

                // Account for "↑/↓ more..." indicator lines
                let indicator_lines = (has_more_up as usize) + (has_more_down as usize);
                let max_items = max_content_rows.saturating_sub(indicator_lines);
                let visible: Vec<&&CommandInfo> =
                    matches.iter().skip(scroll).take(max_items).collect();

                // Recalculate has_more_down based on actual items taken
                let has_more_down = scroll + visible.len() < total;

                let mut popup_text = Text::default();
                if has_more_up {
                    popup_text.push_line(Line::from(Span::styled(
                        "  \u{2191} more...",
                        Style::default().fg(t.subtle),
                    )));
                }
                for (i, cmd) in visible.iter().enumerate() {
                    let abs_idx = scroll + i;
                    let style = if abs_idx == sel {
                        Style::default().fg(t.surface_bg).bg(t.accent)
                    } else {
                        Style::default().fg(t.surface_fg)
                    };
                    popup_text.push_line(Line::from(vec![
                        Span::styled(if abs_idx == sel { "\u{25b8} " } else { "  " }, style),
                        Span::styled(format!("{}  - {}", cmd.name, cmd.description), style),
                    ]));
                }
                if has_more_down {
                    popup_text.push_line(Line::from(Span::styled(
                        "  \u{2193} more...",
                        Style::default().fg(t.subtle),
                    )));
                }

                let popup = Paragraph::new(popup_text)
                    .block(
                        Block::bordered()
                            .title("Commands")
                            .title_alignment(Alignment::Left)
                            .border_type(BorderType::Rounded)
                            .border_style(Style::default().fg(t.accent)),
                    )
                    .style(Style::default().bg(t.surface_bg));
                popup.render(chunks[2], buf);
            }
        }
    }
}