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

update strip-ansi-escapes to use new api #9958

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions crates/nu-explore/src/views/record/tablew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,10 +772,9 @@ fn render_column(
}

fn strip_string(text: &str) -> String {
strip_ansi_escapes::strip(text)
.ok()
.and_then(|s| String::from_utf8(s).ok())
.unwrap_or_else(|| text.to_owned())
String::from_utf8(strip_ansi_escapes::strip(text))
.map_err(|_| ())
.unwrap_or_else(|_| text.to_owned())
}

fn head_row_text(head: &str, style_computer: &StyleComputer) -> NuText {
Expand Down
24 changes: 8 additions & 16 deletions crates/nu-utils/src/deansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ pub fn strip_ansi_unlikely(string: &str) -> Cow<str> {
// which will be stripped. Includes the primary start of ANSI sequences ESC
// (0x1B = decimal 27)
if string.bytes().any(|x| matches!(x, 0..=9 | 11..=31)) {
if let Ok(stripped) = strip_ansi_escapes::strip(string) {
if let Ok(new_string) = String::from_utf8(stripped) {
return Cow::Owned(new_string);
}
if let Ok(stripped) = String::from_utf8(strip_ansi_escapes::strip(string)) {
return Cow::Owned(stripped);
}
}
// Else case includes failures to parse!
Expand All @@ -34,10 +32,8 @@ pub fn strip_ansi_likely(string: &str) -> Cow<str> {
// Check if any ascii control character except LF(0x0A = 10) is present,
// which will be stripped. Includes the primary start of ANSI sequences ESC
// (0x1B = decimal 27)
if let Ok(stripped) = strip_ansi_escapes::strip(string) {
if let Ok(new_string) = String::from_utf8(stripped) {
return Cow::Owned(new_string);
}
if let Ok(stripped) = String::from_utf8(strip_ansi_escapes::strip(string)) {
return Cow::Owned(stripped);
}
// Else case includes failures to parse!
Cow::Borrowed(string)
Expand All @@ -60,10 +56,8 @@ pub fn strip_ansi_string_unlikely(string: String) -> String {
.bytes()
.any(|x| matches!(x, 0..=9 | 11..=31))
{
if let Ok(stripped) = strip_ansi_escapes::strip(&string) {
if let Ok(new_string) = String::from_utf8(stripped) {
return new_string;
}
if let Ok(stripped) = String::from_utf8(strip_ansi_escapes::strip(&string)) {
return stripped;
}
}
// Else case includes failures to parse!
Expand All @@ -81,10 +75,8 @@ pub fn strip_ansi_string_likely(string: String) -> String {
// Check if any ascii control character except LF(0x0A = 10) is present,
// which will be stripped. Includes the primary start of ANSI sequences ESC
// (0x1B = decimal 27)
if let Ok(stripped) = strip_ansi_escapes::strip(&string) {
if let Ok(new_string) = String::from_utf8(stripped) {
return new_string;
}
if let Ok(stripped) = String::from_utf8(strip_ansi_escapes::strip(&string)) {
return stripped;
}
// Else case includes failures to parse!
string
Expand Down