revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
//! Pagination widget for page navigation

use crate::render::{Cell, Modifier};
use crate::style::Color;
use crate::widget::theme::SUBTLE_GRAY;
use crate::widget::traits::{RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};

/// Pagination style
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PaginationStyle {
    /// Full pagination with page numbers
    #[default]
    Full,
    /// Simple prev/next only
    Simple,
    /// Compact with current/total display
    Compact,
    /// Dots indicator
    Dots,
}

/// A pagination widget for navigating pages
///
/// # Example
///
/// ```rust,ignore
/// use revue::prelude::*;
///
/// pagination(10)
///     .current(3)
///     .on_change(|page| { /* ... */ })
/// ```
pub struct Pagination {
    /// Total number of pages
    total: u16,
    /// Current page (1-indexed)
    current: u16,
    /// Style
    style: PaginationStyle,
    /// Max visible page buttons
    max_visible: u16,
    /// Show prev/next buttons
    show_arrows: bool,
    /// Show first/last buttons
    show_edges: bool,
    /// Active color
    active_color: Color,
    /// Inactive color
    inactive_color: Color,
    /// Is focused
    focused: bool,
    /// CSS styling properties (id, classes)
    props: WidgetProps,
}

impl Pagination {
    /// Create a new pagination with total pages
    pub fn new(total: u16) -> Self {
        Self {
            total,
            current: 1,
            style: PaginationStyle::Full,
            max_visible: 7,
            show_arrows: true,
            show_edges: true,
            active_color: Color::rgb(60, 120, 200),
            inactive_color: SUBTLE_GRAY,
            focused: false,
            props: WidgetProps::new(),
        }
    }

    /// Set current page
    pub fn current(mut self, page: u16) -> Self {
        self.current = page.max(1).min(self.total);
        self
    }

    /// Set style
    pub fn style(mut self, style: PaginationStyle) -> Self {
        self.style = style;
        self
    }

    /// Simple style shorthand
    pub fn simple(mut self) -> Self {
        self.style = PaginationStyle::Simple;
        self
    }

    /// Compact style shorthand
    pub fn compact(mut self) -> Self {
        self.style = PaginationStyle::Compact;
        self
    }

    /// Dots style shorthand
    pub fn dots(mut self) -> Self {
        self.style = PaginationStyle::Dots;
        self
    }

    /// Set max visible page buttons
    pub fn max_visible(mut self, max: u16) -> Self {
        self.max_visible = max.max(3);
        self
    }

    /// Hide arrows
    pub fn no_arrows(mut self) -> Self {
        self.show_arrows = false;
        self
    }

    /// Hide first/last buttons
    pub fn no_edges(mut self) -> Self {
        self.show_edges = false;
        self
    }

    /// Set active color
    pub fn active_color(mut self, color: Color) -> Self {
        self.active_color = color;
        self
    }

    /// Set inactive color
    pub fn inactive_color(mut self, color: Color) -> Self {
        self.inactive_color = color;
        self
    }

    /// Set focused state
    pub fn focused(mut self) -> Self {
        self.focused = true;
        self
    }

    /// Go to next page
    pub fn next_page(&mut self) -> bool {
        if self.current < self.total {
            self.current += 1;
            true
        } else {
            false
        }
    }

    /// Go to previous page
    pub fn prev_page(&mut self) -> bool {
        if self.current > 1 {
            self.current -= 1;
            true
        } else {
            false
        }
    }

    /// Go to first page
    pub fn first(&mut self) {
        self.current = 1;
    }

    /// Go to last page
    pub fn last(&mut self) {
        self.current = self.total;
    }

    /// Go to specific page
    pub fn goto(&mut self, page: u16) {
        self.current = page.max(1).min(self.total);
    }

    /// Get current page
    pub fn get_current(&self) -> u16 {
        self.current
    }

    /// Get total pages
    pub fn get_total(&self) -> u16 {
        self.total
    }

    /// Set total pages
    pub fn set_total(&mut self, total: u16) {
        self.total = total;
        if self.current > total {
            self.current = total.max(1);
        }
    }

    /// Check if on first page
    pub fn is_first(&self) -> bool {
        self.current == 1
    }

    /// Check if on last page
    pub fn is_last(&self) -> bool {
        self.current == self.total
    }

    // Getters for testing
    #[doc(hidden)]
    pub fn get_style(&self) -> PaginationStyle {
        self.style
    }

    #[doc(hidden)]
    pub fn get_max_visible(&self) -> u16 {
        self.max_visible
    }

    #[doc(hidden)]
    pub fn get_show_arrows(&self) -> bool {
        self.show_arrows
    }

    #[doc(hidden)]
    pub fn get_show_edges(&self) -> bool {
        self.show_edges
    }

    #[doc(hidden)]
    pub fn get_active_color(&self) -> Color {
        self.active_color
    }

    #[doc(hidden)]
    pub fn get_inactive_color(&self) -> Color {
        self.inactive_color
    }

    #[doc(hidden)]
    pub fn get_focused(&self) -> bool {
        self.focused
    }

