theater-cli 0.2.0

Command-line interface for Theater actor system
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
use ratatui::{
    layout::Rect,
    style::{Color, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState, Wrap},
    Frame,
};

use super::super::app::{DetailMode, EventExplorerApp};
use chrono::{TimeZone, Utc};

pub fn render_event_detail(f: &mut Frame, app: &EventExplorerApp, area: Rect) {
    let title = format!(
        " Event Details - {} [Tab: {}] [←→ scroll] ",
        app.detail_mode.display_name(),
        app.detail_mode.next_mode_name()
    );

    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(if app.show_help {
            Style::default().fg(Color::Gray)
        } else {
            Style::default().fg(Color::Green)
        });

    let content = if let Some(event) = app.get_selected_event() {
        match app.detail_mode {
            DetailMode::Overview => render_event_overview(event),
            DetailMode::JsonData => render_event_json(event),
            DetailMode::RawData => render_event_raw(event),
            DetailMode::ChainView => render_event_chain(event, app),
        }
    } else {
        vec![
            Line::from(""),
            Line::from(Span::styled(
                "No event selected",
                Style::default().fg(Color::Gray),
            )),
            Line::from(""),
            Line::from(Span::styled(
                "Use ↑↓ or j/k to navigate events",
                Style::default().fg(Color::Gray),
            )),
        ]
    };

    // Calculate available height for content (minus borders)
    let content_height = area.height.saturating_sub(2) as usize;
    let total_lines = content.len();

    // Clamp scroll offset to valid range
    let max_scroll = total_lines.saturating_sub(content_height);
    let scroll_offset = app.detail_scroll_offset.min(max_scroll);

    // Take the visible slice of content
    let visible_content: Vec<Line> = content
        .into_iter()
        .skip(scroll_offset)
        .take(content_height)
        .collect();

    let paragraph = Paragraph::new(visible_content)
        .block(block)
        .wrap(Wrap { trim: true });

    f.render_widget(paragraph, area);

    // Render scrollbar if content is longer than available space
    if total_lines > content_height {
        let scrollbar = Scrollbar::new(ScrollbarOrientation::VerticalRight)
            .begin_symbol(Some(""))
            .end_symbol(Some(""));

        let mut scrollbar_state = ScrollbarState::new(total_lines).position(scroll_offset);

        f.render_stateful_widget(
            scrollbar,
            area.inner(&ratatui::layout::Margin {
                vertical: 1,
                horizontal: 0,
            }),
            &mut scrollbar_state,
        );
    }
}

fn render_event_overview(event: &theater::chain::ChainEvent) -> Vec<Line> {
    let mut lines = vec![
        Line::from(""),
        Line::from(vec![
            Span::styled("Type: ", Style::default().fg(Color::Gray)),
            Span::styled(&event.event_type, Style::default().fg(Color::Cyan)),
        ]),
        Line::from(vec![
            Span::styled("Timestamp: ", Style::default().fg(Color::Gray)),
            Span::styled(
                format_full_timestamp(event.timestamp),
                Style::default().fg(Color::White),
            ),
        ]),
        Line::from(vec![
            Span::styled("Hash: ", Style::default().fg(Color::Gray)),
            Span::styled(format_hash(&event.hash), Style::default().fg(Color::Yellow)),
        ]),
    ];

    if let Some(parent_hash) = &event.parent_hash {
        lines.push(Line::from(vec![
            Span::styled("Parent: ", Style::default().fg(Color::Gray)),
            Span::styled(format_hash(parent_hash), Style::default().fg(Color::Yellow)),
        ]));
    } else {
        lines.push(Line::from(vec![
            Span::styled("Parent: ", Style::default().fg(Color::Gray)),
            Span::styled("None (root event)", Style::default().fg(Color::Green)),
        ]));
    }

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

    if let Some(description) = &event.description {
        lines.push(Line::from(vec![Span::styled(
            "Description:",
            Style::default().fg(Color::Gray),
        )]));
        lines.push(Line::from(""));

        // Split long descriptions into multiple lines
        let words: Vec<&str> = description.split_whitespace().collect();
        let mut current_line = String::new();

        for word in words {
            if current_line.len() + word.len() + 1 > 60 {
                if !current_line.is_empty() {
                    lines.push(Line::from(vec![
                        Span::styled("  ", Style::default()),
                        Span::styled(current_line.clone(), Style::default().fg(Color::White)),
                    ]));
                    current_line.clear();
                }
            }

            if !current_line.is_empty() {
                current_line.push(' ');
            }
            current_line.push_str(word);
        }

        if !current_line.is_empty() {
            lines.push(Line::from(vec![
                Span::styled("  ", Style::default()),
                Span::styled(current_line, Style::default().fg(Color::White)),
            ]));
        }
    } else {
        lines.push(Line::from(vec![
            Span::styled("Description: ", Style::default().fg(Color::Gray)),
            Span::styled("None", Style::default().fg(Color::Gray)),
        ]));
    }

    lines.push(Line::from(""));
    lines.push(Line::from(vec![
        Span::styled("Data Size: ", Style::default().fg(Color::Gray)),
        Span::styled(
            format_bytes(event.data.len()),
            Style::default().fg(Color::White),
        ),
    ]));

    // Data type and preview
    let data_type = detect_data_type(&event.data);
    lines.push(Line::from(vec![
        Span::styled("Data Type: ", Style::default().fg(Color::Gray)),
        Span::styled(data_type, Style::default().fg(Color::Cyan)),
    ]));

    // Add data preview in overview mode
    lines.push(Line::from(""));
    lines.push(Line::from(vec![Span::styled(
        "Data Preview:",
        Style::default().fg(Color::Gray),
    )]));

    let preview_lines = render_data_preview(&event.data, 5); // Show up to 5 lines
    lines.extend(preview_lines);

    lines
}

