opendev-tui 0.1.4

Ratatui-based terminal UI for OpenDev
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
//! Popup panel and modal dialog rendering methods.
//!
//! Extracted from `render.rs` — all methods remain on `impl App`.

use ratatui::layout;

use super::App;

impl App {
    /// Shared helper that renders a popup panel matching the Python Textual style:
    /// bright_cyan border, `▸` pointer, bold white active label, dim descriptions.
    /// Padding (1, 2) = 1 empty line top/bottom, 2 spaces horizontal.
    pub(super) fn render_popup_panel(
        frame: &mut ratatui::Frame,
        input_area: layout::Rect,
        title: &str,
        content_lines: &[ratatui::text::Line<'_>],
        option_lines: &[ratatui::text::Line<'_>],
        hint: &str,
        max_width: Option<u16>,
    ) {
        use crate::formatters::style_tokens;
        use ratatui::style::{Modifier, Style};
        use ratatui::text::{Line, Span};
        use ratatui::widgets::{Block, BorderType, Borders, Paragraph};

        let mut lines: Vec<Line> = Vec::new();

        // Top padding (1 empty line)
        lines.push(Line::from(""));

        // Content section
        for line in content_lines {
            lines.push(line.clone());
        }

        // Hint line
        lines.push(Line::from(Span::styled(
            format!("    {hint}"),
            Style::default().fg(style_tokens::DIM_GREY),
        )));

        // Option lines
        for line in option_lines {
            lines.push(line.clone());
        }

        // Bottom padding (1 empty line)
        lines.push(Line::from(""));

        let panel_width = max_width
            .map(|w| input_area.width.min(w))
            .unwrap_or(input_area.width);
        let panel_height = (lines.len() as u16 + 2).min(input_area.y);
        let popup_area = layout::Rect {
            x: input_area.x,
            y: input_area.y.saturating_sub(panel_height),
            width: panel_width,
            height: panel_height,
        };

        let block = Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(Style::default().fg(Self::PANEL_CYAN))
            .title(Span::styled(
                title,
                Style::default()
                    .fg(Self::PANEL_CYAN)
                    .add_modifier(Modifier::BOLD),
            ));

        let paragraph = Paragraph::new(lines).block(block);
        frame.render_widget(ratatui::widgets::Clear, popup_area);
        frame.render_widget(paragraph, popup_area);
    }

    /// Build a single option line matching the Python Textual style.
    /// Active: `▸` bright_cyan pointer + dim number + bold white label + dim description.
    /// Inactive: space pointer + dim number + white label + dim description.
    pub(super) fn build_option_line<'a>(
        is_selected: bool,
        number: &str,
        label: &str,
        description: &str,
    ) -> ratatui::text::Line<'a> {
        use crate::formatters::style_tokens;
        use ratatui::style::{Modifier, Style};
        use ratatui::text::{Line, Span};

        let pointer = if is_selected { "\u{25b8}" } else { " " };
        let pointer_style = if is_selected {
            Style::default()
                .fg(Self::PANEL_CYAN)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(style_tokens::DIM_GREY)
        };
        let num_style = Style::default().fg(style_tokens::DIM_GREY);
        let label_style = if is_selected {
            Style::default()
                .fg(style_tokens::PRIMARY)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(style_tokens::PRIMARY)
        };
        let desc_style = Style::default().fg(style_tokens::DIM_GREY);

        let mut spans = vec![
            Span::styled(format!("    {pointer} "), pointer_style),
            Span::styled(format!("{number} "), num_style),
            Span::styled(label.to_string(), label_style),
        ];
        if !description.is_empty() {
            spans.push(Span::styled(format!("  {description}"), desc_style));
        }
        Line::from(spans)
    }

