tuile 0.3.0

Textual-grade widgets, design system, animation and app runtime on top of ratatui
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
//! Breadcrumb navigation with collapsing middle segments, clickable links, hover highlights,
//! and a paginator widget for page-based navigation.
//!
//! ```
//! use tuile::prelude::*;
//! # let area = Rect::new(0, 0, 40, 3);
//! # let mut buf = Buffer::empty(area);
//! let segments = vec!["Home".to_string(), "Projects".to_string(), "tuile".to_string()];
//! let mut state = BreadcrumbsState::new();
//! Breadcrumbs::new(segments).render(area, &mut buf, &mut state);
//! ```

use crossterm::event::{MouseButton, MouseEvent, MouseEventKind};
use ratatui_core::buffer::Buffer;
use ratatui_core::layout::Rect;
use ratatui_core::style::Modifier;
use ratatui_core::widgets::StatefulWidget;
use unicode_width::UnicodeWidthStr;

use crate::core::{Interactive, MinSize, Outcome, mouse_pos};
use crate::draw::{self, fill, put, st};
use crate::theme::{self, Theme};

// breadcrumbs

/// Breadcrumb segments with collapsing and click handling.
#[derive(Clone, Debug)]
pub struct Breadcrumbs {
    segments: Vec<String>,
    separator: String,
    icons: Vec<Option<String>>,
    focused: bool,
    enabled: bool,
    theme: Option<Theme>,
}

impl Breadcrumbs {
    pub fn new(segments: impl Into<Vec<String>>) -> Self {
        let segs = segments.into();
        Self {
            icons: vec![None; segs.len()],
            segments: segs,
            separator: "›".to_string(),
            focused: false,
            enabled: true,
            theme: None,
        }
    }

    pub fn separator(mut self, s: &str) -> Self {
        self.separator = s.to_string();
        self
    }

    pub fn icons(mut self, icons: Vec<Option<String>>) -> Self {
        self.icons = icons;
        self
    }

    pub fn focused(mut self, f: bool) -> Self {
        self.focused = f;
        self
    }

    pub fn enabled(mut self, e: bool) -> Self {
        self.enabled = e;
        self
    }

    pub fn theme(mut self, t: &Theme) -> Self {
        self.theme = Some(*t);
        self
    }
}

impl MinSize for Breadcrumbs {
    /// Breadcrumbs always take 1 row.
    fn min_size(&self) -> (u16, u16) {
        (8, 1)
    }
}

impl<T: Into<String>> From<Vec<T>> for Breadcrumbs {
    fn from(v: Vec<T>) -> Self {
        Self::new(v.into_iter().map(Into::into).collect::<Vec<_>>())
    }
}

/// Breadcrumbs state: hit boxes, hover, clicked segment.
#[derive(Clone, Debug, Default)]
pub struct BreadcrumbsState {
    pub hits: Vec<(usize, Rect)>,
    pub hover: Option<usize>,
    pub clicked: Option<usize>,
}

impl BreadcrumbsState {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn take_clicked(&mut self) -> Option<usize> {
        self.clicked.take()
    }
}

impl Interactive for BreadcrumbsState {
    fn handle_key(&mut self, _: crossterm::event::KeyEvent) -> Outcome {
        Outcome::Ignored
    }

    fn handle_mouse(&mut self, m: MouseEvent) -> Outcome {
        let pos = mouse_pos(&m);
        let mut out = Outcome::Ignored;

        // hover
        let mut hover = None;
        for (i, r) in &self.hits {
            if r.contains(pos) {
                hover = Some(*i);
                break;
            }
        }
        if hover != self.hover {
            self.hover = hover;
            out = Outcome::Consumed;
        }

        // click
        if matches!(m.kind, MouseEventKind::Down(MouseButton::Left))
            && let Some(i) = hover
        {
            self.clicked = Some(i);
            return Outcome::Submitted;
        }

        out
    }
}

impl StatefulWidget for Breadcrumbs {
    type State = BreadcrumbsState;

    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        if area.width == 0 || area.height == 0 || self.segments.is_empty() {
            state.hits.clear();
            return;
        }

        let th = self.theme.unwrap_or_else(theme::current);
        if draw::refuse(buf, area, self.min_size(), th.text_disabled) {
            state.hits.clear();
            return;
        }

        let bg = th.surface;

        fill(buf, area, bg);

        state.hits.clear();
        state.hits.reserve(self.segments.len());

        // compute widths
        let sep_w = self.separator.width() as u16 + 2; // space around separator
        let seg_widths: Vec<u16> = self
            .segments
            .iter()
            .enumerate()
            .map(|(i, s)| {
                let mut w = s.width() as u16;
                if let Some(Some(icon)) = self.icons.get(i) {
                    w += icon.width() as u16 + 1;
                }
                w
            })
            .collect();

