bottom 0.14.4

A customizable cross-platform graphical process/system monitor for the terminal. Supports Linux, macOS, and Windows.
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
use std::cmp::{max, min};

use ratatui::{
    Frame,
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::Style,
    text::{Line, Span},
    widgets::{Padding, Paragraph, Wrap},
};
use unicode_width::UnicodeWidthStr;

use crate::{
    app::App,
    canvas::{
        Painter,
        components::{
            scroll_bar::{ScrollBarArgs, dialog_scroll_bar_area, draw_scroll_bar},
            search_input,
        },
        drawing_utils::dialog_block,
    },
    constants::{self, HELP_TEXT},
};

/// Append a highlighted match to `lines`, and return whether a match was found.
fn add_highlight_match<'a>(
    query: &str, target: &'a str, lines: &mut Vec<Line<'a>>, normal_style: Style,
    match_style: Style,
) -> bool {
    let lower = target.to_lowercase();

    if let Some(pos) = lower.find(query) {
        let match_end = pos + query.len();
        lines.push(Line::from(vec![
            Span::styled(&target[..pos], normal_style),
            Span::styled(&target[pos..match_end], match_style),
            Span::styled(&target[match_end..], normal_style),
        ]));
        true
    } else {
        false
    }
}

// TODO: [REFACTOR] Make generic dialog boxes to build off of instead?
impl Painter {
    fn help_text_lines(&self, app_state: &App) -> Vec<Line<'_>> {
        let mut lines: Vec<Line<'_>> = Vec::new();

        let query = app_state
            .help_dialog_state
            .search_input_state
            .current_query()
            .trim()
            .to_lowercase();

        let is_filtering = !query.is_empty();
        if is_filtering {
            let selected_header_style = self
                .styles
                .selected_text_style
                .add_modifier(self.styles.table_header_style.add_modifier);

            HELP_TEXT.iter().enumerate().for_each(|(itx, section)| {
                let mut iter = section.iter();

                if itx == 0 {
                    return;
                }

                let header_str = iter.next().copied();

                let mut header_line: Vec<Line<'_>> = Vec::new();
                let header_matches = if let Some(h) = header_str {
                    add_highlight_match(
                        &query,
                        h,
                        &mut header_line,
                        self.styles.table_header_style,
                        selected_header_style,
                    )
                } else {
                    false
                };

                let mut matched_body: Vec<Line<'_>> = Vec::new();
                for &text in iter {
                    add_highlight_match(
                        &query,
                        text,
                        &mut matched_body,
                        self.styles.text_style,
                        self.styles.selected_text_style,
                    );
                }

                if !matched_body.is_empty() || header_matches {
                    if let Some(header) = header_str {
                        // Don't insert the space if there's nothing above anyway.
                        if !lines.is_empty() {
                            lines.push(Line::from(Span::default()));
                        }

                        if header_matches {
                            lines.extend(header_line);
                        } else {
                            lines.push(Line::from(Span::styled(
                                header,
                                self.styles.table_header_style,
                            )));
                        }
                    }

                    lines.extend(matched_body);
                }
            });
        } else {
            HELP_TEXT.iter().enumerate().for_each(|(itx, section)| {
                let mut iter = section.iter();

                if itx == 0 {
                    for &text in iter {
                        lines.push(Line::from(Span::styled(text, self.styles.text_style)));
                    }
                    return;
                }

                let header_opt = iter.next();
                let header_str = header_opt.copied();

                if let Some(header) = header_str {
                    lines.push(Line::from(Span::default()));
                    lines.push(Line::from(Span::styled(
                        header,
                        self.styles.table_header_style,
                    )));
                }
                for &text in iter {
                    lines.push(Line::from(Span::styled(text, self.styles.text_style)));
                }
            });
        }

