ltop 0.1.2

btop-styled TUI monitoring only local network, LAN neighbours and disk I/O
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
use ratatui::style::{Color, Style};
use ratatui::text::Line;
use ratatui::text::Span;

use crate::theme::Gradient;

/// Graph render styles
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum GraphMode {
    /// btop's braille area graph (5x5 lookup, two samples per char)
    Solid,
    /// bars growing out from the vertical center (localist-top extension)
    Centered,
    /// single-dot trace at the value's height (localist-top extension)
    Line,
    /// oscilloscope-style connected trace (localist-top extension)
    Heartbeat,
    /// line + soft fill below (localist-top extension)
    Hybrid,
}

impl GraphMode {
    pub fn next(self) -> Self {
        match self {
            Self::Solid => Self::Centered,
            Self::Centered => Self::Line,
            Self::Line => Self::Heartbeat,
            Self::Heartbeat => Self::Hybrid,
            Self::Hybrid => Self::Solid,
        }
    }

    pub fn label(self) -> &'static str {
        match self {
            Self::Solid => "bar",
            Self::Centered => "center",
            Self::Line => "line",
            Self::Heartbeat => "heart",
            Self::Hybrid => "hybrid",
        }
    }
}

/// btop's braille_up table: [left column height 0-4][right column height 0-4]
/// (verbatim from aristocratos/btop src/btop_draw.cpp Symbols::graph_symbols)
const BRAILLE_UP: [[char; 5]; 5] = [
    [' ', '⢀', '⢠', '⢰', '⢸'],
    ['⡀', '⣀', '⣠', '⣰', '⣸'],
    ['⡄', '⣄', '⣤', '⣴', '⣼'],
    ['⡆', '⣆', '⣦', '⣶', '⣾'],
    ['⡇', '⣇', '⣧', '⣷', '⣿'],
];

fn braille_bits(c: char) -> u32 {
    if c == ' ' {
        0
    } else {
        c as u32 - 0x2800
    }
}

/// Horizontal dot-matrix progress bar (one text line tall): each braille
/// char packs two dot columns, a filled column is a full 4-dot strip — same
/// texture as the graphs. `frac` 0..1.
pub fn braille_bar(frac: f64, width: usize, color: Color) -> Span<'static> {
    let frac = frac.clamp(0.0, 1.0);
    let cols = width * 2;
    let filled = (frac * cols as f64).round() as usize;
    const L: u32 = 0x01 | 0x02 | 0x04 | 0x40;
    const R: u32 = 0x08 | 0x10 | 0x20 | 0x80;
    let mut s = String::with_capacity(width);
    for i in 0..width {
        let mut bits = 0u32;
        if filled > 2 * i {
            bits |= L;
        }
        if filled > 2 * i + 1 {
            bits |= R;
        }
        s.push(char::from_u32(0x2800 + bits).unwrap_or(' '));
    }
    Span::styled(s, Style::new().fg(color))
}

