altui-core 0.2.0

A library to build rich terminal user interfaces or dashboards
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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
// Copyright (c) 2023 The Ratatui Developers

use crate::{
    buffer::Buffer,
    layout::Rect,
    style::Style,
    symbols::{block::FULL, line},
    widgets::Widget,
};

/// Scrollbar Set
/// ```text
/// <--▮------->
/// ^  ^   ^   ^
/// │  │   │   └ end
/// │  │   └──── track
/// │  └──────── thumb
/// └─────────── begin
/// ```
#[derive(Debug, Clone)]
pub struct Set {
    pub track: &'static str,
    pub thumb: &'static str,
    pub begin: &'static str,
    pub end: &'static str,
}

pub const DOUBLE_VERTICAL: Set = Set {
    track: line::DOUBLE_VERTICAL,
    thumb: FULL,
    begin: "",
    end: "",
};

pub const DOUBLE_HORIZONTAL: Set = Set {
    track: line::DOUBLE_HORIZONTAL,
    thumb: FULL,
    begin: "",
    end: "",
};

pub const VERTICAL: Set = Set {
    track: line::VERTICAL,
    thumb: FULL,
    begin: "",
    end: "",
};

pub const HORIZONTAL: Set = Set {
    track: line::HORIZONTAL,
    thumb: FULL,
    begin: "",
    end: "",
};

/// An enum representing the direction of scrolling in a Scrollbar widget.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ScrollDirection {
    /// Forward scroll direction, usually corresponds to scrolling downwards or rightwards.
    #[default]
    Forward,
    /// Backward scroll direction, usually corresponds to scrolling upwards or leftwards.
    Backward,
}

/// Scrollbar Orientation
#[derive(Default, Debug, Clone, Copy)]
pub enum ScrollbarOrientation {
    #[default]
    VerticalRight,
    VerticalLeft,
    HorizontalBottom,
    HorizontalTop,
}

/// Scrollbar widget for altui-core library.
///
/// This widget can be used to display a scrollbar in a terminal user interface.
/// The following components of the scrollbar are customizable in symbol and style.
///
/// ```text
/// <--▮------->
/// ^  ^   ^   ^
/// │  │   │   └ end
/// │  │   └──── track
/// │  └──────── thumb
/// └─────────── begin
/// ```
#[derive(Debug, Clone)]
pub struct Scrollbar<'a> {
    orientation: ScrollbarOrientation,
    thumb_style: Style,
    thumb_symbol: &'a str,
    track_style: Style,
    track_symbol: &'a str,
    begin_symbol: Option<&'a str>,
    begin_style: Style,
    end_symbol: Option<&'a str>,
    end_style: Style,
    // The current position within the scrollable content.
    position: u16,
    // The current offset added to the Rect size.
    offset: u16,
    // The total length of the scrollable content.
    content_length: u16,
    // The length of content in current viewport.
    viewport_content_length: u16,
}

impl<'a> Default for Scrollbar<'a> {
    fn default() -> Self {
        Self {
            orientation: ScrollbarOrientation::default(),
            thumb_symbol: DOUBLE_VERTICAL.thumb,
            thumb_style: Style::default(),
            track_symbol: DOUBLE_VERTICAL.track,
            track_style: Style::default(),
            begin_symbol: Some(DOUBLE_VERTICAL.begin),
            begin_style: Style::default(),
            end_symbol: Some(DOUBLE_VERTICAL.end),
            end_style: Style::default(),
            position: Default::default(),
            offset: Default::default(),
            content_length: Default::default(),
            viewport_content_length: Default::default(),
        }
    }
}

impl<'a> Scrollbar<'a> {
    pub fn new(orientation: ScrollbarOrientation) -> Self {
        Self::default().orientation(orientation)
    }

    /// Sets the orientation of the scrollbar.
    /// Resets the symbols to [`DOUBLE_VERTICAL`] or [`DOUBLE_HORIZONTAL`] based on orientation
    pub fn orientation(mut self, orientation: ScrollbarOrientation) -> Self {
        self.orientation = orientation;
        let set = if self.is_vertical() {
            DOUBLE_VERTICAL
        } else {
            DOUBLE_HORIZONTAL
        };
        self.symbols(set)
    }

    /// Shows scrollbar orientation
    pub fn show_orientation(&self) -> ScrollbarOrientation {
        self.orientation
    }

