droidkraft-tui 1.2.0

A beautiful Terminal User Interface (TUI) for Android development and ADB commands
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! Logcat viewer state for the TUI.
//!
//! The framework-free domain (log levels, parsing, filtering, stats, saving,
//! and the streaming engine) lives in [`droidkraft_core::features::logcat`]; this
//! module keeps only the TUI-specific view state ([`LogcatState`]) and the
//! Ratatui colour mapping.

use ratatui::style::Color;
use std::collections::{BTreeSet, HashSet};
use std::fmt;
use std::io::Write;

// Re-export the shared domain types so existing `crate::logcat::…` paths keep
// working across the TUI.
pub use droidkraft_core::features::logcat::{
    try_format_json, wrap_entry_message, ChannelWriter, FilterField, LogEntry, LogLevel, LogStats,
    LogcatFilter, SaveFormat,
};
use droidkraft_core::features::logcat::{DrainStatus, LogcatStream};
use droidkraft_core::utils::copy_to_clipboard;

// ---------------------------------------------------------------------------
// Ratatui colour mapping (presentation — TUI only)
// ---------------------------------------------------------------------------

/// Colour used for a log level's message text.
pub fn level_color(level: LogLevel) -> Color {
    match level {
        LogLevel::Verbose => Color::DarkGray,
        LogLevel::Debug => Color::Cyan,
        LogLevel::Info => Color::Green,
        LogLevel::Warn => Color::Yellow,
        LogLevel::Error => Color::Red,
        LogLevel::Fatal => Color::LightRed,
        LogLevel::Unknown => Color::Gray,
    }
}

/// Brighter colour used for a log level's badge / label.
pub fn level_label_color(level: LogLevel) -> Color {
    match level {
        LogLevel::Verbose => Color::Gray,
        LogLevel::Debug => Color::LightCyan,
        LogLevel::Info => Color::LightGreen,
        LogLevel::Warn => Color::LightYellow,
        LogLevel::Error => Color::LightRed,
        LogLevel::Fatal => Color::Magenta,
        LogLevel::Unknown => Color::White,
    }
}

/// Hash a tag string to a stable, visually distinct colour (shared palette).
pub fn tag_color(tag: &str) -> Color {
    let (r, g, b) = droidkraft_core::tag_color(tag).tuple();
    Color::Rgb(r, g, b)
}

// ---------------------------------------------------------------------------
// LogcatState
// ---------------------------------------------------------------------------

/// Maximum number of log entries kept in memory.
const MAX_ENTRIES: usize = 50_000;

/// Maximum number of lines drained from the stream per tick to keep the UI
/// responsive.
const MAX_DRAIN_PER_TICK: usize = 500;

/// Full state of the logcat viewer.
pub struct LogcatState {
    /// All received log entries (ring buffer capped at `MAX_ENTRIES`).
    pub entries: Vec<LogEntry>,
    /// Indices into `entries` that match the current filter.
    pub filtered_indices: Vec<usize>,
    /// Current filter settings.
    pub filter: LogcatFilter,
    /// Index of the first visible line within the filtered view.
    pub scroll_position: usize,
    /// Whether the view automatically sticks to the bottom.
    pub auto_scroll: bool,
    /// Whether ingestion of new entries is paused.
    pub paused: bool,
    /// Background streaming engine (shared core type).
    stream: LogcatStream,
    /// Number of entries trimmed from the front of `entries` so far.
    trimmed_total: usize,
    /// Whether the background streaming thread is (believed to be) running.
    pub is_streaming: bool,
    /// Total number of raw lines received since streaming started.
    pub total_received: u64,
    /// Optional status or error message to display in the UI.
    pub status_message: Option<String>,
    /// Whether long lines should be word-wrapped in the view.
    pub word_wrap: bool,
    /// Last known viewport height (set by the view layer each frame).
    pub viewport_height: usize,
    /// Whether the line-detail popup is open.
    pub detail_open: bool,
    /// Index into `filtered_indices` for the currently selected line.
    pub selected_line: usize,
    /// Entry indices of fold-start heads for stack traces.
    pub folded_groups: HashSet<usize>,
    /// Aggregate statistics for the log stream.
    pub stats: LogStats,
    /// Horizontal scroll offset (in characters).
    pub h_scroll: usize,
    /// Whether compact display mode is enabled.
    pub compact: bool,
    /// Bookmarked entry indices (stable across filter changes).
    pub bookmarks: BTreeSet<usize>,
}

