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
|
use crate::constants::{
MINIMUM_COMPACT_WINDOW_WIDTH,
MINIMUM_WINDOW_HEIGHT,
TITLE,
TITLE_HELP_INDICATOR_LENGTH,
TITLE_LENGTH,
TITLE_SHORT,
TITLE_SHORT_LENGTH,
};
use crate::scroll::get_scroll_position;
use crate::view::ViewLine;
use crate::window::Window;
use crate::window::WindowColor;
use crate::Config;
pub struct View<'v> {
config: &'v Config,
window: &'v Window<'v>,
}
impl<'v> View<'v> {
pub fn new(window: &'v Window, config: &'v Config) -> Self {
Self { window, config }
}
pub fn draw_str(&self, s: &str) {
self.window.draw_str(s);
}
pub fn set_color(&self, color: WindowColor) {
self.window.color(color);
}
pub fn set_style(&self, dim: bool, underline: bool, reverse: bool) {
self.window.set_style(dim, underline, reverse);
}
pub fn check_window_size(&self) -> bool {
let (window_width, window_height) = self.get_view_size();
!(window_width <= MINIMUM_COMPACT_WINDOW_WIDTH || window_height <= MINIMUM_WINDOW_HEIGHT)
}
pub fn draw_error(&self, message: &str) {
self.draw_title(false);
self.window.color(WindowColor::Foreground);
self.window.set_style(false, false, false);
self.window.draw_str(message);
self.window.draw_str("\n");
self.window.color(WindowColor::IndicatorColor);
self.window.draw_str("Press any key to continue");
}
pub fn clear(&self) {
self.window.clear();
}
pub fn get_view_size(&self) -> (usize, usize) {
let (view_width, view_height) = self.window.get_window_size();
(view_width as usize, view_height as usize)
}
pub fn refresh(&self) {
self.window.refresh();
}
pub fn draw_view_lines(&self, lines: Vec<ViewLine>, top: usize, height: usize) {
let number_of_lines = lines.len();
let scroll_indicator_index = get_scroll_position(top, number_of_lines, height);
let show_scroll_bar = height < number_of_lines;
let mut index: usize = 0;
for line in lines.iter().skip(top).take(height) {
self.draw_view_line(line, show_scroll_bar);
if show_scroll_bar {
self.window.color(WindowColor::Foreground);
self.window.set_style(scroll_indicator_index != index, false, true);
self.window.draw_str(" ");
}
index += 1;
}
if height > lines.len() {
self.draw_vertical_spacer((height - index) as i32);
}
}
pub fn draw_view_line(&self, line: &ViewLine, scrollbar: bool) {
let (window_width, _) = self.window.get_window_size();
let window_width = if scrollbar { window_width - 1 } else { window_width } as usize;
let mut start = 0;
for segment in line.get_segments() {
start += segment.draw(window_width - start, &self.window);
if start >= window_width {
break;
}
}
if start < window_width {
let padding = " ".repeat(window_width - start);
self.window.draw_str(padding.as_str());
}
}
pub fn draw_title(&self, show_help: bool) {
self.window.color(WindowColor::Foreground);
self.window.set_style(false, true, true);
let (window_width, _) = self.window.get_window_size();
if window_width >= TITLE_LENGTH {
self.window.draw_str(TITLE);
// only draw help if there is room
if window_width > TITLE_LENGTH + TITLE_HELP_INDICATOR_LENGTH {
if (window_width - TITLE_LENGTH - TITLE_HELP_INDICATOR_LENGTH) > 0 {
let padding = " ".repeat((window_width - TITLE_LENGTH - TITLE_HELP_INDICATOR_LENGTH) as usize);
self.window.draw_str(padding.as_str());
}
if show_help {
self.window
.draw_str(format!("Help: {}", self.config.input_help).as_str());
}
else {
let padding = " ".repeat(TITLE_HELP_INDICATOR_LENGTH as usize);
self.window.draw_str(padding.as_str());
}
}
else if (window_width - TITLE_LENGTH) > 0 {
let padding = " ".repeat((window_width - TITLE_LENGTH) as usize);
self.window.draw_str(padding.as_str());
}
}
else {
self.window.draw_str(TITLE_SHORT);
if (window_width - TITLE_SHORT_LENGTH) > 0 {
let padding = " ".repeat((window_width - TITLE_SHORT_LENGTH) as usize);
self.window.draw_str(padding.as_str());
}
}
}
fn draw_vertical_spacer(&self, repeat: i32) {
self.window.color(WindowColor::Foreground);
self.window.set_style(false, false, false);
for _x in 0..repeat {
self.window.draw_vertical_space_character();
}
}
pub fn draw_prompt(&self, message: &str) {
self.draw_title(false);
self.window.set_style(false, false, false);
self.window.draw_str(&format!("\n{} ", message));
}
pub fn draw_confirm(&self, message: &str) {
self.draw_prompt(&format!(
"{} ({}/{})? ",
message, self.config.input_confirm_yes, self.config.input_confirm_no
));
}
}
|