fn render_event_json(event: &theater::chain::ChainEvent) -> Vec<Line> {
    let mut lines = vec![
        Line::from(""),
        Line::from(vec![
            Span::styled("Data Content (", Style::default().fg(Color::Gray)),
            Span::styled(
                format_bytes(event.data.len()),
                Style::default().fg(Color::White),
            ),
            Span::styled("):", Style::default().fg(Color::Gray)),
        ]),
        Line::from(""),
    ];

    // Try to parse event data as JSON first
    if let Ok(data_str) = std::str::from_utf8(&event.data) {
        if let Ok(json_value) = serde_json::from_str::<serde_json::Value>(data_str) {
            if let Ok(pretty_json) = serde_json::to_string_pretty(&json_value) {
                lines.push(Line::from(vec![
                    Span::styled("Format: ", Style::default().fg(Color::Gray)),
                    Span::styled("JSON (pretty-printed)", Style::default().fg(Color::Green)),
                ]));
                lines.push(Line::from(""));

                let json_lines: Vec<&str> = pretty_json.lines().collect();
                for line in json_lines.iter() {
                    // Show all JSON lines
                    lines.push(Line::from(Span::styled(
                        line.to_string(),
                        Style::default().fg(Color::Cyan),
                    )));
                }

                return lines;
            }
        }
    }

    // Fallback to stringified bytes if not JSON
    lines.push(Line::from(vec![
        Span::styled("Format: ", Style::default().fg(Color::Gray)),
        Span::styled("Stringified bytes", Style::default().fg(Color::Yellow)),
    ]));
    lines.push(Line::from(""));

    let stringified = stringify_bytes(&event.data);
    let string_lines: Vec<&str> = stringified.lines().collect();

    for line in string_lines.iter() {
        // Show all stringified lines
        lines.push(Line::from(Span::styled(
            line.to_string(),
            Style::default().fg(Color::White),
        )));
    }

    lines
}

fn render_event_raw(event: &theater::chain::ChainEvent) -> Vec<Line> {
    let mut lines = vec![
        Line::from(""),
        Line::from(vec![
            Span::styled("Raw Data (", Style::default().fg(Color::Gray)),
            Span::styled(
                format_bytes(event.data.len()),
                Style::default().fg(Color::White),
            ),
            Span::styled("):", Style::default().fg(Color::Gray)),
        ]),
        Line::from(""),
    ];

    // Hex dump with ASCII - show all data with scrolling
    for (i, chunk) in event.data.chunks(16).enumerate() {
        let offset = format!("{:08x}", i * 16);
        let hex_part: Vec<String> = chunk.iter().map(|b| format!("{:02x}", b)).collect();
        let ascii_part: String = chunk
            .iter()
            .map(|&b| {
                if b.is_ascii_graphic() || b == b' ' {
                    b as char
                } else {
                    '.'
                }
            })
            .collect();

        let hex_str = format!("{:<48}", hex_part.join(" "));

        lines.push(Line::from(vec![
            Span::styled(offset, Style::default().fg(Color::Yellow)),
            Span::styled("  ", Style::default()),
            Span::styled(hex_str, Style::default().fg(Color::Cyan)),
            Span::styled(" |", Style::default().fg(Color::Gray)),
            Span::styled(ascii_part, Style::default().fg(Color::White)),
            Span::styled("|", Style::default().fg(Color::Gray)),
        ]));
    }

    lines
}

fn render_event_chain<'a>(
    event: &'a theater::chain::ChainEvent,
    app: &'a EventExplorerApp,
) -> Vec<Line<'a>> {
    let mut lines = vec![
        Line::from(""),
        Line::from(vec![Span::styled(
            "Chain Context:",
            Style::default().fg(Color::Gray),
        )]),
        Line::from(""),
    ];

    // Find parent events
    if let Some(parent_hash) = &event.parent_hash {
        if let Some(parent) = find_event_by_hash(&app.events, parent_hash) {
            lines.push(Line::from(vec![
                Span::styled("Parent: ", Style::default().fg(Color::Gray)),
                Span::styled(&parent.event_type, Style::default().fg(Color::Blue)),
                Span::styled("", Style::default().fg(Color::Gray)),
            ]));
        } else {
            lines.push(Line::from(vec![
                Span::styled("Parent: ", Style::default().fg(Color::Gray)),
                Span::styled("Missing (orphaned event)", Style::default().fg(Color::Red)),
            ]));
        }
    } else {
        lines.push(Line::from(vec![
            Span::styled("Parent: ", Style::default().fg(Color::Gray)),
            Span::styled("None (root event)", Style::default().fg(Color::Green)),
        ]));
    }

    // Current event
    lines.push(Line::from(vec![
        Span::styled("Current: ", Style::default().fg(Color::Gray)),
        Span::styled(&event.event_type, Style::default().fg(Color::Yellow)),
    ]));

    // Find child events
    let children = find_child_events(&app.events, &event.hash);
    if !children.is_empty() {
        lines.push(Line::from(vec![
            Span::styled("Children: ", Style::default().fg(Color::Gray)),
            Span::styled(
                format!("({} total)", children.len()),
                Style::default().fg(Color::Gray),
            ),
        ]));

        for child in children.iter().take(5) {
            // Show max 5 children
            lines.push(Line::from(vec![
                Span::styled("", Style::default().fg(Color::Gray)),
                Span::styled(&child.event_type, Style::default().fg(Color::Green)),
            ]));
        }

        if children.len() > 5 {
            lines.push(Line::from(vec![
                Span::styled("  ... and ", Style::default().fg(Color::Gray)),
                Span::styled(
                    format!("{} more", children.len() - 5),
                    Style::default().fg(Color::Gray),
                ),
            ]));
        }
    } else {
        lines.push(Line::from(vec![
            Span::styled("Children: ", Style::default().fg(Color::Gray)),
            Span::styled("None (leaf event)", Style::default().fg(Color::Green)),
        ]));
    }

    lines
}

fn format_full_timestamp(timestamp: u64) -> String {
    let dt = Utc
        .timestamp_opt(timestamp as i64, 0)
        .single()
        .unwrap_or_else(|| Utc::now());
    dt.format("%Y-%m-%d %H:%M:%S UTC").to_string()
}

fn format_hash(hash: &[u8]) -> String {
    let hex = hex::encode(hash);
    if hex.len() > 16 {
        format!("{}...{}", &hex[..8], &hex[hex.len() - 8..])
    } else {
        hex
    }
}

fn format_bytes(bytes: usize) -> String {
    if bytes < 1024 {
        format!("{} B", bytes)
    } else if bytes < 1024 * 1024 {
        format!("{:.1} KB", bytes as f64 / 1024.0)
    } else {
        format!("{:.1} MB", bytes as f64 / (1024.0 * 1024.0))
    }
}

fn detect_data_type(data: &[u8]) -> &'static str {
    if data.is_empty() {
        return "Empty";
    }

    if let Ok(text) = std::str::from_utf8(data) {
        if serde_json::from_str::<serde_json::Value>(text).is_ok() {
            "JSON"
        } else if text
            .chars()
            .all(|c| c.is_ascii_graphic() || c.is_ascii_whitespace())
        {
            "Text (UTF-8)"
        } else {
            "Text (UTF-8 with special chars)"
        }
    } else {
        "Binary"
    }
}

