basalt-tui 0.12.6

Basalt TUI application for Obsidian notes.
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
//! In-TUI debug log overlay.
//!
//! A [`tracing`] [`Layer`] captures every event into a bounded, process-global ring
//! buffer. The [`DebugLogModal`] overlay renders a snapshot of that buffer on top of the
//! application: a live console that can be toggled at any time without interfering with
//! normal usage.

use std::{
    collections::VecDeque,
    fmt::{self, Write},
    sync::{Mutex, OnceLock},
    time::{Duration, Instant},
};

use ratatui::{
    buffer::Buffer,
    layout::{Alignment, Constraint, Flex, Layout, Rect, Size},
    style::{Color, Style, Stylize},
    text::{Line, Span},
    widgets::{
        Block, BorderType, Clear, List, ListItem, ListState, Padding, Scrollbar,
        ScrollbarOrientation, ScrollbarState, StatefulWidget, Widget,
    },
};
use tracing::{field::Field, field::Visit, level_filters::LevelFilter, Event, Subscriber};
use tracing_subscriber::{layer::Context, Layer};

use crate::app::{calc_scroll_amount, Message as AppMessage, ScrollAmount};

/// Maximum number of retained log entries. Oldest entries are evicted past this.
const CAPACITY: usize = 2000;

/// A single captured log record, cheap to clone for rendering snapshots.
#[derive(Clone, Debug, PartialEq)]
pub struct LogEntry {
    pub level: LogLevel,
    pub target: String,
    pub message: String,
    pub elapsed: Duration,
}

/// Severity of a log record. Ordered from least to most severe so that a minimum-level
/// filter is a simple `>=` comparison.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, clap::ValueEnum)]
pub enum LogLevel {
    #[default]
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

impl LogLevel {
    /// Fixed-width label so columns stay aligned across rows.
    pub fn label(self) -> &'static str {
        match self {
            LogLevel::Trace => "TRACE",
            LogLevel::Debug => "DEBUG",
            LogLevel::Info => "INFO ",
            LogLevel::Warn => "WARN ",
            LogLevel::Error => "ERROR",
        }
    }

    pub fn color(self) -> Color {
        match self {
            LogLevel::Trace => Color::DarkGray,
            LogLevel::Debug => Color::Blue,
            LogLevel::Info => Color::Green,
            LogLevel::Warn => Color::Yellow,
            LogLevel::Error => Color::Red,
        }
    }

    /// Next level in a wrapping cycle, used by the overlay's level filter.
    fn next(self) -> Self {
        match self {
            LogLevel::Trace => LogLevel::Debug,
            LogLevel::Debug => LogLevel::Info,
            LogLevel::Info => LogLevel::Warn,
            LogLevel::Warn => LogLevel::Error,
            LogLevel::Error => LogLevel::Trace,
        }
    }
}

impl From<tracing::Level> for LogLevel {
    fn from(level: tracing::Level) -> Self {
        match level {
            tracing::Level::TRACE => LogLevel::Trace,
            tracing::Level::DEBUG => LogLevel::Debug,
            tracing::Level::INFO => LogLevel::Info,
            tracing::Level::WARN => LogLevel::Warn,
            tracing::Level::ERROR => LogLevel::Error,
        }
    }
}

fn buffer() -> &'static Mutex<VecDeque<LogEntry>> {
    static LOG_BUFFER: OnceLock<Mutex<VecDeque<LogEntry>>> = OnceLock::new();
    LOG_BUFFER.get_or_init(|| Mutex::new(VecDeque::with_capacity(CAPACITY)))
}

/// Process start, used to render a monotonic relative timestamp per entry.
fn start() -> Instant {
    static START: OnceLock<Instant> = OnceLock::new();
    *START.get_or_init(Instant::now)
}

/// Pushes an entry into a bounded buffer, evicting the oldest once at capacity.
fn push_bounded(buffer: &mut VecDeque<LogEntry>, entry: LogEntry) {
    if buffer.len() == CAPACITY {
        buffer.pop_front();
    }
    buffer.push_back(entry);
}

/// Snapshots the entries matching `min_level`, newest last.
fn snapshot(min_level: LogLevel) -> Vec<LogEntry> {
    buffer()
        .lock()
        .map(|buffer| {
            buffer
                .iter()
                .filter(|entry| entry.level >= min_level)
                .cloned()
                .collect()
        })
        .unwrap_or_default()
}

/// Empties the ring buffer.
pub fn clear() {
    if let Ok(mut buffer) = buffer().lock() {
        buffer.clear();
    }
}

/// Registers the capturing [`Layer`] as the global tracing subscriber. Call once at startup.
pub fn init() {
    use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

    start();
    let _ = tracing_subscriber::registry()
        .with(DebugLogLayer)
        .try_init();
}

/// A [`tracing`] layer that records every event into the ring buffer.
struct DebugLogLayer;

