rtoolbox/
fix_line_issues.rs

1/// Normalizes the return of `read_line()` in the context of a CLI application
2pub fn fix_line_issues(mut line: String) -> std::io::Result<String> {
3    if !line.ends_with('\n') {
4        return Err(std::io::Error::new(
5            std::io::ErrorKind::UnexpectedEof,
6            "unexpected end of file",
7        ));
8    }
9
10    // Remove the \n from the line.
11    line.pop();
12
13    // Remove the \r from the line if present
14    if line.ends_with('\r') {
15        line.pop();
16    }
17
18    // Ctrl-U should remove the line in terminals
19    if line.contains('') {
20        line = match line.rfind('') {
21            Some(last_ctrl_u_index) => line[last_ctrl_u_index + 1..].to_string(),
22            None => line,
23        };
24    }
25
26    Ok(line)
27}