bubbletea-widgets 0.1.12

A collection of reusable TUI components for building terminal applications with bubbletea-rs
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
//! A paginator component for bubbletea-rs, ported from the Go version.
//!
//! This component is used for calculating pagination and rendering pagination info.
//! Note that this package does not render actual pages of content; it's purely
//! for handling the state and view of the pagination control itself.

use crate::key::{self, KeyMap as KeyMapTrait};
use bubbletea_rs::{KeyMsg, Msg};

/// The type of pagination to display.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Type {
    /// Display pagination as Arabic numerals (e.g., "1/5").
    #[default]
    Arabic,
    /// Display pagination as dots (e.g., "● ○ ○ ○ ○").
    Dots,
}

/// Key bindings for different actions within the paginator.
///
/// This structure defines the key bindings that control pagination navigation.
/// It implements the `KeyMap` trait to provide help information for the
/// paginator component.
///
/// # Examples
///
/// ```rust
/// use bubbletea_widgets::paginator::PaginatorKeyMap;
/// use bubbletea_widgets::key;
///
/// let keymap = PaginatorKeyMap::default();
///
/// // Create custom key bindings
/// let custom_keymap = PaginatorKeyMap {
///     prev_page: key::new_binding(vec![
///         key::with_keys_str(&["a", "left"]),
///         key::with_help("a/←", "previous page"),
///     ]),
///     next_page: key::new_binding(vec![
///         key::with_keys_str(&["d", "right"]),
///         key::with_help("d/→", "next page"),
///     ]),
/// };
/// ```
#[derive(Debug, Clone)]
pub struct PaginatorKeyMap {
    /// Key binding for navigating to the previous page.
    /// Default keys: PageUp, Left Arrow, 'h'
    pub prev_page: key::Binding,
    /// Key binding for navigating to the next page.
    /// Default keys: PageDown, Right Arrow, 'l'
    pub next_page: key::Binding,
}

impl Default for PaginatorKeyMap {
    /// Creates default key bindings for paginator navigation.
    ///
    /// The default key bindings are:
    /// - **Previous page**: PageUp, Left Arrow, 'h'
    /// - **Next page**: PageDown, Right Arrow, 'l'
    ///
    /// These bindings are commonly used in terminal applications and provide
    /// both arrow key navigation and vim-style 'h'/'l' keys.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::PaginatorKeyMap;
    /// use bubbletea_widgets::key::KeyMap;
    ///
    /// let keymap = PaginatorKeyMap::default();
    /// let help = keymap.short_help();
    /// assert_eq!(help.len(), 2); // prev and next bindings
    /// ```
    fn default() -> Self {
        Self {
            prev_page: key::new_binding(vec![
                key::with_keys_str(&["pgup", "left", "h"]),
                key::with_help("←/h", "prev page"),
            ]),
            next_page: key::new_binding(vec![
                key::with_keys_str(&["pgdown", "right", "l"]),
                key::with_help("→/l", "next page"),
            ]),
        }
    }
}

impl KeyMapTrait for PaginatorKeyMap {
    /// Returns key bindings for the short help view.
    ///
    /// This provides the essential pagination key bindings that will be
    /// displayed in compact help views.
    ///
    /// # Returns
    ///
    /// A vector containing references to the previous page and next page bindings.
    fn short_help(&self) -> Vec<&key::Binding> {
        vec![&self.prev_page, &self.next_page]
    }

    /// Returns key bindings for the full help view.
    ///
    /// This organizes all pagination key bindings into columns for display
    /// in expanded help views. Since pagination only has two keys, they're
    /// grouped together in a single column.
    ///
    /// # Returns
    ///
    /// A vector of vectors, where each inner vector represents a column
    /// of related key bindings.
    fn full_help(&self) -> Vec<Vec<&key::Binding>> {
        vec![vec![&self.prev_page, &self.next_page]]
    }
}

