termint 0.8.1

Library for colored printing and Terminal User Interfaces
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
use std::{
    cell::RefCell,
    cmp::{max, min},
    hash::{DefaultHasher, Hash, Hasher},
    rc::Rc,
};

use crate::{
    buffer::Buffer,
    enums::Color,
    geometry::Padding,
    prelude::{MouseButton, MouseEvent, Rect, Vec2},
    style::Style,
    term::backend::MouseEventKind,
    text::Text,
    widgets::{Element, EventResult, LayoutNode, Span, ToSpan, Widget},
};

type ListHandler<M> = Box<dyn Fn(usize) -> M>;

/// A scrollable list widget with support for item selection and highlighting.
///
/// The [`List`] widget displays a list of strings, supporting vertical
/// scrolling, item selection and custom highlighting. A scrollbar is
/// automatically hidden when the content fits in the available height.
///
/// # Mouse support
///
/// List supports mouse event handling. In order to enable it, you have to
/// enable mouse capture. You can do that by calling
/// [`Term::with_mouse`](crate::term::Term::with_mouse) on
/// [`Term`](crate::term::Term) struct or
/// [`enable_mouse_capture`](crate::term::enable_mouse_capture) when not using
/// the [`Term`](crate::term::Term).
///
/// By default [`List`] automatically handles scrolling. When you scroll up,
/// previous item is selected and when you scroll down, the next item is
/// selected. You can customize it using [`List::on_scroll`] or disable it
/// using [`List::scrollable`].
///
/// You can setup click handlers reporting which item was clicked using
/// [`List::on_click`] and [`List::on_press`].
///
/// # Example
/// ```rust
/// use termint::prelude::*;
/// use std::{cell::RefCell, rc::Rc};
///
/// // Creates list state with offset 0 and with selected item at index 1
/// let state = Rc::new(RefCell::new(ListState::selected(0, 1)));
///
/// // Creates a list with given items.
/// let items = vec!["Item1", "Item2", "Item3", "Item4", "Item5", "Item6"];
/// let list = List::<()>::new(items, state.clone())
///     // Highlight the selected item in yellow.
///     .selected_style(Color::Yellow)
///     // Add `*` prefix before the selected item.
///     .highlight_symbol("*")
///     // Automatically scroll selected item into view.
///     .auto_scroll();
/// ```
pub struct List<M: 'static = ()> {
    items: Vec<String>,
    state: Rc<RefCell<ListState>>,
    auto_scroll: bool,
    handle_scroll: bool,
    scroll_step: usize,
    style: Style,
    sel_style: Style,
    highlight: String,
    highlight_style: Style,
    force_scrollbar: bool,
    scrollbar_fg: Color,
    thumb_fg: Color,
    handlers: Vec<(MouseButton, ListHandler<M>)>,
    on_scroll: Option<Box<dyn Fn(isize) -> M>>,
}

/// Stores the state of the [`List`] widget.
///
/// It includes:
/// - Scroll offset = based on items, e.g. 5 means 5th item is first visible
/// - Selected = optional ID of the selected item (zero-based)
#[derive(Debug, Default, Hash)]
pub struct ListState {
    /// The index of the first item currently visible in the list.
    pub offset: usize,
    /// The index of the currently selected item (if any).
    pub selected: Option<usize>,
}

impl<M> List<M> {
    /// Creates a new [`List`] with given items and given state.
    ///
    /// The `items` can be any iterable yielding strings (e.g. `Vec<String>` or
    /// `&[&str]`).
    #[must_use]
    pub fn new<T>(items: T, state: Rc<RefCell<ListState>>) -> Self
    where
        T: IntoIterator,
        T::Item: AsRef<str>,
    {
        let items =
            items.into_iter().map(|i| i.as_ref().to_string()).collect();

        Self {
            items,
            state,
            auto_scroll: false,
            handle_scroll: true,
            scroll_step: 1,
            style: Default::default(),
            sel_style: Default::default(),
            highlight: String::new(),
            highlight_style: Default::default(),
            force_scrollbar: false,
            scrollbar_fg: Color::Default,
            thumb_fg: Color::Default,
            handlers: vec![],
            on_scroll: None,
        }
    }

    /// Sets the currently selected item in the [`List`].
    ///
    /// This directly modifies the inner [`ListState`]. Generally you can
    /// modify the state directly.
    #[must_use]
    pub fn selected<T>(self, current: T) -> Self
    where
        T: Into<Option<usize>>,
    {
        self.state.borrow_mut().selected = current.into();
        self
    }

