~goorzhel/radm

radm/src/tui.rs -rw-r--r-- 733 bytes
d11d708e — Antonio Gurgel 0.6.4 a month ago
                                                                                
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
//! Pre-session user interaction.

#![warn(missing_docs)]

use std::{fmt::Display, io::Write};

use anyhow::{Context, Error, Result};
use console::Term;

/// Whether user input should be echoed back. Use `Silent` when reading sensitive data such as passwords.
pub enum EchoInput {
    Echo,
    Silent,
}

/// Prompts user to supply some information through stdin.
pub fn prompt<S: AsRef<str> + Display>(prompt: S, echo: EchoInput) -> Result<String> {
    let mut term = Term::stdout();

    term.write_fmt(format_args!("{}: ", prompt))
        .context("Couldn't write to stdout")?;
    match echo {
        EchoInput::Echo => term.read_line(),
        EchoInput::Silent => term.read_secure_line(),
    }
    .map_err(Error::msg)
}