impl<S: Subscriber> Layer<S> for DebugLogLayer {
    // Capture everything; the overlay does its own level filtering.
    fn max_level_hint(&self) -> Option<LevelFilter> {
        Some(LevelFilter::TRACE)
    }

    fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) {
        let mut visitor = MessageVisitor::default();
        event.record(&mut visitor);

        let metadata = event.metadata();
        let entry = LogEntry {
            level: (*metadata.level()).into(),
            target: metadata.target().to_string(),
            message: format!("{}{}", visitor.message, visitor.fields),
            elapsed: start().elapsed(),
        };

        if let Ok(mut buffer) = buffer().lock() {
            push_bounded(&mut buffer, entry);
        }
    }
}

/// Collects an event's `message` field and appends any structured fields as `key=value`.
#[derive(Default)]
struct MessageVisitor {
    message: String,
    fields: String,
}

impl Visit for MessageVisitor {
    fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
        if field.name() == "message" {
            self.message = format!("{value:?}");
        } else {
            let _ = write!(self.fields, " {}={value:?}", field.name());
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum Message {
    Toggle,
    Close,
    Clear,
    CycleLevel,
    ScrollUp(ScrollAmount),
    ScrollDown(ScrollAmount),
}

#[derive(Debug, Clone, Default, PartialEq)]
pub struct DebugLogModalState {
    pub visible: bool,
    /// Cursor over the rendered rows. `None` follows the newest line.
    pub list_state: ListState,
    pub min_level: LogLevel,
    pub scrollbar_state: ScrollbarState,
}

impl DebugLogModalState {
    fn cursor(&self) -> usize {
        self.list_state.selected().unwrap_or(0)
    }

    fn cycle_level(&mut self) {
        self.min_level = self.min_level.next();
        self.list_state.select(None);
    }
}

pub fn update<'a>(
    message: &Message,
    screen_size: Size,
    state: &mut DebugLogModalState,
) -> Option<AppMessage<'a>> {
    // The cursor is clamped to the real row count during render, which is the only
    // place the wrapped layout (and therefore the row count) is known.
    let page = list_height(overlay_area(Rect::new(
        0,
        0,
        screen_size.width,
        screen_size.height,
    )));

    match message {
        Message::Toggle => state.visible = !state.visible,
        Message::Close => state.visible = false,
        Message::Clear => {
            clear();
            state.list_state.select(None);
        }
        Message::CycleLevel => state.cycle_level(),
        Message::ScrollUp(amount) => {
            let target = state
                .cursor()
                .saturating_sub(calc_scroll_amount(amount, page));
            state.list_state.select(Some(target));
        }
        Message::ScrollDown(amount) => {
            let target = state.cursor() + calc_scroll_amount(amount, page);
            state.list_state.select(Some(target));
        }
    };

    None
}

/// Bottom-docked overlay area: lower half of the screen, full width minus the app margin.
fn overlay_area(area: Rect) -> Rect {
    let [area] = Layout::vertical([Constraint::Percentage(50)])
        .flex(Flex::End)
        .areas(area);
    let [area] = Layout::horizontal([Constraint::Fill(1)])
        .horizontal_margin(1)
        .areas(area);
    area
}

/// Number of visible log rows: the overlay minus its two borders.
fn list_height(area: Rect) -> usize {
    area.height.saturating_sub(2) as usize
}

/// Renders one entry as wrapped rows. The first row carries the timestamp, level
/// and target; wrapped continuation rows sit flush left.
fn entry_rows(entry: &LogEntry, text_width: usize) -> Vec<Line<'static>> {
    let timestamp = format!("{:<8} ", format!("{:.3}s", entry.elapsed.as_secs_f64()));
    let level = format!("{} ", entry.level.label());
    let target = format!("{} ", entry.target);
    let prefix_width = timestamp.chars().count() + level.chars().count() + target.chars().count();

    // Reserve room for the prefix on the first row only; continuations are flush left.
    let reserved = " ".repeat(prefix_width.min(text_width.saturating_sub(1)));
    let options = textwrap::Options::new(text_width.max(1)).initial_indent(&reserved);

    textwrap::wrap(&entry.message, options)
        .iter()
        .enumerate()
        .map(|(row, part)| {
            if row == 0 {
                let message = part.strip_prefix(reserved.as_str()).unwrap_or(part);
                Line::from(vec![
                    Span::from(timestamp.clone()).dark_gray(),
                    Span::from(level.clone()).fg(entry.level.color()),
                    Span::from(target.clone()).dark_gray(),
                    Span::from(message.to_string()),
                ])
            } else {
                Line::from(part.to_string())
            }
        })
        .collect()
}

pub struct DebugLogModal {
    pub border_type: BorderType,
    /// Resident memory in MiB, supplied by the caller so the widget stays pure.
    pub memory_mb: Option<f64>,
}

