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
|
#[cfg(test)]
mod tests;
use lazy_static::lazy_static;
use crate::{
components::shared::EditableLine,
display::DisplayColor,
input::{Event, InputOptions, KeyCode, KeyEvent, KeyModifiers},
view::{LineSegment, ViewData, ViewDataUpdater, ViewLine},
};
lazy_static! {
pub(crate) static ref INPUT_OPTIONS: InputOptions = InputOptions::RESIZE;
}
const FINISH_EVENT: Event = Event::Key(KeyEvent {
code: KeyCode::Enter,
modifiers: KeyModifiers::NONE,
});
pub(crate) struct Edit {
editable_line: EditableLine,
finished: bool,
view_data: ViewData,
}
impl Edit {
pub(crate) fn new() -> Self {
let view_data = ViewData::new(|updater| {
updater.set_show_title(true);
});
Self {
editable_line: EditableLine::new(),
finished: false,
view_data,
}
}
pub(crate) fn build_view_data<F, G>(&mut self, before_build: F, after_build: G) -> &ViewData
where
F: FnOnce(&mut ViewDataUpdater<'_>),
G: FnOnce(&mut ViewDataUpdater<'_>),
{
self.view_data.update_view_data(|updater| {
updater.clear();
before_build(updater);
updater.push_line(ViewLine::from(self.editable_line.line_segments()));
updater.push_trailing_line(ViewLine::new_pinned(vec![LineSegment::new_with_color(
"Enter to finish",
DisplayColor::IndicatorColor,
)]));
updater.ensure_column_visible(self.editable_line.cursor_position());
updater.ensure_line_visible(0);
after_build(updater);
});
&self.view_data
}
pub(crate) fn get_view_data(&mut self) -> &ViewData {
self.build_view_data(|_| {}, |_| {})
}
pub(crate) fn handle_event(&mut self, event: Event) {
if event == FINISH_EVENT {
self.finished = true;
}
else {
_ = self.editable_line.handle_event(event);
}
}
pub(crate) fn set_label(&mut self, label: &str) {
self.editable_line.set_label(LineSegment::new_with_color_and_style(
label,
DisplayColor::Normal,
true,
false,
false,
));
}
pub(crate) fn set_content(&mut self, content: &str) {
self.editable_line.set_content(content);
}
pub(crate) fn reset(&mut self) {
self.editable_line.clear();
self.editable_line.set_read_only(false);
self.finished = false;
}
#[allow(clippy::unused_self)]
pub(crate) fn input_options(&self) -> &InputOptions {
&INPUT_OPTIONS
}
pub(crate) const fn is_finished(&self) -> bool {
self.finished
}
pub(crate) fn get_content(&self) -> &str {
self.editable_line.get_content()
}
}
|