serenade_console/commands/
debug_container.rs1use std::io::stdout;
4
5use crossterm::ExecutableCommand;
6use crossterm::event::{self, Event, KeyCode, KeyEventKind};
7use crossterm::terminal::{
8 EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode,
9};
10use ratatui::prelude::{Constraint, CrosstermBackend, Layout, Rect, Style, Terminal};
11use ratatui::widgets::{Block, Borders, Paragraph, Row, Table};
12
13use crate::application::stdout_is_terminal;
14use crate::{Command, ConsoleError, Input};
15
16#[derive(Clone, Copy, Debug, Default)]
18pub struct DebugContainerCommand;
19
20impl Command for DebugContainerCommand {
21 fn name(&self) -> &'static str {
22 "debug:container"
23 }
24
25 fn description(&self) -> &'static str {
26 "List DI services (ratatui when interactive)"
27 }
28
29 fn execute(&self, input: &Input) -> Result<(), ConsoleError> {
30 let ids = service_ids(input);
31 let want_plain = input.args().iter().any(|arg| arg == "--plain");
32 if want_plain || !stdout_is_terminal() {
33 print_plain(input, &ids);
34 return Ok(());
35 }
36 run_tui(input, &ids)
37 }
38}
39
40fn service_ids(input: &Input) -> Vec<String> {
41 input.container().map_or_else(Vec::new, |container| {
42 let mut ids: Vec<String> = container
43 .definitions()
44 .iter()
45 .map(|definition| definition.id().to_owned())
46 .collect();
47 ids.sort();
48 ids
49 })
50}
51
52fn print_plain(input: &Input, ids: &[String]) {
53 println!("Environment: {}", input.environment());
54 println!("Debug: {}", input.debug());
55 if ids.is_empty() {
56 println!("Services: (none - pass a container via Application::run_with)");
57 return;
58 }
59 println!("Services ({}):", ids.len());
60 for id in ids {
61 println!(" {id}");
62 }
63}
64
65fn run_tui(input: &Input, ids: &[String]) -> Result<(), ConsoleError> {
66 enable_raw_mode()?;
67 stdout().execute(EnterAlternateScreen)?;
68 let result = draw_loop(input, ids);
69 stdout().execute(LeaveAlternateScreen)?;
70 disable_raw_mode()?;
71 result
72}
73
74fn draw_loop(input: &Input, ids: &[String]) -> Result<(), ConsoleError> {
75 let mut terminal = Terminal::new(CrosstermBackend::new(stdout()))?;
76 loop {
77 terminal.draw(|frame| {
78 let area = frame.area();
79 render(frame, area, input, ids);
80 })?;
81
82 if event::poll(std::time::Duration::from_millis(200))?
83 && let Event::Key(key) = event::read()?
84 && key.kind == KeyEventKind::Press
85 && matches!(key.code, KeyCode::Char('q') | KeyCode::Esc)
86 {
87 break;
88 }
89 }
90 Ok(())
91}
92
93fn render(frame: &mut ratatui::Frame, area: Rect, input: &Input, ids: &[String]) {
94 let chunks = Layout::vertical([Constraint::Length(3), Constraint::Min(3)]).split(area);
95 let header = Paragraph::new(format!(
96 "debug:container env={} debug={} (q/Esc quit)",
97 input.environment(),
98 input.debug()
99 ))
100 .block(Block::default().borders(Borders::ALL).title("Serenade"));
101 frame.render_widget(header, chunks[0]);
102
103 let rows: Vec<Row> = if ids.is_empty() {
104 vec![Row::new(vec!["(no services)".to_owned()])]
105 } else {
106 ids.iter().map(|id| Row::new(vec![id.clone()])).collect()
107 };
108 let table = Table::new(rows, [Constraint::Percentage(100)])
109 .header(Row::new(vec!["Service id"]).style(Style::new().bold()))
110 .block(Block::default().borders(Borders::ALL).title("Container"));
111 frame.render_widget(table, chunks[1]);
112}