-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathlib.rs
More file actions
61 lines (52 loc) · 1.58 KB
/
lib.rs
File metadata and controls
61 lines (52 loc) · 1.58 KB
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
//! This is a library that provides utilities for command-line tools.
//! So far it only provides a function to read a line from stdin.
//! # Examples:
//! ```
//! use cli_utils::read_stdin;
//! let input = read_stdin();
//! ```
//! # Panics:
//! The `read_stdin` function will panic if it fails to read a line with a message "Failed to read input line".
use std::io::{BufRead, BufReader};
pub mod config;
pub mod colors;
/// This function reads a line from stdin and returns it as a String.
/// It will panic if it fails to read a line with a message "Failed to read input line".
/// # Examples:
/// ```
/// use cli_utils::read_stdin;
/// let input = read_stdin();
/// ```
pub fn read_stdin() -> String {
let stdin = std::io::stdin();
let mut reader = BufReader::new(stdin.lock());
_read_stdin(&mut reader)
}
fn _read_stdin<R: BufRead>(reader: &mut R) -> String {
let mut line = String::new();
reader
.read_line(&mut line)
.expect("Failed to read input line");
line.trim().to_string()
}
#[cfg(test)]
mod tests {
use super::_read_stdin;
use std::io::Cursor;
#[test]
fn test_read_input() {
let input = "Hello, world!\n";
let expected_output = "Hello, world!";
let mut reader = Cursor::new(input);
let output = _read_stdin(&mut reader);
assert_eq!(output, expected_output);
}
#[test]
fn test_read_input_empty() {
let input = "";
let expected_output = "";
let mut reader = Cursor::new(input);
let output = _read_stdin(&mut reader);
assert_eq!(output, expected_output);
}
}