    /// Render autocomplete popup above the input area.
    pub(super) fn render_autocomplete(&self, frame: &mut ratatui::Frame, input_area: layout::Rect) {
        use crate::autocomplete::CompletionKind;
        use crate::formatters::style_tokens;
        use ratatui::style::{Color, Modifier, Style};
        use ratatui::text::{Line, Span};
        use ratatui::widgets::{Block, BorderType, Borders, Paragraph};

        let items = self.state.autocomplete.items();
        let selected_idx = self.state.autocomplete.selected_index();
        let max_show = items.len().min(10);
        let popup_height = max_show as u16 + 2; // +2 for borders

        // Determine title and width based on completion kind
        let is_file_mode = items
            .first()
            .is_some_and(|i| i.kind == CompletionKind::File);
        let popup_width = if is_file_mode { 60 } else { 50 };
        let title = if is_file_mode {
            " Files "
        } else {
            " Commands "
        };

        let popup_area = layout::Rect {
            x: input_area.x,
            y: input_area.y.saturating_sub(popup_height),
            width: input_area.width.min(popup_width),
            height: popup_height,
        };

        // Python uses BLUE_BG_ACTIVE (#1f2d3a) as active row bg
        let active_bg = Color::Rgb(31, 45, 58);

        let lines: Vec<Line> = items
            .iter()
            .take(max_show)
            .enumerate()
            .map(|(i, item)| {
                let selected = i == selected_idx;
                let (left, right) =
                    crate::autocomplete::formatters::CompletionFormatter::format(item);

                let pointer = if selected { "\u{25b8}" } else { "\u{2022}" };
                let pointer_style = if selected {
                    Style::default()
                        .fg(Self::PANEL_CYAN)
                        .add_modifier(Modifier::BOLD)
                } else {
                    Style::default().fg(style_tokens::DIM_GREY)
                };
                let label_style = if selected {
                    Style::default()
                        .fg(Self::PANEL_CYAN)
                        .add_modifier(Modifier::BOLD)
                } else {
                    Style::default().fg(style_tokens::PRIMARY)
                };
                let desc_style = if selected {
                    Style::default().fg(style_tokens::GREY)
                } else {
                    Style::default().fg(style_tokens::SUBTLE)
                };

                let line = Line::from(vec![
                    Span::styled(format!(" {pointer} "), pointer_style),
                    Span::styled(left, label_style),
                    Span::styled(format!(" {right}"), desc_style),
                ]);
                if selected {
                    line.style(Style::default().bg(active_bg))
                } else {
                    line
                }
            })
            .collect();

        let block = Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(Style::default().fg(style_tokens::BORDER))
            .title(Span::styled(
                title,
                Style::default()
                    .fg(Self::PANEL_CYAN)
                    .add_modifier(Modifier::BOLD),
            ));

        let paragraph = Paragraph::new(lines).block(block);
        frame.render_widget(ratatui::widgets::Clear, popup_area);
        frame.render_widget(paragraph, popup_area);
    }

    /// Render the plan approval panel above the input area.
    pub(super) fn render_plan_approval(
        &self,
        frame: &mut ratatui::Frame,
        input_area: layout::Rect,
    ) {
        use crate::formatters::style_tokens;
        use ratatui::style::{Modifier, Style};
        use ratatui::text::{Line, Span};

        let plan_options = self.plan_approval_controller.options();
        let selected = self.plan_approval_controller.selected_action();

        let content_lines = vec![Line::from(vec![
            Span::styled("    Plan ", Style::default().fg(style_tokens::DIM_GREY)),
            Span::styled("\u{00b7} ", Style::default().fg(style_tokens::DIM_GREY)),
            Span::styled(
                "Ready for review",
                Style::default()
                    .fg(Self::PANEL_CYAN)
                    .add_modifier(Modifier::BOLD),
            ),
        ])];

        let option_lines: Vec<Line> = plan_options
            .iter()
            .enumerate()
            .map(|(i, opt)| {
                Self::build_option_line(
                    i == selected,
                    &format!("{}.", i + 1),
                    &opt.label,
                    &opt.description,
                )
            })
            .collect();

        Self::render_popup_panel(
            frame,
            input_area,
            " Approval ",
            &content_lines,
            &option_lines,
            "\u{2191}/\u{2193} choose \u{00b7} Enter confirm \u{00b7} Esc cancel",
            None,
        );
    }