impl LogcatState {
    /// Create a new, empty `LogcatState`.
    pub fn new() -> Self {
        Self {
            entries: Vec::with_capacity(1024),
            filtered_indices: Vec::with_capacity(1024),
            filter: LogcatFilter::default(),
            scroll_position: 0,
            auto_scroll: true,
            paused: false,
            stream: LogcatStream::new(),
            trimmed_total: 0,
            is_streaming: false,
            total_received: 0,
            status_message: None,
            word_wrap: false,
            viewport_height: 30,
            detail_open: false,
            selected_line: 0,
            folded_groups: HashSet::new(),
            stats: LogStats::new(),
            h_scroll: 0,
            compact: false,
            bookmarks: BTreeSet::new(),
        }
    }

    /// Start streaming logcat from the device identified by `serial`.
    pub fn start_streaming(&mut self, serial: String) {
        self.stream.start(serial.clone());
        self.is_streaming = true;
        self.status_message = Some(format!("Streaming logcat from {}", serial));
    }

    /// Stop the current logcat stream.
    pub fn stop_streaming(&mut self) {
        self.stream.stop();
        self.is_streaming = false;
        self.status_message = Some("Logcat streaming stopped.".to_string());
    }

    /// Poll the stream for new log lines. Should be called on each UI tick.
    pub fn poll_new_entries(&mut self) {
        if !self.stream.is_running() {
            return;
        }

        let mut lines: Vec<String> = Vec::new();
        let (_drained, status) = self.stream.drain_into(&mut lines, MAX_DRAIN_PER_TICK);

        let mut new_count: usize = 0;
        for line in lines {
            self.total_received += 1;
            if self.paused {
                // Still consumed to avoid backpressure, but not stored.
                continue;
            }
            let entry = LogEntry::parse(&line);
            let idx = self.entries.len();
            self.stats.record(&entry.level);
            self.entries.push(entry);
            new_count += 1;
            if self.filter.matches(&self.entries[idx]) {
                self.filtered_indices.push(idx);
            }
        }

        if status == DrainStatus::Disconnected {
            self.is_streaming = false;
            self.status_message = Some("Logcat stream ended (thread disconnected).".to_string());
        }

        // Trim oldest entries if we exceed the cap.
        if self.entries.len() > MAX_ENTRIES {
            let excess = self.entries.len() - MAX_ENTRIES;
            self.entries.drain(0..excess);
            self.trimmed_total += excess;

            let mut write = 0;
            for read in 0..self.filtered_indices.len() {
                let idx = self.filtered_indices[read];
                if idx >= excess {
                    self.filtered_indices[write] = idx - excess;
                    write += 1;
                }
            }
            self.filtered_indices.truncate(write);

            let len = self.filtered_indices.len();
            if self.scroll_position >= len && len > 0 {
                self.scroll_position = len.saturating_sub(self.viewport_height);
            } else if len == 0 {
                self.scroll_position = 0;
            }
        }

        if new_count > 0 && self.auto_scroll {
            self.scroll_position = self.filtered_indices.len();
            self.selected_line = self.filtered_indices.len().saturating_sub(1);
        }

        self.stats.update_rate(self.total_received);
    }

    /// Recompute `filtered_indices` from scratch, respecting fold state.
    pub fn rebuild_filtered(&mut self) {
        self.filtered_indices.clear();
        let mut skip_continuations = false;
        for (i, entry) in self.entries.iter().enumerate() {
            if !entry.is_stack_continuation {
                skip_continuations = self.folded_groups.contains(&i);
            }
            if skip_continuations && entry.is_stack_continuation {
                continue;
            }
            if self.filter.matches(entry) {
                self.filtered_indices.push(i);
            }
        }

        let len = self.filtered_indices.len();
        if len == 0 {
            self.scroll_position = 0;
            self.selected_line = 0;
        } else {
            if self.scroll_position >= len {
                self.scroll_position = len.saturating_sub(self.viewport_height);
            }
            if self.selected_line >= len {
                self.selected_line = len.saturating_sub(1);
            }
        }
    }