/// A paginator model for handling pagination state and rendering.
///
/// This component manages pagination state including current page, total pages,
/// and pagination display style. It can render pagination in two modes:
/// - **Arabic**: Shows page numbers (e.g., "3/10")
/// - **Dots**: Shows dots representing pages (e.g., "○ ○ ● ○ ○")
///
/// The paginator handles key bindings for navigation and provides helper methods
/// for calculating slice bounds and page information.
///
/// # Examples
///
/// ## Basic Usage
///
/// ```rust
/// use bubbletea_widgets::paginator::{Model, Type};
///
/// let mut paginator = Model::new()
///     .with_per_page(10)
///     .with_total_items(150); // Creates 15 pages
///
/// assert_eq!(paginator.total_pages, 15);
/// assert!(paginator.on_first_page());
///
/// paginator.next_page();
/// assert_eq!(paginator.page, 1);
/// ```
///
/// ## Different Display Types
///
/// ```rust
/// use bubbletea_widgets::paginator::{Model, Type};
///
/// let mut paginator = Model::new()
///     .with_total_items(50)
///     .with_per_page(10);
///
/// // Arabic mode (default): "1/5"
/// paginator.paginator_type = Type::Arabic;
/// let arabic_view = paginator.view();
///
/// // Dots mode: "● ○ ○ ○ ○"
/// paginator.paginator_type = Type::Dots;
/// let dots_view = paginator.view();
/// ```
///
/// ## Integration with bubbletea-rs
///
/// ```rust
/// use bubbletea_widgets::paginator::Model as Paginator;
/// use bubbletea_rs::{Model, Cmd, Msg};
///
/// struct App {
///     paginator: Paginator,
///     items: Vec<String>,
/// }
///
/// impl Model for App {
///     fn init() -> (Self, Option<Cmd>) {
///         let items: Vec<String> = (1..=100).map(|i| format!("Item {}", i)).collect();
///         let paginator = Paginator::new()
///             .with_per_page(10)
///             .with_total_items(items.len());
///             
///         (Self { paginator, items }, None)
///     }
///
///     fn update(&mut self, msg: Msg) -> Option<Cmd> {
///         self.paginator.update(&msg);
///         None
///     }
///
///     fn view(&self) -> String {
///         let (start, end) = self.paginator.get_slice_bounds(self.items.len());
///         let page_items: Vec<String> = self.items[start..end].to_vec();
///         
///         format!(
///             "Items:\n{}\n\nPage: {}",
///             page_items.join("\n"),
///             self.paginator.view()
///         )
///     }
/// }
/// ```
#[derive(Debug, Clone)]
pub struct Model {
    /// The type of pagination to display (Dots or Arabic).
    pub paginator_type: Type,
    /// The current page.
    pub page: usize,
    /// The number of items per page.
    pub per_page: usize,
    /// The total number of pages.
    pub total_pages: usize,

    /// The character to use for the active page in Dots mode.
    pub active_dot: String,
    /// The character to use for inactive pages in Dots mode.
    pub inactive_dot: String,
    /// The format string for Arabic mode (e.g., "%d/%d").
    pub arabic_format: String,

    /// Key bindings.
    pub keymap: PaginatorKeyMap,
}

impl Default for Model {
    /// Creates a paginator with default settings.
    ///
    /// Default configuration:
    /// - Type: Arabic ("1/5" style)
    /// - Current page: 0 (first page)
    /// - Items per page: 1
    /// - Total pages: 1
    /// - Active dot: "•" (for dots mode)
    /// - Inactive dot: "○" (for dots mode)
    /// - Arabic format: "%d/%d" (current/total)
    /// - Default key bindings
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::{Model, Type};
    ///
    /// let paginator = Model::default();
    /// assert_eq!(paginator.paginator_type, Type::Arabic);
    /// assert_eq!(paginator.page, 0);
    /// assert_eq!(paginator.per_page, 1);
    /// assert_eq!(paginator.total_pages, 1);
    /// ```
    fn default() -> Self {
        Self {
            paginator_type: Type::default(),
            page: 0,
            per_page: 1,
            total_pages: 1,
            active_dot: "".to_string(),
            inactive_dot: "".to_string(),
            arabic_format: "%d/%d".to_string(),
            keymap: PaginatorKeyMap::default(),
        }
    }
}