    /// Sets the orientation and symbols for the scrollbar from a [`Set`].
    pub fn orientation_and_symbol(mut self, orientation: ScrollbarOrientation, set: Set) -> Self {
        self.orientation = orientation;
        self.symbols(set)
    }

    /// Sets the symbol that represents the thumb of the scrollbar.
    pub fn thumb_symbol(mut self, thumb_symbol: &'a str) -> Self {
        self.thumb_symbol = thumb_symbol;
        self
    }

    /// Sets the style that represents the thumb of the scrollbar.
    pub fn thumb_style(mut self, thumb_style: Style) -> Self {
        self.thumb_style = thumb_style;
        self
    }

    /// Sets the symbol that represents the track of the scrollbar.
    pub fn track_symbol(mut self, track_symbol: &'a str) -> Self {
        self.track_symbol = track_symbol;
        self
    }

    /// Sets the style that is used for the track of the scrollbar.
    pub fn track_style(mut self, track_style: Style) -> Self {
        self.track_style = track_style;
        self
    }

    /// Sets the symbol that represents the beginning of the scrollbar.
    pub fn begin_symbol(mut self, begin_symbol: Option<&'a str>) -> Self {
        self.begin_symbol = begin_symbol;
        self
    }

    /// Sets the style that is used for the beginning of the scrollbar.
    pub fn begin_style(mut self, begin_style: Style) -> Self {
        self.begin_style = begin_style;
        self
    }

    /// Sets the symbol that represents the end of the scrollbar.
    pub fn end_symbol(mut self, end_symbol: Option<&'a str>) -> Self {
        self.end_symbol = end_symbol;
        self
    }

    /// Sets the style that is used for the end of the scrollbar.
    pub fn end_style(mut self, end_style: Style) -> Self {
        self.end_style = end_style;
        self
    }

    /// Sets the symbols used for the various parts of the scrollbar from a [`Set`].
    ///
    /// ```text
    /// <--▮------->
    /// ^  ^   ^   ^
    /// │  │   │   └ end
    /// │  │   └──── track
    /// │  └──────── thumb
    /// └─────────── begin
    /// ```
    ///
    /// Only sets begin_symbol and end_symbol if they already contain a value.
    /// If begin_symbol and/or end_symbol were set to `None` explicitly, this function will respect
    /// that choice.
    pub fn symbols(mut self, symbol: Set) -> Self {
        self.track_symbol = symbol.track;
        self.thumb_symbol = symbol.thumb;
        if self.begin_symbol.is_some() {
            self.begin_symbol = Some(symbol.begin);
        }
        if self.end_symbol.is_some() {
            self.end_symbol = Some(symbol.end);
        }
        self
    }

    /// Sets the style used for the various parts of the scrollbar from a [`Style`].
    /// ```text
    /// <--▮------->
    /// ^  ^   ^   ^
    /// │  │   │   └ end
    /// │  │   └──── track
    /// │  └──────── thumb
    /// └─────────── begin
    /// ```
    pub fn style(mut self, style: Style) -> Self {
        self.track_style = style;
        self.thumb_style = style;
        self.begin_style = style;
        self.end_style = style;
        self
    }

    fn is_vertical(&self) -> bool {
        match self.orientation {
            ScrollbarOrientation::VerticalRight | ScrollbarOrientation::VerticalLeft => true,
            ScrollbarOrientation::HorizontalBottom | ScrollbarOrientation::HorizontalTop => false,
        }
    }

    fn get_track_area(&self, area: Rect) -> Rect {
        // Decrease track area if a begin arrow is present
        let area = if self.begin_symbol.is_some() {
            if self.is_vertical() {
                // For vertical scrollbar, reduce the height by one
                Rect::new(
                    area.x,
                    area.y + 1,
                    area.width,
                    area.height.saturating_sub(1),
                )
            } else {
                // For horizontal scrollbar, reduce the width by one
                Rect::new(
                    area.x + 1,
                    area.y,
                    area.width.saturating_sub(1),
                    area.height,
                )
            }
        } else {
            area
        };
        // Further decrease scrollbar area if an end arrow is present
        if self.end_symbol.is_some() {
            if self.is_vertical() {
                // For vertical scrollbar, reduce the height by one
                Rect::new(area.x, area.y, area.width, area.height.saturating_sub(1))
            } else {
                // For horizontal scrollbar, reduce the width by one
                Rect::new(area.x, area.y, area.width.saturating_sub(1), area.height)
            }
        } else {
            area
        }
    }

