diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-09-24 15:03:58 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-09-24 19:48:11 +0100 |
| commit | d01380f7b3efebc9c40a2e71c2ddd635fa936be4 (patch) | |
| tree | 58af24e8981bfa4b0c8dc330ea8903dac3bbc479 /src/lib/cli_interactor.rs | |
| parent | 0acc9768c2aff4bf4495c89698f29b56dae2f4b4 (diff) | |
feat(login): login via nip46 QR code
or nostrconnect url string which is a much better UX flow for nip46
Diffstat (limited to 'src/lib/cli_interactor.rs')
| -rw-r--r-- | src/lib/cli_interactor.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/lib/cli_interactor.rs b/src/lib/cli_interactor.rs index 4cf6357..dcaccf1 100644 --- a/src/lib/cli_interactor.rs +++ b/src/lib/cli_interactor.rs | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | use anyhow::{Context, Result}; | 1 | use anyhow::{Context, Result}; |
| 2 | use dialoguer::{theme::ColorfulTheme, Confirm, Input, Password}; | 2 | use dialoguer::{theme::ColorfulTheme, Confirm, Input, Password}; |
| 3 | use indicatif::TermLike; | ||
| 3 | #[cfg(test)] | 4 | #[cfg(test)] |
| 4 | use mockall::*; | 5 | use mockall::*; |
| 5 | 6 | ||
| @@ -184,3 +185,50 @@ impl PromptMultiChoiceParms { | |||
| 184 | self | 185 | self |
| 185 | } | 186 | } |
| 186 | } | 187 | } |
| 188 | |||
| 189 | #[derive(Debug, Default)] | ||
| 190 | pub struct Printer { | ||
| 191 | printed_lines: Vec<String>, | ||
| 192 | } | ||
| 193 | impl Printer { | ||
| 194 | pub fn println(&mut self, line: String) { | ||
| 195 | eprintln!("{line}"); | ||
| 196 | self.printed_lines.push(line); | ||
| 197 | } | ||
| 198 | pub fn println_with_custom_formatting( | ||
| 199 | &mut self, | ||
| 200 | line: String, | ||
| 201 | line_without_formatting: String, | ||
| 202 | ) { | ||
| 203 | eprintln!("{line}"); | ||
| 204 | self.printed_lines.push(line_without_formatting); | ||
| 205 | } | ||
| 206 | pub fn printlns(&mut self, lines: Vec<String>) { | ||
| 207 | for line in lines { | ||
| 208 | self.println(line); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | pub fn clear_all(&mut self) { | ||
| 212 | let term = console::Term::stderr(); | ||
| 213 | let _ = term.clear_last_lines(count_lines_per_msg_vec( | ||
| 214 | term.width(), | ||
| 215 | &self.printed_lines, | ||
| 216 | 0, | ||
| 217 | )); | ||
| 218 | self.printed_lines.drain(..); | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | pub fn count_lines_per_msg(width: u16, msg: &str, prefix_len: usize) -> usize { | ||
| 223 | if width == 0 { | ||
| 224 | return 1; | ||
| 225 | } | ||
| 226 | // ((msg_len+prefix) / width).ceil() implemented using Integer Arithmetic | ||
| 227 | ((msg.chars().count() + prefix_len) + (width - 1) as usize) / width as usize | ||
| 228 | } | ||
| 229 | |||
| 230 | pub fn count_lines_per_msg_vec(width: u16, msgs: &[String], prefix_len: usize) -> usize { | ||
| 231 | msgs.iter() | ||
| 232 | .map(|msg| count_lines_per_msg(width, msg, prefix_len)) | ||
| 233 | .sum() | ||
| 234 | } | ||