Skip to content

Commit

Permalink
src: run clippy and fix issues
Browse files Browse the repository at this point in the history
Signed-off-by: jaudiger <[email protected]>
  • Loading branch information
jaudiger committed May 17, 2024
1 parent eaf4f5f commit 522e6eb
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 74 deletions.
40 changes: 23 additions & 17 deletions src/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Style {

// Prefix everything with reset characters if needed
if self.prefix_with_reset {
write!(f, "\x1B[0m")?
write!(f, "\x1B[0m")?;
}

// Write the codes’ prefix, then write numbers, separated by
Expand All @@ -31,33 +31,33 @@ impl Style {
written_anything = true;
#[cfg(feature = "gnu_legacy")]
write!(f, "0")?;
write!(f, "{}", c)?;
write!(f, "{c}")?;
Ok(())
};

if self.is_bold {
write_char('1')?
write_char('1')?;
}
if self.is_dimmed {
write_char('2')?
write_char('2')?;
}
if self.is_italic {
write_char('3')?
write_char('3')?;
}
if self.is_underline {
write_char('4')?
write_char('4')?;
}
if self.is_blink {
write_char('5')?
write_char('5')?;
}
if self.is_reverse {
write_char('7')?
write_char('7')?;
}
if self.is_hidden {
write_char('8')?
write_char('8')?;
}
if self.is_strikethrough {
write_char('9')?
write_char('9')?;
}
}