    /// Render the ask-user prompt panel.
    pub(super) fn render_ask_user(&self, frame: &mut ratatui::Frame, input_area: layout::Rect) {
        use crate::formatters::style_tokens;
        use ratatui::style::Style;
        use ratatui::text::{Line, Span};

        let ask_options = self.ask_user_controller.options();
        let selected = self.ask_user_controller.selected_index();
        let question = self.ask_user_controller.question();

        let content_lines = vec![Line::from(Span::styled(
            format!("    {question}"),
            Style::default().fg(style_tokens::PRIMARY),
        ))];

        if ask_options.is_empty() {
            // Free-text input mode
            let text = self.ask_user_controller.text_input();
            let input_line = Line::from(vec![
                Span::styled("    ", Style::default()),
                Span::styled(
                    if text.is_empty() {
                        "\u{2588}".to_string()
                    } else {
                        format!("{text}\u{2588}")
                    },
                    Style::default().fg(style_tokens::ACCENT),
                ),
            ]);

            Self::render_popup_panel(
                frame,
                input_area,
                " Question ",
                &content_lines,
                &[input_line],
                "Type answer \u{00b7} Enter confirm \u{00b7} Esc cancel",
                None,
            );
        } else {
            let option_lines: Vec<Line> = ask_options
                .iter()
                .enumerate()
                .map(|(i, opt)| {
                    Self::build_option_line(i == selected, &format!("{}.", i + 1), opt, "")
                })
                .collect();

            Self::render_popup_panel(
                frame,
                input_area,
                " Question ",
                &content_lines,
                &option_lines,
                "\u{2191}/\u{2193} choose \u{00b7} Enter confirm \u{00b7} Esc cancel",
                None,
            );
        }
    }

