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
use {
crate::{
area::Area,
errors::Result,
skin::MadSkin,
views::TextView,
},
crossterm::event::KeyEvent,
std::io::Write,
};
pub struct MadView {
markdown: String,
area: Area,
pub skin: MadSkin,
pub scroll: usize,
}
impl MadView {
pub const fn from(markdown: String, area: Area, skin: MadSkin) -> MadView {
MadView {
markdown,
area,
skin,
scroll: 0,
}
}
pub fn write(&self) -> Result<()> {
self.write_on(&mut std::io::stdout())
}
pub fn write_on<W: Write>(&self, w: &mut W) -> Result<()> {
let text = self.skin.area_text(&self.markdown, &self.area);
let mut text_view = TextView::from(&self.area, &text);
text_view.scroll = self.scroll;
text_view.write_on(w)?;
Ok(())
}
pub fn resize(&mut self, area: &Area) {
if *area == self.area {
return;
}
if area.width != self.area.width {
self.scroll = 0; }
self.area.left = area.left;
self.area.top = area.top;
self.area.height = area.height;
self.area.width = area.width;
}
pub fn try_scroll_lines(&mut self, lines_count: i32) {
let text = self.skin.area_text(&self.markdown, &self.area);
let mut text_view = TextView::from(&self.area, &text);
text_view.scroll = self.scroll;
text_view.try_scroll_lines(lines_count);
self.scroll = text_view.scroll;
}
pub fn try_scroll_pages(&mut self, pages_count: i32) {
self.try_scroll_lines(pages_count * i32::from(self.area.height));
}
pub fn apply_key_event(&mut self, key: KeyEvent) -> bool {
let text = self.skin.area_text(&self.markdown, &self.area);
let mut text_view = TextView::from(&self.area, &text);
text_view.scroll = self.scroll;
if text_view.apply_key_event(key) {
self.scroll = text_view.scroll;
true
} else {
false
}
}
}