diff --git a/Cargo.toml b/Cargo.toml index 97c2b68..94586ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rpassword" -version = "7.0.0" +version = "7.1.0" authors = ["Conrad Kleinespel "] description = "Read passwords in console applications." license = "Apache-2.0" @@ -10,6 +10,7 @@ documentation = "https://docs.rs/rpassword/" readme = "README.md" keywords = ["read", "password", "security", "pass", "getpass"] edition = "2018" +rust-version = "1.60" [features] serde = ["dep:serde", "dep:serde_json"] diff --git a/README.md b/README.md index 9f2f736..ee55955 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ Here's a list of existing `rpassword` contributors: * [@john-sharratt](https://github.com/john-sharratt) * [@joshuef](https://github.com/joshuef) * [@longshorej](https://github.com/longshorej) +* [@LSchallot](https://github.com/LSchallot) * [@nicokoch](https://github.com/nicokoch) * [@NovaliX-Dev](https://github.com/NovaliX-Dev) * [@petevine](https://github.com/petevine) diff --git a/src/rpassword/all.rs b/src/rpassword/all.rs index 343c09d..66ad056 100644 --- a/src/rpassword/all.rs +++ b/src/rpassword/all.rs @@ -1,4 +1,4 @@ -use crate::rutil::fix_new_line::fix_new_line; +use crate::rutil::fix_line_issues::fix_line_issues; use crate::rutil::print_tty::{print_tty, print_writer}; use crate::rutil::safe_string::SafeString; use std::io::{BufRead, Write}; @@ -22,7 +22,7 @@ mod wasm { let mut password = super::SafeString::new(); reader.read_line(&mut password)?; - super::fix_new_line(password.into_inner()) + super::fix_line_issues(password.into_inner()) } } @@ -104,7 +104,7 @@ mod unix { std::mem::drop(hidden_input); - super::fix_new_line(password.into_inner()) + super::fix_line_issues(password.into_inner()) } } @@ -199,7 +199,7 @@ mod windows { std::mem::drop(hidden_input); - super::fix_new_line(password.into_inner()) + super::fix_line_issues(password.into_inner()) } } @@ -215,7 +215,7 @@ pub fn read_password_from_bufread(reader: &mut impl BufRead) -> std::io::Result< let mut password = SafeString::new(); reader.read_line(&mut password)?; - fix_new_line(password.into_inner()) + fix_line_issues(password.into_inner()) } /// Prompts on the TTY and then reads a password from anything that implements BufRead diff --git a/src/rutil.rs b/src/rutil.rs index 6843278..8afead9 100644 --- a/src/rutil.rs +++ b/src/rutil.rs @@ -1,5 +1,5 @@ pub mod atty; -pub mod fix_new_line; +pub mod fix_line_issues; pub mod print_tty; pub mod safe_string; #[cfg(feature = "serde")] diff --git a/src/rutil/fix_new_line.rs b/src/rutil/fix_line_issues.rs similarity index 57% rename from src/rutil/fix_new_line.rs rename to src/rutil/fix_line_issues.rs index 2d8b0ef..9f7a695 100644 --- a/src/rutil/fix_new_line.rs +++ b/src/rutil/fix_line_issues.rs @@ -1,5 +1,5 @@ /// Normalizes the return of `read_line()` in the context of a CLI application -pub fn fix_new_line(mut line: String) -> std::io::Result { +pub fn fix_line_issues(mut line: String) -> std::io::Result { if !line.ends_with('\n') { return Err(std::io::Error::new( std::io::ErrorKind::UnexpectedEof, @@ -15,5 +15,13 @@ pub fn fix_new_line(mut line: String) -> std::io::Result { line.pop(); } + // Ctrl-U should remove the line in terminals + if line.contains('') { + line = match line.rfind('') { + Some(last_ctrl_u_index) => line[last_ctrl_u_index + 1..].to_string(), + None => line, + }; + } + Ok(line) } diff --git a/tests/no-terminal.rs b/tests/no-terminal.rs index 89a595d..b7bd171 100644 --- a/tests/no-terminal.rs +++ b/tests/no-terminal.rs @@ -55,3 +55,16 @@ fn can_read_from_redirected_input_many_times() { let response = crate::read_password_from_bufread(&mut reader_lf).unwrap(); assert_eq!(response, "Another mocked response."); } + +#[test] +fn can_read_from_input_ctrl_u() { + close_stdin(); + + let mut reader_ctrl_u = Cursor::new(&b"A mocked response.Another mocked response.\n"[..]); + let response = crate::read_password_from_bufread(&mut reader_ctrl_u).unwrap(); + assert_eq!(response, "Another mocked response."); + + let mut reader_ctrl_u_at_end = Cursor::new(&b"A mocked response.\n"[..]); + let response = crate::read_password_from_bufread(&mut reader_ctrl_u_at_end).unwrap(); + assert_eq!(response, ""); +}