Skip to main content

rtcom_tui/menu/
placeholder.rs

1//! Dummy dialog used while the real sub-menu dialogs are unimplemented.
2//!
3//! Renders a bordered box titled with whatever the parent menu passed
4//! in and closes on Esc. Tasks T12–T15 replace every use of this with
5//! a real dialog.
6
7use crossterm::event::{KeyCode, KeyEvent};
8use ratatui::{
9    buffer::Buffer,
10    layout::Rect,
11    widgets::{Block, Paragraph, Widget},
12};
13
14use crate::modal::{Dialog, DialogOutcome};
15
16/// Minimal placeholder dialog. Shows `TODO: <title>` and closes on Esc.
17pub struct PlaceholderDialog {
18    title: String,
19}
20
21impl PlaceholderDialog {
22    /// Create a new placeholder labelled `title`.
23    #[must_use]
24    pub fn new(title: impl Into<String>) -> Self {
25        Self {
26            title: title.into(),
27        }
28    }
29}
30
31impl Dialog for PlaceholderDialog {
32    fn title(&self) -> &str {
33        &self.title
34    }
35
36    fn render(&self, area: Rect, buf: &mut Buffer) {
37        let block = Block::bordered().title(self.title.as_str());
38        let para = Paragraph::new(format!("TODO: {}", self.title)).block(block);
39        para.render(area, buf);
40    }
41
42    fn handle_key(&mut self, key: KeyEvent) -> DialogOutcome {
43        if key.code == KeyCode::Esc {
44            DialogOutcome::Close
45        } else {
46            DialogOutcome::Consumed
47        }
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
55
56    const fn key(code: KeyCode) -> KeyEvent {
57        KeyEvent::new(code, KeyModifiers::NONE)
58    }
59
60    #[test]
61    fn placeholder_title_round_trips() {
62        let d = PlaceholderDialog::new("Line endings");
63        assert_eq!(d.title(), "Line endings");
64    }
65
66    #[test]
67    fn placeholder_esc_closes() {
68        let mut d = PlaceholderDialog::new("x");
69        let out = d.handle_key(key(KeyCode::Esc));
70        assert!(matches!(out, DialogOutcome::Close));
71    }
72
73    #[test]
74    fn placeholder_other_keys_consumed() {
75        let mut d = PlaceholderDialog::new("x");
76        let out = d.handle_key(key(KeyCode::Char('a')));
77        assert!(matches!(out, DialogOutcome::Consumed));
78    }
79}