/// Braille graph. Solid mode is a faithful port of btop's `Graph::_create`:
/// one braille char per sample encoding the previous and current value as
/// left/right column heights, values normalized to 0-100 and quantized into
/// per-row bands (round + 0.1 jitter for the btop texture), bottom row of a
/// non-zero sample kept at >= 1 dot. Rows are colored along the gradient from
/// top (end) to bottom (start).
///
/// Centered / Line / Heartbeat are localist-top extensions rendered on a dot grid.
pub fn braille_graph(
    data: &[f64],
    width: usize,
    height: usize,
    max_floor: f64,
    grad: Gradient,
    mode: GraphMode,
) -> Vec<Line<'static>> {
    let width = width.max(1);
    let height = height.max(1);
    let dots_h = height * 4;

    // one sample per column, btop-style sliding window: take the suffix of
    // the history at exactly `width` samples (never resample — bucketing
    // would shift every column each tick and make the graph wobble)
    let mut series: Vec<f64> = if data.len() > width {
        data[data.len() - width..].to_vec()
    } else {
        data.to_vec()
    };
    if series.len() < width {
        let mut padded = vec![0.0; width - series.len()];
        padded.extend_from_slice(&series);
        series = padded;
    }
    let max = series.iter().copied().fold(0.0f64, f64::max).max(max_floor);
    let pct: Vec<f64> = series.iter().map(|v| v.max(0.0) / max * 100.0).collect();
    let fills: Vec<usize> = series
        .iter()
        .map(|v| ((v.max(0.0) / max * dots_h as f64).round() as usize).min(dots_h))
        .collect();

    let rows: Vec<Vec<u32>> = match mode {
        GraphMode::Solid => {
            let mut rows = vec![vec![0u32; width]; height];
            for (l, row) in rows.iter_mut().enumerate() {
                let (cur_high, cur_low) = if height > 1 {
                    (
                        (100.0 * (height - l) as f64 / height as f64).round(),
                        (100.0 * (height - l - 1) as f64 / height as f64).round(),
                    )
                } else {
                    (100.0, 0.0)
                };
                let m: f64 = if height == 1 { 0.3 } else { 0.1 };
                for c in 0..width {
                    let left = if c > 0 { pct[c - 1] } else { 0.0 };
                    let mut res = [0usize; 2];
                    for (ai, &v) in [left, pct[c]].iter().enumerate() {
                        // btop no_zero: floor applies everywhere except the very
                        // first char's left column (the drawing baseline)
                        let clamp_min = if l == height - 1 && !(c == 0 && ai == 0) { 1 } else { 0 };
                        res[ai] = if v >= cur_high {
                            4
                        } else if v <= cur_low {
                            clamp_min
                        } else {
                            ((((v - cur_low) * 4.0 / (cur_high - cur_low)) + m).round() as usize)
                                .clamp(clamp_min, 4)
                        };
                    }
                    row[c] = braille_bits(BRAILLE_UP[res[0]][res[1]]);
                }
            }
            rows
        }
        _ => {
            // line-based modes on a 2x sub-column dot grid: sample i lives in
            // the right half of char i, segments interpolate between samples
            let dw = width * 2;
            let mut grid = vec![vec![false; dots_h]; dw];
            let mid = dots_h / 2;
            let trace_y = |f: usize| -> usize {
                ((f as f32 / dots_h as f32) * (dots_h.saturating_sub(1)) as f32).round() as usize
            };
            match mode {
                GraphMode::Centered => {
                    // two-dot zero axis at the middle, always visible
                    let bl0 = mid.saturating_sub(1);
                    let bl1 = mid.min(dots_h - 1);
                    for (c, &f) in fills.iter().enumerate() {
                        let start = bl0.saturating_sub(f / 2);
                        let end = (bl1 + 1 + f.div_ceil(2)).min(dots_h);
                        let (lo, hi) = grid.split_at_mut(2 * c + 1);
                        let (col0, col1) = (&mut lo[2 * c], &mut hi[0]);
                        for (a, b) in col0.iter_mut().zip(col1.iter_mut()).take(end).skip(start)
                        {
                            *a = true;
                            *b = true;
                        }
                    }
                }
                GraphMode::Hybrid => {
                    // line stroke + soft area fill below it
                    let mut line = vec![vec![false; dots_h]; dw];
                    let mut prev: Option<(usize, usize)> = None;
                    let mut last = 0usize;
                    for (i, &f) in fills.iter().enumerate() {
                        let x = 2 * i + 1;
                        let y = trace_y(f).min(dots_h - 1);
                        if let Some((px, py)) = prev {
                            for (xx, col) in
                            line.iter_mut().enumerate().take(x + 1).skip(px + 1)
                        {
                                let t = (xx - px) as f32 / (x - px) as f32;
                                let yy = (py as f32
                                    + (y as f32 - py as f32) * t)
                                    .round() as usize;
                                for y2 in last.min(yy)..=last.max(yy) {
                                    col[y2.min(dots_h - 1)] = true;
                                }
                                last = yy;
                            }
                        }
                        line[x][y] = true;
                        if y + 1 < dots_h {
                            line[x][y + 1] = true;
                        }
                        prev = Some((x, y));
                    }
                    for dc in 0..dw {
                        // same-color area fill below the line stroke
                        if let Some(top) = (0..dots_h).rev().find(|&y| line[dc][y]) {
                            for cell in grid[dc].iter_mut().take(top + 1) {
                                *cell = true;
                            }
                        }
                    }
                }
                GraphMode::Line | GraphMode::Heartbeat => {
                    // connected strokes, 2 dots thick, no holes on steep slopes
                    fn dot(grid: &mut [Vec<bool>], x: usize, y: usize, dots_h: usize) {
                        let x = x.min(grid.len() - 1);
                        grid[x][y.min(dots_h - 1)] = true;
                        if y + 1 < dots_h {
                            grid[x][y + 1] = true;
                        }
                    }
                    let mut prev: Option<(usize, usize)> = None;
                    for (i, &f) in fills.iter().enumerate() {
                        let x = 2 * i + 1;
                        let y = trace_y(f).min(dots_h - 1);
                        if let Some((px, py)) = prev {
                            let steps = (x - px).max(1);
                            let mut last = py;
                            for s in 1..=steps {
                                let xx = px + s;
                                let t = s as f32 / steps as f32;
                                let yy = (py as f32
                                    + (y as f32 - py as f32) * t)
                                    .round() as usize;
                                match mode {
                                    GraphMode::Line => {
                                        for y2 in last.min(yy)..=last.max(yy) {
                                            dot(&mut grid, xx, y2, dots_h);
                                        }
                                    }
                                    GraphMode::Heartbeat => {
                                        if s < steps {
                                            dot(&mut grid, xx, py, dots_h);
                                        } else {
                                            for y2 in y.min(py)..=y.max(py) {
                                                dot(&mut grid, xx, y2, dots_h);
                                            }
                                        }
                                    }
                                    _ => {}
                                }
                                last = yy;
                            }
                        }
                        dot(&mut grid, x, y, dots_h);
                        prev = Some((x, y));
                    }
                }
                GraphMode::Solid => unreachable!(),
            }
            // braille dot bits: char c = dot columns 2c (x0) + 2c+1 (x1)
            const X0: [u32; 4] = [0x01, 0x02, 0x04, 0x40];
            const X1: [u32; 4] = [0x08, 0x10, 0x20, 0x80];
            let mut rows = vec![vec![0u32; width]; height];
            for (l, row) in rows.iter_mut().enumerate() {
                for (c, cell) in row.iter_mut().enumerate() {
                    let mut bits = 0u32;
                    for j in 0..4 {
                        let y_from_bottom = dots_h - 1 - (l * 4 + j);
                        if grid[2 * c][y_from_bottom] {
                            bits |= X0[j];
                        }
                        if grid[2 * c + 1][y_from_bottom] {
                            bits |= X1[j];
                        }
                    }
                    *cell = bits;
                }
            }
            rows
        }
    };

    let mut out = Vec::with_capacity(height);
    for (l, row) in rows.iter().enumerate() {
        let t = if height <= 1 {
            0.5
        } else {
            l as f32 / (height - 1) as f32
        };
        let color = grad.at(t);
        let s: String = row
            .iter()
            .map(|&b| char::from_u32(0x2800 + b).unwrap_or(' '))
            .collect();
        out.push(Line::from(Span::styled(s, Style::new().fg(color))));
    }
    out
}

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

    /// flatten a rendered row across its color spans
    fn flat(l: &Line) -> String {
        l.spans.iter().map(|s| s.content.clone()).collect()
    }

    #[test]
    fn solid_full_block_at_max() {
        let data = vec![100.0; 10];
        let lines = braille_graph(&data, 10, 1, 1.0, Gradient([Color::Green; 3]), GraphMode::Solid);
        assert_eq!(lines.len(), 1);
        let text = flat(&lines[0]);
        // first char: left column is the baseline (0) => table[0][4] = ⢸;
        // from the second char on both columns are maxed => table[4][4] = ⣿
        assert_eq!(text.chars().next(), Some('\u{28b8}'));
        assert_eq!(text.chars().nth(1), Some('\u{28ff}'));
        assert_eq!(text.chars().count(), 10);
    }

    #[test]
    fn solid_keeps_nonzero_off_the_floor() {
        // tiny values above the graph floor: bottom row keeps >= 1 dot (no_zero)
        let data = vec![10.0; 6];
        let lines = braille_graph(
            &data,
            6,
            2,
            64.0 * 1024.0,
            Gradient([Color::Green; 3]),
            GraphMode::Solid,
        );
        let bottom = flat(&lines[1]);
        let chars: Vec<char> = bottom.chars().collect();
        assert_eq!(chars[0], '⢀'); // baseline left 0, right quantizes to 1 dot
        assert_eq!(chars[2], '⣀'); // both columns at the 1-dot floor
    }

    #[test]
    fn solid_stable_when_history_exceeds_width() {
        // once the history is longer than the graph, rendering must be a pure
        // suffix window: identical to rendering just the last `width` samples,
        // so the shape never wobbles as the window slides
        let width = 20;
        let data: Vec<f64> = (0..width + 37).map(|i| if i % 17 == 0 { 900_000.0 } else { 1200.0 }).collect();
        let grad = Gradient([Color::Green; 3]);
        let full = braille_graph(&data, width, 3, 64.0 * 1024.0, grad, GraphMode::Solid);
        let suffix = braille_graph(&data[data.len() - width..], width, 3, 64.0 * 1024.0, grad, GraphMode::Solid);
        for (a, b) in full.iter().zip(&suffix) {
            assert_eq!(a.spans[0].content, b.spans[0].content);
        }
    }

    #[test]
    fn hybrid_fills_under_the_line() {
        let grad = Gradient([Color::Green; 3]);
        let lines = braille_graph(&[0.0, 100.0], 2, 2, 1.0, grad, GraphMode::Hybrid);
        // area under the interpolated line: col3 top half carries the line
        // segment (⣸), the bottom row is fully filled (⣿)
        assert_eq!(flat(&lines[0]).chars().nth(1), Some('\u{28f8}'));
        assert_eq!(flat(&lines[1]).chars().nth(1), Some('\u{28ff}'));
        // zero-data hybrid: a continuous floor line (interpolation keeps it
        // connected through every sub-column)
        let lines = braille_graph(&[0.0; 4], 4, 2, 1.0, grad, GraphMode::Hybrid);
        let chars: Vec<char> = flat(&lines[1]).chars().collect();
        assert_eq!(chars[0], '\u{28a0}');
        assert_eq!(chars[1], '\u{28e0}');
        assert!(chars.iter().all(|&c| c != ' '));
    }

    #[test]
    fn braille_bar_partial_fill() {
        let green = Color::Green;
        // 25% of 2 chars = exactly one dot column => left col of char 0 only
        let bar = braille_bar(0.25, 2, green);
        assert_eq!(bar.content, "\u{2847}\u{2800}");
        assert_eq!(braille_bar(1.0, 2, green).content, "\u{28ff}\u{28ff}");
        assert_eq!(braille_bar(0.0, 2, green).content, "\u{2800}\u{2800}");
    }

    #[test]
    fn left_pads_short_data() {
        let lines = braille_graph(
            &[50.0],
            4,
            2,
            1.0,
            Gradient([Color::Green; 3]),
            GraphMode::Solid,
        );
        assert_eq!(lines.len(), 2);
        assert_eq!(flat(&lines[0]).chars().count(), 4);
    }

    #[test]
    fn centered_fill_spans_the_middle() {
        let data = vec![100.0; 4];
        let lines = braille_graph(
            &data,
            4,
            1,
            1.0,
            Gradient([Color::Green; 3]),
            GraphMode::Centered,
        );
        // dots_h=4, mid=2, f=4 => rows 0..4 filled => full block
        assert_eq!(flat(&lines[0]).chars().next(), Some('\u{28ff}'));
    }

    #[test]
    fn line_mode_draws_connected_polyline() {
        let data = vec![0.0, 100.0];
        let lines = braille_graph(&data, 2, 2, 1.0, Gradient([Color::Green; 3]), GraphMode::Line);
        // 2-dot stroke: col1 {0,1}; segment to col3 tops out via col2 {4,5},
        // col3 {4..7} (vertical connect) => continuous, no holes
        let row_bottom = flat(&lines[1]);
        let row_top = flat(&lines[0]);
        assert_eq!(row_bottom.chars().next(), Some('\u{28a0}'));
        assert_eq!(row_top.chars().nth(1), Some('\u{28fc}'));
    }

    #[test]
    fn heartbeat_sweeps_flat_then_jumps() {
        let data = vec![0.0, 100.0];
        let lines = braille_graph(
            &data,
            2,
            2,
            1.0,
            Gradient([Color::Green; 3]),
            GraphMode::Heartbeat,
        );
        // 2-dot flat run at y=0..1 through col2; jump at col3 fills all rows
        let row_top = flat(&lines[0]);
        let row_bottom = flat(&lines[1]);
        assert_eq!(row_top.chars().nth(1), Some('\u{28b8}')); // col3: full right col
        assert_eq!(row_bottom.chars().nth(1), Some('\u{28fc}')); // flat + jump bottom
    }
}