Skip to content
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
7 changes: 4 additions & 3 deletions crates/anstream/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ pre-release-replacements = [
]

[features]
default = ["auto", "wincon"]
default = ["auto", "wincon", "utf8"]
auto = ["dep:anstyle-query"]
wincon = ["dep:anstyle-wincon"]
utf8 = ["dep:utf8parse", "anstyle-parse/utf8"]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature name doesn't do a good job of describing what you are getting / losing imo

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I worry that this is an implementation detail and could change and you could lose the benefit

# Enable in `dev-dependencies` to make sure output is captured for tests
test = []

[dependencies]
anstyle = { version = "1.0.0", path = "../anstyle" }
anstyle-parse = { version = "0.2.0", path = "../anstyle-parse" }
anstyle-parse = { version = "0.2.0", path = "../anstyle-parse", default-features = false }
colorchoice = { version = "1.0.0", path = "../colorchoice" }
anstyle-query = { version = "1.0.0", path = "../anstyle-query", optional = true }
utf8parse = "0.2.1"
utf8parse = { version = "0.2.1", optional = true }
is_terminal_polyfill = "1.48"

[target.'cfg(windows)'.dependencies]
Expand Down
13 changes: 13 additions & 0 deletions crates/anstream/src/adapter/strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,22 @@ fn is_utf8_continuation(b: u8) -> bool {
/// let plain_str = anstream::adapter::strip_bytes(styled_text.as_bytes()).into_vec();
/// assert_eq!(plain_str.as_slice(), &b"foo bar"[..]);
/// ```
#[cfg(feature = "utf8")]
#[inline]
pub fn strip_bytes(data: &[u8]) -> StrippedBytes<'_> {
Comment on lines +177 to 179

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking built-in functionality and putting it behind a feature is a breaking change as it may break people who use default-features = false

StrippedBytes::new(data)
}

/// See [`strip_bytes`]
#[cfg(feature = "utf8")]
#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub struct StrippedBytes<'s> {
bytes: &'s [u8],
state: State,
utf8parser: Utf8Parser,
}

#[cfg(feature = "utf8")]
impl<'s> StrippedBytes<'s> {
/// See [`strip_bytes`]
#[inline]
Expand Down Expand Up @@ -231,6 +234,7 @@ impl<'s> StrippedBytes<'s> {
}
}

#[cfg(feature = "utf8")]
impl<'s> Iterator for StrippedBytes<'s> {
type Item = &'s [u8];

Expand All @@ -240,13 +244,15 @@ impl<'s> Iterator for StrippedBytes<'s> {
}
}

#[cfg(feature = "utf8")]
/// Incrementally strip non-contiguous data
#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub struct StripBytes {
state: State,
utf8parser: Utf8Parser,
}

#[cfg(feature = "utf8")]
impl StripBytes {
/// Initial state
pub fn new() -> Self {
Expand All @@ -263,6 +269,7 @@ impl StripBytes {
}
}

#[cfg(feature = "utf8")]
/// See [`StripBytes`]
#[derive(Debug, PartialEq, Eq)]
pub struct StripBytesIter<'s> {
Expand All @@ -271,6 +278,7 @@ pub struct StripBytesIter<'s> {
utf8parser: &'s mut Utf8Parser,
}

#[cfg(feature = "utf8")]
impl<'s> Iterator for StripBytesIter<'s> {
type Item = &'s [u8];

Expand All @@ -280,6 +288,7 @@ impl<'s> Iterator for StripBytesIter<'s> {
}
}

#[cfg(feature = "utf8")]
#[inline]
fn next_bytes<'s>(
bytes: &mut &'s [u8],
Expand Down Expand Up @@ -328,11 +337,13 @@ fn next_bytes<'s>(
}
}

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

#[cfg(feature = "utf8")]
impl Utf8Parser {
fn add(&mut self, byte: u8) -> bool {
let mut b = false;
Expand All @@ -342,8 +353,10 @@ impl Utf8Parser {
}
}

#[cfg(feature = "utf8")]
struct VtUtf8Receiver<'a>(&'a mut bool);

#[cfg(feature = "utf8")]
impl<'a> utf8parse::Receiver for VtUtf8Receiver<'a> {
fn codepoint(&mut self, _: char) {
*self.0 = true;
Expand Down