diff --git a/inquire/src/ansi.rs b/inquire/src/ansi.rs index 4b651c6e..df08d22f 100644 --- a/inquire/src/ansi.rs +++ b/inquire/src/ansi.rs @@ -163,22 +163,6 @@ impl<'a> Iterator for AnsiStrippedChars<'a> { } } -/// Constructs an iterator over the chars of the input string, stripping away ANSI escape codes. -pub trait AnsiStrippable { - fn ansi_stripped_chars(&self) -> AnsiStrippedChars<'_>; -} - -impl AnsiStrippable for T -where - T: AsRef, -{ - fn ansi_stripped_chars(&self) -> AnsiStrippedChars<'_> { - AnsiStrippedChars { - input: self.as_ref(), - } - } -} - pub trait AnsiAware { fn ansi_aware_chars(&self) -> AnsiAwareChars<'_>; } @@ -198,38 +182,6 @@ where mod tests { use super::*; - macro_rules! assert_stripped_eq { - ($input:expr, $expected:expr) => { - let stripped: String = $input.ansi_stripped_chars().collect(); - assert_eq!(&stripped, $expected); - }; - } - - #[test] - fn test_normal_ansi_escapes() { - assert_stripped_eq!("1\x1b[0m2", "12"); - assert_stripped_eq!("\x1b[92mHello, \x1b[91mWorld!\x1b[0m", "Hello, World!"); - assert_stripped_eq!("\x1b[7@Hi", "Hi"); - assert_stripped_eq!( - "\x1b]0;Set The Terminal Title To This\u{9c}Print This", - "Print This" - ); - assert_stripped_eq!("\x1b[96", ""); - } - - #[test] - // Please don't use ansi escapes like these! - fn test_inconsistencies() { - // Some terminals will print "String\u{9c}Hello World", others will print nothing. - assert_stripped_eq!("\x1b[\x1b]String\u{9c}Hello World", "Hello World"); - - // Kitty will print "[38;5;43mABCD", but most will print "ABCD" - assert_stripped_eq!("\x1b[\x1b[38;5;43mAB\x1b[48;5;10mCD\x1b[0m", "ABCD"); - - // Kitty will print "[96mCat\n", but most will print "Cat\n" - assert_stripped_eq!("\x1b\x19[96mCat\x1b[0m\n", "Cat\n"); - } - #[test] fn ansi_aware_test_normal_ansi_escapes() { let chars: Vec> = "\x1b[92mHello, \x1b[91mWorld!\x1b[0m" diff --git a/inquire/src/date_utils.rs b/inquire/src/date_utils.rs index cc348f46..5936eff7 100644 --- a/inquire/src/date_utils.rs +++ b/inquire/src/date_utils.rs @@ -7,7 +7,7 @@ pub fn get_current_date() -> NaiveDate { } pub fn get_start_date(month: chrono::Month, year: i32) -> NaiveDate { - chrono::NaiveDate::from_ymd_opt(year, month.number_from_month(), 1).unwrap() + NaiveDate::from_ymd_opt(year, month.number_from_month(), 1).unwrap() } pub fn get_month(month: u32) -> chrono::Month { @@ -43,23 +43,23 @@ mod tests { fn test_get_start_date() { assert_eq!( get_start_date(chrono::Month::January, 2021), - chrono::NaiveDate::from_ymd_opt(2021, 1, 1).unwrap() + NaiveDate::from_ymd_opt(2021, 1, 1).unwrap() ); assert_eq!( get_start_date(chrono::Month::February, 2021), - chrono::NaiveDate::from_ymd_opt(2021, 2, 1).unwrap() + NaiveDate::from_ymd_opt(2021, 2, 1).unwrap() ); assert_eq!( get_start_date(chrono::Month::March, 2021), - chrono::NaiveDate::from_ymd_opt(2021, 3, 1).unwrap() + NaiveDate::from_ymd_opt(2021, 3, 1).unwrap() ); assert_eq!( get_start_date(chrono::Month::December, 1883), - chrono::NaiveDate::from_ymd_opt(1883, 12, 1).unwrap() + NaiveDate::from_ymd_opt(1883, 12, 1).unwrap() ); assert_eq!( get_start_date(chrono::Month::June, 3042), - chrono::NaiveDate::from_ymd_opt(3042, 6, 1).unwrap() + NaiveDate::from_ymd_opt(3042, 6, 1).unwrap() ); } diff --git a/inquire/src/prompts/one_liners.rs b/inquire/src/prompts/one_liners.rs index d6d76bdc..b348749c 100644 --- a/inquire/src/prompts/one_liners.rs +++ b/inquire/src/prompts/one_liners.rs @@ -122,8 +122,8 @@ where /// # Returns /// /// * `InquireResult`: An enum that represents the result of the prompt operation. If the operation is successful, -/// it returns `InquireResult::Ok(NaiveDate)` where NaiveDate's value is the date selected by the user. If the operation -/// encounters an error, it returns `InquireResult::Err(InquireError)`. +/// it returns `InquireResult::Ok(NaiveDate)` where NaiveDate's value is the date selected by the user. If the operation +/// encounters an error, it returns `InquireResult::Err(InquireError)`. /// /// # Example /// @@ -158,8 +158,8 @@ where /// # Returns /// /// * `InquireResult`: An enum that represents the result of the prompt operation. If the operation is successful, -/// it returns `InquireResult::Ok(f64)` where f64 is the number parsed from the user's input. If the operation -/// encounters an error, it returns `InquireResult::Err(InquireError)`. +/// it returns `InquireResult::Ok(f64)` where f64 is the number parsed from the user's input. If the operation +/// encounters an error, it returns `InquireResult::Err(InquireError)`. /// /// # Example /// @@ -193,8 +193,8 @@ where /// # Returns /// /// * `InquireResult`: An enum that represents the result of the prompt operation. If the operation is successful, -/// it returns `InquireResult::Ok(f32)` where f32 is the number parsed from the user's input. If the operation -/// encounters an error, it returns `InquireResult::Err(InquireError)`. +/// it returns `InquireResult::Ok(f32)` where f32 is the number parsed from the user's input. If the operation +/// encounters an error, it returns `InquireResult::Err(InquireError)`. /// /// # Example /// @@ -228,8 +228,8 @@ where /// # Returns /// /// * `InquireResult`: An enum that represents the result of the prompt operation. If the operation is successful, -/// it returns `InquireResult::Ok(u64)` where u64 is the number parsed from the user's input. If the operation -/// encounters an error, it returns `InquireResult::Err(InquireError)`. +/// it returns `InquireResult::Ok(u64)` where u64 is the number parsed from the user's input. If the operation +/// encounters an error, it returns `InquireResult::Err(InquireError)`. /// /// # Example /// @@ -263,8 +263,8 @@ where /// # Returns /// /// * `InquireResult`: An enum that represents the result of the prompt operation. If the operation is successful, -/// it returns `InquireResult::Ok(u32)` where u32 is the number parsed from the user's input. If the operation -/// encounters an error, it returns `InquireResult::Err(InquireError)`. +/// it returns `InquireResult::Ok(u32)` where u32 is the number parsed from the user's input. If the operation +/// encounters an error, it returns `InquireResult::Err(InquireError)`. /// /// # Example /// @@ -298,8 +298,8 @@ where /// # Returns /// /// * `InquireResult`: An enum that represents the result of the prompt operation. If the operation is successful, -/// it returns `InquireResult::Ok(usize)` where usize is the number parsed from the user's input. If the operation -/// encounters an error, it returns `InquireResult::Err(InquireError)`. +/// it returns `InquireResult::Ok(usize)` where usize is the number parsed from the user's input. If the operation +/// encounters an error, it returns `InquireResult::Err(InquireError)`. /// /// # Example /// @@ -333,8 +333,8 @@ where /// # Returns /// /// * `InquireResult`: An enum that represents the result of the prompt operation. If the operation is successful, -/// it returns `InquireResult::Ok(u128)` where u128 is the number parsed from the user's input. If the operation -/// encounters an error, it returns `InquireResult::Err(InquireError)`. +/// it returns `InquireResult::Ok(u128)` where u128 is the number parsed from the user's input. If the operation +/// encounters an error, it returns `InquireResult::Err(InquireError)`. /// /// # Example /// diff --git a/inquire/src/terminal/console.rs b/inquire/src/terminal/console.rs index 1c56c487..ae0e902b 100644 --- a/inquire/src/terminal/console.rs +++ b/inquire/src/terminal/console.rs @@ -59,14 +59,6 @@ impl Terminal for ConsoleTerminal { } } - fn cursor_move_to_column(&mut self, idx: u16) -> Result<()> { - // console has no built-in method to set cursor column ¯\_(ツ)_/¯ - self.term.move_cursor_left(1000)?; - self.term.move_cursor_right(idx as usize)?; - - Ok(()) - } - fn flush(&mut self) -> Result<()> { self.term.flush() } diff --git a/inquire/src/terminal/crossterm.rs b/inquire/src/terminal/crossterm.rs index 3d0a9cdc..be64c0f2 100644 --- a/inquire/src/terminal/crossterm.rs +++ b/inquire/src/terminal/crossterm.rs @@ -46,7 +46,7 @@ impl InputReader for CrosstermKeyReader { impl CrosstermTerminal { pub fn new() -> InquireResult { - crossterm::terminal::enable_raw_mode()?; + terminal::enable_raw_mode()?; Ok(Self { io: IO::Std(stderr()), @@ -125,10 +125,6 @@ impl Terminal for CrosstermTerminal { } } - fn cursor_move_to_column(&mut self, idx: u16) -> Result<()> { - self.write_command(cursor::MoveToColumn(idx)) - } - fn flush(&mut self) -> Result<()> { self.get_writer().flush() } diff --git a/inquire/src/terminal/mod.rs b/inquire/src/terminal/mod.rs index 0cbf379a..0ce8cae7 100644 --- a/inquire/src/terminal/mod.rs +++ b/inquire/src/terminal/mod.rs @@ -37,7 +37,6 @@ pub trait Terminal: Sized { fn cursor_down(&mut self, cnt: u16) -> Result<()>; fn cursor_left(&mut self, cnt: u16) -> Result<()>; fn cursor_right(&mut self, cnt: u16) -> Result<()>; - fn cursor_move_to_column(&mut self, idx: u16) -> Result<()>; fn flush(&mut self) -> Result<()>; } diff --git a/inquire/src/terminal/termion.rs b/inquire/src/terminal/termion.rs index 6fcd6464..2537371a 100644 --- a/inquire/src/terminal/termion.rs +++ b/inquire/src/terminal/termion.rs @@ -145,10 +145,6 @@ impl<'a> Terminal for TermionTerminal<'a> { } } - fn cursor_move_to_column(&mut self, idx: u16) -> Result<()> { - write!(self.get_writer(), "\x1b[{}G", idx.saturating_add(1)) - } - fn flush(&mut self) -> Result<()> { self.get_writer().flush() } @@ -196,11 +192,11 @@ impl<'a> Terminal for TermionTerminal<'a> { } fn cursor_hide(&mut self) -> Result<()> { - write!(self.get_writer(), "{}", termion::cursor::Hide) + write!(self.get_writer(), "{}", cursor::Hide) } fn cursor_show(&mut self) -> Result<()> { - write!(self.get_writer(), "{}", termion::cursor::Show) + write!(self.get_writer(), "{}", cursor::Show) } } diff --git a/inquire/src/terminal/test.rs b/inquire/src/terminal/test.rs index dc0c7add..5d1350bc 100644 --- a/inquire/src/terminal/test.rs +++ b/inquire/src/terminal/test.rs @@ -1,12 +1,11 @@ use std::{collections::VecDeque, fmt::Display}; -use crate::ui::{Key, Styled}; +use crate::ui::Styled; use super::{Terminal, TerminalSize}; pub struct MockTerminal { pub size: TerminalSize, - pub input: VecDeque, pub output: VecDeque, } @@ -21,7 +20,6 @@ pub enum MockTerminalToken { CursorDown(u16), CursorLeft(u16), CursorRight(u16), - CursorMoveToColumn(u16), } impl From for MockTerminalToken @@ -37,7 +35,6 @@ impl MockTerminal { pub fn new() -> Self { Self { size: TerminalSize::new(80, 40), - input: VecDeque::new(), output: VecDeque::new(), } } @@ -125,12 +122,6 @@ impl Terminal for MockTerminal { Ok(()) } - fn cursor_move_to_column(&mut self, idx: u16) -> std::io::Result<()> { - let token = MockTerminalToken::CursorMoveToColumn(idx); - self.output.push_back(token); - Ok(()) - } - fn flush(&mut self) -> std::io::Result<()> { Ok(()) } diff --git a/inquire/src/validator.rs b/inquire/src/validator.rs index 077123ac..3687e2ee 100644 --- a/inquire/src/validator.rs +++ b/inquire/src/validator.rs @@ -721,7 +721,10 @@ mod validators_test { let validator: &dyn StringValidator = &validator; assert!(matches!(validator.validate("five!")?, Validation::Valid)); - assert!(matches!(validator.validate("♥️♥️♥️♥️♥️")?, Validation::Valid)); + assert!(matches!( + validator.validate("♥️♥️♥️♥️♥️")?, + Validation::Valid + )); assert!(matches!( validator.validate("🤦🏼‍♂️🤦🏼‍♂️🤦🏼‍♂️🤦🏼‍♂️🤦🏼‍♂️")?, Validation::Valid @@ -771,7 +774,10 @@ mod validators_test { assert!(matches!(validator.validate("")?, Validation::Valid)); assert!(matches!(validator.validate("five!")?, Validation::Valid)); - assert!(matches!(validator.validate("♥️♥️♥️♥️♥️")?, Validation::Valid)); + assert!(matches!( + validator.validate("♥️♥️♥️♥️♥️")?, + Validation::Valid + )); assert!(matches!( validator.validate("🤦🏼‍♂️🤦🏼‍♂️🤦🏼‍♂️🤦🏼‍♂️🤦🏼‍♂️")?, Validation::Valid @@ -855,8 +861,14 @@ mod validators_test { assert!(matches!(validator.validate("five!")?, Validation::Valid)); assert!(matches!(validator.validate("five!!!")?, Validation::Valid)); - assert!(matches!(validator.validate("♥️♥️♥️♥️♥️")?, Validation::Valid)); - assert!(matches!(validator.validate("♥️♥️♥️♥️♥️♥️")?, Validation::Valid)); + assert!(matches!( + validator.validate("♥️♥️♥️♥️♥️")?, + Validation::Valid + )); + assert!(matches!( + validator.validate("♥️♥️♥️♥️♥️♥️")?, + Validation::Valid + )); assert!(matches!( validator.validate("🤦🏼‍♂️🤦🏼‍♂️🤦🏼‍♂️🤦🏼‍♂️🤦🏼‍♂️")?, Validation::Valid