impl Model {
    /// Creates a new paginator model with default settings.
    ///
    /// This is equivalent to calling `Model::default()` but provides a more
    /// conventional constructor-style API.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let paginator = Model::new();
    /// assert_eq!(paginator.page, 0);
    /// assert_eq!(paginator.total_pages, 1);
    /// ```
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the total number of items and calculates total pages (builder pattern).
    ///
    /// This method automatically calculates the total number of pages based on
    /// the total items and the current `per_page` setting. If the current page
    /// becomes out of bounds, it will be adjusted to the last valid page.
    ///
    /// # Arguments
    ///
    /// * `items` - The total number of items to paginate
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let paginator = Model::new()
    ///     .with_per_page(10)
    ///     .with_total_items(95); // Will create 10 pages (95/10 = 9.5 -> 10)
    ///
    /// assert_eq!(paginator.total_pages, 10);
    /// ```
    pub fn with_total_items(mut self, items: usize) -> Self {
        self.set_total_items(items);
        self
    }

    /// Sets the number of items per page (builder pattern).
    ///
    /// The minimum value is 1; any value less than 1 will be clamped to 1.
    /// This setting affects how total pages are calculated when using
    /// `set_total_items()` or `with_total_items()`.
    ///
    /// # Arguments
    ///
    /// * `per_page` - Number of items to display per page (minimum 1)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let paginator = Model::new()
    ///     .with_per_page(25)
    ///     .with_total_items(100); // Will create 4 pages
    ///
    /// assert_eq!(paginator.per_page, 25);
    /// assert_eq!(paginator.total_pages, 4);
    ///
    /// // Values less than 1 are clamped to 1
    /// let clamped = Model::new().with_per_page(0);
    /// assert_eq!(clamped.per_page, 1);
    /// ```
    pub fn with_per_page(mut self, per_page: usize) -> Self {
        self.per_page = per_page.max(1);
        self
    }

    /// Sets the number of items per page (mutable version).
    ///
    /// The minimum value is 1; any value less than 1 will be clamped to 1.
    /// This method modifies the paginator in place.
    ///
    /// # Arguments
    ///
    /// * `per_page` - Number of items to display per page (minimum 1)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let mut paginator = Model::new();
    /// paginator.set_per_page(15);
    /// assert_eq!(paginator.per_page, 15);
    ///
    /// // Values less than 1 are clamped to 1
    /// paginator.set_per_page(0);
    /// assert_eq!(paginator.per_page, 1);
    /// ```
    pub fn set_per_page(&mut self, per_page: usize) {
        self.per_page = per_page.max(1);
    }

    /// Sets the active dot character for dots mode (builder pattern).
    ///
    /// # Arguments
    ///
    /// * `dot` - The character or styled string to use for the active page
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let paginator = Model::new().with_active_dot("●");
    /// assert_eq!(paginator.active_dot, "●");
    /// ```
    pub fn with_active_dot(mut self, dot: &str) -> Self {
        self.active_dot = dot.to_string();
        self
    }

    /// Sets the inactive dot character for dots mode (builder pattern).
    ///
    /// # Arguments
    ///
    /// * `dot` - The character or styled string to use for inactive pages
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let paginator = Model::new().with_inactive_dot("○");
    /// assert_eq!(paginator.inactive_dot, "○");
    /// ```
    pub fn with_inactive_dot(mut self, dot: &str) -> Self {
        self.inactive_dot = dot.to_string();
        self
    }

    /// Sets the active dot character for dots mode (mutable version).
    ///
    /// # Arguments
    ///
    /// * `dot` - The character or styled string to use for the active page
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let mut paginator = Model::new();
    /// paginator.set_active_dot("●");
    /// assert_eq!(paginator.active_dot, "●");
    /// ```
    pub fn set_active_dot(&mut self, dot: &str) {
        self.active_dot = dot.to_string();
    }