    /// Enables automatic scrolling to ensure the selected item is always
    /// visible.
    ///
    /// If enabled, the list will automatically adjust its `offset` during
    /// rendering to keep the `selected` item in the view.
    #[must_use]
    pub fn auto_scroll(mut self) -> Self {
        self.auto_scroll = true;
        self
    }

    /// Enables or disables automatic mouse scroll handling.
    ///
    /// **Note:** This requires mouse capture to be enabled. You can do that by
    /// calling [`Term::with_mouse`](crate::term::Term::with_mouse) on
    /// [`Term`](crate::term::Term) struct or
    /// [`enable_mouse_capture`](crate::term::enable_mouse_capture) when not
    /// using  the [`Term`](crate::term::Term).
    #[must_use]
    pub fn scrollable(mut self, enabled: bool) -> Self {
        self.handle_scroll = enabled;
        self
    }

    /// Sets the numbers of items to scroll per mouse wheel step.
    ///
    /// It is mainly used in automatic mouse scroll handling, but the step
    /// size also determines the value returned in the Message if custom
    /// scroll handler is used.
    ///
    /// Default is `1`.
    #[must_use]
    pub fn scroll_step(mut self, size: usize) -> Self {
        self.scroll_step = size;
        self
    }

    /// Sets the base [`Style`] of the [`List`].
    ///
    /// The `style` can be any type convertible to [`Style`].
    #[must_use]
    pub fn style<T>(mut self, style: T) -> Self
    where
        T: Into<Style>,
    {
        self.style = style.into();
        self
    }

    /// Sets the [`Style`] of the selected item in the [`List`].
    ///
    /// The `style` can be any type convertible to [`Style`].
    #[must_use]
    pub fn selected_style<T>(mut self, style: T) -> Self
    where
        T: Into<Style>,
    {
        self.sel_style = style.into();
        self
    }

    /// Sets the prefix string to be displayed before the selected item.
    ///
    /// # Example
    ///
    /// ```rust
    /// use termint::prelude::*;
    /// # fn get_items() -> Vec<String> { vec![] }
    ///
    /// // Selected item will look like: "> Item text".
    /// let list = List::<()>::new(get_items(), Default::default())
    ///     .highlight_symbol("> ");
    /// ```
    #[must_use]
    pub fn highlight_symbol<T>(mut self, sel_char: T) -> Self
    where
        T: AsRef<str>,
    {
        self.highlight = sel_char.as_ref().to_string();
        self
    }

    /// Sets the [`Style`] of the highlight symbol.
    ///
    /// This allows the prefix to have a different color than the selected
    /// item itself.
    ///
    /// The `style` can be any type convertible to [`Style`].
    #[must_use]
    pub fn highlight_style<T>(mut self, style: T) -> Self
    where
        T: Into<Style>,
    {
        self.highlight_style = style.into();
        self
    }

    /// Forces scrollbar to be always visible, even if content fits.
    #[must_use]
    pub fn force_scrollbar(mut self) -> Self {
        self.force_scrollbar = true;
        self
    }

    /// Sets the foreground color of the scrollbar.
    #[must_use]
    pub fn scrollbar_fg(mut self, fg: Color) -> Self {
        self.scrollbar_fg = fg;
        self
    }

    /// Sets the foreground color of the scrollbar's thumb (the moving part).
    #[must_use]
    pub fn thumb_fg(mut self, fg: Color) -> Self {
        self.thumb_fg = fg;
        self
    }

    /// Sets the message to return when the left mouse button is clicked.
    ///
    /// The `response` is a closure that receives the index of the clicked
    /// item and returns the corresponding message.
    ///
    /// If a handler for the left mouse button already exists, it will be
    /// replaced.
    ///
    /// This is a convenience wrapper around [`List::on_press`].
    ///
    /// **Note:** This requires mouse capture to be enabled. You can do that by
    /// calling [`Term::with_mouse`](crate::term::Term::with_mouse) on
    /// [`Term`](crate::term::Term) struct or
    /// [`enable_mouse_capture`](crate::term::enable_mouse_capture) when not
    /// using  the [`Term`](crate::term::Term).
    #[must_use]
    pub fn on_click<F>(self, response: F) -> Self
    where
        F: Fn(usize) -> M + 'static,
    {
        self.on_press(MouseButton::Left, response)
    }

