Skip to content

Commit

Permalink
style: Update for new lints
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Mar 28, 2024
1 parent cb8ab0b commit 6e7778e
Show file tree
Hide file tree
Showing 49 changed files with 316 additions and 277 deletions.
15 changes: 9 additions & 6 deletions crates/anstream/benches/stream.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![allow(missing_docs)]
#![allow(clippy::unwrap_used)]

use std::io::Write as _;

use criterion::{black_box, Criterion};
Expand All @@ -21,7 +24,7 @@ fn stream(c: &mut Criterion) {
stream.write_all(content).unwrap();

black_box(stream)
})
});
});
group.bench_function("StripStream", |b| {
b.iter(|| {
Expand All @@ -31,7 +34,7 @@ fn stream(c: &mut Criterion) {
stream.write_all(content).unwrap();

black_box(stream)
})
});
});
#[cfg(all(windows, feature = "wincon"))]
group.bench_function("WinconStream", |b| {
Expand All @@ -42,7 +45,7 @@ fn stream(c: &mut Criterion) {
stream.write_all(content).unwrap();

black_box(stream)
})
});
});
group.bench_function("AutoStream::always_ansi", |b| {
b.iter(|| {
Expand All @@ -52,7 +55,7 @@ fn stream(c: &mut Criterion) {
stream.write_all(content).unwrap();

black_box(stream)
})
});
});
group.bench_function("AutoStream::always", |b| {
b.iter(|| {
Expand All @@ -62,7 +65,7 @@ fn stream(c: &mut Criterion) {
stream.write_all(content).unwrap();

black_box(stream)
})
});
});
group.bench_function("AutoStream::never", |b| {
b.iter(|| {
Expand All @@ -72,7 +75,7 @@ fn stream(c: &mut Criterion) {
stream.write_all(content).unwrap();

black_box(stream)
})
});
});
}
}
Expand Down
13 changes: 8 additions & 5 deletions crates/anstream/benches/strip.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![allow(missing_docs)]
#![allow(clippy::unwrap_used)]

use criterion::{black_box, Criterion};

#[derive(Default)]
Expand Down Expand Up @@ -59,22 +62,22 @@ fn strip(c: &mut Criterion) {
}

black_box(stripped.0)
})
});
});
group.bench_function("strip_ansi_escapes", |b| {
b.iter(|| {
let stripped = strip_ansi_escapes::strip(content);

black_box(stripped)
})
});
});
if let Ok(content) = std::str::from_utf8(content) {
group.bench_function("strip_str", |b| {
b.iter(|| {
let stripped = anstream::adapter::strip_str(content).to_string();

black_box(stripped)
})
});
});
group.bench_function("StripStr", |b| {
b.iter(|| {
Expand All @@ -85,15 +88,15 @@ fn strip(c: &mut Criterion) {
}

black_box(stripped)
})
});
});
}
group.bench_function("strip_bytes", |b| {
b.iter(|| {
let stripped = anstream::adapter::strip_bytes(content).into_vec();

black_box(stripped)
})
});
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/anstream/benches/wincon.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(missing_docs)]
use criterion::{black_box, Criterion};

fn wincon(c: &mut Criterion) {
Expand All @@ -17,7 +18,7 @@ fn wincon(c: &mut Criterion) {
let stripped = state.extract_next(content).collect::<Vec<_>>();

black_box(stripped)
})
});
});
}
}
Expand Down
7 changes: 6 additions & 1 deletion crates/anstream/examples/dump-stream.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Write colored text, adapting to the terminals capabilities

use std::io::Write;