    /// Sets the inactive dot character for dots mode (mutable version).
    ///
    /// # Arguments
    ///
    /// * `dot` - The character or styled string to use for inactive pages
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let mut paginator = Model::new();
    /// paginator.set_inactive_dot("○");
    /// assert_eq!(paginator.inactive_dot, "○");
    /// ```
    pub fn set_inactive_dot(&mut self, dot: &str) {
        self.inactive_dot = dot.to_string();
    }

    /// Sets the total number of pages directly.
    ///
    /// The minimum value is 1; any value less than 1 will be clamped to 1.
    /// If the current page becomes out of bounds after setting the total pages,
    /// it will be adjusted to the last valid page.
    ///
    /// **Note**: This method sets pages directly. If you want to calculate pages
    /// based on total items, use `set_total_items()` instead.
    ///
    /// # Arguments
    ///
    /// * `pages` - The total number of pages (minimum 1)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let mut paginator = Model::new();
    /// paginator.set_total_pages(10);
    /// assert_eq!(paginator.total_pages, 10);
    ///
    /// // If current page is out of bounds, it gets adjusted
    /// paginator.page = 15; // Out of bounds
    /// paginator.set_total_pages(5);
    /// assert_eq!(paginator.page, 4); // Adjusted to last page (0-indexed)
    /// ```
    pub fn set_total_pages(&mut self, pages: usize) {
        self.total_pages = pages.max(1);
        // Ensure the current page is not out of bounds
        if self.page >= self.total_pages {
            self.page = self.total_pages.saturating_sub(1);
        }
    }

    /// Calculates and sets the total number of pages based on the total items.
    ///
    /// This method divides the total number of items by the current `per_page`
    /// setting to calculate the total pages. The result is always at least 1,
    /// even for 0 items. If the current page becomes out of bounds after
    /// recalculation, it will be adjusted to the last valid page.
    ///
    /// # Arguments
    ///
    /// * `items` - The total number of items to paginate
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let mut paginator = Model::new().with_per_page(10);
    ///
    /// // 95 items with 10 per page = 10 pages (95/10 = 9.5 -> 10)
    /// paginator.set_total_items(95);
    /// assert_eq!(paginator.total_pages, 10);
    ///
    /// // 0 items still results in 1 page minimum
    /// paginator.set_total_items(0);
    /// assert_eq!(paginator.total_pages, 1);
    ///
    /// // Exact division
    /// paginator.set_total_items(100);
    /// assert_eq!(paginator.total_pages, 10);
    /// ```
    pub fn set_total_items(&mut self, items: usize) {
        if items == 0 {
            self.total_pages = 1;
        } else {
            self.total_pages = items.div_ceil(self.per_page);
        }

        // Ensure the current page is not out of bounds
        if self.page >= self.total_pages {
            self.page = self.total_pages.saturating_sub(1);
        }
    }

    /// Returns the number of items on the current page.
    ///
    /// This method calculates how many items are actually present on the
    /// current page, which may be less than `per_page` on the last page
    /// or when there are fewer total items than `per_page`.
    ///
    /// # Arguments
    ///
    /// * `total_items` - The total number of items being paginated
    ///
    /// # Returns
    ///
    /// The number of items on the current page, or 0 if there are no items.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let mut paginator = Model::new().with_per_page(10);
    ///
    /// // Full page
    /// assert_eq!(paginator.items_on_page(100), 10);
    ///
    /// // Partial last page
    /// paginator.page = 9; // Last page (0-indexed)
    /// assert_eq!(paginator.items_on_page(95), 5); // Only 5 items on page 10
    ///
    /// // No items
    /// assert_eq!(paginator.items_on_page(0), 0);
    /// ```
    pub fn items_on_page(&self, total_items: usize) -> usize {
        if total_items == 0 {
            return 0;
        }
        let (start, end) = self.get_slice_bounds(total_items);
        end - start
    }