impl DebugLogModal {
    pub fn new(border_type: BorderType, memory_mb: Option<f64>) -> Self {
        Self {
            border_type,
            memory_mb,
        }
    }
}

impl StatefulWidget for DebugLogModal {
    type State = DebugLogModalState;

    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        let area = overlay_area(area);
        let page = list_height(area);
        // Borders (2), horizontal padding (2) and the cursor gutter (2) leave this
        // many columns for text.
        let text_width = area.width.saturating_sub(6) as usize;

        let entries = snapshot(state.min_level);
        let rows: Vec<Line> = if entries.is_empty() {
            vec![Line::from(Span::from("No log entries").dark_gray())]
        } else {
            entries
                .iter()
                .flat_map(|entry| entry_rows(entry, text_width))
                .collect()
        };
        let total = rows.len();

        // Default the cursor to the newest row, and keep it within bounds.
        let cursor = state
            .list_state
            .selected()
            .unwrap_or(usize::MAX)
            .min(total.saturating_sub(1));
        state.list_state.select(Some(cursor));

        let memory = self
            .memory_mb
            .map(|mb| format!("{mb:.1} MB"))
            .unwrap_or_else(|| "? MB".to_string());
        let title = format!(
            " Debug Log ({}) - {memory} ",
            state.min_level.label().trim()
        );
        let block = Block::bordered()
            .dark_gray()
            .border_type(self.border_type)
            .padding(Padding::horizontal(1))
            .title_style(Style::default().italic().bold())
            .title(title)
            .title(Line::from(" (g<) ").alignment(Alignment::Right));

        Widget::render(Clear, area, buf);
        StatefulWidget::render(
            List::new(rows.into_iter().map(ListItem::new).collect::<Vec<_>>())
                .block(block)
                .fg(Color::default())
                .highlight_symbol(""),
            area,
            buf,
            &mut state.list_state,
        );

        if total > page {
            state.scrollbar_state = ScrollbarState::new(total).position(cursor);
            StatefulWidget::render(
                Scrollbar::new(ScrollbarOrientation::VerticalRight),
                area,
                buf,
                &mut state.scrollbar_state,
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use insta::assert_snapshot;
    use ratatui::{backend::TestBackend, Terminal};

    fn entry(level: LogLevel, message: &str) -> LogEntry {
        LogEntry {
            level,
            target: "basalt_tui::app".to_string(),
            message: message.to_string(),
            elapsed: Duration::from_millis(1234),
        }
    }

    #[test]
    fn level_from_tracing() {
        assert_eq!(LogLevel::from(tracing::Level::TRACE), LogLevel::Trace);
        assert_eq!(LogLevel::from(tracing::Level::ERROR), LogLevel::Error);
    }

    #[test]
    fn levels_are_ordered_by_severity() {
        assert!(LogLevel::Trace < LogLevel::Error);
        assert!(LogLevel::Info < LogLevel::Warn);
    }

    #[test]
    fn push_bounded_evicts_oldest() {
        let mut buffer = VecDeque::new();
        for index in 0..CAPACITY + 5 {
            push_bounded(
                &mut buffer,
                entry(LogLevel::Info, &format!("entry {index}")),
            );
        }
        assert_eq!(buffer.len(), CAPACITY);
        assert_eq!(buffer.front().unwrap().message, "entry 5");
        assert_eq!(
            buffer.back().unwrap().message,
            format!("entry {}", CAPACITY + 4)
        );
    }

    #[test]
    fn cycle_level_advances_and_resets_cursor() {
        let mut state = DebugLogModalState::default();
        state.list_state.select(Some(7));
        state.cycle_level();
        assert_eq!(state.min_level, LogLevel::Debug);
        assert_eq!(state.list_state.selected(), None);
    }

    // Touches the process-global buffer, so it is the only global-state test.
    #[test]
    fn render_overlay() {
        clear();
        let entries = [
            entry(LogLevel::Trace, "entering run loop"),
            entry(LogLevel::Debug, "refreshed 142 entries"),
            entry(LogLevel::Info, "vault opened: Notes"),
            entry(LogLevel::Warn, "wiki link update failed"),
            entry(LogLevel::Error, "failed to create note"),
        ];
        if let Ok(mut buffer) = buffer().lock() {
            entries
                .into_iter()
                .for_each(|e| push_bounded(&mut buffer, e));
        }

        let mut terminal = Terminal::new(TestBackend::new(60, 12)).unwrap();
        terminal
            .draw(|frame| {
                // Fixed memory keeps the snapshot stable; live memory comes from the app.
                DebugLogModal::new(BorderType::Rounded, Some(24.1)).render(
                    frame.area(),
                    frame.buffer_mut(),
                    &mut DebugLogModalState {
                        visible: true,
                        ..Default::default()
                    },
                );
            })
            .unwrap();

        assert_snapshot!(terminal.backend());
        clear();
    }
}