fn main() -> Result<(), lexopt::Error> {
Expand All @@ -6,7 +8,9 @@ fn main() -> Result<(), lexopt::Error> {
let mut stdout = stdout.lock();

for fixed in 0..16 {
let color = anstyle::Ansi256Color(fixed).into_ansi().unwrap();
let color = anstyle::Ansi256Color(fixed)
.into_ansi()
.expect("within 4-bit color range");
let style = style(color, args.layer, args.effects);
let _ = print_number(&mut stdout, fixed, style);
if fixed == 7 || fixed == 15 {
Expand Down Expand Up @@ -70,6 +74,7 @@ enum Layer {

impl Args {
fn parse() -> Result<Self, lexopt::Error> {
#[allow(clippy::wildcard_imports)] // false positive
use lexopt::prelude::*;

let mut res = Args::default();
Expand Down
2 changes: 2 additions & 0 deletions crates/anstream/examples/query-stream.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Report a terminal's capabilities

fn main() {
println!("stdout:");
println!(
Expand Down
20 changes: 11 additions & 9 deletions crates/anstream/src/adapter/strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ fn next_str<'s>(bytes: &mut &'s [u8], state: &mut State) -> Option<&'s str> {

#[inline]
unsafe fn from_utf8_unchecked<'b>(bytes: &'b [u8], safety_justification: &'static str) -> &'b str {
if cfg!(debug_assertions) {
// Catch problems more quickly when testing
std::str::from_utf8(bytes).expect(safety_justification)
} else {
std::str::from_utf8_unchecked(bytes)
unsafe {
if cfg!(debug_assertions) {
// Catch problems more quickly when testing
std::str::from_utf8(bytes).expect(safety_justification)
} else {
std::str::from_utf8_unchecked(bytes)
}
}
}

Expand Down Expand Up @@ -327,7 +329,7 @@ fn next_bytes<'s>(
}

#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub struct Utf8Parser {
pub(crate) struct Utf8Parser {
utf8_parser: utf8parse::Parser,
}

Expand Down Expand Up @@ -440,7 +442,7 @@ mod test {
fn test_strip_byte_multibyte() {
let bytes = [240, 145, 141, 139];
let expected = parser_strip(&bytes);
let actual = String::from_utf8(strip_byte(&bytes).to_vec()).unwrap();
let actual = String::from_utf8(strip_byte(&bytes).clone()).unwrap();
assert_eq!(expected, actual);
}

Expand All @@ -456,7 +458,7 @@ mod test {
fn test_strip_byte_del() {
let bytes = [0x7f];
let expected = "";
let actual = String::from_utf8(strip_byte(&bytes).to_vec()).unwrap();
let actual = String::from_utf8(strip_byte(&bytes).clone()).unwrap();
assert_eq!(expected, actual);
}

Expand Down Expand Up @@ -502,7 +504,7 @@ mod test {
dbg!(&s);
dbg!(s.as_bytes());
let expected = parser_strip(s.as_bytes());
let actual = String::from_utf8(strip_byte(s.as_bytes()).to_vec()).unwrap();
let actual = String::from_utf8(strip_byte(s.as_bytes()).clone()).unwrap();
assert_eq!(expected, actual);
}
}
Expand Down
12 changes: 8 additions & 4 deletions crates/anstream/src/adapter/wincon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl anstyle_parse::Perform for WinconCapture {
break;
}
(State::Normal, 30..=37) => {
let color = to_ansi_color(value - 30).unwrap();
let color = to_ansi_color(value - 30).expect("within 4-bit range");
style = style.fg_color(Some(color.into()));
break;
}
Expand All @@ -163,7 +163,7 @@ impl anstyle_parse::Perform for WinconCapture {
break;
}
(State::Normal, 40..=47) => {
let color = to_ansi_color(value - 40).unwrap();
let color = to_ansi_color(value - 40).expect("within 4-bit range");
style = style.bg_color(Some(color.into()));
break;
}
Expand All @@ -180,12 +180,16 @@ impl anstyle_parse::Perform for WinconCapture {
state = State::PrepareCustomColor;
}
(State::Normal, 90..=97) => {
let color = to_ansi_color(value - 90).unwrap().bright(true);
let color = to_ansi_color(value - 90)
.expect("within 4-bit range")
.bright(true);
style = style.fg_color(Some(color.into()));
break;
}
(State::Normal, 100..=107) => {
let color = to_ansi_color(value - 100).unwrap().bright(true);
let color = to_ansi_color(value - 100)
.expect("within 4-bit range")
.bright(true);
style = style.bg_color(Some(color.into()));
break;
}
Expand Down
1 change: 1 addition & 0 deletions crates/anstream/src/auto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ where
}
}

/// Returns `true` if the descriptor/handle refers to a terminal/tty.
#[inline]
pub fn is_terminal(&self) -> bool {
match &self.inner {
Expand Down
2 changes: 2 additions & 0 deletions crates/anstream/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ pub use wincon::WinconStream;
#[allow(deprecated)]
pub use buffer::Buffer;

/// An adaptive wrapper around the global standard output stream of the current process
pub type Stdout = AutoStream<std::io::Stdout>;
/// An adaptive wrapper around the global standard error stream of the current process
pub type Stderr = AutoStream<std::io::Stderr>;

/// Create an ANSI escape code compatible stdout
Expand Down
5 changes: 5 additions & 0 deletions crates/anstream/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ impl RawStream for crate::Buffer {}
#[allow(deprecated)]
impl RawStream for &'_ mut crate::Buffer {}

/// Trait to determine if a descriptor/handle refers to a terminal/tty.
pub trait IsTerminal: private::Sealed {
/// Returns `true` if the descriptor/handle refers to a terminal/tty.
fn is_terminal(&self) -> bool;
}

Expand Down Expand Up @@ -145,11 +147,14 @@ impl IsTerminal for &'_ mut crate::Buffer {
}
}

/// Lock a stream
pub trait AsLockedWrite: private::Sealed {
/// Locked writer type
type Write<'w>: RawStream + 'w
where
Self: 'w;

/// Lock a stream
fn as_locked_write(&mut self) -> Self::Write<'_>;
}

Expand Down
1 change: 1 addition & 0 deletions crates/anstream/src/strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ where
S: std::io::Write,
S: IsTerminal,
{
/// Returns `true` if the descriptor/handle refers to a terminal/tty.
#[inline]
pub fn is_terminal(&self) -> bool {
self.raw.is_terminal()
Expand Down
2 changes: 1 addition & 1 deletion crates/anstyle-ansi-term/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# anstyle-ansi-term

> Convert from color styling types to [`ansi_term`](https://lib.rs/ansi_term) color types
> Convert between [`ansi_term`](https://lib.rs/ansi_term) and generic styling types
[![Documentation](https://img.shields.io/badge/docs-master-blue.svg)][Documentation]
![License](https://img.shields.io/crates/l/anstyle-ansi-term.svg)
Expand Down
19 changes: 3 additions & 16 deletions crates/anstyle-ansi-term/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
//! Convert between [`ansi_term`](https://lib.rs/ansi_term) and generic styling types

#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![warn(clippy::print_stderr)]
#![warn(clippy::print_stdout)]

mod sealed {
pub(crate) trait Sealed {}
}

trait Ext: sealed::Sealed {
fn to_ansi_term(self) -> ansi_term::Style;
}

impl sealed::Sealed for anstyle::Style {}

impl Ext for anstyle::Style {
fn to_ansi_term(self) -> ansi_term::Style {
to_ansi_term(self)
}
}

/// Adapt generic styling to [`ansi_term`]
pub fn to_ansi_term(astyle: anstyle::Style) -> ansi_term::Style {
let mut style = ansi_term::Style::new();

Expand Down
2 changes: 1 addition & 1 deletion crates/anstyle-crossterm/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# anstyle-crossterm

> Convert from color styling types to [`crossterm`](https://lib.rs/crossterm) color types
> Convert between [`crossterm`](https://lib.rs/crossterm) and generic styling types
[![Documentation](https://img.shields.io/badge/docs-master-blue.svg)][Documentation]
![License](https://img.shields.io/crates/l/anstyle-crossterm.svg)
Expand Down
19 changes: 3 additions & 16 deletions crates/anstyle-crossterm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
//! Convert between [`crossterm`](https://lib.rs/crossterm) and [generic styling types][anstyle]

#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![warn(clippy::print_stderr)]
#![warn(clippy::print_stdout)]

mod sealed {
pub(crate) trait Sealed {}
}

trait Ext: sealed::Sealed {
fn to_crossterm(self) -> crossterm::style::ContentStyle;
}

impl sealed::Sealed for anstyle::Style {}

impl Ext for anstyle::Style {
fn to_crossterm(self) -> crossterm::style::ContentStyle {
to_crossterm(self)
}
}

/// Adapt generic styling to [`crossterm`]
pub fn to_crossterm(astyle: anstyle::Style) -> crossterm::style::ContentStyle {
let foreground_color = astyle.get_fg_color().map(to_ansi_color);
let background_color = astyle.get_bg_color().map(to_ansi_color);
Expand Down
Loading

0 comments on commit 6e7778e

Please sign in to comment.