mecha10-cli 0.1.47

Mecha10 CLI tool
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
//! Topology TUI with ratatui
//!
//! Provides a 3-panel interface for exploring project topology:
//! - Left: Navigation menu (Services, Nodes, Topics)
//! - Right: Details of selected section
//! - Footer: Instructions and links

use crate::services::Topology;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::{
    layout::{Constraint, Direction, Layout},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, List, ListItem, ListState, Paragraph, Wrap},
    Frame,
};

/// Navigation sections
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Section {
    Services,
    Nodes,
    Topics,
}

impl Section {
    fn all() -> Vec<Section> {
        vec![Section::Services, Section::Nodes, Section::Topics]
    }

    fn label(&self) -> &str {
        match self {
            Section::Services => "Services",
            Section::Nodes => "Nodes",
            Section::Topics => "Topics",
        }
    }

    fn icon(&self) -> &str {
        match self {
            Section::Services => "🌐",
            Section::Nodes => "🔌",
            Section::Topics => "📨",
        }
    }
}

/// TUI for topology visualization
pub struct TopologyTui {
    topology: Topology,
    current_section: Section,
    detail_scroll_offset: usize,
    should_quit: bool,
}

impl TopologyTui {
    /// Create a new topology TUI
    pub fn new(topology: Topology) -> Self {
        Self {
            topology,
            current_section: Section::Services,
            detail_scroll_offset: 0,
            should_quit: false,
        }
    }

    /// Check if should quit
    pub fn should_quit(&self) -> bool {
        self.should_quit
    }