    /// Return the currently visible window of `filtered_indices`.
    pub fn visible_entries(&mut self, height: usize) -> Vec<usize> {
        self.viewport_height = height;
        if self.filtered_indices.is_empty() || height == 0 {
            return vec![];
        }
        let total = self.filtered_indices.len();
        let start = if self.auto_scroll {
            let s = total.saturating_sub(height);
            self.scroll_position = s;
            s
        } else {
            self.scroll_position.min(total.saturating_sub(height))
        };
        let end = (start + height).min(total);
        self.filtered_indices[start..end].to_vec()
    }

    /// Scroll up by `n` lines. Disables auto-scroll.
    pub fn scroll_up(&mut self, n: usize) {
        if self.auto_scroll {
            let total = self.filtered_indices.len();
            self.scroll_position = total.saturating_sub(self.viewport_height);
        }
        self.auto_scroll = false;
        self.scroll_position = self.scroll_position.saturating_sub(n);
        self.selected_line = self.scroll_position;
    }

    /// Scroll down by `n` lines. Disables auto-scroll.
    pub fn scroll_down(&mut self, n: usize) {
        if self.auto_scroll {
            return;
        }
        let max_scroll = self
            .filtered_indices
            .len()
            .saturating_sub(self.viewport_height);
        self.scroll_position = (self.scroll_position + n).min(max_scroll);
        let max_line = self.filtered_indices.len().saturating_sub(1);
        self.selected_line =
            (self.scroll_position + self.viewport_height.saturating_sub(1)).min(max_line);
    }

    /// Jump to the bottom and re-enable auto-scroll.
    pub fn scroll_to_bottom(&mut self) {
        self.auto_scroll = true;
        let total = self.filtered_indices.len();
        self.scroll_position = total.saturating_sub(self.viewport_height);
        self.selected_line = self.filtered_indices.len().saturating_sub(1);
    }

    /// Jump to the top of the log. Disables auto-scroll.
    pub fn scroll_to_top(&mut self) {
        self.auto_scroll = false;
        self.scroll_position = 0;
        self.selected_line = 0;
    }

    /// Clear all entries and filtered indices.
    pub fn clear(&mut self) {
        self.entries.clear();
        self.filtered_indices.clear();
        self.scroll_position = 0;
        self.total_received = 0;
        self.trimmed_total = 0;
        self.auto_scroll = true;
        self.stats.reset();
        self.bookmarks.clear();
        self.folded_groups.clear();
        self.detail_open = false;
    }

    /// Directory used for "Save Here".
    pub fn default_save_dir() -> std::path::PathBuf {
        std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."))
    }

    /// Build a timestamped default filename for saving logs.
    pub fn default_save_filename(format: SaveFormat) -> String {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        format!("logcat_{}.{}", now, format.extension())
    }

    /// Toggle the paused state.
    pub fn toggle_pause(&mut self) {
        self.paused = !self.paused;
        self.status_message = Some(if self.paused {
            "Logcat paused.".to_string()
        } else {
            "Logcat resumed.".to_string()
        });
    }

    /// Number of entries matching the current filter.
    pub fn entry_count(&self) -> usize {
        self.filtered_indices.len()
    }

    /// Total number of entries stored (before filtering).
    pub fn total_count(&self) -> usize {
        self.entries.len()
    }

    /// Save all entries to a plain-text file.
    pub fn save_to_file(&self, path: &std::path::Path) -> std::io::Result<usize> {
        self.write_entries(path, self.entries.iter(), false)
    }

    /// Save only filtered entries to a plain-text file.
    pub fn save_filtered_to_file(&self, path: &std::path::Path) -> std::io::Result<usize> {
        let it = self
            .filtered_indices
            .iter()
            .filter_map(|&i| self.entries.get(i));
        self.write_entries(path, it, false)
    }

