build(ariadne): vendor clap
This commit was merged in pull request #30.
This commit is contained in:
1 parent
e6af135702
commit
8855b6d379
681 files changed
+400416
No files matched your search
@@ -0,0 +1,15 @@
|
||||
//! Gracefully degrade styled output
|
||||
|
||||
mod strip;
|
||||
mod wincon;
|
||||
|
||||
pub use strip::strip_bytes;
|
||||
pub use strip::strip_str;
|
||||
pub use strip::StripBytes;
|
||||
pub use strip::StripBytesIter;
|
||||
pub use strip::StripStr;
|
||||
pub use strip::StripStrIter;
|
||||
pub use strip::StrippedBytes;
|
||||
pub use strip::StrippedStr;
|
||||
pub use wincon::WinconBytes;
|
||||
pub use wincon::WinconBytesIter;
|
||||
@@ -0,0 +1,511 @@
|
||||
use anstyle_parse::state::state_change;
|
||||
use anstyle_parse::state::Action;
|
||||
use anstyle_parse::state::State;
|
||||
|
||||
/// Strip ANSI escapes from a `&str`, returning the printable content
|
||||
///
|
||||
/// This can be used to take output from a program that includes escape sequences and write it
|
||||
/// somewhere that does not easily support them, such as a log file.
|
||||
///
|
||||
/// For non-contiguous data, see [`StripStr`].
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use std::io::Write as _;
|
||||
///
|
||||
/// let styled_text = "\x1b[32mfoo\x1b[m bar";
|
||||
/// let plain_str = anstream::adapter::strip_str(&styled_text).to_string();
|
||||
/// assert_eq!(plain_str, "foo bar");
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn strip_str(data: &str) -> StrippedStr<'_> {
|
||||
StrippedStr::new(data)
|
||||
}
|
||||
|
||||
/// See [`strip_str`]
|
||||
#[derive(Default, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct StrippedStr<'s> {
|
||||
bytes: &'s [u8],
|
||||
state: State,
|
||||
}
|
||||
|
||||
impl<'s> StrippedStr<'s> {
|
||||
#[inline]
|
||||
fn new(data: &'s str) -> Self {
|
||||
Self {
|
||||
bytes: data.as_bytes(),
|
||||
state: State::Ground,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a [`String`] of the printable content
|
||||
#[inline]
|
||||
#[allow(clippy::inherent_to_string_shadow_display)] // Single-allocation implementation
|
||||
pub fn to_string(&self) -> String {
|
||||
use std::fmt::Write as _;
|
||||
let mut stripped = String::with_capacity(self.bytes.len());
|
||||
let _ = write!(&mut stripped, "{self}");
|
||||
stripped
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for StrippedStr<'_> {
|
||||
/// **Note:** this does *not* exhaust the [`Iterator`]
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let iter = Self {
|
||||
bytes: self.bytes,
|
||||
state: self.state,
|
||||
};
|
||||
for printable in iter {
|
||||
printable.fmt(f)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> Iterator for StrippedStr<'s> {
|
||||
type Item = &'s str;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
next_str(&mut self.bytes, &mut self.state)
|
||||
}
|
||||
}
|
||||
|
||||
/// Incrementally strip non-contiguous data
|
||||
#[derive(Default, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct StripStr {
|
||||
state: State,
|
||||
}
|
||||
|
||||
impl StripStr {
|
||||
/// Initial state
|
||||
pub fn new() -> Self {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
/// Strip the next segment of data
|
||||
pub fn strip_next<'s>(&'s mut self, data: &'s str) -> StripStrIter<'s> {
|
||||
StripStrIter {
|
||||
bytes: data.as_bytes(),
|
||||
state: &mut self.state,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// See [`StripStr`]
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct StripStrIter<'s> {
|
||||
bytes: &'s [u8],
|
||||
state: &'s mut State,
|
||||
}
|
||||
|
||||
impl<'s> Iterator for StripStrIter<'s> {
|
||||
type Item = &'s str;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
next_str(&mut self.bytes, self.state)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn next_str<'s>(bytes: &mut &'s [u8], state: &mut State) -> Option<&'s str> {
|
||||
let offset = bytes.iter().copied().position(|b| {
|
||||
let (next_state, action) = state_change(*state, b);
|
||||
if next_state != State::Anywhere {
|
||||
*state = next_state;
|
||||
}
|
||||
is_printable_bytes(action, b)
|
||||
});
|
||||
let (_, next) = bytes.split_at(offset.unwrap_or(bytes.len()));
|
||||
*bytes = next;
|
||||
*state = State::Ground;
|
||||
|
||||
let offset = bytes.iter().copied().position(|b| {
|
||||
let (_next_state, action) = state_change(State::Ground, b);
|
||||
!(is_printable_bytes(action, b) || is_utf8_continuation(b))
|
||||
});
|
||||
let (printable, next) = bytes.split_at(offset.unwrap_or(bytes.len()));
|
||||
*bytes = next;
|
||||
if printable.is_empty() {
|
||||
None
|
||||
} else {
|
||||
let printable = unsafe {
|
||||
from_utf8_unchecked(
|
||||
printable,
|
||||
"`bytes` was validated as UTF-8, the parser preserves UTF-8 continuations",
|
||||
)
|
||||
};
|
||||
Some(printable)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn from_utf8_unchecked<'b>(bytes: &'b [u8], safety_justification: &'static str) -> &'b str {
|
||||
unsafe {
|
||||
if cfg!(debug_assertions) {
|
||||
// Catch problems more quickly when testing
|
||||
std::str::from_utf8(bytes).expect(safety_justification)
|
||||
} else {
|
||||
std::str::from_utf8_unchecked(bytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_utf8_continuation(b: u8) -> bool {
|
||||
matches!(b, 0x80..=0xbf)
|
||||
}
|
||||
|
||||
/// Strip ANSI escapes from bytes, returning the printable content
|
||||
///
|
||||
/// This can be used to take output from a program that includes escape sequences and write it
|
||||
/// somewhere that does not easily support them, such as a log file.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use std::io::Write as _;
|
||||
///
|
||||
/// let styled_text = "\x1b[32mfoo\x1b[m bar";
|
||||
/// let plain_str = anstream::adapter::strip_bytes(styled_text.as_bytes()).into_vec();
|
||||
/// assert_eq!(plain_str.as_slice(), &b"foo bar"[..]);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn strip_bytes(data: &[u8]) -> StrippedBytes<'_> {
|
||||
StrippedBytes::new(data)
|
||||
}
|
||||
|
||||
/// See [`strip_bytes`]
|
||||
#[derive(Default, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct StrippedBytes<'s> {
|
||||
bytes: &'s [u8],
|
||||
state: State,
|
||||
utf8parser: Utf8Parser,
|
||||
}
|
||||
|
||||
impl<'s> StrippedBytes<'s> {
|
||||
/// See [`strip_bytes`]
|
||||
#[inline]
|
||||
pub fn new(bytes: &'s [u8]) -> Self {
|
||||
Self {
|
||||
bytes,
|
||||
state: State::Ground,
|
||||
utf8parser: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Strip the next slice of bytes
|
||||
///
|
||||
/// Used when the content is in several non-contiguous slices
|
||||
///
|
||||
/// # Panic
|
||||
///
|
||||
/// May panic if it is not exhausted / empty
|
||||
#[inline]
|
||||
pub fn extend(&mut self, bytes: &'s [u8]) {
|
||||
debug_assert!(
|
||||
self.is_empty(),
|
||||
"current bytes must be processed to ensure we end at the right state"
|
||||
);
|
||||
self.bytes = bytes;
|
||||
}
|
||||
|
||||
/// Report the bytes has been exhausted
|
||||
#[inline]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.bytes.is_empty()
|
||||
}
|
||||
|
||||
/// Create a [`Vec`] of the printable content
|
||||
#[inline]
|
||||
pub fn into_vec(self) -> Vec<u8> {
|
||||
let mut stripped = Vec::with_capacity(self.bytes.len());
|
||||
for printable in self {
|
||||
stripped.extend(printable);
|
||||
}
|
||||
stripped
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> Iterator for StrippedBytes<'s> {
|
||||
type Item = &'s [u8];
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
next_bytes(&mut self.bytes, &mut self.state, &mut self.utf8parser)
|
||||
}
|
||||
}
|
||||
|
||||
/// Incrementally strip non-contiguous data
|
||||
#[derive(Default, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct StripBytes {
|
||||
state: State,
|
||||
utf8parser: Utf8Parser,
|
||||
}
|
||||
|
||||
impl StripBytes {
|
||||
/// Initial state
|
||||
pub fn new() -> Self {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
/// Strip the next segment of data
|
||||
pub fn strip_next<'s>(&'s mut self, bytes: &'s [u8]) -> StripBytesIter<'s> {
|
||||
StripBytesIter {
|
||||
bytes,
|
||||
state: &mut self.state,
|
||||
utf8parser: &mut self.utf8parser,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// See [`StripBytes`]
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct StripBytesIter<'s> {
|
||||
bytes: &'s [u8],
|
||||
state: &'s mut State,
|
||||
utf8parser: &'s mut Utf8Parser,
|
||||
}
|
||||
|
||||
impl<'s> Iterator for StripBytesIter<'s> {
|
||||
type Item = &'s [u8];
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
next_bytes(&mut self.bytes, self.state, self.utf8parser)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn next_bytes<'s>(
|
||||
bytes: &mut &'s [u8],
|
||||
state: &mut State,
|
||||
utf8parser: &mut Utf8Parser,
|
||||
) -> Option<&'s [u8]> {
|
||||
let offset = bytes.iter().copied().position(|b| {
|
||||
if *state == State::Utf8 {
|
||||
true
|
||||
} else {
|
||||
let (next_state, action) = state_change(*state, b);
|
||||
if next_state != State::Anywhere {
|
||||
*state = next_state;
|
||||
}
|
||||
is_printable_bytes(action, b)
|
||||
}
|
||||
});
|
||||
let (_, next) = bytes.split_at(offset.unwrap_or(bytes.len()));
|
||||
*bytes = next;
|
||||
|
||||
let offset = bytes.iter().copied().position(|b| {
|
||||
if *state == State::Utf8 {
|
||||
if utf8parser.add(b) {
|
||||
*state = State::Ground;
|
||||
}
|
||||
false
|
||||
} else {
|
||||
let (next_state, action) = state_change(State::Ground, b);
|
||||
if next_state != State::Anywhere {
|
||||
*state = next_state;
|
||||
}
|
||||
if *state == State::Utf8 {
|
||||
utf8parser.add(b);
|
||||
false
|
||||
} else {
|
||||
!is_printable_bytes(action, b)
|
||||
}
|
||||
}
|
||||
});
|
||||
let (printable, next) = bytes.split_at(offset.unwrap_or(bytes.len()));
|
||||
*bytes = next;
|
||||
if printable.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(printable)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) struct Utf8Parser {
|
||||
utf8_parser: utf8parse::Parser,
|
||||
}
|
||||
|
||||
impl Utf8Parser {
|
||||
fn add(&mut self, byte: u8) -> bool {
|
||||
let mut b = false;
|
||||
let mut receiver = VtUtf8Receiver(&mut b);
|
||||
self.utf8_parser.advance(&mut receiver, byte);
|
||||
b
|
||||
}
|
||||
}
|
||||
|
||||
struct VtUtf8Receiver<'a>(&'a mut bool);
|
||||
|
||||
impl utf8parse::Receiver for VtUtf8Receiver<'_> {
|
||||
fn codepoint(&mut self, _: char) {
|
||||
*self.0 = true;
|
||||
}
|
||||
|
||||
fn invalid_sequence(&mut self) {
|
||||
*self.0 = true;
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_printable_bytes(action: Action, byte: u8) -> bool {
|
||||
// VT320 considered 0x7f to be `Print`able but we expect to be working in UTF-8 systems and not
|
||||
// ISO Latin-1, making it DEL and non-printable
|
||||
const DEL: u8 = 0x7f;
|
||||
|
||||
// Continuations aren't included as they may also be control codes, requiring more context
|
||||
(action == Action::Print && byte != DEL)
|
||||
|| action == Action::BeginUtf8
|
||||
|| (action == Action::Execute && byte.is_ascii_whitespace())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use proptest::prelude::*;
|
||||
|
||||
/// Model based off full parser
|
||||
fn parser_strip(bytes: &[u8]) -> String {
|
||||
#[derive(Default)]
|
||||
struct Strip(String);
|
||||
impl Strip {
|
||||
fn with_capacity(capacity: usize) -> Self {
|
||||
Self(String::with_capacity(capacity))
|
||||
}
|
||||
}
|
||||
impl anstyle_parse::Perform for Strip {
|
||||
fn print(&mut self, c: char) {
|
||||
self.0.push(c);
|
||||
}
|
||||
|
||||
fn execute(&mut self, byte: u8) {
|
||||
if byte.is_ascii_whitespace() {
|
||||
self.0.push(byte as char);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut stripped = Strip::with_capacity(bytes.len());
|
||||
let mut parser = anstyle_parse::Parser::<anstyle_parse::DefaultCharAccumulator>::new();
|
||||
for byte in bytes {
|
||||
parser.advance(&mut stripped, *byte);
|
||||
}
|
||||
stripped.0
|
||||
}
|
||||
|
||||
/// Model verifying incremental parsing
|
||||
fn strip_char(mut s: &str) -> String {
|
||||
let mut result = String::new();
|
||||
let mut state = StripStr::new();
|
||||
while !s.is_empty() {
|
||||
let mut indices = s.char_indices();
|
||||
indices.next(); // current
|
||||
let offset = indices.next().map(|(i, _)| i).unwrap_or_else(|| s.len());
|
||||
let (current, remainder) = s.split_at(offset);
|
||||
for printable in state.strip_next(current) {
|
||||
result.push_str(printable);
|
||||
}
|
||||
s = remainder;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// Model verifying incremental parsing
|
||||
fn strip_byte(s: &[u8]) -> Vec<u8> {
|
||||
let mut result = Vec::new();
|
||||
let mut state = StripBytes::default();
|
||||
for start in 0..s.len() {
|
||||
let current = &s[start..=start];
|
||||
for printable in state.strip_next(current) {
|
||||
result.extend(printable);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_strip_bytes_multibyte() {
|
||||
let bytes = [240, 145, 141, 139];
|
||||
let expected = parser_strip(&bytes);
|
||||
let actual = String::from_utf8(strip_bytes(&bytes).into_vec()).unwrap();
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_strip_byte_multibyte() {
|
||||
let bytes = [240, 145, 141, 139];
|
||||
let expected = parser_strip(&bytes);
|
||||
let actual = String::from_utf8(strip_byte(&bytes).clone()).unwrap();
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_strip_str_del() {
|
||||
let input = std::str::from_utf8(&[0x7f]).unwrap();
|
||||
let expected = "";
|
||||
let actual = strip_str(input).to_string();
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_strip_byte_del() {
|
||||
let bytes = [0x7f];
|
||||
let expected = "";
|
||||
let actual = String::from_utf8(strip_byte(&bytes).clone()).unwrap();
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_strip_str_handles_broken_sequence() {
|
||||
// valid utf8: \xc3\xb6 then \x1b then \xf0\x9f\x98\x80
|
||||
let s = "ö\x1b😀hello😀goodbye";
|
||||
let mut it = strip_str(s);
|
||||
assert_eq!("ö", it.next().unwrap());
|
||||
assert_eq!("ello😀goodbye", it.next().unwrap());
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
|
||||
fn strip_str_no_escapes(s in "\\PC*") {
|
||||
let expected = parser_strip(s.as_bytes());
|
||||
let actual = strip_str(&s).to_string();
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
|
||||
fn strip_char_no_escapes(s in "\\PC*") {
|
||||
let expected = parser_strip(s.as_bytes());
|
||||
let actual = strip_char(&s);
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
|
||||
fn strip_bytes_no_escapes(s in "\\PC*") {
|
||||
dbg!(&s);
|
||||
dbg!(s.as_bytes());
|
||||
let expected = parser_strip(s.as_bytes());
|
||||
let actual = String::from_utf8(strip_bytes(s.as_bytes()).into_vec()).unwrap();
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
|
||||
fn strip_byte_no_escapes(s in "\\PC*") {
|
||||
dbg!(&s);
|
||||
dbg!(s.as_bytes());
|
||||
let expected = parser_strip(s.as_bytes());
|
||||
let actual = String::from_utf8(strip_byte(s.as_bytes()).clone()).unwrap();
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,378 @@
|
||||
/// Incrementally convert to wincon calls for non-contiguous data
|
||||
#[derive(Default, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct WinconBytes {
|
||||
parser: anstyle_parse::Parser,
|
||||
capture: WinconCapture,
|
||||
}
|
||||
|
||||
impl WinconBytes {
|
||||
/// Initial state
|
||||
pub fn new() -> Self {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
/// Strip the next segment of data
|
||||
pub fn extract_next<'s>(&'s mut self, bytes: &'s [u8]) -> WinconBytesIter<'s> {
|
||||
self.capture.reset();
|
||||
self.capture.printable.reserve(bytes.len());
|
||||
WinconBytesIter {
|
||||
bytes,
|
||||
parser: &mut self.parser,
|
||||
capture: &mut self.capture,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// See [`WinconBytes`]
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct WinconBytesIter<'s> {
|
||||
bytes: &'s [u8],
|
||||
parser: &'s mut anstyle_parse::Parser,
|
||||
capture: &'s mut WinconCapture,
|
||||
}
|
||||
|
||||
impl Iterator for WinconBytesIter<'_> {
|
||||
type Item = (anstyle::Style, String);
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
next_bytes(&mut self.bytes, self.parser, self.capture)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn next_bytes(
|
||||
bytes: &mut &[u8],
|
||||
parser: &mut anstyle_parse::Parser,
|
||||
capture: &mut WinconCapture,
|
||||
) -> Option<(anstyle::Style, String)> {
|
||||
capture.reset();
|
||||
while capture.ready.is_none() {
|
||||
let byte = if let Some((byte, remainder)) = (*bytes).split_first() {
|
||||
*bytes = remainder;
|
||||
*byte
|
||||
} else {
|
||||
break;
|
||||
};
|
||||
parser.advance(capture, byte);
|
||||
}
|
||||
if capture.printable.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let style = capture.ready.unwrap_or(capture.style);
|
||||
Some((style, std::mem::take(&mut capture.printable)))
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Debug, PartialEq, Eq)]
|
||||
struct WinconCapture {
|
||||
style: anstyle::Style,
|
||||
printable: String,
|
||||
ready: Option<anstyle::Style>,
|
||||
}
|
||||
|
||||
impl WinconCapture {
|
||||
fn reset(&mut self) {
|
||||
self.ready = None;
|
||||
}
|
||||
}
|
||||
|
||||
impl anstyle_parse::Perform for WinconCapture {
|
||||
/// Draw a character to the screen and update states.
|
||||
fn print(&mut self, c: char) {
|
||||
self.printable.push(c);
|
||||
}
|
||||
|
||||
/// Execute a C0 or C1 control function.
|
||||
fn execute(&mut self, byte: u8) {
|
||||
if byte.is_ascii_whitespace() {
|
||||
self.printable.push(byte as char);
|
||||
}
|
||||
}
|
||||
|
||||
fn csi_dispatch(
|
||||
&mut self,
|
||||
params: &anstyle_parse::Params,
|
||||
_intermediates: &[u8],
|
||||
ignore: bool,
|
||||
action: u8,
|
||||
) {
|
||||
if ignore {
|
||||
return;
|
||||
}
|
||||
if action != b'm' {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut style = self.style;
|
||||
// param/value differences are dependent on the escape code
|
||||
let mut state = CsiState::Normal;
|
||||
let mut r = None;
|
||||
let mut g = None;
|
||||
let mut color_target = ColorTarget::Fg;
|
||||
for param in params {
|
||||
for value in param {
|
||||
match (state, *value) {
|
||||
(CsiState::Normal, 0) => {
|
||||
style = anstyle::Style::default();
|
||||
break;
|
||||
}
|
||||
(CsiState::Normal, 1) => {
|
||||
style = style.bold();
|
||||
break;
|
||||
}
|
||||
(CsiState::Normal, 2) => {
|
||||
style = style.dimmed();
|
||||
break;
|
||||
}
|
||||
(CsiState::Normal, 3) => {
|
||||
style = style.italic();
|
||||
break;
|
||||
}
|
||||
(CsiState::Normal, 4) => {
|
||||
style = style.underline();
|
||||
state = CsiState::Underline;
|
||||
}
|
||||
(CsiState::Normal, 21) => {
|
||||
style |= anstyle::Effects::DOUBLE_UNDERLINE;
|
||||
break;
|
||||
}
|
||||
(CsiState::Normal, 7) => {
|
||||
style = style.invert();
|
||||
break;
|
||||
}
|
||||
(CsiState::Normal, 8) => {
|
||||
style = style.hidden();
|
||||
break;
|
||||
}
|
||||
(CsiState::Normal, 9) => {
|
||||
style = style.strikethrough();
|
||||
break;
|
||||
}
|
||||
(CsiState::Normal, 30..=37) => {
|
||||
let color = to_ansi_color(value - 30).expect("within 4-bit range");
|
||||
style = style.fg_color(Some(color.into()));
|
||||
break;
|
||||
}
|
||||
(CsiState::Normal, 38) => {
|
||||
color_target = ColorTarget::Fg;
|
||||
state = CsiState::PrepareCustomColor;
|
||||
}
|
||||
(CsiState::Normal, 39) => {
|
||||
style = style.fg_color(None);
|
||||
break;
|
||||
}
|
||||
(CsiState::Normal, 40..=47) => {
|
||||
let color = to_ansi_color(value - 40).expect("within 4-bit range");
|
||||
style = style.bg_color(Some(color.into()));
|
||||
break;
|
||||
}
|
||||
(CsiState::Normal, 48) => {
|
||||
color_target = ColorTarget::Bg;
|
||||
state = CsiState::PrepareCustomColor;
|
||||
}
|
||||
(CsiState::Normal, 49) => {
|
||||
style = style.bg_color(None);
|
||||
break;
|
||||
}
|
||||
(CsiState::Normal, 58) => {
|
||||
color_target = ColorTarget::Underline;
|
||||
state = CsiState::PrepareCustomColor;
|
||||
}
|
||||
(CsiState::Normal, 90..=97) => {
|
||||
let color = to_ansi_color(value - 90)
|
||||
.expect("within 4-bit range")
|
||||
.bright(true);
|
||||
style = style.fg_color(Some(color.into()));
|
||||
break;
|
||||
}
|
||||
(CsiState::Normal, 100..=107) => {
|
||||
let color = to_ansi_color(value - 100)
|
||||
.expect("within 4-bit range")
|
||||
.bright(true);
|
||||
style = style.bg_color(Some(color.into()));
|
||||
break;
|
||||
}
|
||||
(CsiState::PrepareCustomColor, 5) => {
|
||||
state = CsiState::Ansi256;
|
||||
}
|
||||
(CsiState::PrepareCustomColor, 2) => {
|
||||
state = CsiState::Rgb;
|
||||
r = None;
|
||||
g = None;
|
||||
}
|
||||
(CsiState::Ansi256, n) => {
|
||||
let color = anstyle::Ansi256Color(n as u8);
|
||||
style = match color_target {
|
||||
ColorTarget::Fg => style.fg_color(Some(color.into())),
|
||||
ColorTarget::Bg => style.bg_color(Some(color.into())),
|
||||
ColorTarget::Underline => style.underline_color(Some(color.into())),
|
||||
};
|
||||
break;
|
||||
}
|
||||
(CsiState::Rgb, b) => match (r, g) {
|
||||
(None, _) => {
|
||||
r = Some(b);
|
||||
}
|
||||
(Some(_), None) => {
|
||||
g = Some(b);
|
||||
}
|
||||
(Some(r), Some(g)) => {
|
||||
let color = anstyle::RgbColor(r as u8, g as u8, b as u8);
|
||||
style = match color_target {
|
||||
ColorTarget::Fg => style.fg_color(Some(color.into())),
|
||||
ColorTarget::Bg => style.bg_color(Some(color.into())),
|
||||
ColorTarget::Underline => style.underline_color(Some(color.into())),
|
||||
};
|
||||
break;
|
||||
}
|
||||
},
|
||||
(CsiState::Underline, 0) => {
|
||||
style =
|
||||
style.effects(style.get_effects().remove(anstyle::Effects::UNDERLINE));
|
||||
}
|
||||
(CsiState::Underline, 1) => {
|
||||
// underline already set
|
||||
}
|
||||
(CsiState::Underline, 2) => {
|
||||
style = style
|
||||
.effects(style.get_effects().remove(anstyle::Effects::UNDERLINE))
|
||||
| anstyle::Effects::DOUBLE_UNDERLINE;
|
||||
}
|
||||
(CsiState::Underline, 3) => {
|
||||
style = style
|
||||
.effects(style.get_effects().remove(anstyle::Effects::UNDERLINE))
|
||||
| anstyle::Effects::CURLY_UNDERLINE;
|
||||
}
|
||||
(CsiState::Underline, 4) => {
|
||||
style = style
|
||||
.effects(style.get_effects().remove(anstyle::Effects::UNDERLINE))
|
||||
| anstyle::Effects::DOTTED_UNDERLINE;
|
||||
}
|
||||
(CsiState::Underline, 5) => {
|
||||
style = style
|
||||
.effects(style.get_effects().remove(anstyle::Effects::UNDERLINE))
|
||||
| anstyle::Effects::DASHED_UNDERLINE;
|
||||
}
|
||||
_ => {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if style != self.style && !self.printable.is_empty() {
|
||||
self.ready = Some(self.style);
|
||||
}
|
||||
self.style = style;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
enum CsiState {
|
||||
Normal,
|
||||
PrepareCustomColor,
|
||||
Ansi256,
|
||||
Rgb,
|
||||
Underline,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
enum ColorTarget {
|
||||
Fg,
|
||||
Bg,
|
||||
Underline,
|
||||
}
|
||||
|
||||
fn to_ansi_color(digit: u16) -> Option<anstyle::AnsiColor> {
|
||||
match digit {
|
||||
0 => Some(anstyle::AnsiColor::Black),
|
||||
1 => Some(anstyle::AnsiColor::Red),
|
||||
2 => Some(anstyle::AnsiColor::Green),
|
||||
3 => Some(anstyle::AnsiColor::Yellow),
|
||||
4 => Some(anstyle::AnsiColor::Blue),
|
||||
5 => Some(anstyle::AnsiColor::Magenta),
|
||||
6 => Some(anstyle::AnsiColor::Cyan),
|
||||
7 => Some(anstyle::AnsiColor::White),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use proptest::prelude::*;
|
||||
|
||||
#[track_caller]
|
||||
fn verify(input: &str, expected: Vec<(anstyle::Style, &str)>) {
|
||||
let expected = expected
|
||||
.into_iter()
|
||||
.map(|(style, value)| (style, value.to_owned()))
|
||||
.collect::<Vec<_>>();
|
||||
let mut state = WinconBytes::new();
|
||||
let actual = state.extract_next(input.as_bytes()).collect::<Vec<_>>();
|
||||
assert_eq!(expected, actual, "{input:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn start() {
|
||||
let green_on_red = anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red);
|
||||
let input = format!("{green_on_red}Hello{green_on_red:#} world!");
|
||||
let expected = vec![
|
||||
(green_on_red, "Hello"),
|
||||
(anstyle::Style::default(), " world!"),
|
||||
];
|
||||
verify(&input, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn middle() {
|
||||
let green_on_red = anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red);
|
||||
let input = format!("Hello {green_on_red}world{green_on_red:#}!");
|
||||
let expected = vec![
|
||||
(anstyle::Style::default(), "Hello "),
|
||||
(green_on_red, "world"),
|
||||
(anstyle::Style::default(), "!"),
|
||||
];
|
||||
verify(&input, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn end() {
|
||||
let green_on_red = anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red);
|
||||
let input = format!("Hello {green_on_red}world!{green_on_red:#}");
|
||||
let expected = vec![
|
||||
(anstyle::Style::default(), "Hello "),
|
||||
(green_on_red, "world!"),
|
||||
];
|
||||
verify(&input, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ansi256_colors() {
|
||||
let ansi_11 = anstyle::Ansi256Color(11).on_default();
|
||||
// termcolor only supports "brights" via these
|
||||
let input = format!("Hello {ansi_11}world{ansi_11:#}!");
|
||||
let expected = vec![
|
||||
(anstyle::Style::default(), "Hello "),
|
||||
(ansi_11, "world"),
|
||||
(anstyle::Style::default(), "!"),
|
||||
];
|
||||
verify(&input, expected);
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
#[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
|
||||
fn wincon_no_escapes(s in "\\PC*") {
|
||||
let expected = if s.is_empty() {
|
||||
vec![]
|
||||
} else {
|
||||
vec![(anstyle::Style::default(), s.clone())]
|
||||
};
|
||||
let mut state = WinconBytes::new();
|
||||
let actual = state.extract_next(s.as_bytes()).collect::<Vec<_>>();
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user