mirror of
https://github.com/sharkdp/bat.git
synced 2025-09-03 11:52:26 +01:00
Add --strip-ansi
option
This commit is contained in:
@@ -136,6 +136,26 @@ pub fn replace_nonprintable(
|
||||
output
|
||||
}
|
||||
|
||||
/// Strips ANSI escape sequences from the input.
|
||||
pub fn strip_ansi(line: &str) -> String {
|
||||
let mut buffer = String::with_capacity(line.len());
|
||||
|
||||
for seq in EscapeSequenceOffsetsIterator::new(line) {
|
||||
if let EscapeSequenceOffsets::Text { .. } = seq {
|
||||
buffer.push_str(&line[seq.index_of_start()..seq.index_past_end()]);
|
||||
}
|
||||
}
|
||||
|
||||
buffer
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy, Default)]
|
||||
pub enum StripAnsiMode {
|
||||
#[default]
|
||||
Never,
|
||||
Always,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try_parse_utf8_char() {
|
||||
assert_eq!(try_parse_utf8_char(&[0x20]), Some((' ', 1)));
|
||||
@@ -179,3 +199,14 @@ fn test_try_parse_utf8_char() {
|
||||
assert_eq!(try_parse_utf8_char(&[0xef, 0x20]), None);
|
||||
assert_eq!(try_parse_utf8_char(&[0xf0, 0xf0]), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_strip_ansi() {
|
||||
// The sequence detection is covered by the tests in the vscreen module.
|
||||
assert_eq!(strip_ansi("no ansi"), "no ansi");
|
||||
assert_eq!(strip_ansi("\x1B[33mone"), "one");
|
||||
assert_eq!(
|
||||
strip_ansi("\x1B]1\x07multiple\x1B[J sequences"),
|
||||
"multiple sequences"
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user