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
use cursive_core::{
Cursive,
With,
view::Nameable,
event::{Event, Key},
views::{EditView, Dialog, OnEventView, NamedView, Checkbox, LinearLayout, TextView},
utils::markup::StyledString,
theme::{
PaletteColor,
Color,
BaseColor,
ColorType,
ColorStyle,
BorderStyle,
Theme
}
};
use std::fmt::Display;
use crate::views::LoadingAnimation;
pub fn better_theme() -> Theme {
let mut theme_def = Theme {
shadow: false,
.. Theme::default()
};
theme_def.palette[PaletteColor::Background] = Color::TerminalDefault;
theme_def.palette[PaletteColor::Primary] = Color::Light(BaseColor::White);
theme_def.palette[PaletteColor::View] = Color::TerminalDefault;
theme_def.palette[PaletteColor::Highlight] = Color::Light(BaseColor::Blue);
theme_def.palette[PaletteColor::HighlightText] = Color::Dark(BaseColor::White);
theme_def.palette[PaletteColor::Secondary] = Color::Dark(BaseColor::Blue);
theme_def.palette[PaletteColor::TitlePrimary] = Color::Light(BaseColor::Blue);
theme_def.borders = BorderStyle::Outset;
theme_def
}
pub fn confirm_dialog<C, T, U>(title: T, text: U, cb: C) -> OnEventView<Dialog>
where
C: Fn(&mut Cursive) + 'static,
T: Display,
U: Display
{
Dialog::text(text.to_string())
.dismiss_button("No")
.button("Yes", cb)
.title(title.to_string())
.wrap_with(OnEventView::new)
.on_event(Event::Key(Key::Esc), |v| { v.pop_layer(); })
}
pub fn confirm_dialog_styled<T:Display, C: Fn(&mut Cursive) + 'static>(title: T, text: StyledString, cb: C) -> OnEventView<Dialog> {
Dialog::text(text)
.dismiss_button("No")
.button("Yes", cb)
.title(title.to_string())
.wrap_with(OnEventView::new)
.on_event(Event::Key(Key::Esc), |v| { v.pop_layer(); })
}
pub fn info_dialog<T: Display, U: Display>(title: T, text: U) -> OnEventView<Dialog> {
Dialog::text(text.to_string())
.dismiss_button("Back")
.title(title.to_string())
.wrap_with(OnEventView::new)
.on_event(Event::Key(Key::Esc), |v| { v.pop_layer(); })
}
pub fn info_dialog_styled<T: Display>(title: T, text: StyledString) -> OnEventView<Dialog> {
Dialog::text(text)
.dismiss_button("Back")
.title(title.to_string())
.wrap_with(OnEventView::new)
.on_event(Event::Key(Key::Esc), |v| { v.pop_layer(); })
}
pub fn styled_editview<C: Display>(content: C, view_name: &'static str, password: bool) -> NamedView<EditView> {
styled_editview_color(content, view_name, password, PaletteColor::Highlight)
}
pub fn styled_editview_color<T: Display, C: Into<ColorType>>(content: T, view_name: &'static str, password: bool, color: C) -> NamedView<EditView> {
let input_style = ColorStyle::new(
color,
Color::Light(BaseColor::White),
);
let view = EditView::new().content(content.to_string()).style(input_style).filler(" ");
if password {
view.secret().with_name(view_name)
}
else {
view.with_name(view_name)
}
}
pub fn get_checkbox_option(view: &mut Cursive, name: &str) -> bool {
let cbox = view.find_name::<Checkbox>(name).unwrap();
cbox.is_checked()
}
pub fn labeled_checkbox(text: &str, name: &str, checked: bool) -> LinearLayout {
labeled_checkbox_cb(text, name, checked, |_, _| { })
}
pub fn labeled_checkbox_cb<C: Fn(&mut Cursive, bool) + 'static>(text: &str, name: &str, checked: bool, callback: C) -> LinearLayout {
LinearLayout::horizontal()
.child(Checkbox::new().with_checked(checked).on_change(callback).with_name(name))
.child(TextView::new(format!(" {}", text)))
}
pub fn load_resource<T, R, F>(view: &mut Cursive, title: &str, msg: &str, task: R, finish_task: F)
where
T: Send + Sync + 'static,
R: FnOnce() -> T + Send + Sync + 'static,
F: FnOnce(&mut Cursive, T) + Send + Sync + 'static
{
let loader = LoadingAnimation::new(msg, move || (task(), finish_task));
view.add_layer(
Dialog::around(loader.with_name("load")).title(title)
.wrap_with(OnEventView::new)
.on_event(Event::Refresh, |view: &mut Cursive| {
let mut loader = view.find_name::<LoadingAnimation<(T, F)>>("load").unwrap();
if loader.is_done() {
view.pop_layer();
let (val, finish_func) = loader.finish().unwrap();
finish_func(view, val);
}
})
);
}