jabba_lib/
jconsole.rs

1//! console
2
3use crate::jstring;
4use std::io::{self, Write};
5
6/// Flushes stdout.
7///
8/// # Examples
9///
10/// ```
11/// jabba_lib::jconsole::flush();
12/// ```
13pub fn flush() {
14    io::stdout().flush().unwrap();
15}
16
17/// Shows a prompt to the user and reads a line from stdin.
18///
19/// It is similar to Python's `input()` function.
20///
21/// # Examples
22///
23/// ```ignore
24/// let name = jabba_lib::jconsole::input("Name: ");
25/// // assume you type "Anna" (without quotes) and press Enter
26/// assert_eq!(name, "Anna");
27/// ```
28pub fn input(prompt: &str) -> String {
29    print!("{}", prompt);
30    flush();
31
32    let mut text = String::new();
33    io::stdin()
34        .read_line(&mut text)
35        .expect("Failed to read line from stdin");
36
37    jstring::chomp(&mut text);
38    text
39}