-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.rs
395 lines (349 loc) · 11.4 KB
/
ui.rs
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
use tui::backend::CrosstermBackend;
use tui::Frame;
use tui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use tui::text::Text;
use tui::widgets::{Block, BorderType, Borders, Clear, ListState, Paragraph, Wrap};
use crossterm::event::KeyCode;
use std::io::Stdout;
use tui::style::{Color, Style};
#[derive(Default)]
pub struct DisplayList<T> {
pub(crate) state: ListState,
pub(crate) array: Vec<T>,
}
#[derive(SmartDefault)]
pub enum InputMode {
#[default]
CommandMode,
WriteMode,
}
pub trait Drawable {
fn display(&self, frame: &mut Frame<CrosstermBackend<Stdout>>, layout: Rect);
fn centered_rect(&self, percent_x: u16, percent_y: u16, r: Rect) -> Rect {
let popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage((100 - percent_y) / 2),
Constraint::Percentage(percent_y),
Constraint::Percentage((100 - percent_y) / 2),
]
.as_ref(),
)
.split(r);
Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Percentage((100 - percent_x) / 2),
Constraint::Percentage(percent_x),
Constraint::Percentage((100 - percent_x) / 2),
]
.as_ref(),
)
.split(popup_layout[1])[1]
}
}
pub trait InputReceptor {
fn handle_input_key(&mut self, key_code: KeyCode);
fn get_controls_description(&self) -> String;
fn get_input_mode(&self) -> InputMode;
}
pub trait InputReturn {
fn get_input_data(&self) -> String;
}
pub trait Completable {
fn is_completed(&self) -> bool;
fn reset_completion(&mut self);
fn is_active(&self) -> bool;
fn set_active(&mut self, new_active: bool);
}
impl<T> DisplayList<T> {
pub(crate) fn next(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if self.array.len() > 0 {
if i < self.array.len() - 1 {
i + 1
} else {
i
}
} else {
0
}
}
None => 0,
};
if self.array.len() > 0 {
self.state.select(Some(i));
}
}
pub(crate) fn previous(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if i > 0 {
i - 1
} else {
0
}
}
None => 0,
};
if self.array.len() > 0 {
self.state.select(Some(i));
}
}
pub fn from(content: Vec<T>) -> DisplayList<T> {
let content_len = content.len();
let mut dl = DisplayList {
state: Default::default(),
array: content,
};
if content_len > 0 {
dl.state.select(Some(0));
}
dl
}
}
#[derive(Default)]
pub struct PopupMessageWindow {
description: String,
is_active: bool,
is_done: bool,
}
// PopupMessageWindow
impl PopupMessageWindow {
pub fn new(popup_message: String) -> PopupMessageWindow {
PopupMessageWindow {
description: popup_message,
is_active: true,
is_done: false,
}
}
}
impl Completable for PopupMessageWindow {
fn is_completed(&self) -> bool {
self.is_done
}
fn reset_completion(&mut self) {
self.is_done = false;
}
fn is_active(&self) -> bool {
self.is_active
}
fn set_active(&mut self, new_active: bool) {
self.is_active = new_active;
}
}
impl Drawable for PopupMessageWindow {
fn display(&self, frame: &mut Frame<CrosstermBackend<Stdout>>, layout: Rect) {
let popup_layout = self.centered_rect(50, 25, layout);
frame.render_widget(Clear, popup_layout);
let popup_block = Block::default().borders(Borders::ALL);
frame.render_widget(popup_block, popup_layout);
let main_popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(80), Constraint::Percentage(20)])
.split(popup_layout);
let block = Block::default().borders(Borders::ALL);
let description_paragraph = Paragraph::new(Text::from(self.description.clone()))
.alignment(Alignment::Center)
.block(block)
.wrap(Wrap { trim: false });
frame.render_widget(description_paragraph, main_popup_layout[0]);
let block = Block::default().borders(Borders::ALL);
let ok_message = Paragraph::new(Text::from("Ok"))
.wrap(Wrap { trim: false })
.alignment(Alignment::Center)
.block(block);
frame.render_widget(ok_message, main_popup_layout[1]);
}
}
impl InputReceptor for PopupMessageWindow {
fn handle_input_key(&mut self, key_code: KeyCode) {
match key_code {
KeyCode::Enter => {
self.is_done = true;
}
_ => {}
}
}
fn get_controls_description(&self) -> String {
String::from("Press enter to continue")
}
fn get_input_mode(&self) -> InputMode {
InputMode::CommandMode
}
}
// PopupBinaryChoice
#[derive(Default)]
pub struct PopupBinaryChoice {
choice_message: String,
current_choice: bool,
is_completed: bool,
is_active: bool,
}
impl PopupBinaryChoice {
pub fn new(message: String) -> PopupBinaryChoice {
PopupBinaryChoice {
choice_message: message,
current_choice: false,
is_completed: false,
is_active: true,
}
}
pub fn get_choice(&self) -> bool {
self.current_choice
}
}
impl InputReceptor for PopupBinaryChoice {
fn handle_input_key(&mut self, key_code: KeyCode) {
match key_code {
KeyCode::Left => self.current_choice = true,
KeyCode::Right => self.current_choice = false,
KeyCode::Enter => self.is_completed = true,
_ => {}
}
}
fn get_controls_description(&self) -> String {
String::from("<-: Go Left | ->: Go Right | Enter: Confirm Selection ")
}
fn get_input_mode(&self) -> InputMode {
InputMode::CommandMode
}
}
impl Completable for PopupBinaryChoice {
fn is_completed(&self) -> bool {
self.is_completed
}
fn reset_completion(&mut self) {
self.is_completed = false;
}
fn is_active(&self) -> bool {
self.is_active
}
fn set_active(&mut self, new_active: bool) {
self.is_active = new_active;
}
}
impl Drawable for PopupBinaryChoice {
fn display(&self, frame: &mut Frame<CrosstermBackend<Stdout>>, layout: Rect) {
let popup_layout = self.centered_rect(50, 20, layout);
frame.render_widget(Clear, popup_layout);
let main_split = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
.split(popup_layout);
let message_block = Block::default().borders(Borders::ALL);
let message_paragraph = Paragraph::new(Text::from(self.choice_message.clone()))
.alignment(Alignment::Center)
.wrap(Wrap { trim: false })
.block(message_block);
frame.render_widget(message_paragraph, main_split[0]);
let choice_layout = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
.split(main_split[1]);
let normal_block = Block::default().borders(Borders::ALL);
let choice_block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Thick)
.style(Style::default().fg(Color::Red));
let mut yes_paragraph = Paragraph::new(Text::from("Yes")).alignment(Alignment::Center);
let mut no_paragraph = Paragraph::new(Text::from("No")).alignment(Alignment::Center);
if self.current_choice {
yes_paragraph = yes_paragraph.block(choice_block);
no_paragraph = no_paragraph.block(normal_block);
} else {
no_paragraph = no_paragraph.block(choice_block);
yes_paragraph = yes_paragraph.block(normal_block);
}
frame.render_widget(yes_paragraph, choice_layout[0]);
frame.render_widget(no_paragraph, choice_layout[1]);
}
}
#[derive(Default)]
pub struct PopupInputWindow {
description: String,
input_string: String,
is_active: bool,
message_input_finished: bool,
}
impl PopupInputWindow {
pub fn new(popup_description: String) -> PopupInputWindow {
PopupInputWindow {
description: popup_description,
input_string: String::new(),
is_active: true,
message_input_finished: false,
}
}
pub fn set_input_string(&mut self, new_input_string: String) {
self.input_string = new_input_string;
}
}
impl Drawable for PopupInputWindow {
fn display(&self, frame: &mut Frame<CrosstermBackend<Stdout>>, layout: Rect) {
let popup_layout = self.centered_rect(50, 25, layout);
frame.render_widget(Clear, popup_layout);
let popup_block = Block::default().borders(Borders::ALL);
frame.render_widget(popup_block, popup_layout);
let main_popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(20), Constraint::Percentage(80)])
.split(popup_layout);
let description_paragraph =
Paragraph::new(Text::from(self.description.clone())).alignment(Alignment::Center);
frame.render_widget(description_paragraph, main_popup_layout[0]);
let input_paragraph = Paragraph::new(Text::from(self.input_string.clone()))
.wrap(Wrap { trim: false })
.alignment(Alignment::Center);
frame.render_widget(input_paragraph, main_popup_layout[1]);
}
}
impl Completable for PopupInputWindow {
fn is_completed(&self) -> bool {
self.message_input_finished
}
fn reset_completion(&mut self) {
self.message_input_finished = false;
}
fn is_active(&self) -> bool {
self.is_active
}
fn set_active(&mut self, new_active: bool) {
self.is_active = new_active;
}
}
impl InputReturn for PopupInputWindow {
fn get_input_data(&self) -> String {
self.input_string.clone()
}
}
impl InputReceptor for PopupInputWindow {
fn handle_input_key(&mut self, key_code: KeyCode) {
match key_code {
KeyCode::Char(char) => {
self.input_string.push(char);
}
KeyCode::Backspace => {
if self.input_string.len() > 0 {
self.input_string.pop();
}
}
KeyCode::Enter => {
self.message_input_finished = true;
}
KeyCode::Esc => {
self.set_active(false);
}
_ => {}
};
}
fn get_controls_description(&self) -> String {
String::from("esc - Cancel | Enter - Confirm entry")
}
fn get_input_mode(&self) -> InputMode {
InputMode::CommandMode
}
}