    /// Save all entries to a JSONL file.
    pub fn save_to_json_file(&self, path: &std::path::Path) -> std::io::Result<usize> {
        self.write_entries(path, self.entries.iter(), true)
    }

    /// Save filtered entries to a JSONL file.
    pub fn save_filtered_to_json_file(&self, path: &std::path::Path) -> std::io::Result<usize> {
        let it = self
            .filtered_indices
            .iter()
            .filter_map(|&i| self.entries.get(i));
        self.write_entries(path, it, true)
    }

    /// Shared writer for the four save variants.
    fn write_entries<'a>(
        &self,
        path: &std::path::Path,
        entries: impl Iterator<Item = &'a LogEntry>,
        json: bool,
    ) -> std::io::Result<usize> {
        use std::io::BufWriter;
        let file = std::fs::File::create(path)?;
        let mut writer = BufWriter::new(file);
        let mut count = 0;
        for entry in entries {
            if json {
                serde_json::to_writer(&mut writer, entry).map_err(std::io::Error::other)?;
                writeln!(writer)?;
            } else {
                writeln!(writer, "{}", entry.raw)?;
            }
            count += 1;
        }
        writer.flush()?;
        Ok(count)
    }

    /// Toggle word wrapping for long lines.
    pub fn toggle_word_wrap(&mut self) {
        self.word_wrap = !self.word_wrap;
    }

    /// Toggle the detail popup open/closed.
    pub fn toggle_detail(&mut self) {
        self.detail_open = !self.detail_open;
    }

    /// Reference to the currently selected log entry (if any).
    pub fn selected_entry(&self) -> Option<&LogEntry> {
        self.filtered_indices
            .get(self.selected_line)
            .and_then(|&idx| self.entries.get(idx))
    }

    /// Move the selection up by one line.
    pub fn select_up(&mut self) {
        if self.selected_line > 0 {
            self.selected_line -= 1;
        }
        if self.selected_line < self.scroll_position {
            self.scroll_position = self.selected_line;
        }
        self.auto_scroll = false;
    }

    /// Move the selection down by one line.
    pub fn select_down(&mut self) {
        let max = self.filtered_indices.len().saturating_sub(1);
        if self.selected_line < max {
            self.selected_line += 1;
        }
        if self.selected_line >= self.scroll_position + self.viewport_height {
            self.scroll_position = self.selected_line.saturating_sub(self.viewport_height - 1);
        }
    }

    /// Toggle fold state at the currently selected line.
    pub fn toggle_fold_at_selected(&mut self) {
        if let Some(&entry_idx) = self.filtered_indices.get(self.selected_line) {
            let head = if self.entries[entry_idx].is_stack_continuation {
                (0..entry_idx)
                    .rev()
                    .find(|&i| !self.entries[i].is_stack_continuation)
                    .unwrap_or(entry_idx)
            } else {
                entry_idx
            };
            if !self.folded_groups.remove(&head) {
                self.folded_groups.insert(head);
            }
            self.rebuild_filtered();
        }
    }

    /// Scroll left by `n` characters.
    pub fn h_scroll_left(&mut self, n: usize) {
        self.h_scroll = self.h_scroll.saturating_sub(n);
    }

    /// Scroll right by `n` characters.
    pub fn h_scroll_right(&mut self, n: usize) {
        self.h_scroll += n;
    }

    /// Reset horizontal scroll to the beginning.
    pub fn h_scroll_reset(&mut self) {
        self.h_scroll = 0;
    }

    /// Toggle compact display mode.
    pub fn toggle_compact(&mut self) {
        self.compact = !self.compact;
    }

    /// Copy the currently selected log line to the system clipboard.
    pub fn copy_selected_to_clipboard(&self) -> Result<(), String> {
        match self.selected_entry() {
            Some(entry) => copy_to_clipboard(&entry.raw),
            None => Err("No line selected".into()),
        }
    }

    /// Toggle a bookmark on the currently selected line.
    pub fn toggle_bookmark(&mut self) {
        if let Some(&entry_idx) = self.filtered_indices.get(self.selected_line) {
            if !self.bookmarks.remove(&entry_idx) {
                self.bookmarks.insert(entry_idx);
            }
        }
    }

    /// Whether the given entry index is bookmarked.
    pub fn is_bookmarked(&self, entry_idx: usize) -> bool {
        self.bookmarks.contains(&entry_idx)
    }

    /// Jump to the next bookmark (wrapping around).
    pub fn next_bookmark(&mut self) {
        self.jump_bookmark(true);
    }

    /// Jump to the previous bookmark (wrapping around).
    pub fn prev_bookmark(&mut self) {
        self.jump_bookmark(false);
    }

    /// Shared bookmark navigation for [`next_bookmark`](Self::next_bookmark)
    /// and [`prev_bookmark`](Self::prev_bookmark).
    fn jump_bookmark(&mut self, forward: bool) {
        if self.bookmarks.is_empty() {
            return;
        }
        let current = self
            .filtered_indices
            .get(self.selected_line)
            .copied()
            .unwrap_or(0);

        let target = if forward {
            self.bookmarks
                .range((current + 1)..)
                .next()
                .copied()
                .or_else(|| self.bookmarks.iter().next().copied())
        } else {
            self.bookmarks
                .range(..current)
                .next_back()
                .copied()
                .or_else(|| self.bookmarks.iter().next_back().copied())
        };

        if let Some(entry_idx) = target {
            if let Some(pos) = self.filtered_indices.iter().position(|&i| i == entry_idx) {
                self.selected_line = pos;
                self.auto_scroll = false;
                if self.selected_line < self.scroll_position
                    || self.selected_line >= self.scroll_position + self.viewport_height
                {
                    self.scroll_position =
                        self.selected_line.saturating_sub(self.viewport_height / 2);
                }
            }
        }
    }
}

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

