generated from Xithrius/rust-binary-project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathinput_widget.rs
236 lines (208 loc) · 7.42 KB
/
input_widget.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
use rustyline::{line_buffer::LineBuffer, At, Word};
use tui::{
layout::Rect,
style::{Color, Modifier, Style},
text::{Line, Span},
widgets::{block::Position, Block, Borders, Clear, Paragraph},
Frame,
};
use crate::{
handlers::{
config::SharedCompleteConfig,
storage::SharedStorage,
user_input::events::{Event, Key},
},
terminal::TerminalAction,
ui::{components::Component, statics::LINE_BUFFER_CAPACITY},
utils::text::{get_cursor_position, title_line, TitleStyle},
};
use super::centered_rect;
pub type InputValidator = Box<dyn Fn(String) -> bool>;
pub type VisualValidator = Box<dyn Fn(String) -> String>;
pub type InputSuggester = Box<dyn Fn(SharedStorage, String) -> Option<String>>;
pub struct InputWidget {
config: SharedCompleteConfig,
input: LineBuffer,
title: String,
focused: bool,
input_validator: Option<InputValidator>,
visual_indicator: Option<VisualValidator>,
input_suggester: Option<(SharedStorage, InputSuggester)>,
suggestion: Option<String>,
}
impl InputWidget {
pub fn new(
config: SharedCompleteConfig,
title: &str,
input_validator: Option<InputValidator>,
visual_indicator: Option<VisualValidator>,
input_suggester: Option<(SharedStorage, InputSuggester)>,
) -> Self {
Self {
config,
input: LineBuffer::with_capacity(LINE_BUFFER_CAPACITY),
title: title.to_string(),
focused: false,
input_validator,
visual_indicator,
input_suggester,
suggestion: None,
}
}
pub fn update(&mut self, s: &str) {
self.input.update(s, 0);
}
pub const fn is_focused(&self) -> bool {
self.focused
}
pub fn toggle_focus(&mut self) {
self.focused = !self.focused;
}
pub fn toggle_focus_with(&mut self, s: &str) {
self.focused = !self.focused;
self.input.update(s, 1);
}
pub fn is_valid(&self) -> bool {
self.input_validator
.as_ref()
.map_or(true, |validator| validator(self.input.to_string()))
}
}
impl ToString for InputWidget {
fn to_string(&self) -> String {
self.input.to_string()
}
}
impl Component for InputWidget {
fn draw(&mut self, f: &mut Frame, area: Option<Rect>) {
let r = area.map_or_else(|| centered_rect(60, 60, 20, f.size()), |a| a);
let cursor_pos = get_cursor_position(&self.input);
f.set_cursor(
(r.x + cursor_pos as u16 + 1).min(r.x + r.width.saturating_sub(2)),
r.y + 1,
);
let current_input = self.input.as_str();
let binding = [TitleStyle::Single(&self.title)];
let status_color = if self.is_valid() {
Color::Green
} else {
Color::Red
};
self.suggestion = if self.config.borrow().storage.channels {
if let Some((storage, suggester)) = &self.input_suggester {
suggester(storage.clone(), self.input.to_string())
} else {
None
}
} else {
None
};
let block = Block::default()
.borders(Borders::ALL)
.border_type(self.config.borrow().frontend.border_type.clone().into())
.border_style(Style::default().fg(status_color))
.title(title_line(
&binding,
Style::default()
.fg(status_color)
.add_modifier(Modifier::BOLD),
));
let paragraph_lines = Line::from(vec![
Span::raw(current_input),
Span::styled(
self.suggestion
.as_ref()
.map_or_else(String::new, |suggestion_buffer| {
if suggestion_buffer.len() > current_input.len() {
suggestion_buffer[current_input.len()..].to_string()
} else {
String::new()
}
}),
Style::default().add_modifier(Modifier::DIM),
),
]);
let paragraph = Paragraph::new(paragraph_lines)
.block(block)
.scroll((0, ((cursor_pos + 3) as u16).saturating_sub(r.width)));
f.render_widget(Clear, r);
f.render_widget(paragraph, r);
if let Some(visual) = &self.visual_indicator {
let contents = visual(self.input.to_string());
let title = [TitleStyle::Single(&contents)];
let bottom_block = Block::default()
.title(title_line(
&title,
Style::default()
.fg(status_color)
.add_modifier(Modifier::BOLD),
))
.title_position(Position::Bottom)
.borders(Borders::BOTTOM | Borders::LEFT | Borders::RIGHT)
.border_type(self.config.borrow().frontend.border_type.clone().into());
// This is only supposed to render on the very bottom line of the area.
// If some rendering breaks for input boxes, this is a possible source.
let rect = Rect::new(r.x, r.bottom() - 1, r.width, 1);
f.render_widget(bottom_block, rect);
}
}
async fn event(&mut self, event: &Event) -> Option<TerminalAction> {
if let Event::Input(key) = event {
match key {
Key::Ctrl('f') | Key::Right => {
self.input.move_forward(1);
}
Key::Ctrl('b') | Key::Left => {
self.input.move_backward(1);
}
Key::Ctrl('a') | Key::Home => {
self.input.move_home();
}
Key::Ctrl('e') | Key::End => {
self.input.move_end();
}
Key::Alt('f') => {
self.input.move_to_next_word(At::AfterEnd, Word::Emacs, 1);
}
Key::Alt('b') => {
self.input.move_to_prev_word(Word::Emacs, 1);
}
Key::Ctrl('t') => {
self.input.transpose_chars();
}
Key::Alt('t') => {
self.input.transpose_words(1);
}
Key::Ctrl('u') => {
self.input.discard_line();
}
Key::Ctrl('k') => {
self.input.kill_line();
}
Key::Ctrl('w') => {
self.input.delete_prev_word(Word::Emacs, 1);
}
Key::Ctrl('d') => {
self.input.delete(1);
}
Key::Backspace | Key::Delete => {
self.input.backspace(1);
}
Key::Tab => {
if self.config.borrow().storage.channels {
if let Some(suggestion) = &self.suggestion {
self.input.update(suggestion, suggestion.len());
}
}
}
Key::Ctrl('p') => panic!("Manual panic triggered by user."),
Key::Ctrl('q') => return Some(TerminalAction::Quit),
Key::Char(c) => {
self.input.insert(*c, 1);
}
_ => {}
}
}
None
}
}