ngit 2.4.1

nostr plugin for git
Documentation
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
use std::fmt;

use anyhow::{Context, Result, bail};
use console::Style;
use dialoguer::{
    Confirm, Input, Password,
    theme::{ColorfulTheme, Theme},
};
use indicatif::TermLike;
#[cfg(test)]
use mockall::*;

/// Sentinel error type indicating the error has already been printed to stderr.
///
/// When this propagates up to `main()`, it signals "already printed styled
/// output to stderr, don't double-print". This is the same pattern clap uses
/// internally.
#[derive(Debug)]
pub struct CliError;

impl fmt::Display for CliError {
    fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Empty display — the error message was already printed to stderr
        Ok(())
    }
}

impl std::error::Error for CliError {}

/// Print a styled CLI error to stderr and return an `anyhow::Error` wrapping
/// [`CliError`].
///
/// - `message`: the main error text (printed after the red `error:` prefix)
/// - `details`: flag/description pairs shown as gray indented lines (for
///   multiple missing fields). Descriptions are aligned to the longest flag.
/// - `suggestions`: command suggestions shown in yellow
///
/// This function does NOT call `process::exit()`. It prints to stderr and
/// returns an error that the caller should propagate with `?` or `return Err`.
pub fn cli_error(message: &str, details: &[(&str, &str)], suggestions: &[&str]) -> anyhow::Error {
    let dim = Style::new().for_stderr().color256(247);

    eprint!(
        "{} {}",
        console::style("error:").for_stderr().red(),
        message
    );
    if details.is_empty() {
        eprintln!();
    } else {
        let max_flag_len = details
            .iter()
            .map(|(flag, _)| flag.len())
            .max()
            .unwrap_or(0);
        eprintln!();
        for (flag, desc) in details {
            eprintln!(
                "  {:width$}  {}",
                dim.apply_to(flag),
                dim.apply_to(desc),
                width = max_flag_len
            );
        }
    }

    if !suggestions.is_empty() {
        eprintln!();
        for cmd in suggestions {
            eprintln!(
                "{}",
                console::style(format!("    {cmd}")).for_stderr().yellow(),
            );
        }
    }

    CliError.into()
}

#[derive(Default)]
pub struct Interactor {
    theme: ColorfulTheme,
    non_interactive: bool,
}

impl Interactor {
    pub fn new(non_interactive: bool) -> Self {
        Self {
            theme: ColorfulTheme::default(),
            non_interactive,
        }
    }

    /// Returns true if running in non-interactive mode (the default).
    /// Interactive mode is only enabled when NGIT_INTERACTIVE_MODE env var is
    /// set (via -i flag).
    pub fn is_non_interactive() -> bool {
        std::env::var("NGIT_INTERACTIVE_MODE").is_err()
    }
}

#[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> {
        if self.non_interactive || Self::is_non_interactive() {
            if parms.optional || !parms.default.is_empty() {
                return Ok(parms.default);
            }
            let flag_hint = parms
                .flag_name
                .as_ref()
                .map(|f| format!(" (provide {} or use -i/-d)", f))
                .unwrap_or_else(|| " (use -i for interactive mode or -d for defaults)".to_string());
            bail!(
                "interactive input required but running in non-interactive mode: {}{}",
                parms.prompt,
                flag_hint
            );
        }
        let mut input = Input::with_theme(&self.theme)
            .with_prompt(parms.prompt)
            .allow_empty(parms.optional)
            .report(parms.report);
        if !parms.default.is_empty() {
            input = input.default(parms.default);
        }
        Ok(input.interact_text()?)
    }
    fn password(&self, parms: PromptPasswordParms) -> Result<String> {
        if self.non_interactive || Self::is_non_interactive() {
            bail!(
                "password input required but running in non-interactive mode: {}",
                parms.prompt
            );
        }
        let mut p = Password::with_theme(&self.theme)
            .with_prompt(parms.prompt)
            .report(parms.report);
        if parms.confirm {
            p = p.with_confirmation("confirm password", "passwords didnt match...");
        }
        let pass: String = p.interact()?;
        Ok(pass)
    }
    fn confirm(&self, params: PromptConfirmParms) -> Result<bool> {
        if self.non_interactive || Self::is_non_interactive() {
            return Ok(params.default);
        }
        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> {
        if self.non_interactive || Self::is_non_interactive() {
            if let Some(default) = parms.default {
                return Ok(default);
            }
            bail!(
                "interactive choice required but running in non-interactive mode: {}",
                parms.prompt
            );
        }
        let mut choice = dialoguer::Select::with_theme(&self.theme)
            .with_prompt(parms.prompt)
            .report(parms.report)
            .items(&parms.choices);
        if let Some(default) = parms.default {
            if std::env::var("NGITTEST").is_err() {
                choice = choice.default(default);
            }
        }
        choice.interact().context("failed to get choice")
    }
    fn multi_choice(&self, parms: PromptMultiChoiceParms) -> Result<Vec<usize>> {
        if self.non_interactive || Self::is_non_interactive() {
            if let Some(defaults) = &parms.defaults {
                return Ok(defaults
                    .iter()
                    .enumerate()
                    .filter(|(_, &selected)| selected)
                    .map(|(i, _)| i)
                    .collect());
            }
            return Ok(vec![]); // Empty selection if no defaults
        }
        // the colorful theme is not very clear so falling back to default
        let mut choice = dialoguer::MultiSelect::default()
            .with_prompt(parms.prompt)
            .report(parms.report)
            .items(&parms.choices);
        if let Some(defaults) = parms.defaults {
            choice = choice.defaults(&defaults);
        }
        choice.interact().context("failed to get choice")
    }
}

/// Parameters for interactive input prompts.
///
/// Supports both interactive and non-interactive modes:
/// - Interactive mode (NGIT_INTERACTIVE_MODE set): prompts user
/// - Non-interactive mode (default): returns default value or errors
///
/// The `flag_name` field improves error messages by telling users
/// which CLI flag would provide the missing value.
pub struct PromptInputParms {
    pub prompt: String,
    pub default: String,
    pub report: bool,
    pub optional: bool,
    pub flag_name: Option<String>,
}

impl Default for PromptInputParms {
    fn default() -> Self {
        Self {
            prompt: String::new(),
            default: String::new(),
            optional: false,
            report: true,
            flag_name: None,
        }
    }
}

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
    }

    pub fn with_flag_name<S: Into<String>>(mut self, flag_name: S) -> Self {
        self.flag_name = Some(flag_name.into());
        self
    }
}

pub struct PromptPasswordParms {
    pub prompt: String,
    pub confirm: bool,
    pub report: bool,
}

impl Default for PromptPasswordParms {
    fn default() -> Self {
        Self {
            prompt: String::new(),
            confirm: false,
            report: true,
        }
    }
}

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
    }
}

pub struct PromptChoiceParms {
    pub prompt: String,
    pub choices: Vec<String>,
    pub default: Option<usize>,
    pub report: bool,
}

impl Default for PromptChoiceParms {
    fn default() -> Self {
        Self {
            prompt: String::new(),
            choices: vec![],
            default: None,
            report: true,
        }
    }
}

impl PromptChoiceParms {
    pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
        self.prompt = prompt.into();
        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
    }
}

pub struct PromptMultiChoiceParms {
    pub prompt: String,
    pub choices: Vec<String>,
    pub defaults: Option<Vec<bool>>,
    pub report: bool,
}

impl Default for PromptMultiChoiceParms {
    fn default() -> Self {
        Self {
            prompt: String::new(),
            choices: vec![],
            defaults: None,
            report: true,
        }
    }
}

impl PromptMultiChoiceParms {
    pub fn with_prompt<S: Into<String>>(mut self, prompt: S) -> Self {
        self.prompt = prompt.into();
        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
    }
}

pub fn multi_select_with_custom_value<F>(
    prompt: &str,
    custom_choice_prompt: &str,
    mut choices: Vec<String>,
    mut defaults: Vec<bool>,
    validate_choice: F,
) -> Result<Vec<String>>
where
    F: Fn(&str) -> Result<String>,
{
    let mut selected_choices = vec![];

    // Loop to allow users to add more choices
    loop {
        // Add 'add another' option at the end of the choices
        let mut current_choices = choices.clone();
        current_choices.push(if current_choices.is_empty() {
            "add".to_string()
        } else {
            "add another".to_string()
        });

        // Create default selections based on the provided defaults
        let mut current_defaults = defaults.clone();
        current_defaults.push(false); // 'add'/'add another' should not be selected by default

        // Prompt for selections
        let selected_indices: Vec<usize> = Interactor::default().multi_choice(
            PromptMultiChoiceParms::default()
                .with_prompt(prompt)
                .dont_report()
                .with_choices(current_choices.clone())
                .with_defaults(current_defaults),
        )?;

        // Collect selected choices and update defaults to reflect the user's actual
        // selections
        selected_choices.clear(); // Clear previous selections to update
        for (i, default) in defaults.iter_mut().enumerate() {
            *default = selected_indices.contains(&i);
        }
        for &index in &selected_indices {
            if index < choices.len() {
                // Exclude 'add another' option
                selected_choices.push(choices[index].clone());
            }
        }

        // Check if 'add another' was selected
        if selected_indices.contains(&(choices.len())) {
            // Last index is 'add another'
            let mut new_choice: String;
            loop {
                new_choice = Interactor::default().input(
                    PromptInputParms::default()
                        .with_prompt(custom_choice_prompt)
                        .dont_report()
                        .optional(),
                )?;

                if new_choice.is_empty() {
                    break;
                }
                // Validate the new choice
                match validate_choice(&new_choice) {
                    Ok(valid_choice) => {
                        new_choice = valid_choice; // Use the fixed version of the input
                        break; // Valid choice, exit the loop
                    }
                    Err(err) => {
                        // Inform the user about the validation error
                        println!("Error: {err}");
                    }
                }
            }

            // Add the new choice to the choices vector
            if !new_choice.is_empty() {
                choices.push(new_choice.clone()); // Add new choice to the end of the list
                selected_choices.push(new_choice); // Automatically select the new choice
                defaults.push(true); // Set the new choice as selected by default
            }
        } else {
            // Exit the loop if 'add another' was not selected
            break;
        }
    }

    Ok(selected_choices)
}

pub fn show_multi_input_prompt_success(label: &str, values: &[String]) {
    let values_str: Vec<&str> = values.iter().map(std::string::String::as_str).collect();
    eprintln!("{}", {
        let mut s = String::new();
        let _ = ColorfulTheme::default().format_multi_select_prompt_selection(
            &mut s,
            label,
            &values_str,
        );
        s
    });
}

#[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()
}