Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format validator.rs #265

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions inquire/src/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> AnsiStrippable for T
where
T: AsRef<str>,
{
fn ansi_stripped_chars(&self) -> AnsiStrippedChars<'_> {
AnsiStrippedChars {
input: self.as_ref(),
}
}
}

pub trait AnsiAware {
fn ansi_aware_chars(&self) -> AnsiAwareChars<'_>;
}
Expand All @@ -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<AnsiAwareChar<'_>> = "\x1b[92mHello, \x1b[91mWorld!\x1b[0m"
Expand Down
12 changes: 6 additions & 6 deletions inquire/src/date_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
);
}

Expand Down
28 changes: 14 additions & 14 deletions inquire/src/prompts/one_liners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ where
/// # Returns
///
/// * `InquireResult<NaiveDate>`: 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
///
Expand Down Expand Up @@ -158,8 +158,8 @@ where
/// # Returns
///
/// * `InquireResult<f64>`: 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
///
Expand Down Expand Up @@ -193,8 +193,8 @@ where
/// # Returns
///
/// * `InquireResult<f32>`: 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
///
Expand Down Expand Up @@ -228,8 +228,8 @@ where
/// # Returns
///
/// * `InquireResult<u64>`: 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
///
Expand Down Expand Up @@ -263,8 +263,8 @@ where
/// # Returns
///
/// * `InquireResult<u32>`: 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
///
Expand Down Expand Up @@ -298,8 +298,8 @@ where
/// # Returns
///
/// * `InquireResult<usize>`: 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
///
Expand Down Expand Up @@ -333,8 +333,8 @@ where
/// # Returns
///
/// * `InquireResult<u128>`: 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
///
Expand Down
8 changes: 0 additions & 8 deletions inquire/src/terminal/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
6 changes: 1 addition & 5 deletions inquire/src/terminal/crossterm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl InputReader for CrosstermKeyReader {

impl CrosstermTerminal {
pub fn new() -> InquireResult<Self> {
crossterm::terminal::enable_raw_mode()?;
terminal::enable_raw_mode()?;

Ok(Self {
io: IO::Std(stderr()),
Expand Down Expand Up @@ -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()
}
Expand Down
1 change: 0 additions & 1 deletion inquire/src/terminal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()>;
}
Expand Down
8 changes: 2 additions & 6 deletions inquire/src/terminal/termion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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)
}
}

Expand Down
11 changes: 1 addition & 10 deletions inquire/src/terminal/test.rs
Original file line number Diff line number Diff line change
@@ -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<Key>,
pub output: VecDeque<MockTerminalToken>,
}

Expand All @@ -21,7 +20,6 @@ pub enum MockTerminalToken {
CursorDown(u16),
CursorLeft(u16),
CursorRight(u16),
CursorMoveToColumn(u16),
}

impl<T> From<T> for MockTerminalToken
Expand All @@ -37,7 +35,6 @@ impl MockTerminal {
pub fn new() -> Self {
Self {
size: TerminalSize::new(80, 40),
input: VecDeque::new(),
output: VecDeque::new(),
}
}
Expand Down Expand Up @@ -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(())
}
Expand Down
20 changes: 16 additions & 4 deletions inquire/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading