egui-cha-ds 0.6.0

Design System for egui-cha (Atoms, Molecules, Theme)
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
//! LogStream - Real-time log viewer component
//!
//! A scrollable log viewer with filtering and auto-scroll capabilities.
//!
//! # Example
//!
//! ```ignore
//! // State initialization (in Model)
//! let mut log_state = LogStreamState::new();
//!
//! // Pushing logs
//! log_state.push_info("Brain #0", "Starting inference task");
//! log_state.push_warn("Manager", "Queue depth > 100");
//! log_state.push_error("Worker #42", "Connection timeout");
//!
//! // Rendering
//! LogStream::new(&log_state)
//!     .height(300.0)
//!     .show_toolbar(true)
//!     .show(ui);
//! ```

use crate::atoms::{Button, Input};
use crate::semantics::LogSeverity;
use crate::Theme;
use egui::{FontFamily, Response, RichText, ScrollArea, Ui};
use std::collections::VecDeque;
use std::time::Instant;

/// A single log entry
#[derive(Clone, Debug)]
pub struct LogEntry {
    /// Timestamp when the log was created
    pub timestamp: Instant,
    /// Severity level
    pub severity: LogSeverity,
    /// Source identifier (e.g., "Brain #0", "Worker #42")
    pub source: Option<String>,
    /// Log message
    pub message: String,
}

impl LogEntry {
    /// Create a new log entry
    pub fn new(severity: LogSeverity, message: impl Into<String>) -> Self {
        Self {
            timestamp: Instant::now(),
            severity,
            source: None,
            message: message.into(),
        }
    }

    /// Create with source
    pub fn with_source(mut self, source: impl Into<String>) -> Self {
        self.source = Some(source.into());
        self
    }
}

/// Filter settings for log display
#[derive(Clone, Debug, Default)]
pub struct LogFilter {
    /// Minimum severity to show (None = show all)
    pub min_severity: Option<LogSeverity>,
    /// Source filter (None = show all)
    pub source: Option<String>,
    /// Text search query
    pub search: String,
}

impl LogFilter {
    /// Check if an entry passes the filter
    pub fn matches(&self, entry: &LogEntry) -> bool {
        // Severity filter
        if let Some(min) = self.min_severity {
            if entry.severity < min {
                return false;
            }
        }

        // Source filter
        if let Some(ref src) = self.source {
            if let Some(ref entry_src) = entry.source {
                if !entry_src.contains(src) {
                    return false;
                }
            } else {
                return false;
            }
        }

        // Search filter
        if !self.search.is_empty() {
            let query = self.search.to_lowercase();
            let msg_match = entry.message.to_lowercase().contains(&query);
            let src_match = entry
                .source
                .as_ref()
                .map(|s| s.to_lowercase().contains(&query))
                .unwrap_or(false);
            if !msg_match && !src_match {
                return false;
            }
        }

        true
    }
}

/// State for LogStream (owned by parent)
pub struct LogStreamState {
    entries: VecDeque<LogEntry>,
    max_entries: usize,
    /// Auto-scroll to bottom on new entries
    pub auto_scroll: bool,
    /// Current filter settings
    pub filter: LogFilter,
    /// Track if new entries were added (for auto-scroll)
    scroll_to_bottom: bool,
}

impl Default for LogStreamState {
    fn default() -> Self {
        Self::new()
    }
}

impl LogStreamState {
    /// Create a new empty state
    pub fn new() -> Self {
        Self {
            entries: VecDeque::new(),
            max_entries: 1000,
            auto_scroll: true,
            filter: LogFilter::default(),
            scroll_to_bottom: false,
        }
    }

    /// Set maximum entries to keep
    pub fn with_max_entries(mut self, max: usize) -> Self {
        self.max_entries = max;
        self
    }

    /// Push a log entry
    pub fn push(&mut self, entry: LogEntry) {
        self.entries.push_back(entry);

        // Trim old entries
        while self.entries.len() > self.max_entries {
            self.entries.pop_front();
        }

        if self.auto_scroll {
            self.scroll_to_bottom = true;
        }
    }

    /// Push a debug log
    pub fn push_debug(&mut self, source: &str, message: impl Into<String>) {
        self.push(LogEntry::new(LogSeverity::Debug, message).with_source(source));
    }

    /// Push an info log
    pub fn push_info(&mut self, source: &str, message: impl Into<String>) {
        self.push(LogEntry::new(LogSeverity::Info, message).with_source(source));
    }

    /// Push a warning log
    pub fn push_warn(&mut self, source: &str, message: impl Into<String>) {
        self.push(LogEntry::new(LogSeverity::Warn, message).with_source(source));
    }

    /// Push an error log
    pub fn push_error(&mut self, source: &str, message: impl Into<String>) {
        self.push(LogEntry::new(LogSeverity::Error, message).with_source(source));
    }

    /// Push a critical log
    pub fn push_critical(&mut self, source: &str, message: impl Into<String>) {
        self.push(LogEntry::new(LogSeverity::Critical, message).with_source(source));
    }

    /// Clear all entries
    pub fn clear(&mut self) {
        self.entries.clear();
    }

    /// Get total entry count
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Get filtered entries
    pub fn filtered_entries(&self) -> impl Iterator<Item = &LogEntry> {
        self.entries.iter().filter(|e| self.filter.matches(e))
    }

    /// Get filtered entry count
    pub fn filtered_len(&self) -> usize {
        self.filtered_entries().count()
    }

    /// Check and consume scroll flag
    fn take_scroll_flag(&mut self) -> bool {
        std::mem::take(&mut self.scroll_to_bottom)
    }
}

/// Timestamp display format
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TimestampFormat {
    /// Don't show timestamp
    None,
    /// Show time only (HH:MM:SS)
    #[default]
    TimeOnly,
    /// Show relative time (e.g., "2s ago")
    Relative,
}

/// LogStream component - displays a scrollable log view
pub struct LogStream<'a> {
    state: &'a mut LogStreamState,
    height: Option<f32>,
    show_timestamp: bool,
    show_source: bool,
    show_toolbar: bool,
    monospace: bool,
    timestamp_format: TimestampFormat,
}

impl<'a> LogStream<'a> {
    /// Create a new LogStream viewer
    pub fn new(state: &'a mut LogStreamState) -> Self {
        Self {
            state,
            height: None,
            show_timestamp: true,
            show_source: true,
            show_toolbar: true,
            monospace: true,
            timestamp_format: TimestampFormat::TimeOnly,
        }
    }

    /// Set fixed height (None = fill available space)
    pub fn height(mut self, h: f32) -> Self {
        self.height = Some(h);
        self
    }

    /// Show/hide timestamp column
    pub fn show_timestamp(mut self, show: bool) -> Self {
        self.show_timestamp = show;
        self
    }

    /// Show/hide source column
    pub fn show_source(mut self, show: bool) -> Self {
        self.show_source = show;
        self
    }

    /// Show/hide toolbar (filter controls)
    pub fn show_toolbar(mut self, show: bool) -> Self {
        self.show_toolbar = show;
        self
    }

    /// Use monospace font for log messages
    pub fn monospace(mut self, mono: bool) -> Self {
        self.monospace = mono;
        self
    }

    /// Set timestamp format
    pub fn timestamp_format(mut self, format: TimestampFormat) -> Self {
        self.timestamp_format = format;
        self
    }

    /// Show the log stream
    pub fn show(mut self, ui: &mut Ui) -> Response {
        let theme = Theme::current(ui.ctx());

        let response = ui
            .vertical(|ui| {
                // Toolbar
                if self.show_toolbar {
                    self.render_toolbar(ui, &theme);
                    ui.add_space(theme.spacing_sm);
                }

                // Log entries
                self.render_entries(ui, &theme);
            })
            .response;

        response
    }