        let total: u16 =
            seg_widths.iter().sum::<u16>() + sep_w * (self.segments.len().saturating_sub(1)) as u16;

        // collapse middle if too wide
        let mut visible: Vec<(usize, &str)> = self
            .segments
            .iter()
            .enumerate()
            .map(|(i, s)| (i, s.as_str()))
            .collect();
        if total > area.width && self.segments.len() > 2 {
            // keep first and last, collapse the middle to "…"; the `len() > 2` guard makes
            // index 0 and `last` in bounds
            let last = self.segments.len() - 1;
            let needed = seg_widths[0] + sep_w + 1 + sep_w + seg_widths[last]; // "first › … › last"
            if needed <= area.width {
                visible = vec![
                    (0, self.segments[0].as_str()),
                    (usize::MAX, "…"),
                    (last, self.segments[last].as_str()),
                ];
            }
        }

        let mut x = area.x;
        for (vi, (i, seg)) in visible.iter().enumerate() {
            let is_last = vi == visible.len() - 1;
            let is_hover = state.hover == Some(*i);
            let is_ellipsis = *i == usize::MAX;

            let (_fg, style) = if is_last {
                (th.text, st(th.text, bg).add_modifier(Modifier::BOLD))
            } else if is_hover && !is_ellipsis {
                (
                    th.primary,
                    st(th.primary, bg).add_modifier(Modifier::UNDERLINED),
                )
            } else {
                (th.text_muted, st(th.text_muted, bg))
            };

            if is_ellipsis {
                let w = put(
                    buf,
                    x,
                    area.y,
                    seg,
                    area.right().saturating_sub(x),
                    st(th.text_muted, bg),
                );
                x += w;
            } else {
                let start_x = x;
                if let Some(Some(icon)) = self.icons.get(*i) {
                    let iw = put(buf, x, area.y, icon, area.right().saturating_sub(x), style);
                    x += iw + 1;
                }
                let w = put(buf, x, area.y, seg, area.right().saturating_sub(x), style);
                x += w;
                state
                    .hits
                    .push((*i, Rect::new(start_x, area.y, x - start_x, 1)));
            }

            if !is_last && x < area.right() {
                x += 1;
                put(
                    buf,
                    x,
                    area.y,
                    &self.separator,
                    area.right().saturating_sub(x),
                    st(th.text_muted, bg),
                );
                x += self.separator.width() as u16 + 1;
            }
        }
    }
}

// paginator

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PaginatorHit {
    Prev,
    Next,
    Page(usize),
}

/// Paginator widget for page-based navigation.
#[derive(Clone, Debug)]
pub struct Paginator {
    total: usize,
    window: usize,
    focused: bool,
    enabled: bool,
    theme: Option<Theme>,
}

impl Paginator {
    pub fn new(total: usize) -> Self {
        Self {
            total,
            window: 2,
            focused: false,
            enabled: true,
            theme: None,
        }
    }

    pub fn window(mut self, w: usize) -> Self {
        self.window = w;
        self
    }

    pub fn focused(mut self, f: bool) -> Self {
        self.focused = f;
        self
    }

    pub fn enabled(mut self, e: bool) -> Self {
        self.enabled = e;
        self
    }

    pub fn theme(mut self, t: &Theme) -> Self {
        self.theme = Some(*t);
        self
    }
}

/// Paginator state: current page, hit boxes.
#[derive(Clone, Debug)]
pub struct PaginatorState {
    pub page: usize,
    pub hits: Vec<(PaginatorHit, Rect)>,
}

impl PaginatorState {
    pub fn new() -> Self {
        Self {
            page: 0,
            hits: Vec::new(),
        }
    }
}

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

impl Interactive for PaginatorState {
    fn handle_key(&mut self, k: crossterm::event::KeyEvent) -> Outcome {
        use crossterm::event::{KeyCode, KeyEventKind};
        if k.kind != KeyEventKind::Press && k.kind != KeyEventKind::Repeat {
            return Outcome::Ignored;
        }
        match k.code {
            KeyCode::Left => {
                if self.page > 0 {
                    self.page -= 1;
                    return Outcome::Changed;
                }
            }
            KeyCode::Right => {
                self.page += 1;
                return Outcome::Changed;
            }
            KeyCode::Home => {
                if self.page != 0 {
                    self.page = 0;
                    return Outcome::Changed;
                }
            }
            KeyCode::End => {
                self.page = usize::MAX;
                return Outcome::Changed;
            }
            _ => return Outcome::Ignored,
        }
        Outcome::Ignored
    }

    fn handle_mouse(&mut self, m: MouseEvent) -> Outcome {
        let pos = mouse_pos(&m);
        if !matches!(m.kind, MouseEventKind::Down(MouseButton::Left)) {
            return Outcome::Ignored;
        }

        for (hit, r) in &self.hits {
            if r.contains(pos) {
                match hit {
                    PaginatorHit::Prev => {
                        if self.page > 0 {
                            self.page -= 1;
                            return Outcome::Changed;
                        }
                    }
                    PaginatorHit::Next => {
                        self.page += 1;
                        return Outcome::Changed;
                    }
                    PaginatorHit::Page(p) => {
                        if self.page != *p {
                            self.page = *p;
                            return Outcome::Changed;
                        }
                    }
                }
            }
        }

        Outcome::Ignored
    }
}

