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
237
238
239
240
241
242
243
244
245
246
247
248
|
use anyhow::{Context, Result};
use dialoguer::{theme::ColorfulTheme, Confirm, Input, Password};
use indicatif::TermLike;
#[cfg(test)]
use mockall::*;
#[derive(Default)]
pub struct Interactor {
theme: ColorfulTheme,
}
#[cfg_attr(test, automock)]
pub trait InteractorPrompt {
fn input(&self, parms: PromptInputParms) -> Result<String>;
fn password(&self, parms: PromptPasswordParms) -> Result<String>;
fn confirm(&self, params: PromptConfirmParms) -> Result<bool>;
fn choice(&self, params: PromptChoiceParms) -> Result<usize>;
fn multi_choice(&self, params: PromptMultiChoiceParms) -> Result<Vec<usize>>;
}
impl InteractorPrompt for Interactor {
fn input(&self, parms: PromptInputParms) -> Result<String> {
let mut input = Input::with_theme(&self.theme);
input.with_prompt(parms.prompt).allow_empty(parms.optional);
if !parms.default.is_empty() {
input.default(parms.default);
}
input.report(parms.report);
Ok(input.interact_text()?)
}
fn password(&self, parms: PromptPasswordParms) -> Result<String> {
let mut p = Password::with_theme(&self.theme);
p.with_prompt(parms.prompt);
p.report(parms.report);
if parms.confirm {
p.with_confirmation("confirm password", "passwords didnt match...");
}
let pass: String = p.interact()?;
Ok(pass)
}
fn confirm(&self, params: PromptConfirmParms) -> Result<bool> {
let confirm: bool = Confirm::with_theme(&self.theme)
.with_prompt(params.prompt)
.default(params.default)
.interact()?;
Ok(confirm)
}
fn choice(&self, parms: PromptChoiceParms) -> Result<usize> {
let mut choice = dialoguer::Select::with_theme(&self.theme);
choice
.with_prompt(parms.prompt)
.report(parms.report)
.items(&parms.choices);
if let Some(default) = parms.default {
if std::env::var("NGITTEST").is_err() {
choice.default(default);
}
}
choice.interact().context("failed to get choice")
}
fn multi_choice(&self, parms: PromptMultiChoiceParms) -> Result<Vec<usize>> {
// the colorful theme is not very clear so falling back to default
let mut choice = dialoguer::MultiSelect::default();
choice
.with_prompt(parms.prompt)
.report(parms.report)
.items(&parms.choices);
if let Some(defaults) = parms.defaults {
choice.defaults(&defaults);
}
choice.interact().context("failed to get choice")
}
}
#[derive(Default)]
pub struct PromptInputParms {
pub prompt: String,
pub default: String,
pub report: bool,
pub optional: bool,
}
impl PromptInputParms {
pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
self.prompt = prompt.into();
self
}
pub fn with_default<S: Into<String>>(mut self, default: S) -> Self {
self.default = default.into();
self
}
pub fn optional(mut self) -> Self {
self.optional = true;
self
}
pub fn dont_report(mut self) -> Self {
self.report = false;
self
}
}
#[derive(Default)]
pub struct PromptPasswordParms {
pub prompt: String,
pub confirm: bool,
pub report: bool,
}
impl PromptPasswordParms {
pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
self.prompt = prompt.into();
self
}
pub const fn with_confirm(mut self) -> Self {
self.confirm = true;
self
}
pub fn dont_report(mut self) -> Self {
self.report = false;
self
}
}
#[derive(Default)]
pub struct PromptConfirmParms {
pub prompt: String,
pub default: bool,
}
impl PromptConfirmParms {
pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
self.prompt = prompt.into();
self
}
pub fn with_default(mut self, default: bool) -> Self {
self.default = default;
self
}
}
#[derive(Default)]
pub struct PromptChoiceParms {
pub prompt: String,
pub choices: Vec<String>,
pub default: Option<usize>,
pub report: bool,
}
impl PromptChoiceParms {
pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
self.prompt = prompt.into();
self.report = true;
self
}
pub fn dont_report(mut self) -> Self {
self.report = false;
self
}
pub fn with_choices(mut self, choices: Vec<String>) -> Self {
self.choices = choices;
self
}
pub fn with_default(mut self, index: usize) -> Self {
self.default = Some(index);
self
}
}
#[derive(Default)]
pub struct PromptMultiChoiceParms {
pub prompt: String,
pub choices: Vec<String>,
pub defaults: Option<Vec<bool>>,
pub report: bool,
}
impl PromptMultiChoiceParms {
pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
self.prompt = prompt.into();
self.report = true;
self
}
pub fn dont_report(mut self) -> Self {
self.report = false;
self
}
pub fn with_choices(mut self, choices: Vec<String>) -> Self {
self.choices = choices;
self
}
pub fn with_defaults(mut self, defaults: Vec<bool>) -> Self {
self.defaults = Some(defaults);
self
}
}
#[derive(Debug, Default)]
pub struct Printer {
printed_lines: Vec<String>,
}
impl Printer {
pub fn println(&mut self, line: String) {
eprintln!("{line}");
self.printed_lines.push(line);
}
pub fn println_with_custom_formatting(
&mut self,
line: String,
line_without_formatting: String,
) {
eprintln!("{line}");
self.printed_lines.push(line_without_formatting);
}
pub fn printlns(&mut self, lines: Vec<String>) {
for line in lines {
self.println(line);
}
}
pub fn clear_all(&mut self) {
let term = console::Term::stderr();
let _ = term.clear_last_lines(count_lines_per_msg_vec(
term.width(),
&self.printed_lines,
0,
));
self.printed_lines.drain(..);
}
}
pub fn count_lines_per_msg(width: u16, msg: &str, prefix_len: usize) -> usize {
if width == 0 {
return 1;
}
// ((msg_len+prefix) / width).ceil() implemented using Integer Arithmetic
((msg.chars().count() + prefix_len) + (width - 1) as usize) / width as usize
}
pub fn count_lines_per_msg_vec(width: u16, msgs: &[String], prefix_len: usize) -> usize {
msgs.iter()
.map(|msg| count_lines_per_msg(width, msg, prefix_len))
.sum()
}
|