Skip to content

Commit 6c9e884

Browse files
committed
feat: make Scrollable generic over each line; expose type alias
1 parent abbb068 commit 6c9e884

File tree

6 files changed

+68
-24
lines changed

6 files changed

+68
-24
lines changed

src/bin/ui/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use self::scrollable::Scrollable;
1+
use self::scrollable::{ScrollText, ScrollTreeLines, Scrollable};
22
use crate::{app::App, Result};
33
use ratatui::prelude::*;
4-
use term_rustdoc::tree::{TreeLine, TreeLines};
4+
use term_rustdoc::tree::TreeLines;
55

66
/// scroll up/down behavior and with what offset
77
mod page_scroll;
@@ -58,17 +58,14 @@ pub struct Selected {
5858
col_end: u16,
5959
}
6060

61-
/// Scrollable tree view but stored in lines.
62-
type ScrollTreeLines = Scrollable<TreeLines>;
63-
6461
#[derive(Default, Debug)]
6562
struct Outline {
6663
display: ScrollTreeLines,
6764
}
6865

6966
#[derive(Default, Debug)]
7067
struct Content {
71-
display: Scrollable<Vec<TreeLine>>,
68+
display: ScrollText,
7269
}
7370

7471
#[derive(Default, Debug)]

src/bin/ui/scrollable/interaction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::ui::ScrollOffset;
33
use term_rustdoc::tree::TreeLine;
44

55
/// Scrolling
6-
impl<Line: AsRef<[TreeLine]>> Scrollable<Line> {
6+
impl<Lines: AsRef<[TreeLine]>> Scrollable<Lines, TreeLine> {
77
pub fn scrolldown(&mut self, offset: ScrollOffset) {
88
let height = self.area.height as usize;
99
let len = self.len();
@@ -48,7 +48,7 @@ impl<Line: AsRef<[TreeLine]>> Scrollable<Line> {
4848
}
4949

5050
/// Cursor movement
51-
impl<Line: AsRef<[TreeLine]>> Scrollable<Line> {
51+
impl<Lines: AsRef<[TreeLine]>> Scrollable<Lines, TreeLine> {
5252
pub fn move_forward_cursor(&mut self) {
5353
let height = self.area.height;
5454
let reach_sceen_bottom = (self.cursor + 1) == height;

src/bin/ui/scrollable/markdown/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use super::Scrollable;
2+
use std::fmt;
3+
use term_rustdoc::tree::Text as StyledText;
4+
5+
/// Scrollable text area for displaying markdown.
6+
pub type ScrollText = Scrollable<Vec<StyledLine>, StyledLine>;
7+
8+
#[derive(Default)]
9+
pub struct StyledLine {
10+
line: Vec<StyledText>,
11+
}
12+
13+
impl fmt::Debug for StyledLine {
14+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15+
f.debug_struct("StyledLine")
16+
.field("line-len", &self.line.len())
17+
.finish()
18+
}
19+
}
20+
21+
impl AsRef<[StyledText]> for StyledLine {
22+
fn as_ref(&self) -> &[StyledText] {
23+
&self.line
24+
}
25+
}

src/bin/ui/scrollable/mod.rs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
use super::Selected;
22
use crate::{err, Result};
33
use ratatui::prelude::Rect;
4-
use std::fmt;
5-
use term_rustdoc::tree::TreeLine;
4+
use std::{fmt, marker::PhantomData};
5+
use term_rustdoc::tree::{TreeLine, TreeLines};
66

77
mod interaction;
8+
mod markdown;
89
mod render;
910

11+
pub use self::markdown::ScrollText;
12+
13+
/// Scrollable tree view but stored in lines.
14+
pub type ScrollTreeLines = Scrollable<TreeLines, TreeLine>;
15+
1016
/// A text panel that can be scrolled and select texts when the cursor is inside of it.
11-
#[derive(Default)]
12-
pub struct Scrollable<Lines> {
17+
pub struct Scrollable<Lines, L> {
1318
/// Styled texts on each line
1419
pub lines: Lines,
1520
/// The start of row to be displayed
@@ -22,10 +27,18 @@ pub struct Scrollable<Lines> {
2227
pub select: Option<Selected>,
2328
/// The widget area, usually not the full screen
2429
pub area: Rect,
30+
_ph: PhantomData<L>,
31+
}
32+
33+
impl<Lines, L> Scrollable<Lines, L> {
34+
/// The index the current cursor on screen points to.
35+
pub fn idx_of_current_cursor(&self) -> usize {
36+
self.cursor as usize + self.start
37+
}
2538
}
2639

27-
impl<Line: AsRef<[TreeLine]>> Scrollable<Line> {
28-
pub fn lines(&self) -> &[TreeLine] {
40+
impl<L, Lines: AsRef<[L]>> Scrollable<Lines, L> {
41+
pub fn lines(&self) -> &[L] {
2942
self.lines.as_ref()
3043
}
3144

@@ -34,7 +47,22 @@ impl<Line: AsRef<[TreeLine]>> Scrollable<Line> {
3447
}
3548
}
3649

37-
impl<Lines: Default + AsRef<[TreeLine]>> Scrollable<Lines> {
50+
impl<Lines: Default, L> Default for Scrollable<Lines, L> {
51+
fn default() -> Self {
52+
let (lines, start, cursor, max_windth, select, area, _ph) = Default::default();
53+
Scrollable {
54+
lines,
55+
start,
56+
cursor,
57+
max_windth,
58+
select,
59+
area,
60+
_ph,
61+
}
62+
}
63+
}
64+
65+
impl<Lines: Default + AsRef<[TreeLine]>> Scrollable<Lines, TreeLine> {
3866
pub fn new(lines: Lines, full: Rect) -> Result<Self> {
3967
let w = lines.as_ref().iter().map(TreeLine::width).max();
4068
let max_windth = w.ok_or_else(|| err!("The documentation is empty with no items."))?;
@@ -53,14 +81,7 @@ impl<Lines: Default + AsRef<[TreeLine]>> Scrollable<Lines> {
5381
}
5482
}
5583

56-
impl<Lines> Scrollable<Lines> {
57-
/// The index the current cursor on screen points to.
58-
pub fn idx_of_current_cursor(&self) -> usize {
59-
self.cursor as usize + self.start
60-
}
61-
}
62-
63-
impl<Lines: AsRef<[TreeLine]>> fmt::Debug for Scrollable<Lines> {
84+
impl<L: fmt::Debug, Lines: AsRef<[L]>> fmt::Debug for Scrollable<Lines, L> {
6485
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6586
let mut s = f.debug_struct("Scrollable");
6687
s.field("lines.len", &self.len())

src/bin/ui/scrollable/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::Scrollable;
22
use ratatui::prelude::{Buffer, Color, Rect, Widget};
33
use term_rustdoc::tree::TreeLine;
44

5-
impl<Lines: AsRef<[TreeLine]>> Widget for &mut Scrollable<Lines> {
5+
impl<Lines: AsRef<[TreeLine]>> Widget for &mut Scrollable<Lines, TreeLine> {
66
fn render(self, area: Rect, buf: &mut Buffer) {
77
// render tree by each line
88
write_lines(self.lines.as_ref(), self.start, area, buf);

src/tree/textline.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl fmt::Display for TextTag {
2424
}
2525

2626
/// Onwed [`ratatui::text::Span`], mainly being a small styled string.
27+
#[derive(Debug)]
2728
pub struct Text {
2829
pub text: XString,
2930
pub style: Style,

0 commit comments

Comments
 (0)