    /// Calculates slice bounds for the current page.
    ///
    /// This is a helper function for paginating slices. Given the total length
    /// of your data, it returns the start and end indices for the current page.
    /// The returned bounds can be used directly with slice notation.
    ///
    /// # Arguments
    ///
    /// * `length` - The total length of the data being paginated
    ///
    /// # Returns
    ///
    /// A tuple `(start, end)` where:
    /// - `start` is the inclusive start index for the current page
    /// - `end` is the exclusive end index for the current page
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let items: Vec<i32> = (1..=100).collect();
    /// let mut paginator = Model::new().with_per_page(10);
    ///
    /// // First page (0)
    /// let (start, end) = paginator.get_slice_bounds(items.len());
    /// assert_eq!((start, end), (0, 10));
    /// let page_items = &items[start..end]; // Items 1-10
    ///
    /// // Third page (2)
    /// paginator.page = 2;
    /// let (start, end) = paginator.get_slice_bounds(items.len());
    /// assert_eq!((start, end), (20, 30));
    /// let page_items = &items[start..end]; // Items 21-30
    /// ```
    pub fn get_slice_bounds(&self, length: usize) -> (usize, usize) {
        let start = self.page * self.per_page;
        let end = (start + self.per_page).min(length);
        (start, end)
    }

    /// Returns slice bounds assuming maximum possible data length.
    ///
    /// This is a convenience method that calls `get_slice_bounds()` with
    /// the maximum possible data length (`per_page * total_pages`). It's
    /// useful when you know your data exactly fills the pagination structure.
    ///
    /// # Returns
    ///
    /// A tuple `(start, end)` representing slice bounds for the current page.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let mut paginator = Model::new()
    ///     .with_per_page(10)
    ///     .with_total_items(100); // Exactly 10 pages
    ///
    /// paginator.page = 3;
    /// let (start, end) = paginator.start_index_end_index();
    /// assert_eq!((start, end), (30, 40));
    /// ```
    pub fn start_index_end_index(&self) -> (usize, usize) {
        self.get_slice_bounds(self.per_page * self.total_pages)
    }

    /// Navigates to the previous page.
    ///
    /// If the paginator is already on the first page (page 0), this method
    /// has no effect. The page number will not go below 0.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let mut paginator = Model::new().with_per_page(10).with_total_items(100);
    /// paginator.page = 5;
    ///
    /// paginator.prev_page();
    /// assert_eq!(paginator.page, 4);
    ///
    /// // Won't go below 0
    /// paginator.page = 0;
    /// paginator.prev_page();
    /// assert_eq!(paginator.page, 0);
    /// ```
    pub fn prev_page(&mut self) {
        if self.page > 0 {
            self.page -= 1;
        }
    }

    /// Navigates to the next page.
    ///
    /// If the paginator is already on the last page, this method has no effect.
    /// The page number will not exceed `total_pages - 1`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let mut paginator = Model::new().with_per_page(10).with_total_items(100);
    /// // total_pages = 10, so last page is 9 (0-indexed)
    ///
    /// paginator.page = 5;
    /// paginator.next_page();
    /// assert_eq!(paginator.page, 6);
    ///
    /// // Won't go beyond last page  
    /// paginator.page = 8; // Second to last page
    /// paginator.next_page();
    /// assert_eq!(paginator.page, 9); // Should go to last page (9 is the last valid page)
    /// paginator.next_page();
    /// assert_eq!(paginator.page, 9); // Should stay at last page
    /// ```
    pub fn next_page(&mut self) {
        if !self.on_last_page() {
            self.page += 1;
        }
    }