    fn should_not_render(&self, track_start: u16, track_end: u16, content_length: u16) -> bool {
        if track_end - track_start == 0 || content_length == 0 {
            return true;
        }
        false
    }

    fn get_track_start_end(&self, area: Rect) -> (u16, u16, u16) {
        match self.orientation {
            ScrollbarOrientation::VerticalRight => {
                (area.top(), area.bottom(), area.right().saturating_sub(1))
            }
            ScrollbarOrientation::VerticalLeft => (area.top(), area.bottom(), area.left()),
            ScrollbarOrientation::HorizontalBottom => {
                (area.left(), area.right(), area.bottom().saturating_sub(1))
            }
            ScrollbarOrientation::HorizontalTop => (area.left(), area.right(), area.top()),
        }
    }

    /// Calculate the starting and ending position of a scrollbar thumb.
    ///
    /// The scrollbar thumb's position and size are determined based on the current state of the
    /// scrollbar, and the dimensions of the scrollbar track.
    ///
    /// This function returns a tuple `(thumb_start, thumb_end)` where `thumb_start` is the position
    /// at which the scrollbar thumb begins, and `thumb_end` is the position at which the
    /// scrollbar thumb ends.
    ///
    /// The size of the thumb (i.e., `thumb_end - thumb_start`) is proportional to the ratio of the
    /// viewport content length to the total content length.
    ///
    /// The position of the thumb (i.e., `thumb_start`) is proportional to the ratio of the current
    /// scroll position to the total content length.
    fn get_thumb_start_end(&self, track_start_end: (u16, u16)) -> (u16, u16) {
        // let (track_start, track_end) = track_start_end;
        // let track_size = track_end - track_start;
        // let thumb_size =
        //     ((state.viewport_content_length / state.content_length) * track_size).max(1);
        // let thumb_start = (state.position / state.content_length) *
        //                    state.viewport_content_length;
        // let thumb_end = thumb_size + thumb_start;
        // (thumb_start, thumb_end)

        let (track_start, track_end) = track_start_end;

        let viewport_content_length = if self.viewport_content_length == 0 {
            track_end - track_start
        } else {
            self.viewport_content_length
        };

        let scroll_position_ratio = if self.offset == 0 {
            self.position as f64 / self.content_length as f64
        } else {
            let gap = f64::from(self.content_length.saturating_sub(viewport_content_length));
            if gap != 0.0 {
                f64::from(self.offset) / gap
            } else {
                0.0
            }
        }
        .min(1.0);

        let thumb_size = (((viewport_content_length as f64 / self.content_length as f64)
            * (track_end - track_start) as f64)
            .round() as u16)
            .max(1);

        let track_size = (track_end - track_start).saturating_sub(thumb_size);

        let thumb_start = track_start + (scroll_position_ratio * track_size as f64).round() as u16;

        let thumb_end = thumb_start + thumb_size;

        (thumb_start, thumb_end)
    }

    /// Sets the scroll position of the scrollbar and returns the modified ScrollbarState.
    pub fn offset(&mut self, offset: u16) {
        self.offset = offset;
    }

    /// Sets the scroll position of the scrollbar and returns the modified ScrollbarState.
    pub fn position(&mut self, position: u16) {
        self.position = position;
    }

    /// Sets the length of the scrollable content and returns the modified ScrollbarState.
    pub fn content_length(&mut self, content_length: u16) {
        self.content_length = content_length;
    }

    /// Sets the length of the viewport content and returns the modified ScrollbarState.
    pub fn viewport_content_length(&mut self, viewport_content_length: u16) {
        self.viewport_content_length = viewport_content_length;
    }

    /// Decrements the scroll position by one, ensuring it doesn't go below zero.
    pub fn prev(&mut self) {
        self.position = self.position.saturating_sub(1);
    }

    /// Increments the scroll position by one, ensuring it doesn't exceed the length of the content.
    pub fn next(&mut self) {
        self.position = self
            .position
            .saturating_add(1)
            .clamp(0, self.content_length.saturating_sub(1))
    }

    /// Sets the scroll position to the start of the scrollable content.
    pub fn first(&mut self) {
        self.position = 0;
    }

    /// Sets the scroll position to the end of the scrollable content.
    pub fn last(&mut self) {
        self.position = self.content_length.saturating_sub(1)
    }