    /// Render the model picker panel above the input area.
    pub(super) fn render_model_picker(&self, frame: &mut ratatui::Frame, input_area: layout::Rect) {
        use crate::controllers::ModelPickerController;
        use crate::formatters::style_tokens;
        use ratatui::style::{Color, Modifier, Style};
        use ratatui::text::{Line, Span};
        use ratatui::widgets::{Block, BorderType, Borders, Paragraph};

        let picker = match self.model_picker_controller {
            Some(ref p) => p,
            None => return,
        };

        let visible = picker.visible_models();
        let selected_idx = picker.selected_index();
        let total = picker.filtered_count();
        let query = picker.search_query();

        let active_bg = Color::Rgb(31, 45, 58);
        let mut lines: Vec<Line> = Vec::new();

        // Search bar
        let search_display = if query.is_empty() {
            "Type to search...".to_string()
        } else {
            query.to_string()
        };
        let search_style = if query.is_empty() {
            Style::default().fg(style_tokens::DIM_GREY)
        } else {
            Style::default()
                .fg(Self::PANEL_CYAN)
                .add_modifier(Modifier::BOLD)
        };
        lines.push(Line::from(vec![
            Span::styled("  \u{1f50d} ", Style::default().fg(style_tokens::DIM_GREY)),
            Span::styled(search_display, search_style),
        ]));

        // Separator
        lines.push(Line::from(Span::styled(
            "  \u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}",
            Style::default().fg(style_tokens::BORDER),
        )));

        // Track current provider for group headers
        let mut current_provider = String::new();

        for (display_idx, model) in &visible {
            // Provider group header
            if model.provider != current_provider {
                current_provider = model.provider.clone();
                let mut header_spans = vec![Span::styled(
                    format!("  {} {}", "\u{25cf}", model.provider_display),
                    Style::default()
                        .fg(if model.has_api_key {
                            style_tokens::GREY
                        } else {
                            style_tokens::DIM_GREY
                        })
                        .add_modifier(Modifier::BOLD),
                )];
                if !model.has_api_key {
                    header_spans.push(Span::styled(
                        "  \u{26a0} no key",
                        Style::default().fg(Color::Rgb(120, 120, 120)),
                    ));
                }
                lines.push(Line::from(header_spans));
            }

            let selected = *display_idx == selected_idx;
            let is_current = model.id == self.state.model;

            // Pointer
            let pointer = if selected { "\u{25b8}" } else { " " };
            let pointer_style = if selected {
                Style::default()
                    .fg(Self::PANEL_CYAN)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(style_tokens::DIM_GREY)
            };

            // Model name
            let name_style = if !model.has_api_key {
                Style::default().fg(style_tokens::DIM_GREY)
            } else if selected {
                Style::default()
                    .fg(Self::PANEL_CYAN)
                    .add_modifier(Modifier::BOLD)
            } else if is_current {
                Style::default()
                    .fg(Color::Rgb(0, 200, 100))
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(style_tokens::PRIMARY)
            };

            // Context and pricing info
            let ctx = ModelPickerController::format_context(model.context_length);
            let pricing =
                ModelPickerController::format_pricing(model.pricing_input, model.pricing_output);

            let mut spans = vec![
                Span::styled(format!("    {pointer} "), pointer_style),
                Span::styled(model.name.clone(), name_style),
            ];

            // Current model indicator
            if is_current {
                spans.push(Span::styled(
                    " \u{2713}",
                    Style::default().fg(Color::Rgb(0, 200, 100)),
                ));
            }

            // Recommended badge
            if model.recommended {
                spans.push(Span::styled(
                    " \u{2605}",
                    Style::default().fg(Color::Rgb(255, 200, 50)),
                ));
            }

            // Context length
            spans.push(Span::styled(
                format!("  {ctx}"),
                Style::default().fg(style_tokens::DIM_GREY),
            ));

            // Pricing
            spans.push(Span::styled(
                format!("  {pricing}"),
                Style::default().fg(style_tokens::SUBTLE),
            ));

            let line = Line::from(spans);
            if selected {
                lines.push(line.style(Style::default().bg(active_bg)));
            } else {
                lines.push(line);
            }
        }

        // Empty state
        if visible.is_empty() {
            lines.push(Line::from(Span::styled(
                "    No models match your search.",
                Style::default().fg(style_tokens::DIM_GREY),
            )));
        }

        // Bottom hint with count
        lines.push(Line::from(""));
        lines.push(Line::from(vec![
            Span::styled(
                format!("  {total} model{}", if total == 1 { "" } else { "s" }),
                Style::default().fg(style_tokens::DIM_GREY),
            ),
            Span::styled(
                "  \u{2191}/\u{2193} navigate \u{00b7} Enter select \u{00b7} Esc cancel",
                Style::default().fg(style_tokens::DIM_GREY),
            ),
        ]));

        let panel_height = (lines.len() as u16 + 2).min(input_area.y);
        let panel_width = input_area.width.min(80);
        let popup_area = layout::Rect {
            x: input_area.x,
            y: input_area.y.saturating_sub(panel_height),
            width: panel_width,
            height: panel_height,
        };

        let block = Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(Style::default().fg(Self::PANEL_CYAN))
            .title(Span::styled(
                " Models ",
                Style::default()
                    .fg(Self::PANEL_CYAN)
                    .add_modifier(Modifier::BOLD),
            ));

        let paragraph = Paragraph::new(lines).block(block);
        frame.render_widget(ratatui::widgets::Clear, popup_area);
        frame.render_widget(paragraph, popup_area);
    }