    /// Sets the message to return when the given [`MouseButton`] is clicked.
    ///
    /// The `response` is a closure that receives the index of the clicked
    /// item and returns the corresponding message.
    ///
    /// If a handler for the given mouse button already exists, it will be
    /// replaced.
    ///
    /// **Note:** This requires mouse capture to be enabled. You can do that by
    /// calling [`Term::with_mouse`](crate::term::Term::with_mouse) on
    /// [`Term`](crate::term::Term) struct or
    /// [`enable_mouse_capture`](crate::term::enable_mouse_capture) when not
    /// using  the [`Term`](crate::term::Term).
    ///
    /// # Example
    ///
    /// ```rust
    /// use termint::prelude::*;
    /// # fn get_items() -> Vec<String> { vec![] }
    ///
    /// let btn = List::new(get_items(), Default::default())
    ///     .on_press(MouseButton::Middle, |i| format!("Clicked {i}!"));
    /// ```
    #[must_use]
    pub fn on_press<F>(mut self, button: MouseButton, response: F) -> Self
    where
        F: Fn(usize) -> M + 'static,
    {
        self.handlers.retain(|(b, _)| *b != button);
        self.handlers.push((button, Box::new(response)));
        self
    }

    /// Sets the message to return when the mouse scroll event occures.
    ///
    /// The `response` is a closure that receives the scroll delta (e.g. -1
    /// when scrolled up) and returns the corresponding message.
    ///
    /// This disables the default on scroll handler, so only the given response
    /// will be used.
    ///
    /// **Note:** This requires mouse capture to be enabled. You can do that by
    /// calling [`Term::with_mouse`](crate::term::Term::with_mouse) on
    /// [`Term`](crate::term::Term) struct or
    /// [`enable_mouse_capture`](crate::term::enable_mouse_capture) when not
    /// using  the [`Term`](crate::term::Term).
    #[must_use]
    pub fn on_scroll<F>(mut self, response: F) -> Self
    where
        F: Fn(isize) -> M + 'static,
    {
        self.on_scroll = Some(Box::new(response));
        self
    }
}

impl ListState {
    /// Creates a new [`ListState`] with the given scroll offset and no
    /// selected item.
    #[must_use]
    pub fn new(offset: usize) -> Self {
        Self {
            offset,
            selected: None,
        }
    }

    /// Creates a new [`ListState`] with given scroll offset and selected item.
    #[must_use]
    pub fn selected(offset: usize, selected: usize) -> Self {
        Self {
            offset,
            selected: Some(selected),
        }
    }
}

impl<M: Clone + 'static> Widget<M> for List<M> {
    fn render(&self, buffer: &mut Buffer, layout: &LayoutNode) {
        let rect = layout.area;
        let mut text_pos =
            Vec2::new(rect.x() + self.highlight.len(), rect.y());
        let mut text_size =
            Vec2::new(rect.width() - self.highlight.len(), rect.height());

        let has_bar = self.force_scrollbar || !self.fits(&text_size);
        if has_bar {
            text_size.x = text_size.x.saturating_sub(1);
        }

        if self.auto_scroll {
            self.scroll_offset(&text_size);
        }

        if has_bar {
            self.render_scrollbar(buffer, &rect);
        }

        let selected = self.state.borrow().selected;
        for i in self.state.borrow().offset..self.items.len() {
            let mut span = self.items[i].style(self.style);
            if Some(i) == selected {
                buffer.set_str_styled(
                    &self.highlight,
                    &Vec2::new(rect.x(), text_pos.y),
                    self.highlight_style,
                );
                span = self.items[i].style(self.sel_style);
            }

            let irect = Rect::from_coords(text_pos, text_size);
            let res_pos = span.render_offset(buffer, irect, 0, None);

            text_size.y = text_size.y.saturating_sub(res_pos.y - text_pos.y);
            text_pos.y = res_pos.y + 1;

            if rect.y() + rect.height() <= text_pos.y {
                break;
            }
            text_size.y = rect.y() + rect.height() - text_pos.y;
        }
    }

    fn height(&self, size: &Vec2) -> usize {
        self.items
            .iter()
            .map(|i| <Span as Widget<M>>::height(&i.to_span(), size))
            .sum()
    }

    fn width(&self, size: &Vec2) -> usize {
        let mut width = 0;
        let mut height = 0;
        for item in self.items.iter() {
            let span = item.to_span();
            let h = <Span as Widget<M>>::height(&span, size);
            width = max(
                <Span as Widget<M>>::width(&span, &Vec2::new(size.x, h)),
                width,
            );
            height += h;
        }
        width + self.highlight.len() + (height > size.y) as usize
    }

    fn layout_hash(&self) -> u64 {
        let mut hasher = DefaultHasher::new();

        self.items.hash(&mut hasher);
        self.force_scrollbar.hash(&mut hasher);
        self.highlight.hash(&mut hasher);
        self.state.borrow().hash(&mut hasher);

        hasher.finish()
    }

    fn on_event(&self, node: &LayoutNode, e: &MouseEvent) -> EventResult<M> {
        if !node.area.contains_pos(&e.pos) {
            return EventResult::None;
        }

        let mut area = node.area.inner(Padding::left(self.highlight.len()));
        if self.force_scrollbar || !self.fits(area.size()) {
            area.size.x -= 1;
        }

        for i in self.state.borrow().offset..self.items.len() {
            let span: Element<M> = self.items[i].to_span().into();
            let height = span.height(area.size());

            let mut irect = Rect::from_coords(*area.pos(), *area.size());
            irect.size.y = height;
            if !irect.contains_pos(&e.pos) {
                area = area.inner(Padding::top(height));
                continue;
            }

            let m = self.item_event(e, i);
            if !m.is_none() {
                return m;
            }
        }
        self.handle_mouse(e)
    }
}