    /// Changes the scroll position based on the provided ScrollDirection.
    pub fn scroll(&mut self, direction: ScrollDirection) {
        match direction {
            ScrollDirection::Forward => {
                self.next();
            }
            ScrollDirection::Backward => {
                self.prev();
            }
        }
    }
}

impl<'a> Widget for Scrollbar<'a> {
    fn render(&mut self, area: Rect, buf: &mut Buffer) {
        //
        // For ScrollbarOrientation::VerticalRight
        //
        //                   ┌───────── track_axis  (x)
        //                   v
        //   ┌───────────────┐
        //   │               ║<──────── track_start (y1)
        //   │               █
        //   │               █
        //   │               ║
        //   │               ║<──────── track_end   (y2)
        //   └───────────────┘
        //
        // For ScrollbarOrientation::HorizontalBottom
        //
        //   ┌───────────────┐
        //   │               │
        //   │               │
        //   │               │
        //   └═══███═════════┘<──────── track_axis  (y)
        //    ^             ^
        //    │             └────────── track_end   (x2)
        //        //    └──────────────────────── track_start (x1)
        //

        // Find track_start, track_end, and track_axis
        let area = self.get_track_area(area);
        let (track_start, track_end, track_axis) = self.get_track_start_end(area);

        if self.should_not_render(track_start, track_end, self.content_length) {
            return;
        }

        let (thumb_start, thumb_end) = self.get_thumb_start_end((track_start, track_end));

        for i in track_start..track_end {
            let (style, symbol) = if i >= thumb_start && i < thumb_end {
                (self.thumb_style, self.thumb_symbol)
            } else {
                (self.track_style, self.track_symbol)
            };

            if self.is_vertical() {
                buf.set_string(track_axis, i, symbol, style);
            } else {
                buf.set_string(i, track_axis, symbol, style);
            }
        }

        if let Some(s) = self.begin_symbol {
            if self.is_vertical() {
                buf.set_string(track_axis, track_start - 1, s, self.begin_style);
            } else {
                buf.set_string(track_start - 1, track_axis, s, self.begin_style);
            }
        };
        if let Some(s) = self.end_symbol {
            if self.is_vertical() {
                buf.set_string(track_axis, track_end, s, self.end_style);
            } else {
                buf.set_string(track_end, track_axis, s, self.end_style);
            }
        }
    }
}

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

    #[test]
    fn test_no_render_when_area_zero() {
        let mut buffer = Buffer::empty(Rect::new(0, 0, 0, 0));
        let mut scrollbar = Scrollbar::default();
        scrollbar.position(0);
        scrollbar.content_length(1);
        scrollbar.render(buffer.area, &mut buffer);
        assert_buffer_eq!(buffer, Buffer::empty(buffer.area));
    }

    #[test]
    fn test_no_render_when_height_zero_with_without_arrows() {
        let mut buffer = Buffer::empty(Rect::new(0, 0, 3, 0));
        let mut scrollbar = Scrollbar::default();
        scrollbar.position(0);
        scrollbar.content_length(1);
        scrollbar.render(buffer.area, &mut buffer);
        assert_buffer_eq!(buffer, Buffer::empty(buffer.area));

        let mut buffer = Buffer::empty(Rect::new(0, 0, 3, 0));
        let mut scrollbar = Scrollbar::default().begin_symbol(None).end_symbol(None);
        scrollbar.position(0);
        scrollbar.content_length(1);
        scrollbar.render(buffer.area, &mut buffer);
        assert_buffer_eq!(buffer, Buffer::empty(buffer.area));
    }

    #[test]
    fn test_no_render_when_height_too_small_for_arrows() {
        let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 2));
        let mut scrollbar = Scrollbar::default();
        scrollbar.position(0);
        scrollbar.content_length(1);
        scrollbar.render(buffer.area, &mut buffer);
        assert_buffer_eq!(buffer, Buffer::with_lines(vec!["    ", "    "]));
    }

    #[test]
    fn test_renders_all_thumbs_at_minimum_height_without_arrows() {
        let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 2));
        let mut scrollbar = Scrollbar::default().begin_symbol(None).end_symbol(None);
        scrollbar.position(0);
        scrollbar.content_length(1);
        scrollbar.render(buffer.area, &mut buffer);
        assert_buffer_eq!(buffer, Buffer::with_lines(vec!["", ""]));
    }

    #[test]
    fn test_renders_all_thumbs_at_minimum_height_and_minimum_width_without_arrows() {
        let mut buffer = Buffer::empty(Rect::new(0, 0, 1, 2));
        let mut scrollbar = Scrollbar::default().begin_symbol(None).end_symbol(None);
        scrollbar.position(0);
        scrollbar.content_length(1);
        scrollbar.render(buffer.area, &mut buffer);
        assert_buffer_eq!(buffer, Buffer::with_lines(vec!["", ""]));
    }

    #[test]
    fn test_renders_two_arrows_one_thumb_at_minimum_height_with_arrows() {
        let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 3));
        let mut scrollbar = Scrollbar::default();
        scrollbar.position(0);
        scrollbar.content_length(1);
        scrollbar.render(buffer.area, &mut buffer);
        assert_buffer_eq!(buffer, Buffer::with_lines(vec!["", "", ""]));
    }

    #[test]
    fn test_no_render_when_content_length_zero() {
        let mut buffer = Buffer::empty(Rect::new(0, 0, 2, 2));
        let mut scrollbar = Scrollbar::default();
        scrollbar.position(0);
        scrollbar.content_length(0);
        scrollbar.render(buffer.area, &mut buffer);
        assert_buffer_eq!(buffer, Buffer::with_lines(vec!["  ", "  "]));
    }

    #[test]
    fn test_renders_all_thumbs_when_height_equals_content_length() {
        let mut buffer = Buffer::empty(Rect::new(0, 0, 2, 2));
        let mut scrollbar = Scrollbar::default().begin_symbol(None).end_symbol(None);
        scrollbar.position(0);
        scrollbar.content_length(2);
        scrollbar.render(buffer.area, &mut buffer);
        assert_buffer_eq!(buffer, Buffer::with_lines(vec!["", ""]));

        let mut buffer = Buffer::empty(Rect::new(0, 0, 2, 8));
        let mut scrollbar = Scrollbar::default().begin_symbol(None).end_symbol(None);
        scrollbar.position(0);
        scrollbar.content_length(8);
        scrollbar.render(buffer.area, &mut buffer);
        assert_buffer_eq!(
            buffer,
            Buffer::with_lines(vec!["", "", "", "", "", "", "", ""])
        );
    }

    #[test]
    fn test_renders_single_vertical_thumb_when_content_length_square_of_height() {
        for i in 0..=17 {
            let mut buffer = Buffer::empty(Rect::new(0, 0, 2, 4));
            let mut scrollbar = Scrollbar::default().begin_symbol(None).end_symbol(None);
            scrollbar.position(i);
            scrollbar.content_length(16);
            scrollbar.render(buffer.area, &mut buffer);
            let expected = if i <= 2 {
                vec!["", "", "", ""]
            } else if i <= 7 {
                vec!["", "", "", ""]
            } else if i <= 13 {
                vec!["", "", "", ""]
            } else {
                vec!["", "", "", ""]
            };
            assert_buffer_eq!(buffer, Buffer::with_lines(expected.clone()));
        }
    }

    #[test]
    fn test_renders_single_horizontal_thumb_when_content_length_square_of_width() {
        for i in 0..=17 {
            let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 2));
            let mut scrollbar = Scrollbar::default()
                .begin_symbol(None)
                .end_symbol(None)
                .orientation(ScrollbarOrientation::HorizontalBottom);
            scrollbar.position(i);
            scrollbar.content_length(16);
            scrollbar.render(buffer.area, &mut buffer);
            let expected = if i <= 2 {
                vec!["    ", "█═══"]
            } else if i <= 7 {
                vec!["    ", "═█══"]
            } else if i <= 13 {
                vec!["    ", "══█═"]
            } else {
                vec!["    ", "═══█"]
            };
            assert_buffer_eq!(buffer, Buffer::with_lines(expected.clone()));
        }
    }

    #[test]
    fn test_renders_one_thumb_for_large_content_relative_to_height() {
        let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 2));
        let mut scrollbar = Scrollbar::default()
            .begin_symbol(None)
            .end_symbol(None)
            .orientation(ScrollbarOrientation::HorizontalBottom);
        scrollbar.position(0);
        scrollbar.content_length(1600);
        scrollbar.render(buffer.area, &mut buffer);
        let expected = vec!["    ", "█═══"];
        assert_buffer_eq!(buffer, Buffer::with_lines(expected.clone()));

        let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 2));
        let mut scrollbar = Scrollbar::default()
            .begin_symbol(None)
            .end_symbol(None)
            .orientation(ScrollbarOrientation::HorizontalBottom);
        scrollbar.position(800);
        scrollbar.content_length(1600);
        scrollbar.render(buffer.area, &mut buffer);
        let expected = vec!["    ", "══█═"];
        assert_buffer_eq!(buffer, Buffer::with_lines(expected.clone()));
    }

    #[test]
    fn test_renders_two_thumb_default_symbols_for_content_double_height() {
        for i in 0..=7 {
            let mut buffer = Buffer::empty(Rect::new(0, 0, 2, 4));
            let mut scrollbar = Scrollbar::default().begin_symbol(None).end_symbol(None);
            scrollbar.position(i);
            scrollbar.content_length(8);
            scrollbar.render(buffer.area, &mut buffer);
            let expected = if i <= 1 {
                vec!["", "", "", ""]
            } else if i <= 5 {
                vec!["", "", "", ""]
            } else {
                vec!["", "", "", ""]
            };
            assert_buffer_eq!(buffer, Buffer::with_lines(expected.clone()));
        }
    }

    #[test]
    fn test_renders_two_thumb_custom_symbols_for_content_double_height() {
        for i in 0..=7 {
            let mut buffer = Buffer::empty(Rect::new(0, 0, 2, 4));
            let mut scrollbar = Scrollbar::default()
                .symbols(VERTICAL)
                .begin_symbol(None)
                .end_symbol(None);
            scrollbar.position(i);
            scrollbar.content_length(8);
            scrollbar.render(buffer.area, &mut buffer);
            let expected = if i <= 1 {
                vec!["", "", "", ""]
            } else if i <= 5 {
                vec!["", "", "", ""]
            } else {
                vec!["", "", "", ""]
            };
            assert_buffer_eq!(buffer, Buffer::with_lines(expected.clone()));
        }
    }

    #[test]
    fn test_renders_two_thumb_default_symbols_for_content_double_width() {
        for i in 0..=7 {
            let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 2));
            let mut scrollbar = Scrollbar::default()
                .orientation(ScrollbarOrientation::HorizontalBottom)
                .begin_symbol(None)
                .end_symbol(None);
            scrollbar.position(i);
            scrollbar.content_length(8);
            scrollbar.render(buffer.area, &mut buffer);
            let expected = if i <= 1 {
                vec!["    ", "██══"]
            } else if i <= 5 {
                vec!["    ", "═██═"]
            } else {
                vec!["    ", "══██"]
            };
            assert_buffer_eq!(buffer, Buffer::with_lines(expected.clone()));
        }
    }

    #[test]
    fn test_renders_two_thumb_custom_symbols_for_content_double_width() {
        for i in 0..=7 {
            let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 2));
            let mut scrollbar = Scrollbar::default()
                .orientation(ScrollbarOrientation::HorizontalBottom)
                .symbols(HORIZONTAL)
                .begin_symbol(None)
                .end_symbol(None);
            scrollbar.position(i);
            scrollbar.content_length(8);
            scrollbar.render(buffer.area, &mut buffer);
            let expected = if i <= 1 {
                vec!["    ", "██──"]
            } else if i <= 5 {
                vec!["    ", "─██─"]
            } else {
                vec!["    ", "──██"]
            };
            assert_buffer_eq!(buffer, Buffer::with_lines(expected.clone()));
        }
    }

    #[test]
    fn test_rendering_viewport_content_length() {
        for i in 0..=16 {
            let mut buffer = Buffer::empty(Rect::new(0, 0, 8, 2));
            let mut scrollbar = Scrollbar::default()
                .orientation(ScrollbarOrientation::HorizontalBottom)
                .begin_symbol(Some(DOUBLE_HORIZONTAL.begin))
                .end_symbol(Some(DOUBLE_HORIZONTAL.end));
            scrollbar.position(i);
            scrollbar.content_length(16);
            scrollbar.viewport_content_length(4);
            scrollbar.render(buffer.area, &mut buffer);
            let expected = if i <= 1 {
                vec!["        ", "◄██════►"]
            } else if i <= 5 {
                vec!["        ", "◄═██═══►"]
            } else if i <= 9 {
                vec!["        ", "◄══██══►"]
            } else if i <= 13 {
                vec!["        ", "◄═══██═►"]
            } else {
                vec!["        ", "◄════██►"]
            };
            assert_buffer_eq!(buffer, Buffer::with_lines(expected.clone()));
        }

        for i in 0..=16 {
            let mut buffer = Buffer::empty(Rect::new(0, 0, 8, 2));
            let mut scrollbar = Scrollbar::default()
                .orientation(ScrollbarOrientation::HorizontalBottom)
                .begin_symbol(Some(DOUBLE_HORIZONTAL.begin))
                .end_symbol(Some(DOUBLE_HORIZONTAL.end));
            scrollbar.position(i);
            scrollbar.content_length(16);
            scrollbar.viewport_content_length(1);
            scrollbar.render(buffer.area, &mut buffer);
            dbg!(i);
            let expected = if i <= 1 {
                vec!["        ", "◄█═════►"]
            } else if i <= 4 {
                vec!["        ", "◄═█════►"]
            } else if i <= 7 {
                vec!["        ", "◄══█═══►"]
            } else if i <= 11 {
                vec!["        ", "◄═══█══►"]
            } else if i <= 14 {
                vec!["        ", "◄════█═►"]
            } else {
                vec!["        ", "◄═════█►"]
            };
            assert_buffer_eq!(buffer, Buffer::with_lines(expected.clone()));
        }
    }

    #[test]
    fn test_rendering_begin_end_arrows_horizontal_bottom() {
        for i in 0..=16 {
            let mut buffer = Buffer::empty(Rect::new(0, 0, 8, 2));
            let mut scrollbar = Scrollbar::default()
                .orientation(ScrollbarOrientation::HorizontalBottom)
                .begin_symbol(Some(DOUBLE_HORIZONTAL.begin))
                .end_symbol(Some(DOUBLE_HORIZONTAL.end));
            scrollbar.position(i);
            scrollbar.content_length(16);
            scrollbar.render(buffer.area, &mut buffer);
            let expected = if i <= 1 {
                vec!["        ", "◄██════►"]
            } else if i <= 5 {
                vec!["        ", "◄═██═══►"]
            } else if i <= 9 {
                vec!["        ", "◄══██══►"]
            } else if i <= 13 {
                vec!["        ", "◄═══██═►"]
            } else {
                vec!["        ", "◄════██►"]
            };
            assert_buffer_eq!(buffer, Buffer::with_lines(expected.clone()));
        }
    }

    #[test]
    fn test_rendering_begin_end_arrows_horizontal_top() {
        for i in 0..=16 {
            let mut buffer = Buffer::empty(Rect::new(0, 0, 8, 2));
            let mut scrollbar = Scrollbar::default()
                .orientation(ScrollbarOrientation::HorizontalTop)
                .begin_symbol(Some(DOUBLE_HORIZONTAL.begin))
                .end_symbol(Some(DOUBLE_HORIZONTAL.end));
            scrollbar.position(i);
            scrollbar.content_length(16);
            scrollbar.render(buffer.area, &mut buffer);
            let expected = if i <= 1 {
                vec!["◄██════►", "        "]
            } else if i <= 5 {
                vec!["◄═██═══►", "        "]
            } else if i <= 9 {
                vec!["◄══██══►", "        "]
            } else if i <= 13 {
                vec!["◄═══██═►", "        "]
            } else {
                vec!["◄════██►", "        "]
            };
            assert_buffer_eq!(buffer, Buffer::with_lines(expected.clone()));
        }
    }

    #[test]
    fn test_rendering_only_begin_arrow_horizontal_bottom() {
        for i in 0..=16 {
            let mut buffer = Buffer::empty(Rect::new(0, 0, 8, 2));
            let mut scrollbar = Scrollbar::default()
                .orientation(ScrollbarOrientation::HorizontalBottom)
                .begin_symbol(Some(DOUBLE_HORIZONTAL.begin))
                .end_symbol(None);
            scrollbar.position(i);
            scrollbar.content_length(16);
            scrollbar.render(buffer.area, &mut buffer);
            let expected = if i <= 1 {
                vec!["        ", "◄███════"]
            } else if i <= 5 {
                vec!["        ", "◄═███═══"]
            } else if i <= 9 {
                vec!["        ", "◄══███══"]
            } else if i <= 13 {
                vec!["        ", "◄═══███═"]
            } else {
                vec!["        ", "◄════███"]
            };
            assert_buffer_eq!(buffer, Buffer::with_lines(expected.clone()));
        }
    }
}