impl fmt::Debug for LogcatState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LogcatState")
            .field("entries_len", &self.entries.len())
            .field("filtered_len", &self.filtered_indices.len())
            .field("filter", &self.filter)
            .field("scroll_position", &self.scroll_position)
            .field("auto_scroll", &self.auto_scroll)
            .field("paused", &self.paused)
            .field("is_streaming", &self.is_streaming)
            .field("total_received", &self.total_received)
            .field("trimmed_total", &self.trimmed_total)
            .field("status_message", &self.status_message)
            .field("word_wrap", &self.word_wrap)
            .field("viewport_height", &self.viewport_height)
            .field("detail_open", &self.detail_open)
            .field("selected_line", &self.selected_line)
            .field("folded_groups_len", &self.folded_groups.len())
            .field("stats", &self.stats)
            .field("h_scroll", &self.h_scroll)
            .field("compact", &self.compact)
            .field("bookmarks_len", &self.bookmarks.len())
            .finish()
    }
}

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

    #[test]
    fn new_state_is_empty() {
        let s = LogcatState::new();
        assert_eq!(s.entry_count(), 0);
        assert_eq!(s.total_count(), 0);
        assert!(s.auto_scroll);
    }

    #[test]
    fn filter_rebuild_selects_matching() {
        let mut s = LogcatState::new();
        s.entries
            .push(LogEntry::parse("01-15 12:34:56.789  1  2 I Tag: hello"));
        s.entries
            .push(LogEntry::parse("01-15 12:34:56.789  1  2 E Tag: boom"));
        s.filter.min_level = LogLevel::Error;
        s.rebuild_filtered();
        assert_eq!(s.filtered_indices, vec![1]);
    }

    #[test]
    fn bookmark_toggle() {
        let mut s = LogcatState::new();
        s.entries.push(LogEntry::parse("x"));
        s.filtered_indices.push(0);
        s.selected_line = 0;
        s.toggle_bookmark();
        assert!(s.is_bookmarked(0));
        s.toggle_bookmark();
        assert!(!s.is_bookmarked(0));
    }

    #[test]
    fn level_colors_distinct() {
        assert_ne!(level_color(LogLevel::Error), level_color(LogLevel::Info));
    }

    #[test]
    fn tag_color_is_stable() {
        assert_eq!(tag_color("ActivityManager"), tag_color("ActivityManager"));
    }
}