-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_format.rs
188 lines (162 loc) · 5.12 KB
/
custom_format.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
use promptuity::prompts::{
Input, MultiSelect, MultiSelectOption, Number, Password, Select, SelectOption,
};
use promptuity::style::{Color, Styled, Symbol};
use promptuity::themes::FancyTheme;
use promptuity::{Error, Promptuity, Term};
const ERR_REQUIRED: &str = "Required!";
const S_SELECT_CURSOR: Symbol = Symbol("❯", " > ");
const S_SELECTED: Symbol = Symbol("✓", "[x]");
const S_UNSELECTED: Symbol = Symbol(" ", "[ ]");
struct InputFormatter;
impl promptuity::prompts::InputFormatter for InputFormatter {
fn err_required(&self) -> String {
ERR_REQUIRED.to_string()
}
}
struct PasswordFormatter;
impl promptuity::prompts::PasswordFormatter for PasswordFormatter {
fn err_required(&self) -> String {
ERR_REQUIRED.to_string()
}
}
struct NumberFormatter;
impl promptuity::prompts::NumberFormatter for NumberFormatter {
fn err_required(&self) -> String {
ERR_REQUIRED.to_string()
}
fn err_invalid_format(&self) -> String {
"Invalid!".into()
}
fn err_invalid_range(&self, min: isize, max: isize) -> String {
format!("Invalid Range! (expect min={}, max={})", min, max)
}
}
struct SelectFormatter;
impl promptuity::prompts::SelectFormatter for SelectFormatter {
fn option_icon(&self, active: bool) -> String {
if active {
Styled::new(S_SELECT_CURSOR).fg(Color::Cyan).to_string()
} else {
Styled::new(S_UNSELECTED).fg(Color::DarkGrey).to_string()
}
}
fn option_label(&self, label: String, active: bool) -> String {
if active {
Styled::new(label)
.fg(Color::Magenta)
.underline()
.to_string()
} else {
label
}
}
fn option_hint(&self, hint: Option<String>, active: bool) -> String {
if active {
hint.as_ref().map_or_else(String::new, |hint| {
format!(
" {}",
Styled::new(format!("({})", hint)).fg(Color::DarkGrey)
)
})
} else {
String::new()
}
}
fn option(&self, icon: String, label: String, hint: String, _active: bool) -> String {
format!("{} {}{}", icon, label, hint)
}
}
struct MultiSelectFormatter;
impl promptuity::prompts::MultiSelectFormatter for MultiSelectFormatter {
fn option_icon(&self, active: bool, selected: bool) -> String {
if active {
Styled::new(S_SELECT_CURSOR).fg(Color::Cyan).to_string()
} else if selected {
Styled::new(S_SELECTED).fg(Color::Cyan).to_string()
} else {
Styled::new(S_UNSELECTED).fg(Color::DarkGrey).to_string()
}
}
fn option_label(&self, label: String, active: bool, _selected: bool) -> String {
if active {
Styled::new(label)
.fg(Color::Magenta)
.underline()
.to_string()
} else {
label
}
}
fn option_hint(&self, hint: Option<String>, active: bool, selected: bool) -> String {
if active || selected {
hint.as_ref().map_or_else(String::new, |hint| {
format!(
" {}",
Styled::new(format!("({})", hint)).fg(Color::DarkGrey)
)
})
} else {
String::new()
}
}
fn option(
&self,
icon: String,
label: String,
hint: String,
_active: bool,
_selected: bool,
) -> String {
format!("{} {}{}", icon, label, hint)
}
fn submit(&self, labels: Vec<String>) -> String {
labels.join(" / ")
}
fn err_required(&self) -> String {
ERR_REQUIRED.to_string()
}
}
fn main() -> Result<(), Error> {
let mut term = Term::default();
let mut theme = FancyTheme::default();
let mut p = Promptuity::new(&mut term, &mut theme);
p.term().clear()?;
p.with_intro("Custom Format").begin()?;
let _ = p.prompt(Input::new("Input message").with_formatter(InputFormatter))?;
let _ = p.prompt(
Password::new("Password message")
.with_formatter(PasswordFormatter)
.with_mask('∙'),
)?;
let _ = p.prompt(
Number::new("Number message")
.with_formatter(NumberFormatter)
.with_min(0)
.with_max(10),
)?;
let _ = p.prompt(
Select::new(
"Select message",
vec![
SelectOption::new("Option1", "option1"),
SelectOption::new("Option2", "option2").with_hint("Hint!"),
SelectOption::new("Option3", "option3"),
],
)
.with_formatter(SelectFormatter),
)?;
let _ = p.prompt(
MultiSelect::new(
"Select message",
vec![
MultiSelectOption::new("Option1", "option1"),
MultiSelectOption::new("Option2", "option2").with_hint("Hint!"),
MultiSelectOption::new("Option3", "option3"),
],
)
.with_formatter(MultiSelectFormatter),
)?;
p.with_outro("Finish!").finish()?;
Ok(())
}