impl MinSize for Paginator {
    /// Paginator always takes 1 row.
    fn min_size(&self) -> (u16, u16) {
        (8, 1)
    }
}

impl StatefulWidget for Paginator {
    type State = PaginatorState;
    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        if area.width == 0 || area.height == 0 || self.total == 0 {
            state.hits.clear();
            return;
        }

        let th = self.theme.unwrap_or_else(theme::current);
        if draw::refuse(buf, area, self.min_size(), th.text_disabled) {
            state.hits.clear();
            return;
        }

        let bg = th.surface;

        fill(buf, area, bg);

        state.page = state.page.min(self.total - 1);
        state.hits.clear();

        let pages = page_window(state.page, self.total, self.window);

        let mut x = area.x;

        // prev arrow
        if state.page > 0 {
            let w = put(buf, x, area.y, "‹", 1, st(th.accent, bg));
            state
                .hits
                .push((PaginatorHit::Prev, Rect::new(x, area.y, w, 1)));
            x += w + 1;
        }

        for p_opt in pages {
            if x >= area.right() {
                break;
            }

            match p_opt {
                Some(p) => {
                    let is_current = p == state.page;
                    let label = format!("{}", p + 1);
                    let (fg, page_bg) = if is_current {
                        (th.cursor_fg, th.primary)
                    } else {
                        (th.text_muted, bg)
                    };

                    let label_w = label.width() as u16;
                    if is_current {
                        fill(buf, Rect::new(x, area.y, label_w + 2, 1), page_bg);
                        put(
                            buf,
                            x + 1,
                            area.y,
                            &label,
                            label_w,
                            st(fg, page_bg).add_modifier(Modifier::BOLD),
                        );
                        state
                            .hits
                            .push((PaginatorHit::Page(p), Rect::new(x, area.y, label_w + 2, 1)));
                        x += label_w + 2;
                    } else {
                        let w = put(buf, x, area.y, &label, label_w, st(fg, page_bg));
                        state
                            .hits
                            .push((PaginatorHit::Page(p), Rect::new(x, area.y, w, 1)));
                        x += w;
                    }

                    x += 1;
                }
                None => {
                    put(buf, x, area.y, "…", 1, st(th.text_muted, bg));
                    x += 2;
                }
            }
        }

        // next arrow
        if state.page < self.total - 1 && x < area.right() {
            let w = put(buf, x, area.y, "›", 1, st(th.accent, bg));
            state
                .hits
                .push((PaginatorHit::Next, Rect::new(x, area.y, w, 1)));
        }
    }
}

/// Compute visible page window: always show first, last, and `window` pages around current.
/// Returns `Some(page)` or `None` for ellipsis.
pub fn page_window(current: usize, total: usize, window: usize) -> Vec<Option<usize>> {
    if total == 0 {
        return vec![];
    }
    if total <= 2 * window + 5 {
        // show all
        return (0..total).map(Some).collect();
    }

    let mut pages = Vec::new();
    pages.push(Some(0));

    let start = current.saturating_sub(window);
    let end = (current + window + 1).min(total);

    if start > 1 {
        pages.push(None); // ellipsis
    }

    for p in start..end {
        if p > 0 && p < total - 1 {
            pages.push(Some(p));
        }
    }

    if end < total - 1 {
        pages.push(None); // ellipsis
    }

    pages.push(Some(total - 1));
    pages
}

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

    #[test]
    fn page_window_shows_all_when_small() {
        let w = page_window(2, 7, 2);
        assert_eq!(w.len(), 7);
        assert!(w.iter().all(|x| x.is_some()));
    }

    #[test]
    fn page_window_collapses_middle() {
        let w = page_window(20, 42, 2);
        // should be [0, None, 18, 19, 20, 21, 22, None, 41]
        assert_eq!(w[0], Some(0));
        assert_eq!(w[1], None);
        assert!(w.contains(&Some(20)));
        assert_eq!(w.last(), Some(&Some(41)));
    }

    #[test]
    fn breadcrumbs_handles_click() {
        let mut state = BreadcrumbsState::new();
        state.hits.push((1, Rect::new(5, 0, 10, 1)));
        let m = MouseEvent {
            kind: MouseEventKind::Down(MouseButton::Left),
            column: 7,
            row: 0,
            modifiers: crossterm::event::KeyModifiers::empty(),
        };
        assert_eq!(state.handle_mouse(m), Outcome::Submitted);
        assert_eq!(state.take_clicked(), Some(1));
    }
}