    /// Draw the TUI (single frame)
    pub fn draw(&mut self, f: &mut Frame) {
        // Split terminal vertically: content area + footer
        let vertical_chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Min(10),   // Main content area
                Constraint::Length(3), // Footer
            ])
            .split(f.area());

        // Split main content horizontally: navigation + details
        let horizontal_chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Percentage(30), // Navigation
                Constraint::Percentage(70), // Details
            ])
            .split(vertical_chunks[0]);

        // Draw panels
        self.draw_navigation(f, horizontal_chunks[0]);
        self.draw_details(f, horizontal_chunks[1]);
        self.draw_footer(f, vertical_chunks[1]);
    }

    /// Draw navigation panel
    fn draw_navigation(&mut self, f: &mut Frame, area: ratatui::layout::Rect) {
        let sections = Section::all();
        let items: Vec<ListItem> = sections
            .iter()
            .map(|section| {
                let is_selected = *section == self.current_section;

                let (prefix, style) = if is_selected {
                    ("", Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD))
                } else {
                    ("  ", Style::default().fg(Color::White))
                };

                let formatted = format!("{} {} {}", prefix, section.icon(), section.label());
                ListItem::new(formatted).style(style)
            })
            .collect();

        let title = format!(" {} ", self.topology.project_name);
        let list = List::new(items).block(
            Block::default()
                .borders(Borders::ALL)
                .title(title)
                .border_style(Style::default().fg(Color::Cyan)),
        );

        // Create list state for highlighting
        let mut state = ListState::default();
        let selected_index = sections.iter().position(|s| *s == self.current_section).unwrap_or(0);
        state.select(Some(selected_index));

        f.render_stateful_widget(list, area, &mut state);
    }

    /// Draw details panel
    fn draw_details(&self, f: &mut Frame, area: ratatui::layout::Rect) {
        match self.current_section {
            Section::Services => self.draw_services_details(f, area),
            Section::Nodes => self.draw_nodes_details(f, area),
            Section::Topics => self.draw_topics_details(f, area),
        }
    }

    /// Draw Services details
    fn draw_services_details(&self, f: &mut Frame, area: ratatui::layout::Rect) {
        let mut text = vec![
            Line::from(vec![Span::styled(
                "🌐 SERVICES",
                Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD),
            )]),
            Line::from(""),
        ];

        if self.topology.services.is_empty() {
            text.push(Line::from(vec![Span::styled(
                "No services configured.",
                Style::default().fg(Color::DarkGray),
            )]));
            text.push(Line::from(""));
            text.push(Line::from("Services can include:"));
            text.push(Line::from("  • HTTP API servers"));
            text.push(Line::from("  • Database connections"));
            text.push(Line::from("  • External integrations"));
        } else {
            for service in &self.topology.services {
                text.push(Line::from(vec![Span::styled(
                    &service.name,
                    Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD),
                )]));
                text.push(Line::from(vec![
                    Span::raw("  Host: "),
                    Span::styled(&service.host, Style::default().fg(Color::Green)),
                ]));
                text.push(Line::from(vec![
                    Span::raw("  Port: "),
                    Span::styled(service.port.to_string(), Style::default().fg(Color::Green)),
                ]));
                text.push(Line::from(vec![
                    Span::raw("  URL:  "),
                    Span::styled(
                        format!("http://{}:{}", service.host, service.port),
                        Style::default().fg(Color::Yellow),
                    ),
                ]));
                text.push(Line::from(""));
            }
        }

        let paragraph = Paragraph::new(text)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title(format!(" Services ({}) ", self.topology.services.len()))
                    .border_style(Style::default().fg(Color::Yellow)),
            )
            .wrap(Wrap { trim: true });

        f.render_widget(paragraph, area);
    }

    /// Draw Nodes details
    fn draw_nodes_details(&self, f: &mut Frame, area: ratatui::layout::Rect) {
        let mut text = vec![
            Line::from(vec![Span::styled(
                "🔌 NODES & TOPICS",
                Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD),
            )]),
            Line::from(""),
        ];

        if self.topology.nodes.is_empty() {
            text.push(Line::from(vec![Span::styled(
                "No nodes found.",
                Style::default().fg(Color::DarkGray),
            )]));
        } else {
            for node in &self.topology.nodes {
                text.push(Line::from(vec![
                    Span::raw("📦 "),
                    Span::styled(
                        &node.name,
                        Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD),
                    ),
                ]));

                text.push(Line::from(vec![
                    Span::raw("   Package: "),
                    Span::styled(&node.package, Style::default().fg(Color::Yellow)),
                ]));

                if let Some(desc) = &node.description {
                    text.push(Line::from(vec![
                        Span::raw("   "),
                        Span::styled(desc, Style::default().fg(Color::DarkGray)),
                    ]));
                }

                // Publishes
                if !node.publishes.is_empty() {
                    text.push(Line::from(vec![Span::styled(
                        "   ├─ Publishes:",
                        Style::default().fg(Color::DarkGray),
                    )]));
                    for topic in &node.publishes {
                        let msg_type = topic
                            .message_type
                            .as_ref()
                            .map(|t| format!(" ({})", t))
                            .unwrap_or_default();
                        text.push(Line::from(vec![
                            Span::raw("   │  ├─ "),
                            Span::styled(&topic.path, Style::default().fg(Color::Green)),
                            Span::styled(msg_type, Style::default().fg(Color::Yellow)),
                        ]));
                    }
                } else {
                    text.push(Line::from(vec![Span::styled(
                        "   ├─ Publishes: (none)",
                        Style::default().fg(Color::DarkGray),
                    )]));
                }

                // Subscribes
                if !node.subscribes.is_empty() {
                    text.push(Line::from(vec![Span::styled(
                        "   └─ Subscribes:",
                        Style::default().fg(Color::DarkGray),
                    )]));
                    for topic in &node.subscribes {
                        let msg_type = topic
                            .message_type
                            .as_ref()
                            .map(|t| format!(" ({})", t))
                            .unwrap_or_default();
                        text.push(Line::from(vec![
                            Span::raw("      ├─ "),
                            Span::styled(&topic.path, Style::default().fg(Color::Blue)),
                            Span::styled(msg_type, Style::default().fg(Color::Yellow)),
                        ]));
                    }
                } else {
                    text.push(Line::from(vec![Span::styled(
                        "   └─ Subscribes: (none)",
                        Style::default().fg(Color::DarkGray),
                    )]));
                }

                text.push(Line::from(""));
            }
        }

        // Skip lines for scroll offset
        let text: Vec<Line> = text.into_iter().skip(self.detail_scroll_offset).collect();

        let paragraph = Paragraph::new(text)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title(format!(" Nodes ({}) ", self.topology.nodes.len()))
                    .border_style(Style::default().fg(Color::Yellow)),
            )
            .wrap(Wrap { trim: true });

        f.render_widget(paragraph, area);
    }

    /// Draw Topics details
    fn draw_topics_details(&self, f: &mut Frame, area: ratatui::layout::Rect) {
        let mut text = vec![
            Line::from(vec![Span::styled(
                "📨 TOPICS",
                Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD),
            )]),
            Line::from(""),
        ];

        if self.topology.topics.is_empty() {
            text.push(Line::from(vec![Span::styled(
                "No topics found.",
                Style::default().fg(Color::DarkGray),
            )]));
        } else {
            for topic in &self.topology.topics {
                let msg_type = topic
                    .message_type
                    .as_ref()
                    .map(|t| format!(" ({})", t))
                    .unwrap_or_default();

                text.push(Line::from(vec![
                    Span::raw("📨 "),
                    Span::styled(
                        &topic.path,
                        Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD),
                    ),
                    Span::styled(msg_type, Style::default().fg(Color::Yellow)),
                ]));

                // Publishers
                if !topic.publishers.is_empty() {
                    text.push(Line::from(vec![Span::styled(
                        "   ├─ Publishers:",
                        Style::default().fg(Color::DarkGray),
                    )]));
                    for node in &topic.publishers {
                        text.push(Line::from(vec![
                            Span::raw("   │  ├─ "),
                            Span::styled(node, Style::default().fg(Color::Green)),
                        ]));
                    }
                } else {
                    text.push(Line::from(vec![Span::styled(
                        "   ├─ Publishers: (none)",
                        Style::default().fg(Color::DarkGray),
                    )]));
                }

                // Subscribers
                if !topic.subscribers.is_empty() {
                    text.push(Line::from(vec![Span::styled(
                        "   └─ Subscribers:",
                        Style::default().fg(Color::DarkGray),
                    )]));
                    for node in &topic.subscribers {
                        text.push(Line::from(vec![
                            Span::raw("      ├─ "),
                            Span::styled(node, Style::default().fg(Color::Blue)),
                        ]));
                    }
                } else {
                    text.push(Line::from(vec![Span::styled(
                        "   └─ Subscribers: (none)",
                        Style::default().fg(Color::DarkGray),
                    )]));
                }

                text.push(Line::from(""));
            }
        }

        // Skip lines for scroll offset
        let text: Vec<Line> = text.into_iter().skip(self.detail_scroll_offset).collect();

        let paragraph = Paragraph::new(text)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .title(format!(" Topics ({}) ", self.topology.topics.len()))
                    .border_style(Style::default().fg(Color::Yellow)),
            )
            .wrap(Wrap { trim: true });

        f.render_widget(paragraph, area);
    }

    /// Draw footer with links
    fn draw_footer(&self, f: &mut Frame, area: ratatui::layout::Rect) {
        let footer_text = vec![Line::from(vec![
            Span::raw("  "),
            Span::styled("↑/↓:", Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD)),
            Span::raw(" Navigate  "),
            Span::styled("j/k:", Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)),
            Span::raw(" Scroll  "),
            Span::styled("Q/ESC:", Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)),
            Span::raw(" Exit"),
        ])];

        let footer = Paragraph::new(footer_text).block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::DarkGray)),
        );

        f.render_widget(footer, area);
    }

    /// Handle keyboard input
    pub fn handle_key(&mut self, key: KeyEvent) {
        match key.code {
            // Exit
            KeyCode::Char('q') | KeyCode::Esc => {
                self.should_quit = true;
            }
            KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                self.should_quit = true;
            }

            // Navigate sections
            KeyCode::Up => {
                let sections = Section::all();
                let current_index = sections.iter().position(|s| *s == self.current_section).unwrap_or(0);
                if current_index > 0 {
                    self.current_section = sections[current_index - 1];
                    self.detail_scroll_offset = 0; // Reset scroll when changing sections
                }
            }
            KeyCode::Down => {
                let sections = Section::all();
                let current_index = sections.iter().position(|s| *s == self.current_section).unwrap_or(0);
                if current_index < sections.len() - 1 {
                    self.current_section = sections[current_index + 1];
                    self.detail_scroll_offset = 0; // Reset scroll when changing sections
                }
            }

            // Scroll details (PgUp/PgDn, Ctrl+U/D, or j/k for laptops)
            KeyCode::PageUp | KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                self.detail_scroll_offset = self.detail_scroll_offset.saturating_sub(10);
            }
            KeyCode::PageDown | KeyCode::Char('d') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                self.detail_scroll_offset = self.detail_scroll_offset.saturating_add(10);
            }
            KeyCode::Char('k') => {
                self.detail_scroll_offset = self.detail_scroll_offset.saturating_sub(1);
            }
            KeyCode::Char('j') => {
                self.detail_scroll_offset = self.detail_scroll_offset.saturating_add(1);
            }

            _ => {}
        }
    }
}