    /// Returns true if the paginator is on the first page.
    ///
    /// The first page is always page 0 in the 0-indexed pagination system.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let mut paginator = Model::new().with_per_page(10).with_total_items(100);
    ///
    /// assert!(paginator.on_first_page());
    ///
    /// paginator.next_page();
    /// assert!(!paginator.on_first_page());
    /// ```
    pub fn on_first_page(&self) -> bool {
        self.page == 0
    }

    /// Returns true if the paginator is on the last page.
    ///
    /// The last page is `total_pages - 1` in the 0-indexed pagination system.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model;
    ///
    /// let mut paginator = Model::new().with_per_page(10).with_total_items(90);
    /// // Creates 9 pages (0-8), so last page is 8
    ///
    /// assert!(!paginator.on_last_page());
    ///
    /// paginator.page = 8; // Last page  
    /// assert!(paginator.on_last_page());
    /// ```
    pub fn on_last_page(&self) -> bool {
        self.page == self.total_pages.saturating_sub(1)
    }

    /// Updates the paginator based on received messages.
    ///
    /// This method should be called from your application's `update()` method
    /// to handle pagination key presses. It automatically responds to the
    /// configured key bindings for next/previous page navigation.
    ///
    /// # Arguments
    ///
    /// * `msg` - The message to process, typically containing key press events
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::Model as Paginator;
    /// use bubbletea_rs::{Model, Msg};
    ///
    /// struct App {
    ///     paginator: Paginator,
    /// }
    ///
    /// impl Model for App {
    ///     fn update(&mut self, msg: Msg) -> Option<bubbletea_rs::Cmd> {
    ///         // Forward messages to paginator
    ///         self.paginator.update(&msg);
    ///         None
    ///     }
    ///     
    ///     // ... other methods
    /// #   fn init() -> (Self, Option<bubbletea_rs::Cmd>) { (Self { paginator: Paginator::new() }, None) }
    /// #   fn view(&self) -> String { String::new() }
    /// }
    /// ```
    pub fn update(&mut self, msg: &Msg) {
        if let Some(key_msg) = msg.downcast_ref::<KeyMsg>() {
            if self.keymap.next_page.matches(key_msg) {
                self.next_page();
            } else if self.keymap.prev_page.matches(key_msg) {
                self.prev_page();
            }
        }
    }

    /// Renders the paginator as a string.
    ///
    /// The output format depends on the `paginator_type` setting:
    /// - **Arabic**: Shows "current/total" (e.g., "3/10")
    /// - **Dots**: Shows dots with active page highlighted (e.g., "○ ○ ● ○ ○")
    ///
    /// # Returns
    ///
    /// A string representation of the current pagination state.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::paginator::{Model, Type};
    ///
    /// let mut paginator = Model::new().with_per_page(10).with_total_items(50);
    /// // Creates 5 pages, currently on page 0
    ///
    /// // Arabic mode (default)
    /// paginator.paginator_type = Type::Arabic;
    /// assert_eq!(paginator.view(), "1/5"); // 1-indexed for display
    ///
    /// // Dots mode  
    /// paginator.paginator_type = Type::Dots;
    /// assert_eq!(paginator.view(), "•○○○○"); // Active page shows filled bullet, others are hollow
    ///
    /// // Move to page 2
    /// paginator.page = 2;
    /// assert_eq!(paginator.view(), "○○•○○"); // Third bullet filled (active page), others hollow
    /// ```
    pub fn view(&self) -> String {
        match self.paginator_type {
            Type::Arabic => self.arabic_view(),
            Type::Dots => self.dots_view(),
        }
    }

    fn arabic_view(&self) -> String {
        self.arabic_format
            .replacen("%d", &(self.page + 1).to_string(), 1)
            .replacen("%d", &self.total_pages.to_string(), 1)
    }

    fn dots_view(&self) -> String {
        let mut s = String::new();
        for i in 0..self.total_pages {
            if i == self.page {
                s.push_str(&self.active_dot);
            } else {
                s.push_str(&self.inactive_dot);
            }
            // Remove spacing between dots to match Go version (••••)
            // Go version shows compact dots without spaces
        }
        s
    }
}