    /// Render the debug panel overlay.
    pub(super) fn render_debug_panel(&self, frame: &mut ratatui::Frame, area: layout::Rect) {
        use crate::formatters::style_tokens;
        use ratatui::style::{Modifier, Style};
        use ratatui::text::{Line, Span};
        use ratatui::widgets::{Block, BorderType, Borders, Paragraph};

        let mut lines: Vec<Line> = Vec::new();
        lines.push(Line::from(""));

        let label_style = Style::default().fg(style_tokens::DIM_GREY);
        let value_style = Style::default()
            .fg(Self::PANEL_CYAN)
            .add_modifier(Modifier::BOLD);

        let stats = [
            ("Model", self.state.model.clone()),
            (
                "Tokens",
                format!("{} / {}", self.state.tokens_used, self.state.tokens_limit),
            ),
            ("Context", format!("{:.1}%", self.state.context_usage_pct)),
            ("Cost", format!("${:.4}", self.state.session_cost)),
            ("Messages", format!("{}", self.state.messages.len())),
            ("Active tools", format!("{}", self.state.active_tools.len())),
            (
                "Subagents",
                format!("{}", self.state.active_subagents.len()),
            ),
            (
                "Background tasks",
                format!("{}", self.state.background_task_count),
            ),
            ("Mode", format!("{}", self.state.mode)),
            ("Autonomy", format!("{}", self.state.autonomy)),
            ("Reasoning", format!("{}", self.state.reasoning_level)),
            (
                "Terminal",
                format!(
                    "{}x{}",
                    self.state.terminal_width, self.state.terminal_height
                ),
            ),
            ("Undo stack", format!("{}", self.state.undo_stack.len())),
        ];

        for (label, value) in &stats {
            lines.push(Line::from(vec![
                Span::styled(format!("    {label}: "), label_style),
                Span::styled(value.clone(), value_style),
            ]));
        }

        lines.push(Line::from(""));

        let panel_height = (lines.len() as u16 + 2).min(area.height.saturating_sub(4));
        let panel_width = 50u16.min(area.width.saturating_sub(4));
        let popup_area = layout::Rect {
            x: (area.width.saturating_sub(panel_width)) / 2,
            y: (area.height.saturating_sub(panel_height)) / 2,
            width: panel_width,
            height: panel_height,
        };

        let block = Block::default()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(Style::default().fg(Self::PANEL_CYAN))
            .title(Span::styled(
                " Debug ",
                Style::default()
                    .fg(Self::PANEL_CYAN)
                    .add_modifier(Modifier::BOLD),
            ));

        let paragraph = Paragraph::new(lines).block(block);
        frame.render_widget(ratatui::widgets::Clear, popup_area);
        frame.render_widget(paragraph, popup_area);
    }

    /// Render the tool approval prompt panel.
    pub(super) fn render_approval(&self, frame: &mut ratatui::Frame, input_area: layout::Rect) {
        use crate::formatters::style_tokens;
        use ratatui::style::{Modifier, Style};
        use ratatui::text::{Line, Span};

        let approval_options = self.approval_controller.options();
        let selected = self.approval_controller.selected_index();
        let command = self.approval_controller.command();
        let working_dir = self.approval_controller.working_dir();

        let content_lines = vec![
            Line::from(vec![
                Span::styled("    Command ", Style::default().fg(style_tokens::DIM_GREY)),
                Span::styled("\u{00b7} ", Style::default().fg(style_tokens::DIM_GREY)),
                Span::styled(
                    command.to_string(),
                    Style::default()
                        .fg(Self::PANEL_CYAN)
                        .add_modifier(Modifier::BOLD),
                ),
            ]),
            Line::from(Span::styled(
                format!("    Directory \u{00b7} {working_dir}"),
                Style::default().fg(style_tokens::DIM_GREY),
            )),
        ];

        let option_lines: Vec<Line> = approval_options
            .iter()
            .enumerate()
            .map(|(i, opt)| {
                Self::build_option_line(
                    i == selected,
                    &format!("{}.", opt.choice),
                    &opt.label,
                    &opt.description,
                )
            })
            .collect();

        Self::render_popup_panel(
            frame,
            input_area,
            " Approval ",
            &content_lines,
            &option_lines,
            "\u{2191}/\u{2193} choose \u{00b7} Enter confirm \u{00b7} Esc cancel",
            None,
        );
    }
}