        lines
    }

    pub fn draw_help_dialog(&self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect) {
        // TODO: We can reduce lines processed based on the known height.
        let styled_help_text = self.help_text_lines(app_state);

        // Reserve one column on the right for the scroll bar.
        let block = dialog_block(self.styles.border_type, self.styles.border_style)
            .title_top(Line::styled(" Help ", self.styles.widget_title_style))
            .title_top(
                Line::styled(" Esc to close ", self.styles.widget_title_style).right_aligned(),
            )
            .padding(Padding::right(1));

        let [content_area, input_area] = if app_state.help_dialog_state.is_searching() {
            Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Min(1), Constraint::Length(1)])
                .areas::<2>(draw_loc)
        } else {
            [draw_loc, Rect::default()]
        };

        if app_state.should_get_widget_bounds() {
            // We must also recalculate how many lines are wrapping to properly get
            // scrolling to work on small terminal sizes... oh joy.

            let inner = block.inner(content_area);
            app_state.help_dialog_state.height = inner.height;

            // The overflow buffer is used to account for lines that wrap onto multiple lines,
            // so we can properly calculate the max scroll index later.
            let mut overflow_buffer = 0;
            let paragraph_width: usize = max(inner.width, 1).into();
            let mut prev_section_len = 0;

            if app_state
                .help_dialog_state
                .search_input_state
                .current_query()
                .is_empty()
            {
                constants::HELP_TEXT
                    .iter()
                    .enumerate()
                    .for_each(|(itx, section)| {
                        let mut buffer = 0;

                        section.iter().for_each(|text_line| {
                            buffer += UnicodeWidthStr::width(*text_line).saturating_sub(1) as u16
                                / paragraph_width as u16;
                        });

                        if itx == 0 {
                            app_state.help_dialog_state.index_shortcuts[itx] = 0;
                        } else {
                            app_state.help_dialog_state.index_shortcuts[itx] =
                                app_state.help_dialog_state.index_shortcuts[itx - 1]
                                    + 1
                                    + prev_section_len;
                        }
                        prev_section_len = section.len() as u16 + buffer;
                        overflow_buffer += buffer;
                    });
            } else {
                for line in &styled_help_text {
                    let width: usize = line
                        .spans
                        .iter()
                        .map(|s| UnicodeWidthStr::width(s.content.as_ref()))
                        .sum();

                    if width > paragraph_width {
                        overflow_buffer += (width.saturating_sub(1) / paragraph_width) as u16;
                    }
                }
            }

            let max_scroll_index = &mut app_state.help_dialog_state.scroll_state.max_scroll_index;
            *max_scroll_index = (styled_help_text.len() as u16 + 3 + overflow_buffer)
                .saturating_sub(draw_loc.height + 1);

            // Fix the scroll index if it is over-scrolled
            let index = &mut app_state
                .help_dialog_state
                .scroll_state
                .current_scroll_index;

            *index = min(*index, *max_scroll_index);
        }

        f.render_widget(
            Paragraph::new(styled_help_text)
                .block(block)
                .style(self.styles.text_style)
                .alignment(Alignment::Left)
                .wrap(Wrap { trim: true })
                .scroll((
                    app_state
                        .help_dialog_state
                        .scroll_state
                        .current_scroll_index,
                    0,
                )),
            content_area,
        );

        if app_state.help_dialog_state.is_searching() {
            search_input::render_search_input(
                f,
                input_area,
                search_input::SearchInputState {
                    input_field_state: &app_state.help_dialog_state.search_input_state,
                    is_focused: true,
                    prefix: "Search: ",
                    hint: Some("Type to search, Esc to close"),
                },
                search_input::SearchInputStyles {
                    prefix_style: self.styles.widget_title_style,
                    text_style: self.styles.text_style,
                    cursor_style: self.styles.selected_text_style,
                    hint_style: self.styles.text_style.dim(),
                },
            );
        }

        let scrollbar_area = dialog_scroll_bar_area(draw_loc);
        let content_length = app_state
            .help_dialog_state
            .scroll_state
            .max_scroll_index
            .into();
        let viewport_length = app_state.help_dialog_state.height.into();
        let position = app_state
            .help_dialog_state
            .scroll_state
            .current_scroll_index
            .into();

        draw_scroll_bar(
            f,
            scrollbar_area,
            ScrollBarArgs {
                content_length,
                viewport_length,
                position,
                style: self.styles.text_style,
            },
        );
    }
}

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

    fn check_spans(line: &Line<'_>, expected: [&str; 3]) {
        assert_eq!(line.spans[0].content, expected[0]);
        assert_eq!(line.spans[1].content, expected[1]);
        assert_eq!(line.spans[2].content, expected[2]);
    }

    #[test]
    fn test_no_match() {
        let mut lines = Vec::new();
        let matched = add_highlight_match(
            "xyz",
            "hello world",
            &mut lines,
            Style::default(),
            Style::default(),
        );
        assert!(!matched);
        assert!(lines.is_empty());
    }

    #[test]
    fn test_match_in_middle() {
        let mut lines = Vec::new();
        assert!(add_highlight_match(
            "world",
            "hello world!",
            &mut lines,
            Style::default(),
            Style::default()
        ));
        check_spans(&lines[0], ["hello ", "world", "!"]);
    }

    #[test]
    fn test_match_at_start() {
        let mut lines = Vec::new();
        assert!(add_highlight_match(
            "hello",
            "hello world",
            &mut lines,
            Style::default(),
            Style::default()
        ));
        check_spans(&lines[0], ["", "hello", " world"]);
    }

    #[test]
    fn test_match_at_end() {
        let mut lines = Vec::new();
        assert!(add_highlight_match(
            "world",
            "hello world",
            &mut lines,
            Style::default(),
            Style::default()
        ));
        check_spans(&lines[0], ["hello ", "world", ""]);
    }

    #[test]
    fn test_full_match() {
        let mut lines = Vec::new();
        assert!(add_highlight_match(
            "hello",
            "hello",
            &mut lines,
            Style::default(),
            Style::default()
        ));
        check_spans(&lines[0], ["", "hello", ""]);
    }

    #[test]
    fn test_case_insensitive() {
        let mut lines = Vec::new();
        assert!(add_highlight_match(
            "cpu",
            "CPU widget",
            &mut lines,
            Style::default(),
            Style::default()
        ));
        check_spans(&lines[0], ["", "CPU", " widget"]);
    }

    #[test]
    fn test_unicode_in_target() {
        let mut lines = Vec::new();
        assert!(add_highlight_match(
            "éléphants",
            "J'adore les éléphants et les lapins.",
            &mut lines,
            Style::default(),
            Style::default()
        ));
        check_spans(&lines[0], ["J'adore les ", "éléphants", " et les lapins."]);
    }

    #[test]
    fn test_unicode() {
        let mut lines = Vec::new();
        assert!(add_highlight_match(
            "",
            "Hi 你好!🇨🇦",
            &mut lines,
            Style::default(),
            Style::default()
        ));
        check_spans(&lines[0], ["Hi ", "", "好!🇨🇦"]);
    }

    #[test]
    fn test_unicode_2() {
        let mut lines = Vec::new();
        assert!(add_highlight_match(
            "🇨🇦",
            "Hi 你好!🇨🇦",
            &mut lines,
            Style::default(),
            Style::default()
        ));
        check_spans(&lines[0], ["Hi 你好!", "🇨🇦", ""]);
    }

    /// Shows that we only match the first occurrence in the line. This is expected behaviour as we're passing
    /// in separate strings.
    #[test]
    fn test_unicode_3() {
        let mut lines = Vec::new();
        assert!(add_highlight_match(
            "🇨🇦",
            "Hi 🇨🇦 你好!🇨🇦",
            &mut lines,
            Style::default(),
            Style::default()
        ));
        check_spans(&lines[0], ["Hi ", "🇨🇦", " 你好!🇨🇦"]);
    }

    #[test]
    fn test_unicode_case_insensitive() {
        let mut lines = Vec::new();
        assert!(add_highlight_match(
            "éléphants",
            "J'adore les Éléphants et les lapins.",
            &mut lines,
            Style::default(),
            Style::default()
        ));
        check_spans(&lines[0], ["J'adore les ", "Éléphants", " et les lapins."]);
    }

    #[test]
    fn test_accumulates_across_calls() {
        let mut lines = Vec::new();
        add_highlight_match("a", "cat", &mut lines, Style::default(), Style::default()); // match
        add_highlight_match("a", "dog", &mut lines, Style::default(), Style::default()); // no match
        add_highlight_match("a", "rat", &mut lines, Style::default(), Style::default()); // match
        assert_eq!(lines.len(), 2);
    }
}