diff: Strip ANSI from pager input
182029928387048f6d61793fcfd64e85efc9de29
Git can feed colored show output through the pager path, so strip terminal control sequences before detecting and decoding patches while preserving pass-through for non-diff input.
1 parent
9158624c
crates/diff/src/bin/rustik-diff.rs
+12 -3
| 127 | 127 | run_or_forward(&input, Header::stdin(cwd)) |
|
| 128 | 128 | } |
|
| 129 | 129 | ||
| 130 | 130 | /// Renders diff input or passes non-diff data through unchanged. |
|
| 131 | 131 | fn run_or_forward(input: &[u8], header: Header) -> Result<(), Error> { |
|
| 132 | - | if !contains_diff(input) { |
|
| 132 | + | let text = match std::str::from_utf8(input) { |
|
| 133 | + | Ok(text) => text, |
|
| 134 | + | Err(_) if !contains_diff(input) => { |
|
| 135 | + | io::stdout().write_all(input)?; |
|
| 136 | + | return Ok(()); |
|
| 137 | + | } |
|
| 138 | + | Err(error) => return Err(Error::Input(error.to_string())), |
|
| 139 | + | }; |
|
| 140 | + | let text = tui::ansi::strip(text); |
|
| 141 | + | ||
| 142 | + | if !contains_diff(text.as_bytes()) { |
|
| 133 | 143 | io::stdout().write_all(input)?; |
|
| 134 | 144 | return Ok(()); |
|
| 135 | 145 | } |
|
| 136 | - | let text = std::str::from_utf8(input).map_err(|error| Error::Input(error.to_string()))?; |
|
| 137 | - | let document = DiffDocument::decode_str(text)?; |
|
| 146 | + | let document = DiffDocument::decode_str(text.as_ref())?; |
|
| 138 | 147 | let raw_theme = BUILTIN_THEME.parse::<syntax::RawTheme>()?; |
|
| 139 | 148 | let options = DiffOptions { |
|
| 140 | 149 | diff_palette: (&raw_theme).into(), |
|
| 141 | 150 | ..DiffOptions::default() |
|
| 142 | 151 | }; |
+0 -0
+0 -0
a.rs
+0 -0
a.rs\x1b[m
+0 -0
crates/tui-core/src/ansi.rs
+63 -0
| 3 | 3 | //! The rest of the crate stores style as data and only turns it into escape |
|
| 4 | 4 | //! sequences at explicit formatting boundaries. This module is derived from |
|
| 5 | 5 | //! the `yansi` crate. |
|
| 6 | 6 | //! Copyright (c) 2017 Sergio Benitez |
|
| 7 | 7 | //! |
|
| 8 | + | ||
| 9 | + | use std::borrow::Cow; |
|
| 10 | + | ||
| 8 | 11 | /// ANSI color type. |
|
| 9 | 12 | mod color; |
|
| 10 | 13 | /// Styled display wrapper. |
|
| 11 | 14 | mod paint; |
|
| 12 | 15 | /// ANSI style properties. |
| 17 | 20 | pub use paint::Paint; |
|
| 18 | 21 | pub use paint::PaintMode; |
|
| 19 | 22 | pub use paint::Painted; |
|
| 20 | 23 | pub use paint::paint; |
|
| 21 | 24 | pub use style::Style; |
|
| 25 | + | ||
| 26 | + | /// Removes ANSI control sequences from text. |
|
| 27 | + | pub fn strip(input: &str) -> Cow<'_, str> { |
|
| 28 | + | if !input.as_bytes().contains(&b'\x1b') { |
|
| 29 | + | return Cow::Borrowed(input); |
|
| 30 | + | } |
|
| 31 | + | let mut output = String::with_capacity(input.len()); |
|
| 32 | + | let mut chars = input.chars().peekable(); |
|
| 33 | + | ||
| 34 | + | while let Some(ch) = chars.next() { |
|
| 35 | + | if ch != '\x1b' { |
|
| 36 | + | output.push(ch); |
|
| 37 | + | continue; |
|
| 38 | + | } |
|
| 39 | + | ||
| 40 | + | match chars.peek().copied() { |
|
| 41 | + | Some('[') => { |
|
| 42 | + | chars.next(); |
|
| 43 | + | while let Some(ch) = chars.next() { |
|
| 44 | + | if ('\u{40}'..='\u{7e}').contains(&ch) { |
|
| 45 | + | break; |
|
| 46 | + | } |
|
| 47 | + | } |
|
| 48 | + | } |
|
| 49 | + | Some(']') => { |
|
| 50 | + | chars.next(); |
|
| 51 | + | while let Some(ch) = chars.next() { |
|
| 52 | + | if ch == '\u{7}' { |
|
| 53 | + | break; |
|
| 54 | + | } |
|
| 55 | + | if ch == '\x1b' && chars.peek() == Some(&'\\') { |
|
| 56 | + | chars.next(); |
|
| 57 | + | break; |
|
| 58 | + | } |
|
| 59 | + | } |
|
| 60 | + | } |
|
| 61 | + | Some(_) => { |
|
| 62 | + | chars.next(); |
|
| 63 | + | } |
|
| 64 | + | None => {} |
|
| 65 | + | } |
|
| 66 | + | } |
|
| 67 | + | Cow::Owned(output) |
|
| 68 | + | } |
|
| 69 | + | ||
| 70 | + | #[cfg(test)] |
|
| 71 | + | mod tests { |
|
| 72 | + | use super::*; |
|
| 73 | + | ||
| 74 | + | #[test] |
|
| 75 | + | fn strip_borrows_plain_text() { |
|
| 76 | + | let stripped = strip("plain text"); |
|
| 77 | + | ||
| 78 | + | assert!(matches!(stripped, Cow::Borrowed("plain text"))); |
|
| 79 | + | } |
|
| 80 | + | ||
| 81 | + | #[test] |
|
| 82 | + | fn strip_removes_csi_sequences() { |
|
| 83 | + | assert_eq!( |
|
| 84 | + | strip("\x1b[33m |
f\x1b[m").as_ref(),
+0 -0
f"
+0 -0