    /// Calculate visible page range
    fn visible_range(&self) -> (u16, u16) {
        let half = self.max_visible / 2;
        let start = if self.current <= half {
            1
        } else if self.current >= self.total - half {
            self.total.saturating_sub(self.max_visible - 1)
        } else {
            self.current - half
        };

        let end = (start + self.max_visible - 1).min(self.total);
        (start.max(1), end)
    }
}

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

impl View for Pagination {
    crate::impl_view_meta!("Pagination");

    fn render(&self, ctx: &mut RenderContext) {
        let area = ctx.area;
        let mut x: u16 = 0;

        match self.style {
            PaginationStyle::Full => {
                // « < 1 2 [3] 4 5 > »
                if self.show_edges {
                    // First button
                    let color = if self.is_first() {
                        self.inactive_color
                    } else {
                        self.active_color
                    };
                    let mut cell = Cell::new('«');
                    cell.fg = Some(color);
                    ctx.set(x, 0, cell);
                    x += 2;
                }

                if self.show_arrows {
                    // Prev button
                    let color = if self.is_first() {
                        self.inactive_color
                    } else {
                        self.active_color
                    };
                    let mut cell = Cell::new('');
                    cell.fg = Some(color);
                    ctx.set(x, 0, cell);
                    x += 2;
                }

                // Page numbers
                let (start, end) = self.visible_range();

                // Ellipsis before
                if start > 1 {
                    let mut one = Cell::new('1');
                    one.fg = Some(self.inactive_color);
                    ctx.set(x, 0, one);
                    x += 2;

                    if start > 2 {
                        let mut dots = Cell::new('');
                        dots.fg = Some(self.inactive_color);
                        ctx.set(x, 0, dots);
                        x += 2;
                    }
                }

                // Page numbers
                for page in start..=end {
                    let is_current = page == self.current;

                    if is_current {
                        let mut lb = Cell::new('[');
                        lb.fg = Some(self.active_color);
                        ctx.set(x, 0, lb);
                        x += 1;
                    }

                    let page_str = page.to_string();
                    for ch in page_str.chars() {
                        let mut cell = Cell::new(ch);
                        if is_current {
                            cell.fg = Some(self.active_color);
                            cell.modifier |= Modifier::BOLD;
                        } else {
                            cell.fg = Some(self.inactive_color);
                        }
                        ctx.set(x, 0, cell);
                        x += 1;
                    }

                    if is_current {
                        let mut rb = Cell::new(']');
                        rb.fg = Some(self.active_color);
                        ctx.set(x, 0, rb);
                        x += 1;
                    }

                    x += 1; // Space between
                }

                // Ellipsis after
                if end < self.total {
                    if end < self.total - 1 {
                        let mut dots = Cell::new('');
                        dots.fg = Some(self.inactive_color);
                        ctx.set(x, 0, dots);
                        x += 2;
                    }

                    let total_str = self.total.to_string();
                    for ch in total_str.chars() {
                        let mut cell = Cell::new(ch);
                        cell.fg = Some(self.inactive_color);
                        ctx.set(x, 0, cell);
                        x += 1;
                    }
                    x += 1;
                }

                if self.show_arrows {
                    // Next button
                    let color = if self.is_last() {
                        self.inactive_color
                    } else {
                        self.active_color
                    };
                    let mut cell = Cell::new('');
                    cell.fg = Some(color);
                    ctx.set(x, 0, cell);
                    x += 2;
                }

                if self.show_edges {
                    // Last button
                    let color = if self.is_last() {
                        self.inactive_color
                    } else {
                        self.active_color
                    };
                    let mut cell = Cell::new('»');
                    cell.fg = Some(color);
                    ctx.set(x, 0, cell);
                }
            }
            PaginationStyle::Simple => {
                // ← Page 3 of 10 →
                let prev_color = if self.is_first() {
                    self.inactive_color
                } else {
                    self.active_color
                };
                let mut prev = Cell::new('');
                prev.fg = Some(prev_color);
                ctx.set(x, 0, prev);
                x += 2;

                let text = format!("Page {} of {}", self.current, self.total);
                for ch in text.chars() {
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(self.inactive_color);
                    ctx.set(x, 0, cell);
                    x += 1;
                }
                x += 1;

                let next_color = if self.is_last() {
                    self.inactive_color
                } else {
                    self.active_color
                };
                let mut next = Cell::new('');
                next.fg = Some(next_color);
                ctx.set(x, 0, next);
            }
            PaginationStyle::Compact => {
                // 3/10
                let text = format!("{}/{}", self.current, self.total);
                for ch in text.chars() {
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(self.active_color);
                    cell.modifier |= Modifier::BOLD;
                    ctx.set(x, 0, cell);
                    x += 1;
                }
            }
            PaginationStyle::Dots => {
                // ○ ○ ● ○ ○
                for page in 1..=self.total {
                    if x >= area.width {
                        break;
                    }

                    let is_current = page == self.current;
                    let ch = if is_current { '' } else { '' };
                    let mut cell = Cell::new(ch);
                    cell.fg = Some(if is_current {
                        self.active_color
                    } else {
                        self.inactive_color
                    });
                    ctx.set(x, 0, cell);
                    x += 2;
                }
            }
        }
    }
}

impl_styled_view!(Pagination);
impl_props_builders!(Pagination);

/// Create a new pagination
pub fn pagination(total: u16) -> Pagination {
    Pagination::new(total)
}