Expand Down Expand Up @@ -111,7 +111,7 @@ impl Style {
if self.is_plain() {
Ok(())
} else {
write!(f, "{}", RESET)
write!(f, "{RESET}")
}
}
}
Expand All @@ -131,8 +131,8 @@ impl Color {
Color::Magenta => write!(f, "35"),
Color::Cyan => write!(f, "36"),
Color::White => write!(f, "37"),
Color::Fixed(num) => write!(f, "38;5;{}", num),
Color::Rgb(r, g, b) => write!(f, "38;2;{};{};{}", r, g, b),
Color::Fixed(num) => write!(f, "38;5;{num}"),
Color::Rgb(r, g, b) => write!(f, "38;2;{r};{g};{b}"),
Color::Default => write!(f, "39"),
Color::DarkGray => write!(f, "90"),
Color::LightRed => write!(f, "91"),
Expand All @@ -157,8 +157,8 @@ impl Color {
Color::Magenta => write!(f, "45"),
Color::Cyan => write!(f, "46"),
Color::White => write!(f, "47"),
Color::Fixed(num) => write!(f, "48;5;{}", num),
Color::Rgb(r, g, b) => write!(f, "48;2;{};{};{}", r, g, b),
Color::Fixed(num) => write!(f, "48;5;{num}"),
Color::Rgb(r, g, b) => write!(f, "48;2;{r};{g};{b}"),
Color::Default => write!(f, "49"),
Color::DarkGray => write!(f, "100"),
Color::LightRed => write!(f, "101"),
Expand Down Expand Up @@ -226,7 +226,7 @@ impl Style {
/// # }
/// ```
///
/// # Examples with gnu_legacy feature enabled
/// # Examples with `gnu_legacy` feature enabled
/// Styles like bold, underlined, etc. are two-digit now
///
/// ```
Expand All @@ -243,6 +243,7 @@ impl Style {
/// style.prefix().to_string());
/// # }
/// ```
#[must_use]
pub const fn prefix(self) -> Prefix {
Prefix(self)
}
Expand Down Expand Up @@ -270,7 +271,7 @@ impl Style {
/// style.infix(style).to_string());
/// # }
/// ```
/// # Examples with gnu_legacy feature enabled
/// # Examples with `gnu_legacy` feature enabled
/// Styles like bold, underlined, etc. are two-digit now
/// ```
/// # #[cfg(feature = "gnu_legacy")]
Expand All @@ -282,6 +283,7 @@ impl Style {
/// style.infix(Green.bold()).to_string());
/// # }
/// ```
#[must_use]
pub const fn infix(self, next: Style) -> Infix {
Infix(self, next)
}
Expand All @@ -306,6 +308,7 @@ impl Style {
/// assert_eq!("",
/// style.suffix().to_string());
/// ```
#[must_use]
pub const fn suffix(self) -> Suffix {
Suffix(self)
}
Expand All @@ -325,6 +328,7 @@ impl Color {
/// assert_eq!("\x1b[32m",
/// Green.prefix().to_string());
/// ```
#[must_use]
pub fn prefix(self) -> Prefix {
Prefix(self.normal())
}
Expand All @@ -343,6 +347,7 @@ impl Color {
/// assert_eq!("\x1b[33m",
/// Red.infix(Yellow).to_string());
/// ```
#[must_use]
pub fn infix(self, next: Color) -> Infix {
Infix(self.normal(), next.normal())
}
Expand All @@ -360,6 +365,7 @@ impl Color {
/// assert_eq!("\x1b[0m",
/// Purple.suffix().to_string());
/// ```
#[must_use]
pub fn suffix(self) -> Suffix {
Suffix(self.normal())
}
Expand Down
34 changes: 17 additions & 17 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,52 +34,52 @@ impl fmt::Debug for Style {

if let Some(fg) = self.foreground {
if written_anything {
fmt.write_str(", ")?
fmt.write_str(", ")?;
}
written_anything = true;
write!(fmt, "fg({:?})", fg)?
write!(fmt, "fg({fg:?})")?;
}

if let Some(bg) = self.background {
if written_anything {
fmt.write_str(", ")?
fmt.write_str(", ")?;
}
written_anything = true;
write!(fmt, "on({:?})", bg)?
write!(fmt, "on({bg:?})")?;
}

{
let mut write_flag = |name| {
if written_anything {
fmt.write_str(", ")?
fmt.write_str(", ")?;
}
written_anything = true;
fmt.write_str(name)
};

if self.is_blink {
write_flag("blink")?
write_flag("blink")?;
}
if self.is_bold {
write_flag("bold")?
write_flag("bold")?;
}
if self.is_dimmed {
write_flag("dimmed")?
write_flag("dimmed")?;
}
if self.is_hidden {
write_flag("hidden")?
write_flag("hidden")?;
}
if self.is_italic {
write_flag("italic")?
write_flag("italic")?;
}
if self.is_reverse {
write_flag("reverse")?
write_flag("reverse")?;
}
if self.is_strikethrough {
write_flag("strikethrough")?
write_flag("strikethrough")?;
}
if self.is_underline {
write_flag("underline")?
write_flag("underline")?;
}
}

Expand Down Expand Up @@ -133,10 +133,10 @@ mod test {
}";

let style = Blue.bold();
let style_fmt_debug = format!("{:?}", style);
let style_fmt_pretty = format!("{:#?}", style);
println!("style_fmt_debug:\n{}", style_fmt_debug);
println!("style_fmt_pretty:\n{}", style_fmt_pretty);
let style_fmt_debug = format!("{style:?}");
let style_fmt_pretty = format!("{style:#?}");
println!("style_fmt_debug:\n{style_fmt_debug}");
println!("style_fmt_pretty:\n{style_fmt_pretty}");

assert_eq!(expected_debug, style_fmt_debug);
assert_eq!(expected_pretty_repat, style_fmt_pretty);
Expand Down
2 changes: 1 addition & 1 deletion src/difference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Difference {
/// happen, this function returns None, meaning that the entire set of
/// styles should be reset and begun again.
pub fn between(first: &Style, next: &Style) -> Difference {
use self::Difference::*;
use self::Difference::{Empty, ExtraStyles, Reset};

// XXX(Havvy): This algorithm is kind of hard to replicate without
// having the Plain/Foreground enum variants, so I'm just leaving
Expand Down
40 changes: 11 additions & 29 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ pub type AnsiStrings<'a> = AnsiGenericStrings<'a, str>;

/// A function to construct an `AnsiStrings` instance.
#[allow(non_snake_case)]
#[must_use]
pub const fn AnsiStrings<'a>(arg: &'a [AnsiString<'a>]) -> AnsiStrings<'a> {
AnsiGenericStrings(arg)
}
Expand All @@ -228,6 +229,7 @@ pub type AnsiByteStrings<'a> = AnsiGenericStrings<'a, [u8]>;

/// A function to construct an `AnsiByteStrings` instance.
#[allow(non_snake_case)]
#[must_use]
pub const fn AnsiByteStrings<'a>(arg: &'a [AnsiByteString<'a>]) -> AnsiByteStrings<'a> {
AnsiGenericStrings(arg)
}
Expand Down Expand Up @@ -347,7 +349,7 @@ where
&'a S: AsRef<[u8]>,
{
fn write_to_any<W: AnyWrite<Wstr = S> + ?Sized>(&self, w: &mut W) -> Result<(), W::Error> {
use self::Difference::*;
use self::Difference::{Empty, ExtraStyles, Reset};

let first = match self.0.first() {
None => return Ok(()),
Expand All @@ -372,7 +374,7 @@ where
// have already been written by this point.
if let Some(last) = self.0.last() {
if !last.style.is_plain() {
write!(w, "{}", RESET)?;
write!(w, "{RESET}")?;
}
}

Expand Down Expand Up @@ -409,54 +411,34 @@ mod tests {
assert!(joined.starts_with("\x1B[32mBefore is Green. \x1B[0m"));
assert!(
joined.ends_with(unstyled_s.as_str()),
"{:?} does not end with {:?}",
joined,
unstyled_s
"{joined:?} does not end with {unstyled_s:?}"
);

// check that RESET does not follow unstyled when appending styled
let joined = AnsiStrings(&[unstyled.clone(), after_g.clone()]).to_string();
assert!(
joined.starts_with(unstyled_s.as_str()),
"{:?} does not start with {:?}",
joined,
unstyled_s
"{joined:?} does not start with {unstyled_s:?}"
);
assert!(joined.ends_with("\x1B[32m After is Green.\x1B[0m"));

// does not introduce spurious SGR codes (reset or otherwise) adjacent
// to plain strings
let joined = AnsiStrings(&[unstyled.clone()]).to_string();
assert!(
!joined.contains("\x1B["),
"{:?} does contain \\x1B[",
joined
);
assert!(!joined.contains("\x1B["), "{joined:?} does contain \\x1B[");
let joined = AnsiStrings(&[before.clone(), unstyled.clone()]).to_string();
assert!(
!joined.contains("\x1B["),
"{:?} does contain \\x1B[",
joined
);
assert!(!joined.contains("\x1B["), "{joined:?} does contain \\x1B[");
let joined = AnsiStrings(&[before.clone(), unstyled.clone(), after.clone()]).to_string();
assert!(
!joined.contains("\x1B["),
"{:?} does contain \\x1B[",
joined
);
assert!(!joined.contains("\x1B["), "{joined:?} does contain \\x1B[");
let joined = AnsiStrings(&[unstyled.clone(), after.clone()]).to_string();
assert!(
!joined.contains("\x1B["),
"{:?} does contain \\x1B[",
joined
);
assert!(!joined.contains("\x1B["), "{joined:?} does contain \\x1B[");
}

#[test]
fn title() {
let title = AnsiGenericString::title("Test Title");
assert_eq!(title.clone().to_string(), "\x1B]2;Test Title\x1B\\");
idempotent(title)
idempotent(title);
}

#[test]
Expand Down
7 changes: 7 additions & 0 deletions src/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ pub struct Gradient {
impl Gradient {
/// Creates a new [Gradient] with two [Rgb] colors, `start` and `end`
#[inline]
#[must_use]
pub const fn new(start: Rgb, end: Rgb) -> Self {
Self { start, end }
}

#[must_use]
pub const fn from_color_rgb(start: Color, end: Color) -> Self {
let start_grad = match start {
Color::Rgb(r, g, b) => Rgb { r, g, b },
Expand All @@ -34,16 +36,19 @@ impl Gradient {
}

/// Computes the [Rgb] color between `start` and `end` for `t`
#[must_use]
pub fn at(&self, t: f32) -> Rgb {
self.start.lerp(self.end, t)
}

/// Returns the reverse of `self`
#[inline]
#[must_use]
pub const fn reverse(&self) -> Self {
Self::new(self.end, self.start)
}

#[must_use]
pub fn build(&self, text: &str, target: TargetGround) -> String {
let delta = 1.0 / text.len() as f32;
let mut result = text.char_indices().fold(String::new(), |mut acc, (i, c)| {
Expand All @@ -61,6 +66,7 @@ impl Gradient {
}
}

#[must_use]
pub fn build_all_gradient_text(text: &str, foreground: Gradient, background: Gradient) -> String {
let delta = 1.0 / text.len() as f32;
let mut result = text.char_indices().fold(String::new(), |mut acc, (i, c)| {
Expand Down Expand Up @@ -91,6 +97,7 @@ pub enum TargetGround {

impl TargetGround {
#[inline]
#[must_use]
pub const fn code(&self) -> u8 {
match self {
Self::Foreground => 30,
Expand Down
Loading

0 comments on commit 522e6eb

Please sign in to comment.