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
#![deny(missing_docs)]

//! Command-line input utilities.

#[macro_use] extern crate cfg_if;
extern crate rpassword;
extern crate tempfile;
#[cfg(unix)] extern crate termios;
#[cfg(windows)] extern crate winapi;

use interactive::Interactive;
use std::ffi::OsStr;
use std::fs::File;
use std::io::{Read, Write};
use std::process::Command;
use std::{env, io};
use tempfile::TempDir;

mod interactive;
mod util;

/// The ASCII escape character.
const ESC: u8 = 0x1B;

/// Print a question, in bold, without creating a new line.
pub fn ask(q: &str) {
    print!("\u{1B}[1m{}\u{1B}[0m", q);
    io::stdout().flush().unwrap();
}

/// Print a message of success, with a newline.
pub fn success(s: &str) {
    println!("\u{1B}[1;92m{}\u{1B}[0m", s);
}

/// Print an error message, with a newline.
pub fn error(s: &str) {
    println!("\u{1B}[1;91m{}\u{1B}[0m", s);
}

/// Ask for a password (the password will not be visible).
pub fn password() -> io::Result<String> {
    rpassword::read_password()
}

/// Ask for a line of text.
pub fn text() -> io::Result<String> {
    // Read up to the first newline or EOF.

    let mut out = String::new();
    io::stdin().read_line(&mut out)?;

    // Only capture up to the first newline.

    if let Some(mut newline) = out.find('\n') {
        if newline > 0 && out.as_bytes()[newline - 1] == b'\r' { newline -= 1; }
        out.truncate(newline);
    }

    Ok(out)
}

/// Ask a yes-or-no question.
///
/// `None` indicates an invalid response.
pub fn yesno(default: bool) -> io::Result<Option<bool>> {
    let s = text()?.to_lowercase();
    Ok(if s.is_empty() {
        Some(default)
    } else if "yes".starts_with(&s) {
        Some(true)
    } else if "no".starts_with(&s) {
        Some(false)
    } else {
        None
    })
}

/// Ask the user to enter some text through their editor.
///
/// We'll check the `VISUAL` environment variable, then `EDITOR`, and then
/// finally default to `vi`. The message will be the initial contents of the
/// file, and the result will be the final contents of the file, after the user
/// has quit their editor.
///
/// On Windows, the editor defaults to `notepad`.
pub fn editor(name: &str, message: &[u8]) -> io::Result<String> {
    // Create a temporary file with the message.

    let dir = TempDir::new()?;
    let path = dir.path().join(name);
    File::create(&path)?.write_all(message)?;

    // Get the editor command from the environment.

    let editor = env::var_os("VISUAL").or_else(|| env::var_os("EDITOR"));

    let editor = match editor {
        Some(ref editor) => editor,
        None => OsStr::new(
            #[cfg(windows)] "notepad",
            #[cfg(unix)] "vi"),
    };

    // Call the editor.

    Command::new(editor).arg(&path).spawn()?.wait()?;

    // Read the file back.

    let mut out = String::new();
    File::open(&path)?.read_to_string(&mut out)?;
    Ok(out)
}

/// The text to use when indicating whether an item is selected.
#[derive(Clone, Copy, Debug)]
pub struct Boxes<'a> {
    /// The text to use when an item is selected.
    pub on: &'a str,
    /// The text to use when an item is not selected.
    pub off: &'a str,
}

impl<'a> Default for Boxes<'a> {
    fn default() -> Self {
        Self {
            on: ">",
            off: " ",
        }
    }
}

/// Ask the user to choose exactly one option from a list.
pub fn choose<S: AsRef<str>>(boxes: Boxes, items: &[S]) -> io::Result<usize> {
    assert!(items.len() > 0);

    let stdin = io::stdin();
    let mut stdin = stdin.bytes();
    let mut selected = 0;

    let interactive = Interactive::start()?;

    loop {
        for (i, item) in items.iter().enumerate() {
            println!(
                "{} {}",
                if i == selected { boxes.on } else { boxes.off },
                item.as_ref(),
            );
        }

        match util::or2ro(stdin.next())? {
            Some(ESC) => match util::or2ro(stdin.next())? {
                Some(b'[') => match util::or2ro(stdin.next())? {
                    Some(b'A') => {
                        selected = selected.saturating_sub(1);
                    }
                    Some(b'B') => {
                        selected = selected.saturating_add(1).min(items.len() - 1);
                    }
                    None => break,
                    Some(_) => (),
                },
                None => break,
                Some(_) => (),
            },
            Some(b'\r') | Some(b'\n') => break,
            Some(b'k') => {
                selected = selected.saturating_sub(1);
            }
            Some(b'j') => {
                selected = selected.saturating_add(1).min(items.len() - 1);
            }
            None => break,
            Some(_) => (),
        }

        interactive.up(items.len());
        interactive.clear_right();
    }

    Ok(selected)
}