/// Stringify bytes into a readable format
fn stringify_bytes(data: &[u8]) -> String {
    if data.is_empty() {
        return "(empty)".to_string();
    }

    // Try UTF-8 first
    if let Ok(text) = std::str::from_utf8(data) {
        return text.to_string();
    }

    // If not valid UTF-8, convert each byte to its string representation
    // This will show the actual byte values in a readable way
    let mut result = String::new();
    for (i, &byte) in data.iter().enumerate() {
        if i > 0 && i % 16 == 0 {
            result.push('\n');
        } else if i > 0 && i % 8 == 0 {
            result.push_str("  ");
        } else if i > 0 {
            result.push(' ');
        }

        // Show printable ASCII as characters, others as hex
        if byte.is_ascii_graphic() || byte == b' ' {
            result.push_str(&format!("'{}'", byte as char));
        } else {
            result.push_str(&format!("0x{:02x}", byte));
        }
    }
    result
}

/// Render a preview of the data (first few lines)
fn render_data_preview(data: &[u8], max_lines: usize) -> Vec<Line> {
    let mut lines = Vec::new();

    if data.is_empty() {
        lines.push(Line::from(vec![
            Span::styled("  ", Style::default()),
            Span::styled("(no data)", Style::default().fg(Color::Gray)),
        ]));
        return lines;
    }

    // Try UTF-8 first
    if let Ok(text) = std::str::from_utf8(data) {
        // Check if it's JSON for nice formatting
        if let Ok(json_value) = serde_json::from_str::<serde_json::Value>(text) {
            if let Ok(pretty_json) = serde_json::to_string_pretty(&json_value) {
                let json_lines: Vec<&str> = pretty_json.lines().collect();
                for line in json_lines.iter().take(max_lines) {
                    lines.push(Line::from(vec![
                        Span::styled("  ", Style::default()),
                        Span::styled(line.to_string(), Style::default().fg(Color::Cyan)),
                    ]));
                }
                if json_lines.len() > max_lines {
                    lines.push(Line::from(vec![
                        Span::styled("  ", Style::default()),
                        Span::styled(
                            format!(
                                "... ({} more lines - switch to Data view)",
                                json_lines.len() - max_lines
                            ),
                            Style::default().fg(Color::Yellow),
                        ),
                    ]));
                }
                return lines;
            }
        }

        // Regular text
        for line in text.lines().take(max_lines) {
            let display_line = if line.len() > 60 {
                format!("{}...", &line[..57])
            } else {
                line.to_string()
            };
            lines.push(Line::from(vec![
                Span::styled("  ", Style::default()),
                Span::styled(display_line, Style::default().fg(Color::White)),
            ]));
        }

        if text.lines().count() > max_lines {
            lines.push(Line::from(vec![
                Span::styled("  ", Style::default()),
                Span::styled(
                    format!(
                        "... ({} more lines - switch to Data view)",
                        text.lines().count() - max_lines
                    ),
                    Style::default().fg(Color::Yellow),
                ),
            ]));
        }
    } else {
        // Binary data - show first few bytes
        let preview_bytes = &data[..data.len().min(32)];
        let mut preview = String::new();

        for (i, &byte) in preview_bytes.iter().enumerate() {
            if i > 0 {
                preview.push(' ');
            }
            if byte.is_ascii_graphic() {
                preview.push_str(&format!("'{}'", byte as char));
            } else {
                preview.push_str(&format!("0x{:02x}", byte));
            }
        }

        if data.len() > 32 {
            preview.push_str(" ...");
        }

        lines.push(Line::from(vec![
            Span::styled("  ", Style::default()),
            Span::styled(preview, Style::default().fg(Color::Magenta)),
        ]));

        lines.push(Line::from(vec![
            Span::styled("  ", Style::default()),
            Span::styled(
                "(binary data - use JSON or Raw view for details)",
                Style::default().fg(Color::Gray),
            ),
        ]));
    }

    lines
}

fn find_event_by_hash<'a>(
    events: &'a [theater::chain::ChainEvent],
    hash: &[u8],
) -> Option<&'a theater::chain::ChainEvent> {
    events.iter().find(|e| e.hash == hash)
}

fn find_child_events<'a>(
    events: &'a [theater::chain::ChainEvent],
    parent_hash: &[u8],
) -> Vec<&'a theater::chain::ChainEvent> {
    events
        .iter()
        .filter(|e| e.parent_hash.as_deref() == Some(parent_hash))
        .collect()
}