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
use ratatui::{
layout::Position,
style::Style,
text::{Line, Span},
Frame,
};
use regex::Regex;
use unicode_width::UnicodeWidthStr;
use crate::popup::{centered_rect, render_popup};
#[derive(Debug, Default)]
pub struct Input {
/// Current value of the input box
pub input: String,
}
impl Input {
pub fn new() -> Self {
Self::default()
}
pub fn render(&self, frame: &mut Frame) {
let pos = (40, 8);
let area = centered_rect(pos.0, pos.1, frame.area());
let text = vec![Line::from(Span::styled(
self.input.clone(),
Style::default(),
))];
let title = if self.is_valid() {
"Input"
} else {
"Input (non valid regexp)"
};
let position = Position::new(area.x + self.input.width() as u16 + 1, area.y + 1);
frame.set_cursor_position(position);
render_popup(frame, title, &text, (pos.0, pos.1));
}
pub fn reset(&mut self) {
self.input = String::new();
}
pub fn push(&mut self, ch: char) {
self.input.push(ch);
}
pub fn pop(&mut self) {
self.input.pop();
}
pub fn inner_clone(&self) -> String {
self.input.clone()
}
pub fn is_valid(&self) -> bool {
Regex::new(&self.input).is_ok()
}
}
#[cfg(test)]
mod tests {
use ratatui::{backend::TestBackend, buffer::Buffer, prelude::Color, Terminal};
use super::*;
#[test]
fn simple_full_test() {
let mut input = Input::new();
assert_eq!(input.input, String::new());
input.push('a');
assert_eq!(input.input, "a");
input.push('b');
assert_eq!(input.input, "ab");
input.pop();
assert_eq!(input.input, "a");
input.reset();
assert_eq!(input.input, String::new());
}
#[test]
fn test_render_input() {
let mut input = Input::new();
input.push('a');
input.push('b');
let backend = TestBackend::new(20, 37);
let mut terminal = Terminal::new(backend).unwrap();
terminal.draw(|f| input.render(f)).unwrap();
let mut expected = Buffer::with_lines(vec![
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ┌Input─┐ ",
" │ab │ ",
" └──────┘ ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
]);
for x in 6..=13 {
for y in 17..=19 {
expected[(x, y)].set_fg(Color::White);
expected[(x, y)].set_bg(Color::Black);
}
}
terminal.backend().assert_buffer(&expected);
}
#[test]
fn test_render_non_valid_input() {
let mut input = Input::new();
input.push('[');
let backend = TestBackend::new(65, 37);
let mut terminal = Terminal::new(backend).unwrap();
terminal.draw(|f| input.render(f)).unwrap();
let mut expected = Buffer::with_lines(vec![
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ┌Input (non valid regexp)┐ ",
" │[ │ ",
" └────────────────────────┘ ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
]);
for x in 20..=45 {
for y in 17..=19 {
expected[(x, y)].set_fg(Color::White);
expected[(x, y)].set_bg(Color::Black);
}
}
terminal.backend().assert_buffer(&expected);
}
}