basalt_widgets/markdown/
state.rs1use ratatui::widgets::ScrollbarState;
2
3#[derive(Clone, Debug, PartialEq, Default)]
4pub struct Scrollbar {
5 pub state: ScrollbarState,
6 pub position: usize,
7}
8
9#[derive(Clone, Debug, PartialEq, Default)]
10pub struct MarkdownViewState {
11 pub(crate) text: String,
12 pub(crate) scrollbar: Scrollbar,
13}
14
15impl MarkdownViewState {
16 pub fn new(text: &str) -> Self {
17 Self {
18 text: text.into(),
19 ..Default::default()
20 }
21 }
22
23 pub fn get_lines(&self) -> Vec<&str> {
24 self.text.lines().collect()
25 }
26
27 pub fn scroll_up(self, amount: usize) -> Self {
28 let new_position = self.scrollbar.position.saturating_sub(amount);
29 let new_state = self.scrollbar.state.position(new_position);
30
31 Self {
32 scrollbar: Scrollbar {
33 state: new_state,
34 position: new_position,
35 },
36 ..self
37 }
38 }
39
40 pub fn scroll_down(self, amount: usize) -> Self {
41 let new_position = self.scrollbar.position.saturating_add(amount);
42 let new_state = self.scrollbar.state.position(new_position);
43
44 Self {
45 scrollbar: Scrollbar {
46 state: new_state,
47 position: new_position,
48 },
49 ..self
50 }
51 }
52
53 pub fn set_text(self, text: String) -> Self {
54 Self { text, ..self }
55 }
56
57 pub fn reset_scrollbar(self) -> Self {
58 Self {
59 scrollbar: Scrollbar::default(),
60 ..self
61 }
62 }
63}