    fn render_toolbar(&mut self, ui: &mut Ui, theme: &Theme) {
        ui.horizontal(|ui| {
            // Severity filter dropdown
            let severity_options: [(_, Option<LogSeverity>); 6] = [
                ("All", None),
                ("Debug+", Some(LogSeverity::Debug)),
                ("Info+", Some(LogSeverity::Info)),
                ("Warn+", Some(LogSeverity::Warn)),
                ("Error+", Some(LogSeverity::Error)),
                ("Critical", Some(LogSeverity::Critical)),
            ];

            let current_label = severity_options
                .iter()
                .find(|(_, v)| *v == self.state.filter.min_severity)
                .map(|(l, _)| *l)
                .unwrap_or("All");

            egui::ComboBox::from_id_salt("log_severity_filter")
                .selected_text(current_label)
                .width(80.0)
                .show_ui(ui, |ui| {
                    for (label, severity) in &severity_options {
                        if ui
                            .selectable_label(self.state.filter.min_severity == *severity, *label)
                            .clicked()
                        {
                            self.state.filter.min_severity = *severity;
                        }
                    }
                });

            // Search input
            ui.add_space(theme.spacing_sm);
            Input::new()
                .placeholder("Search...")
                .desired_width(150.0)
                .show(ui, &mut self.state.filter.search);

            // Clear button
            ui.add_space(theme.spacing_sm);
            if Button::ghost("Clear").show(ui) {
                self.state.clear();
            }

            // Auto-scroll toggle
            ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                ui.checkbox(&mut self.state.auto_scroll, "Auto-scroll");
            });
        });
    }

    fn render_entries(&mut self, ui: &mut Ui, theme: &Theme) {
        let scroll_to_bottom = self.state.take_scroll_flag();

        let scroll_area = if let Some(h) = self.height {
            ScrollArea::vertical().max_height(h)
        } else {
            ScrollArea::vertical()
        };

        scroll_area
            .auto_shrink([false, false])
            .stick_to_bottom(scroll_to_bottom)
            .show(ui, |ui| {
                let now = Instant::now();

                // Collect filtered entries (to avoid borrow issues)
                let entries: Vec<_> = self
                    .state
                    .entries
                    .iter()
                    .filter(|e| self.state.filter.matches(e))
                    .collect();

                if entries.is_empty() {
                    ui.label(
                        RichText::new("No log entries")
                            .italics()
                            .color(theme.text_muted),
                    );
                } else {
                    for entry in entries {
                        self.render_entry(ui, entry, theme, now);
                    }
                }
            });
    }

    fn render_entry(&self, ui: &mut Ui, entry: &LogEntry, theme: &Theme, now: Instant) {
        let severity_color = entry.severity.color(theme);

        ui.horizontal(|ui| {
            // Timestamp
            if self.show_timestamp && self.timestamp_format != TimestampFormat::None {
                let ts_text = match self.timestamp_format {
                    TimestampFormat::None => String::new(),
                    TimestampFormat::TimeOnly => {
                        let elapsed = now.duration_since(entry.timestamp);
                        let secs = elapsed.as_secs();
                        let mins = secs / 60;
                        let hours = mins / 60;
                        format!("{:02}:{:02}:{:02}", hours % 24, mins % 60, secs % 60)
                    }
                    TimestampFormat::Relative => {
                        let elapsed = now.duration_since(entry.timestamp);
                        if elapsed.as_secs() < 60 {
                            format!("{}s ago", elapsed.as_secs())
                        } else if elapsed.as_secs() < 3600 {
                            format!("{}m ago", elapsed.as_secs() / 60)
                        } else {
                            format!("{}h ago", elapsed.as_secs() / 3600)
                        }
                    }
                };
                ui.label(RichText::new(ts_text).color(theme.text_muted).monospace());
            }

            // Severity icon
            ui.label(
                RichText::new(entry.severity.icon())
                    .family(FontFamily::Name("icons".into()))
                    .color(severity_color),
            );

            // Severity label
            ui.label(
                RichText::new(format!("[{}]", entry.severity.label()))
                    .color(severity_color)
                    .strong(),
            );

            // Source
            if self.show_source {
                if let Some(ref source) = entry.source {
                    ui.label(RichText::new(source).color(theme.text_secondary));
                }
            }

            // Message
            let msg = if self.monospace {
                RichText::new(&entry.message).monospace()
            } else {
                RichText::new(&entry.message)
            };
            ui.label(msg);
        });
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_log_entry_creation() {
        let entry = LogEntry::new(LogSeverity::Info, "Test message").with_source("TestSource");

        assert_eq!(entry.severity, LogSeverity::Info);
        assert_eq!(entry.message, "Test message");
        assert_eq!(entry.source, Some("TestSource".to_string()));
    }

    #[test]
    fn test_log_stream_state_push() {
        let mut state = LogStreamState::new().with_max_entries(5);

        for i in 0..10 {
            state.push_info("test", format!("Message {}", i));
        }

        // Should only keep last 5
        assert_eq!(state.len(), 5);
    }

    #[test]
    fn test_log_filter_severity() {
        let filter = LogFilter {
            min_severity: Some(LogSeverity::Warn),
            ..Default::default()
        };

        let debug = LogEntry::new(LogSeverity::Debug, "debug");
        let info = LogEntry::new(LogSeverity::Info, "info");
        let warn = LogEntry::new(LogSeverity::Warn, "warn");
        let error = LogEntry::new(LogSeverity::Error, "error");

        assert!(!filter.matches(&debug));
        assert!(!filter.matches(&info));
        assert!(filter.matches(&warn));
        assert!(filter.matches(&error));
    }

    #[test]
    fn test_log_filter_search() {
        let filter = LogFilter {
            search: "error".to_string(),
            ..Default::default()
        };

        let entry1 = LogEntry::new(LogSeverity::Info, "An error occurred");
        let entry2 = LogEntry::new(LogSeverity::Info, "All good");
        let entry3 = LogEntry::new(LogSeverity::Info, "ok").with_source("ErrorHandler");

        assert!(filter.matches(&entry1));
        assert!(!filter.matches(&entry2));
        assert!(filter.matches(&entry3)); // matches source
    }

    #[test]
    fn test_filtered_entries() {
        let mut state = LogStreamState::new();
        state.push_debug("src", "debug msg");
        state.push_info("src", "info msg");
        state.push_warn("src", "warn msg");
        state.push_error("src", "error msg");

        state.filter.min_severity = Some(LogSeverity::Warn);

        assert_eq!(state.filtered_len(), 2);
    }
}