impl<M: Clone + 'static> List<M> {
    /// Renders [`List`] scrollbar
    fn render_scrollbar(&self, buffer: &mut Buffer, rect: &Rect) {
        let rat = self.items.len() as f32 / rect.height() as f32;
        let thumb_size = max(
            1,
            min((rect.height() as f32 / rat).round() as usize, rect.height()),
        );
        let mut thumb_offset = min(
            (self.state.borrow().offset as f32 / rat) as usize,
            rect.height() - thumb_size,
        );
        if let Some(selected) = self.state.borrow().selected
            && selected + 1 == self.items.len()
        {
            thumb_offset = rect.height() - thumb_size;
        }

        let x = (rect.x() + rect.width()).saturating_sub(1);
        let mut bar_pos = Vec2::new(x, rect.y());
        for _ in 0..rect.height() {
            buffer[bar_pos].char('').fg(self.scrollbar_fg);
            bar_pos.y += 1;
        }

        bar_pos = Vec2::new(x, rect.y() + thumb_offset);
        for _ in 0..thumb_size {
            buffer[bar_pos].char('').fg(self.thumb_fg);
            bar_pos.y += 1;
        }
    }

    /// Automatically scrolls so the selected item is visible
    fn scroll_offset(&self, size: &Vec2) {
        let Some(selected) = self.state.borrow().selected else {
            return;
        };

        if selected < self.state.borrow().offset {
            self.state.borrow_mut().offset = selected;
            return;
        }

        while !self.is_visible(selected, self.state.borrow().offset, size) {
            self.state.borrow_mut().offset += 1;
        }
    }

    fn move_selection(&self, delta: isize) {
        let mut state = self.state.borrow_mut();
        let Some(selected) = state.selected else {
            return;
        };

        let id = match delta < 0 {
            true => selected.saturating_sub(delta.unsigned_abs()),
            false => (selected + delta as usize)
                .min(self.items.len().saturating_sub(1)),
        };
        state.selected = Some(id);
    }

    /// Checks if item is visible with given offset
    fn is_visible(&self, item: usize, offset: usize, size: &Vec2) -> bool {
        let mut height = 0;
        for i in offset..self.items.len() {
            height +=
                <Span as Widget<M>>::height(&self.items[i].to_span(), size);
            if height > size.y {
                return false;
            }

            if i == item {
                return true;
            }
        }
        false
    }

    /// Checks if list fits to the visible area
    fn fits(&self, size: &Vec2) -> bool {
        self.is_visible(self.items.len() - 1, 0, size)
    }

    fn item_event(&self, event: &MouseEvent, id: usize) -> EventResult<M> {
        match &event.kind {
            MouseEventKind::Down(button) => self
                .handlers
                .iter()
                .find(|(b, _)| b == button)
                .map(|(_, m)| EventResult::Response(m(id)))
                .unwrap_or(EventResult::None),
            _ => EventResult::None,
        }
    }

    fn handle_mouse(&self, event: &MouseEvent) -> EventResult<M> {
        let delta = match &event.kind {
            MouseEventKind::ScrollDown => self.scroll_step as isize,
            MouseEventKind::ScrollUp => -(self.scroll_step as isize),
            _ => return EventResult::None,
        };

        if let Some(handler) = &self.on_scroll {
            return EventResult::Response(handler(delta));
        }

        if self.handle_scroll {
            self.move_selection(delta);
            return EventResult::Consumed;
        }

        EventResult::None
    }
}

// From implementations
impl<M: Clone + 'static> From<List<M>> for Box<dyn Widget<M>> {
    fn from(value: List<M>) -> Self {
        Box::new(value)
    }
}

impl<M: Clone + 'static> From<List<M>> for Element<M> {
    fn from(value: List<M>) -> Self {
        Element::new(value)
    }
}