.cargo/
crates/
brain/
diff/
files/
highlight/
repo/
signals/
tui/
tui-core/
src/
ansi/
aligned.rs
2.3 KiB
ansi.rs
2.3 KiB
app.rs
4.2 KiB
backend.rs
3.9 KiB
border.rs
6.5 KiB
buffer.rs
20.6 KiB
cell.rs
7.2 KiB
colors.rs
4.2 KiB
event.rs
3.4 KiB
frame.rs
3.7 KiB
hstack.rs
6.5 KiB
label.rs
5.5 KiB
layout.rs
11.2 KiB
lib.rs
2.4 KiB
primitive.rs
19.4 KiB
table.rs
23.5 KiB
terminal.rs
5.2 KiB
text.rs
3.0 KiB
textarea.rs
6.4 KiB
vstack.rs
16.4 KiB
widget.rs
11.0 KiB
Cargo.toml
298 B
README.md
521 B
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/tui-core/src/ansi.rs
raw
| 1 | //! ANSI color, style, and painted-display support. |
| 2 | //! |
| 3 | //! The rest of the crate stores style as data and only turns it into escape |
| 4 | //! sequences at explicit formatting boundaries. This module is derived from |
| 5 | //! the `yansi` crate. |
| 6 | //! Copyright (c) 2017 Sergio Benitez |
| 7 | //! |
| 8 | |
| 9 | use std::borrow::Cow; |
| 10 | |
| 11 | /// ANSI color type. |
| 12 | mod color; |
| 13 | /// Styled display wrapper. |
| 14 | mod paint; |
| 15 | /// ANSI style properties. |
| 16 | mod style; |
| 17 | |
| 18 | pub use color::Color; |
| 19 | pub use paint::Filled; |
| 20 | pub use paint::Paint; |
| 21 | pub use paint::PaintMode; |
| 22 | pub use paint::Painted; |
| 23 | pub use paint::paint; |
| 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 | for ch in chars.by_ref() { |
| 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[33mdiff --git a/f b/f\x1b[m").as_ref(), |
| 85 | "diff --git a/f b/f" |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn strip_removes_osc_sequences() { |
| 91 | assert_eq!( |
| 92 | strip("a\x1b]8;;https://example.invalid\x07link\x1b]8;;\x07b").as_ref(), |
| 93 | "alinkb" |
| 94 | ); |
| 95 | } |
| 96 | } |