diff options
Diffstat (limited to '')
-rw-r--r-- | vendor/nu-ansi-term/src/ansi.rs (renamed from vendor/ansi_term/src/ansi.rs) | 229 | ||||
-rw-r--r-- | vendor/nu-ansi-term/src/debug.rs (renamed from vendor/ansi_term/src/debug.rs) | 84 | ||||
-rw-r--r-- | vendor/nu-ansi-term/src/difference.rs (renamed from vendor/ansi_term/src/difference.rs) | 25 | ||||
-rw-r--r-- | vendor/nu-ansi-term/src/display.rs (renamed from vendor/ansi_term/src/display.rs) | 220 | ||||
-rw-r--r-- | vendor/nu-ansi-term/src/gradient.rs | 105 | ||||
-rw-r--r-- | vendor/nu-ansi-term/src/lib.rs (renamed from vendor/ansi_term/src/lib.rs) | 145 | ||||
-rw-r--r-- | vendor/nu-ansi-term/src/rgb.rs | 173 | ||||
-rw-r--r-- | vendor/nu-ansi-term/src/style.rs | 629 | ||||
-rw-r--r-- | vendor/nu-ansi-term/src/util.rs (renamed from vendor/ansi_term/src/util.rs) | 40 | ||||
-rw-r--r-- | vendor/nu-ansi-term/src/windows.rs (renamed from vendor/ansi_term/src/windows.rs) | 17 | ||||
-rw-r--r-- | vendor/nu-ansi-term/src/write.rs (renamed from vendor/ansi_term/src/write.rs) | 19 |
11 files changed, 1315 insertions, 371 deletions
diff --git a/vendor/ansi_term/src/ansi.rs b/vendor/nu-ansi-term/src/ansi.rs index aaf215234..8f393fcdc 100644 --- a/vendor/ansi_term/src/ansi.rs +++ b/vendor/nu-ansi-term/src/ansi.rs @@ -1,17 +1,11 @@ -use style::{Colour, Style}; - +#![allow(missing_docs)] +use crate::style::{Color, Style}; +use crate::write::AnyWrite; use std::fmt; -use write::AnyWrite; - - -// ---- generating ANSI codes ---- - impl Style { - /// Write any bytes that go *before* a piece of text to the given writer. fn write_prefix<W: AnyWrite + ?Sized>(&self, f: &mut W) -> Result<(), W::Error> { - // If there are actually no styles here, then don’t write *any* codes // as the prefix. An empty ANSI code may not affect the terminal // output at all, but a user may just want a code-free string. @@ -26,33 +20,55 @@ impl Style { { let mut write_char = |c| { - if written_anything { write!(f, ";")?; } + if written_anything { + write!(f, ";")?; + } written_anything = true; write!(f, "{}", c)?; Ok(()) }; - if self.is_bold { write_char('1')? } - if self.is_dimmed { write_char('2')? } - if self.is_italic { write_char('3')? } - if self.is_underline { write_char('4')? } - if self.is_blink { write_char('5')? } - if self.is_reverse { write_char('7')? } - if self.is_hidden { write_char('8')? } - if self.is_strikethrough { write_char('9')? } + if self.is_bold { + write_char('1')? + } + if self.is_dimmed { + write_char('2')? + } + if self.is_italic { + write_char('3')? + } + if self.is_underline { + write_char('4')? + } + if self.is_blink { + write_char('5')? + } + if self.is_reverse { + write_char('7')? + } + if self.is_hidden { + write_char('8')? + } + if self.is_strikethrough { + write_char('9')? + } } - // The foreground and background colours, if specified, need to be + // The foreground and background colors, if specified, need to be // handled specially because the number codes are more complicated. // (see `write_background_code` and `write_foreground_code`) if let Some(bg) = self.background { - if written_anything { write!(f, ";")?; } + if written_anything { + write!(f, ";")?; + } written_anything = true; bg.write_background_code(f)?; } if let Some(fg) = self.foreground { - if written_anything { write!(f, ";")?; } + if written_anything { + write!(f, ";")?; + } fg.write_foreground_code(f)?; } @@ -66,53 +82,70 @@ impl Style { fn write_suffix<W: AnyWrite + ?Sized>(&self, f: &mut W) -> Result<(), W::Error> { if self.is_plain() { Ok(()) - } - else { + } else { write!(f, "{}", RESET) } } } - /// The code to send to reset all styles and return to `Style::default()`. pub static RESET: &str = "\x1B[0m"; - - -impl Colour { +impl Color { fn write_foreground_code<W: AnyWrite + ?Sized>(&self, f: &mut W) -> Result<(), W::Error> { - match *self { - Colour::Black => write!(f, "30"), - Colour::Red => write!(f, "31"), - Colour::Green => write!(f, "32"), - Colour::Yellow => write!(f, "33"), - Colour::Blue => write!(f, "34"), - Colour::Purple => write!(f, "35"), - Colour::Cyan => write!(f, "36"), - Colour::White => write!(f, "37"), - Colour::Fixed(num) => write!(f, "38;5;{}", &num), - Colour::RGB(r,g,b) => write!(f, "38;2;{};{};{}", &r, &g, &b), + match self { + Color::Black => write!(f, "30"), + Color::Red => write!(f, "31"), + Color::Green => write!(f, "32"), + Color::Yellow => write!(f, "33"), + Color::Blue => write!(f, "34"), + Color::Purple => write!(f, "35"), + Color::Magenta => write!(f, "35"), + Color::Cyan => write!(f, "36"), + Color::White => write!(f, "37"), + Color::Fixed(num) => write!(f, "38;5;{}", num), + Color::Rgb(r, g, b) => write!(f, "38;2;{};{};{}", r, g, b), + Color::Default => write!(f, "39"), + Color::DarkGray => write!(f, "90"), + Color::LightRed => write!(f, "91"), + Color::LightGreen => write!(f, "92"), + Color::LightYellow => write!(f, "93"), + Color::LightBlue => write!(f, "94"), + Color::LightPurple => write!(f, "95"), + Color::LightMagenta => write!(f, "95"), + Color::LightCyan => write!(f, "96"), + Color::LightGray => write!(f, "97"), } } fn write_background_code<W: AnyWrite + ?Sized>(&self, f: &mut W) -> Result<(), W::Error> { - match *self { - Colour::Black => write!(f, "40"), - Colour::Red => write!(f, "41"), - Colour::Green => write!(f, "42"), - Colour::Yellow => write!(f, "43"), - Colour::Blue => write!(f, "44"), - Colour::Purple => write!(f, "45"), - Colour::Cyan => write!(f, "46"), - Colour::White => write!(f, "47"), - Colour::Fixed(num) => write!(f, "48;5;{}", &num), - Colour::RGB(r,g,b) => write!(f, "48;2;{};{};{}", &r, &g, &b), + match self { + Color::Black => write!(f, "40"), + Color::Red => write!(f, "41"), + Color::Green => write!(f, "42"), + Color::Yellow => write!(f, "43"), + Color::Blue => write!(f, "44"), + Color::Purple => write!(f, "45"), + Color::Magenta => write!(f, "45"), + Color::Cyan => write!(f, "46"), + Color::White => write!(f, "47"), + Color::Fixed(num) => write!(f, "48;5;{}", num), + Color::Rgb(r, g, b) => write!(f, "48;2;{};{};{}", r, g, b), + Color::Default => write!(f, "49"), + Color::DarkGray => write!(f, "100"), + Color::LightRed => write!(f, "101"), + Color::LightGreen => write!(f, "102"), + Color::LightYellow => write!(f, "103"), + Color::LightBlue => write!(f, "104"), + Color::LightPurple => write!(f, "105"), + Color::LightMagenta => write!(f, "105"), + Color::LightCyan => write!(f, "106"), + Color::LightGray => write!(f, "107"), } } } - -/// Like `ANSIString`, but only displays the style prefix. +/// Like `AnsiString`, but only displays the style prefix. /// /// This type implements the `Display` trait, meaning it can be written to a /// `std::fmt` formatting without doing any extra allocation, and written to a @@ -121,7 +154,7 @@ impl Colour { #[derive(Clone, Copy, Debug)] pub struct Prefix(Style); -/// Like `ANSIString`, but only displays the difference between two +/// Like `AnsiString`, but only displays the difference between two /// styles. /// /// This type implements the `Display` trait, meaning it can be written to a @@ -131,7 +164,7 @@ pub struct Prefix(Style); #[derive(Clone, Copy, Debug)] pub struct Infix(Style, Style); -/// Like `ANSIString`, but only displays the style suffix. +/// Like `AnsiString`, but only displays the style suffix. /// /// This type implements the `Display` trait, meaning it can be written to a /// `std::fmt` formatting without doing any extra allocation, and written to a @@ -140,16 +173,14 @@ pub struct Infix(Style, Style); #[derive(Clone, Copy, Debug)] pub struct Suffix(Style); - impl Style { - /// The prefix bytes for this style. These are the bytes that tell the - /// terminal to use a different colour or font style. + /// terminal to use a different color or font style. /// /// # Examples /// /// ``` - /// use ansi_term::{Style, Colour::Blue}; + /// use nu_ansi_term::{Style, Color::Blue}; /// /// let style = Style::default().bold(); /// assert_eq!("\x1b[1m", @@ -169,12 +200,12 @@ impl Style { /// The infix bytes between this style and `next` style. These are the bytes /// that tell the terminal to change the style to `next`. These may include - /// a reset followed by the next colour and style, depending on the two styles. + /// a reset followed by the next color and style, depending on the two styles. /// /// # Examples /// /// ``` - /// use ansi_term::{Style, Colour::Green}; + /// use nu_ansi_term::{Style, Color::Green}; /// /// let style = Style::default().bold(); /// assert_eq!("\x1b[32m", @@ -193,12 +224,12 @@ impl Style { } /// The suffix for this style. These are the bytes that tell the terminal - /// to reset back to its normal colour and font style. + /// to reset back to its normal color and font style. /// /// # Examples /// /// ``` - /// use ansi_term::{Style, Colour::Green}; + /// use nu_ansi_term::{Style, Color::Green}; /// /// let style = Style::default().bold(); /// assert_eq!("\x1b[0m", @@ -217,18 +248,16 @@ impl Style { } } - -impl Colour { - - /// The prefix bytes for this colour as a `Style`. These are the bytes - /// that tell the terminal to use a different colour or font style. +impl Color { + /// The prefix bytes for this color as a `Style`. These are the bytes + /// that tell the terminal to use a different color or font style. /// /// See also [`Style::prefix`](struct.Style.html#method.prefix). /// /// # Examples /// /// ``` - /// use ansi_term::Colour::Green; + /// use nu_ansi_term::Color::Green; /// /// assert_eq!("\x1b[0m", /// Green.suffix().to_string()); @@ -237,33 +266,33 @@ impl Colour { Prefix(self.normal()) } - /// The infix bytes between this colour and `next` colour. These are the bytes - /// that tell the terminal to use the `next` colour, or to do nothing if - /// the two colours are equal. + /// The infix bytes between this color and `next` color. These are the bytes + /// that tell the terminal to use the `next` color, or to do nothing if + /// the two colors are equal. /// /// See also [`Style::infix`](struct.Style.html#method.infix). /// /// # Examples /// /// ``` - /// use ansi_term::Colour::{Red, Yellow}; + /// use nu_ansi_term::Color::{Red, Yellow}; /// /// assert_eq!("\x1b[33m", /// Red.infix(Yellow).to_string()); /// ``` - pub fn infix(self, next: Colour) -> Infix { + pub fn infix(self, next: Color) -> Infix { Infix(self.normal(), next.normal()) } - /// The suffix for this colour as a `Style`. These are the bytes that - /// tell the terminal to reset back to its normal colour and font style. + /// The suffix for this color as a `Style`. These are the bytes that + /// tell the terminal to reset back to its normal color and font style. /// /// See also [`Style::suffix`](struct.Style.html#method.suffix). /// /// # Examples /// /// ``` - /// use ansi_term::Colour::Purple; + /// use nu_ansi_term::Color::Purple; /// /// assert_eq!("\x1b[0m", /// Purple.suffix().to_string()); @@ -273,49 +302,44 @@ impl Colour { } } - impl fmt::Display for Prefix { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let f: &mut fmt::Write = f; + let f: &mut dyn fmt::Write = f; self.0.write_prefix(f) } } - impl fmt::Display for Infix { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use difference::Difference; + use crate::difference::Difference; match Difference::between(&self.0, &self.1) { Difference::ExtraStyles(style) => { - let f: &mut fmt::Write = f; + let f: &mut dyn fmt::Write = f; style.write_prefix(f) - }, + } Difference::Reset => { - let f: &mut fmt::Write = f; + let f: &mut dyn fmt::Write = f; write!(f, "{}{}", RESET, self.1.prefix()) - }, - Difference::NoDifference => { - Ok(()) // nothing to write - }, + } + Difference::Empty => { + Ok(()) // nothing to write + } } } } - impl fmt::Display for Suffix { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let f: &mut fmt::Write = f; + let f: &mut dyn fmt::Write = f; self.0.write_suffix(f) } } - - #[cfg(test)] mod test { - use style::Style; - use style::Colour::*; + use crate::style::Color::*; + use crate::style::Style; macro_rules! test { ($name: ident: $style: expr; $input: expr => $result: expr) => { @@ -341,6 +365,8 @@ mod test { test!(purple_on_white: Purple.on(White); "hi" => "\x1B[47;35mhi\x1B[0m"); test!(purple_on_white_2: Purple.normal().on(White); "hi" => "\x1B[47;35mhi\x1B[0m"); test!(yellow_on_blue: Style::new().on(Blue).fg(Yellow); "hi" => "\x1B[44;33mhi\x1B[0m"); + test!(magenta_on_white: Magenta.on(White); "hi" => "\x1B[47;35mhi\x1B[0m"); + test!(magenta_on_white_2: Magenta.normal().on(White); "hi" => "\x1B[47;35mhi\x1B[0m"); test!(yellow_on_blue_2: Cyan.on(Blue).fg(Yellow); "hi" => "\x1B[44;33mhi\x1B[0m"); test!(cyan_bold_on_white: Cyan.bold().on(White); "hi" => "\x1B[1;47;36mhi\x1B[0m"); test!(cyan_ul_on_white: Cyan.underline().on(White); "hi" => "\x1B[4;47;36mhi\x1B[0m"); @@ -349,10 +375,10 @@ mod test { test!(fixed: Fixed(100); "hi" => "\x1B[38;5;100mhi\x1B[0m"); test!(fixed_on_purple: Fixed(100).on(Purple); "hi" => "\x1B[45;38;5;100mhi\x1B[0m"); test!(fixed_on_fixed: Fixed(100).on(Fixed(200)); "hi" => "\x1B[48;5;200;38;5;100mhi\x1B[0m"); - test!(rgb: RGB(70,130,180); "hi" => "\x1B[38;2;70;130;180mhi\x1B[0m"); - test!(rgb_on_blue: RGB(70,130,180).on(Blue); "hi" => "\x1B[44;38;2;70;130;180mhi\x1B[0m"); - test!(blue_on_rgb: Blue.on(RGB(70,130,180)); "hi" => "\x1B[48;2;70;130;180;34mhi\x1B[0m"); - test!(rgb_on_rgb: RGB(70,130,180).on(RGB(5,10,15)); "hi" => "\x1B[48;2;5;10;15;38;2;70;130;180mhi\x1B[0m"); + test!(rgb: Rgb(70,130,180); "hi" => "\x1B[38;2;70;130;180mhi\x1B[0m"); + test!(rgb_on_blue: Rgb(70,130,180).on(Blue); "hi" => "\x1B[44;38;2;70;130;180mhi\x1B[0m"); + test!(blue_on_rgb: Blue.on(Rgb(70,130,180)); "hi" => "\x1B[48;2;70;130;180;34mhi\x1B[0m"); + test!(rgb_on_rgb: Rgb(70,130,180).on(Rgb(5,10,15)); "hi" => "\x1B[48;2;5;10;15;38;2;70;130;180mhi\x1B[0m"); test!(bold: Style::new().bold(); "hi" => "\x1B[1mhi\x1B[0m"); test!(underline: Style::new().underline(); "hi" => "\x1B[4mhi\x1B[0m"); test!(bunderline: Style::new().bold().underline(); "hi" => "\x1B[1;4mhi\x1B[0m"); @@ -362,11 +388,18 @@ mod test { test!(reverse: Style::new().reverse(); "hi" => "\x1B[7mhi\x1B[0m"); test!(hidden: Style::new().hidden(); "hi" => "\x1B[8mhi\x1B[0m"); test!(stricken: Style::new().strikethrough(); "hi" => "\x1B[9mhi\x1B[0m"); + test!(lr_on_lr: LightRed.on(LightRed); "hi" => "\x1B[101;91mhi\x1B[0m"); #[test] fn test_infix() { - assert_eq!(Style::new().dimmed().infix(Style::new()).to_string(), "\x1B[0m"); - assert_eq!(White.dimmed().infix(White.normal()).to_string(), "\x1B[0m\x1B[37m"); + assert_eq!( + Style::new().dimmed().infix(Style::new()).to_string(), + "\x1B[0m" + ); + assert_eq!( + White.dimmed().infix(White.normal()).to_string(), + "\x1B[0m\x1B[37m" + ); assert_eq!(White.normal().infix(White.bold()).to_string(), "\x1B[1m"); assert_eq!(White.normal().infix(Blue.normal()).to_string(), "\x1B[34m"); assert_eq!(Blue.bold().infix(Blue.bold()).to_string(), ""); diff --git a/vendor/ansi_term/src/debug.rs b/vendor/nu-ansi-term/src/debug.rs index 4877323ff..1dcde52be 100644 --- a/vendor/ansi_term/src/debug.rs +++ b/vendor/nu-ansi-term/src/debug.rs @@ -1,67 +1,86 @@ +use crate::style::Style; use std::fmt; -use style::Style; - /// Styles have a special `Debug` implementation that only shows the fields that /// are set. Fields that haven’t been touched aren’t included in the output. /// /// This behaviour gets bypassed when using the alternate formatting mode /// `format!("{:#?}")`. /// -/// use ansi_term::Colour::{Red, Blue}; +/// use nu_ansi_term::Color::{Red, Blue}; /// assert_eq!("Style { fg(Red), on(Blue), bold, italic }", /// format!("{:?}", Red.on(Blue).bold().italic())); impl fmt::Debug for Style { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { if fmt.alternate() { fmt.debug_struct("Style") - .field("foreground", &self.foreground) - .field("background", &self.background) - .field("blink", &self.is_blink) - .field("bold", &self.is_bold) - .field("dimmed", &self.is_dimmed) - .field("hidden", &self.is_hidden) - .field("italic", &self.is_italic) - .field("reverse", &self.is_reverse) - .field("strikethrough", &self.is_strikethrough) - .field("underline", &self.is_underline) - .finish() - } - else if self.is_plain() { + .field("foreground", &self.foreground) + .field("background", &self.background) + .field("blink", &self.is_blink) + .field("bold", &self.is_bold) + .field("dimmed", &self.is_dimmed) + .field("hidden", &self.is_hidden) + .field("italic", &self.is_italic) + .field("reverse", &self.is_reverse) + .field("strikethrough", &self.is_strikethrough) + .field("underline", &self.is_underline) + .finish() + } else if self.is_plain() { fmt.write_str("Style {}") - } - else { + } else { fmt.write_str("Style { ")?; let mut written_anything = false; if let Some(fg) = self.foreground { - if written_anything { fmt.write_str(", ")? } + if written_anything { + fmt.write_str(", ")? + } written_anything = true; write!(fmt, "fg({:?})", fg)? } if let Some(bg) = self.background { - if written_anything { fmt.write_str(", ")? } + if written_anything { + fmt.write_str(", ")? + } written_anything = true; write!(fmt, "on({:?})", bg)? } { let mut write_flag = |name| { - if written_anything { fmt.write_str(", ")? } + if written_anything { + fmt.write_str(", ")? + } written_anything = true; fmt.write_str(name) }; - if self.is_blink { write_flag("blink")? } - if self.is_bold { write_flag("bold")? } - if self.is_dimmed { write_flag("dimmed")? } - if self.is_hidden { write_flag("hidden")? } - if self.is_italic { write_flag("italic")? } - if self.is_reverse { write_flag("reverse")? } - if self.is_strikethrough { write_flag("strikethrough")? } - if self.is_underline { write_flag("underline")? } + if self.is_blink { + write_flag("blink")? + } + if self.is_bold { + write_flag("bold")? + } + if self.is_dimmed { + write_flag("dimmed")? + } + if self.is_hidden { + write_flag("hidden")? + } + if self.is_italic { + write_flag("italic")? + } + if self.is_reverse { + write_flag("reverse")? + } + if self.is_strikethrough { + write_flag("strikethrough")? + } + if self.is_underline { + write_flag("underline")? + } } write!(fmt, " }}") @@ -69,11 +88,10 @@ impl fmt::Debug for Style { } } - #[cfg(test)] mod test { - use style::Colour::*; - use style::Style; + use crate::style::Color::*; + use crate::style::Style; fn style() -> Style { Style::new() @@ -94,7 +112,7 @@ mod test { test!(both: style().bold().italic() => "Style { bold, italic }"); test!(red: Red.normal() => "Style { fg(Red) }"); - test!(redblue: Red.normal().on(RGB(3, 2, 4)) => "Style { fg(Red), on(RGB(3, 2, 4)) }"); + test!(redblue: Red.normal().on(Rgb(3, 2, 4)) => "Style { fg(Red), on(Rgb(3, 2, 4)) }"); test!(everything: Red.on(Blue).blink().bold().dimmed().hidden().italic().reverse().strikethrough().underline() => diff --git a/vendor/ansi_term/src/difference.rs b/vendor/nu-ansi-term/src/difference.rs index b0de07f7e..beee8ea25 100644 --- a/vendor/ansi_term/src/difference.rs +++ b/vendor/nu-ansi-term/src/difference.rs @@ -1,11 +1,9 @@ use super::Style; - -/// When printing out one coloured string followed by another, use one of +/// When printing out one colored string followed by another, use one of /// these rules to figure out which *extra* control codes need to be sent. #[derive(PartialEq, Clone, Copy, Debug)] pub enum Difference { - /// Print out the control codes specified by this style to end up looking /// like the second string's styles. ExtraStyles(Style), @@ -16,19 +14,17 @@ pub enum Difference { /// The before style is exactly the same as the after style, so no further /// control codes need to be printed. - NoDifference, + Empty, } - impl Difference { - /// Compute the 'style difference' required to turn an existing style into /// the given, second style. /// /// For example, to turn green text into green bold text, it's redundant /// to write a reset command then a second green+bold command, instead of /// just writing one bold command. This method should see that both styles - /// use the foreground colour green, and reduce it to a single command. + /// use the foreground color green, and reduce it to a single command. /// /// This method returns an enum value because it's not actually always /// possible to turn one style into another: for example, text could be @@ -44,7 +40,7 @@ impl Difference { // it commented out for now, and defaulting to Reset. if first == next { - return NoDifference; + return Empty; } // Cannot un-bold, so must Reset. @@ -137,13 +133,12 @@ impl Difference { } } - #[cfg(test)] mod test { - use super::*; use super::Difference::*; - use style::Colour::*; - use style::Style; + use super::*; + use crate::style::Color::*; + use crate::style::Style; fn style() -> Style { Style::new() @@ -158,12 +153,12 @@ mod test { }; } - test!(nothing: Green.normal(); Green.normal() => NoDifference); + test!(nothing: Green.normal(); Green.normal() => Empty); test!(uppercase: Green.normal(); Green.bold() => ExtraStyles(style().bold())); test!(lowercase: Green.bold(); Green.normal() => Reset); - test!(nothing2: Green.bold(); Green.bold() => NoDifference); + test!(nothing2: Green.bold(); Green.bold() => Empty); - test!(colour_change: Red.normal(); Blue.normal() => ExtraStyles(Blue.normal())); + test!(color_change: Red.normal(); Blue.normal() => ExtraStyles(Blue.normal())); test!(addition_of_blink: style(); style().blink() => ExtraStyles(style().blink())); test!(addition_of_dimmed: style(); style().dimmed() => ExtraStyles(style().dimmed())); diff --git a/vendor/ansi_term/src/display.rs b/vendor/nu-ansi-term/src/display.rs index 17c54f008..bed934cb3 100644 --- a/vendor/ansi_term/src/display.rs +++ b/vendor/nu-ansi-term/src/display.rs @@ -1,40 +1,40 @@ +use crate::ansi::RESET; +use crate::difference::Difference; +use crate::style::{Color, Style}; +use crate::write::AnyWrite; use std::borrow::Cow; use std::fmt; use std::io; -use std::ops::Deref; -use ansi::RESET; -use difference::Difference; -use style::{Style, Colour}; -use write::AnyWrite; - - -/// An `ANSIGenericString` includes a generic string type and a `Style` to -/// display that string. `ANSIString` and `ANSIByteString` are aliases for +/// An `AnsiGenericString` includes a generic string type and a `Style` to +/// display that string. `AnsiString` and `AnsiByteString` are aliases for /// this type on `str` and `\[u8]`, respectively. #[derive(PartialEq, Debug)] -pub struct ANSIGenericString<'a, S: 'a + ToOwned + ?Sized> -where <S as ToOwned>::Owned: fmt::Debug { - style: Style, - string: Cow<'a, S>, +pub struct AnsiGenericString<'a, S: 'a + ToOwned + ?Sized> +where + <S as ToOwned>::Owned: fmt::Debug, +{ + pub(crate) style: Style, + pub(crate) string: Cow<'a, S>, } - -/// Cloning an `ANSIGenericString` will clone its underlying string. +/// Cloning an `AnsiGenericString` will clone its underlying string. /// /// # Examples /// /// ``` -/// use ansi_term::ANSIString; +/// use nu_ansi_term::AnsiString; /// -/// let plain_string = ANSIString::from("a plain string"); +/// let plain_string = AnsiString::from("a plain string"); /// let clone_string = plain_string.clone(); /// assert_eq!(clone_string, plain_string); /// ``` -impl<'a, S: 'a + ToOwned + ?Sized> Clone for ANSIGenericString<'a, S> -where <S as ToOwned>::Owned: fmt::Debug { - fn clone(&self) -> ANSIGenericString<'a, S> { - ANSIGenericString { +impl<'a, S: 'a + ToOwned + ?Sized> Clone for AnsiGenericString<'a, S> +where + <S as ToOwned>::Owned: fmt::Debug, +{ + fn clone(&self) -> AnsiGenericString<'a, S> { + AnsiGenericString { style: self.style, string: self.string.clone(), } @@ -56,14 +56,12 @@ where <S as ToOwned>::Owned: fmt::Debug { // that used it: // // #[derive(PartialEq, Debug, Clone, Default)] -// pub struct TextCellContents(Vec<ANSIString<'static>>); +// pub struct TextCellContents(Vec<AnsiString<'static>>); // ^^^^^^^^^^^^^^^^^^^^^^^^^ // error[E0277]: the trait `std::clone::Clone` is not implemented for `str` // // The hand-written impl above can ignore that constraint and still compile. - - /// An ANSI String is a string coupled with the `Style` to display it /// in a terminal. /// @@ -73,39 +71,41 @@ where <S as ToOwned>::Owned: fmt::Debug { /// # Examples /// /// ``` -/// use ansi_term::ANSIString; -/// use ansi_term::Colour::Red; +/// use nu_ansi_term::AnsiString; +/// use nu_ansi_term::Color::Red; /// /// let red_string = Red.paint("a red string"); /// println!("{}", red_string); /// ``` /// /// ``` -/// use ansi_term::ANSIString; +/// use nu_ansi_term::AnsiString; /// -/// let plain_string = ANSIString::from("a plain string"); -/// assert_eq!(&*plain_string, "a plain string"); +/// let plain_string = AnsiString::from("a plain string"); /// ``` -pub type ANSIString<'a> = ANSIGenericString<'a, str>; - -/// An `ANSIByteString` represents a formatted series of bytes. Use -/// `ANSIByteString` when styling text with an unknown encoding. -pub type ANSIByteString<'a> = ANSIGenericString<'a, [u8]>; - -impl<'a, I, S: 'a + ToOwned + ?Sized> From<I> for ANSIGenericString<'a, S> -where I: Into<Cow<'a, S>>, - <S as ToOwned>::Owned: fmt::Debug { - fn from(input: I) -> ANSIGenericString<'a, S> { - ANSIGenericString { +pub type AnsiString<'a> = AnsiGenericString<'a, str>; + +/// An `AnsiByteString` represents a formatted series of bytes. Use +/// `AnsiByteString` when styling text with an unknown encoding. +pub type AnsiByteString<'a> = AnsiGenericString<'a, [u8]>; + +impl<'a, I, S: 'a + ToOwned + ?Sized> From<I> for AnsiGenericString<'a, S> +where + I: Into<Cow<'a, S>>, + <S as ToOwned>::Owned: fmt::Debug, +{ + fn from(input: I) -> AnsiGenericString<'a, S> { + AnsiGenericString { string: input.into(), - style: Style::default(), + style: Style::default(), } } } -impl<'a, S: 'a + ToOwned + ?Sized> ANSIGenericString<'a, S> - where <S as ToOwned>::Owned: fmt::Debug { - +impl<'a, S: 'a + ToOwned + ?Sized> AnsiGenericString<'a, S> +where + <S as ToOwned>::Owned: fmt::Debug, +{ /// Directly access the style pub fn style_ref(&self) -> &Style { &self.style @@ -117,133 +117,128 @@ impl<'a, S: 'a + ToOwned + ?Sized> ANSIGenericString<'a, S> } } -impl<'a, S: 'a + ToOwned + ?Sized> Deref for ANSIGenericString<'a, S> -where <S as ToOwned>::Owned: fmt::Debug { - type Target = S; - - fn deref(&self) -> &S { - self.string.deref() - } -} - - -/// A set of `ANSIGenericString`s collected together, in order to be +/// A set of `AnsiGenericStrings`s collected together, in order to be /// written with a minimum of control characters. #[derive(Debug, PartialEq)] -pub struct ANSIGenericStrings<'a, S: 'a + ToOwned + ?Sized> - (pub &'a [ANSIGenericString<'a, S>]) - where <S as ToOwned>::Owned: fmt::Debug, S: PartialEq; +pub struct AnsiGenericStrings<'a, S: 'a + ToOwned + ?Sized>(pub &'a [AnsiGenericString<'a, S>]) +where + <S as ToOwned>::Owned: fmt::Debug, + S: PartialEq; -/// A set of `ANSIString`s collected together, in order to be written with a +/// A set of `AnsiString`s collected together, in order to be written with a /// minimum of control characters. -pub type ANSIStrings<'a> = ANSIGenericStrings<'a, str>; +pub type AnsiStrings<'a> = AnsiGenericStrings<'a, str>; -/// A function to construct an `ANSIStrings` instance. +/// A function to construct an `AnsiStrings` instance. #[allow(non_snake_case)] -pub fn ANSIStrings<'a>(arg: &'a [ANSIString<'a>]) -> ANSIStrings<'a> { - ANSIGenericStrings(arg) +pub fn AnsiStrings<'a>(arg: &'a [AnsiString<'a>]) -> AnsiStrings<'a> { + AnsiGenericStrings(arg) } -/// A set of `ANSIByteString`s collected together, in order to be +/// A set of `AnsiByteString`s collected together, in order to be /// written with a minimum of control characters. -pub type ANSIByteStrings<'a> = ANSIGenericStrings<'a, [u8]>; +pub type AnsiByteStrings<'a> = AnsiGenericStrings<'a, [u8]>; -/// A function to construct an `ANSIByteStrings` instance. +/// A function to construct an `AnsiByteStrings` instance. #[allow(non_snake_case)] -pub fn ANSIByteStrings<'a>(arg: &'a [ANSIByteString<'a>]) -> ANSIByteStrings<'a> { - ANSIGenericStrings(arg) +pub fn AnsiByteStrings<'a>(arg: &'a [AnsiByteString<'a>]) -> AnsiByteStrings<'a> { + AnsiGenericStrings(arg) } - // ---- paint functions ---- impl Style { - - /// Paints the given text with this colour, returning an ANSI string. + /// Paints the given text with this color, returning an ANSI string. #[must_use] - pub fn paint<'a, I, S: 'a + ToOwned + ?Sized>(self, input: I) -> ANSIGenericString<'a, S> - where I: Into<Cow<'a, S>>, - <S as ToOwned>::Owned: fmt::Debug { - ANSIGenericString { + pub fn paint<'a, I, S: 'a + ToOwned + ?Sized>(self, input: I) -> AnsiGenericString<'a, S> + where + I: Into<Cow<'a, S>>, + <S as ToOwned>::Owned: fmt::Debug, + { + AnsiGenericString { string: input.into(), - style: self, + style: self, } } } - -impl Colour { - - /// Paints the given text with this colour, returning an ANSI string. +impl Color { + /// Paints the given text with this color, returning an ANSI string. /// This is a short-cut so you don’t have to use `Blue.normal()` just /// to get blue text. /// /// ``` - /// use ansi_term::Colour::Blue; + /// use nu_ansi_term::Color::Blue; /// println!("{}", Blue.paint("da ba dee")); /// ``` #[must_use] - pub fn paint<'a, I, S: 'a + ToOwned + ?Sized>(self, input: I) -> ANSIGenericString<'a, S> - where I: Into<Cow<'a, S>>, - <S as ToOwned>::Owned: fmt::Debug { - ANSIGenericString { + pub fn paint<'a, I, S: 'a + ToOwned + ?Sized>(self, input: I) -> AnsiGenericString<'a, S> + where + I: Into<Cow<'a, S>>, + <S as ToOwned>::Owned: fmt::Debug, + { + AnsiGenericString { string: input.into(), - style: self.normal(), + style: self.normal(), } } } - // ---- writers for individual ANSI strings ---- -impl<'a> fmt::Display for ANSIString<'a> { +impl<'a> fmt::Display for AnsiString<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let w: &mut fmt::Write = f; + let w: &mut dyn fmt::Write = f; self.write_to_any(w) } } -impl<'a> ANSIByteString<'a> { - /// Write an `ANSIByteString` to an `io::Write`. This writes the escape +impl<'a> AnsiByteString<'a> { + /// Write an `AnsiByteString` to an `io::Write`. This writes the escape /// sequences for the associated `Style` around the bytes. pub fn write_to<W: io::Write>(&self, w: &mut W) -> io::Result<()> { - let w: &mut io::Write = w; + let w: &mut dyn io::Write = w; self.write_to_any(w) } } -impl<'a, S: 'a + ToOwned + ?Sized> ANSIGenericString<'a, S> -where <S as ToOwned>::Owned: fmt::Debug, &'a S: AsRef<[u8]> { - fn write_to_any<W: AnyWrite<wstr=S> + ?Sized>(&self, w: &mut W) -> Result<(), W::Error> { +impl<'a, S: 'a + ToOwned + ?Sized> AnsiGenericString<'a, S> +where + <S as ToOwned>::Owned: fmt::Debug, + &'a S: AsRef<[u8]>, +{ + fn write_to_any<W: AnyWrite<Wstr = S> + ?Sized>(&self, w: &mut W) -> Result<(), W::Error> { write!(w, "{}", self.style.prefix())?; w.write_str(self.string.as_ref())?; write!(w, "{}", self.style.suffix()) } } - // ---- writers for combined ANSI strings ---- -impl<'a> fmt::Display for ANSIStrings<'a> { +impl<'a> fmt::Display for AnsiStrings<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let f: &mut fmt::Write = f; + let f: &mut dyn fmt::Write = f; self.write_to_any(f) } } -impl<'a> ANSIByteStrings<'a> { - /// Write `ANSIByteStrings` to an `io::Write`. This writes the minimal +impl<'a> AnsiByteStrings<'a> { + /// Write `AnsiByteStrings` to an `io::Write`. This writes the minimal /// escape sequences for the associated `Style`s around each set of /// bytes. pub fn write_to<W: io::Write>(&self, w: &mut W) -> io::Result<()> { - let w: &mut io::Write = w; + let w: &mut dyn io::Write = w; self.write_to_any(w) } } -impl<'a, S: 'a + ToOwned + ?Sized + PartialEq> ANSIGenericStrings<'a, S> -where <S as ToOwned>::Owned: fmt::Debug, &'a S: AsRef<[u8]> { - fn write_to_any<W: AnyWrite<wstr=S> + ?Sized>(&self, w: &mut W) -> Result<(), W::Error> { +impl<'a, S: 'a + ToOwned + ?Sized + PartialEq> AnsiGenericStrings<'a, S> +where + <S as ToOwned>::Owned: fmt::Debug, + &'a S: AsRef<[u8]>, +{ + fn write_to_any<W: AnyWrite<Wstr = S> + ?Sized>(&self, w: &mut W) -> Result<(), W::Error> { use self::Difference::*; let first = match self.0.first() { @@ -257,14 +252,14 @@ where <S as ToOwned>::Owned: fmt::Debug, &'a S: AsRef<[u8]> { for window in self.0.windows(2) { match Difference::between(&window[0].style, &window[1].style) { ExtraStyles(style) => write!(w, "{}", style.prefix())?, - Reset => write!(w, "{}{}", RESET, window[1].style.prefix())?, - NoDifference => {/* Do nothing! */}, + Reset => write!(w, "{}{}", RESET, window[1].style.prefix())?, + Empty => { /* Do nothing! */ } } w.write_str(&window[1].string)?; } - // Write the final reset string after all of the ANSIStrings have been + // Write the final reset string after all of the AnsiStrings have been // written, *except* if the last one has no styles, because it would // have already been written by this point. if let Some(last) = self.0.last() { @@ -277,20 +272,19 @@ where <S as ToOwned>::Owned: fmt::Debug, &'a S: AsRef<[u8]> { } } - // ---- tests ---- #[cfg(test)] mod tests { - pub use super::super::ANSIStrings; - pub use style::Style; - pub use style::Colour::*; + pub use super::super::AnsiStrings; + pub use crate::style::Color::*; + pub use crate::style::Style; #[test] fn no_control_codes_for_plain() { let one = Style::default().paint("one"); let two = Style::default().paint("two"); - let output = format!("{}", ANSIStrings( &[ one, two ] )); - assert_eq!(&*output, "onetwo"); + let output = AnsiStrings(&[one, two]).to_string(); + assert_eq!(output, "onetwo"); } } diff --git a/vendor/nu-ansi-term/src/gradient.rs b/vendor/nu-ansi-term/src/gradient.rs new file mode 100644 index 000000000..a0d94c8cd --- /dev/null +++ b/vendor/nu-ansi-term/src/gradient.rs @@ -0,0 +1,105 @@ +use crate::{rgb::Rgb, Color}; + +/// Linear color gradient between two color stops +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Gradient { + /// Start Color of Gradient + pub start: Rgb, + + /// End Color of Gradient + pub end: Rgb, +} + +impl Gradient { + /// Creates a new [Gradient] with two [Rgb] colors, `start` and `end` + #[inline] + pub const fn new(start: Rgb, end: Rgb) -> Self { + Self { start, end } + } + pub const fn from_color_rgb(start: Color, end: Color) -> Self { + let start_grad = match start { + Color::Rgb(r, g, b) => Rgb { r, g, b }, + _ => Rgb { r: 0, g: 0, b: 0 }, + }; + let end_grad = match end { + Color::Rgb(r, g, b) => Rgb { r, g, b }, + _ => Rgb { r: 0, g: 0, b: 0 }, + }; + + Self { + start: start_grad, + end: end_grad, + } + } + + /// Computes the [Rgb] color between `start` and `end` for `t` + pub fn at(&self, t: f32) -> Rgb { + self.start.lerp(self.end, t) + } + + /// Returns the reverse of `self` + #[inline] + pub const fn reverse(&self) -> Self { + Self::new(self.end, self.start) + } + + #[allow(dead_code)] + pub fn build(&self, text: &str, target: TargetGround) -> String { + let delta = 1.0 / text.len() as f32; + let mut result = text.char_indices().fold(String::new(), |mut acc, (i, c)| { + let temp = format!( + "\x1B[{}m{}", + self.at(i as f32 * delta).ansi_color_code(target), + c + ); + acc.push_str(&temp); + acc + }); + + result.push_str("\x1B[0m"); + result + } +} + +#[allow(dead_code)] +pub fn build_all_gradient_text(text: &str, foreground: Gradient, background: Gradient) -> String { + let delta = 1.0 / text.len() as f32; + let mut result = text.char_indices().fold(String::new(), |mut acc, (i, c)| { + let step = i as f32 * delta; + let temp = format!( + "\x1B[{};{}m{}", + foreground + .at(step) + .ansi_color_code(TargetGround::Foreground), + background + .at(step) + .ansi_color_code(TargetGround::Background), + c + ); + acc.push_str(&temp); + acc + }); + + result.push_str("\x1B[0m"); + result +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum TargetGround { + Foreground, + Background, +} + +impl TargetGround { + #[inline] + pub const fn code(&self) -> u8 { + match self { + Self::Foreground => 30, + Self::Background => 40, + } + } +} + +pub trait ANSIColorCode { + fn ansi_color_code(&self, target: TargetGround) -> String; +} diff --git a/vendor/ansi_term/src/lib.rs b/vendor/nu-ansi-term/src/lib.rs index 2d2f83ae6..c04fd3276 100644 --- a/vendor/ansi_term/src/lib.rs +++ b/vendor/nu-ansi-term/src/lib.rs @@ -1,40 +1,40 @@ -//! This is a library for controlling colours and formatting, such as +//! This is a library for controlling colors and formatting, such as //! red bold text or blue underlined text, on ANSI terminals. //! //! //! ## Basic usage //! //! There are three main types in this crate that you need to be -//! concerned with: [`ANSIString`], [`Style`], and [`Colour`]. +//! concerned with: [`AnsiString`], [`Style`], and [`Color`]. //! -//! A `Style` holds stylistic information: foreground and background colours, +//! A `Style` holds stylistic information: foreground and background colors, //! whether the text should be bold, or blinking, or other properties. The -//! [`Colour`] enum represents the available colours. And an [`ANSIString`] is a +//! [`Color`] enum represents the available colors. And an [`AnsiString`] is a //! string paired with a [`Style`]. //! -//! [`Color`] is also available as an alias to `Colour`. +//! [`Color`] is also available as an alias to `Color`. //! -//! To format a string, call the `paint` method on a `Style` or a `Colour`, +//! To format a string, call the `paint` method on a `Style` or a `Color`, //! passing in the string you want to format as the argument. For example, //! here’s how to get some red text: //! //! ``` -//! use ansi_term::Colour::Red; +//! use nu_ansi_term::Color::Red; //! //! println!("This is in red: {}", Red.paint("a red string")); //! ``` //! //! It’s important to note that the `paint` method does *not* actually return a //! string with the ANSI control characters surrounding it. Instead, it returns -//! an [`ANSIString`] value that has a [`Display`] implementation that, when +//! an [`AnsiString`] value that has a [`Display`] implementation that, when //! formatted, returns the characters. This allows strings to be printed with a //! minimum of [`String`] allocations being performed behind the scenes. //! //! If you *do* want to get at the escape codes, then you can convert the -//! [`ANSIString`] to a string as you would any other `Display` value: +//! [`AnsiString`] to a string as you would any other `Display` value: //! //! ``` -//! use ansi_term::Colour::Red; +//! use nu_ansi_term::Color::Red; //! //! let red_string = Red.paint("a red string").to_string(); //! ``` @@ -42,26 +42,26 @@ //! //! ## Bold, underline, background, and other styles //! -//! For anything more complex than plain foreground colour changes, you need to -//! construct `Style` values themselves, rather than beginning with a `Colour`. +//! For anything more complex than plain foreground color changes, you need to +//! construct `Style` values themselves, rather than beginning with a `Color`. //! You can do this by chaining methods based on a new `Style`, created with //! [`Style::new()`]. Each method creates a new style that has that specific //! property set. For example: //! //! ``` -//! use ansi_term::Style; +//! use nu_ansi_term::Style; //! //! println!("How about some {} and {}?", //! Style::new().bold().paint("bold"), //! Style::new().underline().paint("underline")); //! ``` //! -//! For brevity, these methods have also been implemented for `Colour` values, -//! so you can give your styles a foreground colour without having to begin with +//! For brevity, these methods have also been implemented for `Color` values, +//! so you can give your styles a foreground color without having to begin with //! an empty `Style` value: //! //! ``` -//! use ansi_term::Colour::{Blue, Yellow}; +//! use nu_ansi_term::Color::{Blue, Yellow}; //! //! println!("Demonstrating {} and {}!", //! Blue.bold().paint("blue bold"), @@ -72,68 +72,68 @@ //! //! The complete list of styles you can use are: [`bold`], [`dimmed`], [`italic`], //! [`underline`], [`blink`], [`reverse`], [`hidden`], [`strikethrough`], and [`on`] for -//! background colours. +//! background colors. //! //! In some cases, you may find it easier to change the foreground on an -//! existing `Style` rather than starting from the appropriate `Colour`. +//! existing `Style` rather than starting from the appropriate `Color`. //! You can do this using the [`fg`] method: //! //! ``` -//! use ansi_term::Style; -//! use ansi_term::Colour::{Blue, Cyan, Yellow}; +//! use nu_ansi_term::Style; +//! use nu_ansi_term::Color::{Blue, Cyan, Yellow}; //! //! println!("Yellow on blue: {}", Style::new().on(Blue).fg(Yellow).paint("yow!")); //! println!("Also yellow on blue: {}", Cyan.on(Blue).fg(Yellow).paint("zow!")); //! ``` //! -//! You can turn a `Colour` into a `Style` with the [`normal`] method. -//! This will produce the exact same `ANSIString` as if you just used the -//! `paint` method on the `Colour` directly, but it’s useful in certain cases: +//! You can turn a `Color` into a `Style` with the [`normal`] method. +//! This will produce the exact same `AnsiString` as if you just used the +//! `paint` method on the `Color` directly, but it’s useful in certain cases: //! for example, you may have a method that returns `Styles`, and need to //! represent both the “red bold” and “red, but not bold” styles with values of //! the same type. The `Style` struct also has a [`Default`] implementation if you //! want to have a style with *nothing* set. //! //! ``` -//! use ansi_term::Style; -//! use ansi_term::Colour::Red; +//! use nu_ansi_term::Style; +//! use nu_ansi_term::Color::Red; //! //! Red.normal().paint("yet another red string"); //! Style::default().paint("a completely regular string"); //! ``` //! //! -//! ## Extended colours +//! ## Extended colors //! -//! You can access the extended range of 256 colours by using the `Colour::Fixed` -//! variant, which takes an argument of the colour number to use. This can be -//! included wherever you would use a `Colour`: +//! You can access the extended range of 256 colors by using the `Color::Fixed` +//! variant, which takes an argument of the color number to use. This can be +//! included wherever you would use a `Color`: //! //! ``` -//! use ansi_term::Colour::Fixed; +//! use nu_ansi_term::Color::Fixed; //! //! Fixed(134).paint("A sort of light purple"); //! Fixed(221).on(Fixed(124)).paint("Mustard in the ketchup"); //! ``` //! //! The first sixteen of these values are the same as the normal and bold -//! standard colour variants. There’s nothing stopping you from using these as -//! `Fixed` colours instead, but there’s nothing to be gained by doing so +//! standard color variants. There’s nothing stopping you from using these as +//! `Fixed` colors instead, but there’s nothing to be gained by doing so //! either. //! -//! You can also access full 24-bit colour by using the `Colour::RGB` variant, +//! You can also access full 24-bit color by using the `Color::Rgb` variant, //! which takes separate `u8` arguments for red, green, and blue: //! //! ``` -//! use ansi_term::Colour::RGB; +//! use nu_ansi_term::Color::Rgb; //! -//! RGB(70, 130, 180).paint("Steel blue"); +//! Rgb(70, 130, 180).paint("Steel blue"); //! ``` //! -//! ## Combining successive coloured strings +//! ## Combining successive colored strings //! //! The benefit of writing ANSI escape codes to the terminal is that they -//! *stack*: you do not need to end every coloured string with a reset code if +//! *stack*: you do not need to end every colored string with a reset code if //! the text that follows it is of a similar style. For example, if you want to //! have some blue text followed by some blue bold text, it’s possible to send //! the ANSI code for blue, followed by the ANSI code for bold, and finishing @@ -141,8 +141,8 @@ //! strings. //! //! This crate can optimise the ANSI codes that get printed in situations like -//! this, making life easier for your terminal renderer. The [`ANSIStrings`] -//! type takes a slice of several [`ANSIString`] values, and will iterate over +//! this, making life easier for your terminal renderer. The [`AnsiStrings`] +//! type takes a slice of several [`AnsiString`] values, and will iterate over //! each of them, printing only the codes for the styles that need to be updated //! as part of its formatting routine. //! @@ -150,25 +150,25 @@ //! red bold text inside some red, but not bold, brackets: //! //! ``` -//! use ansi_term::Colour::Red; -//! use ansi_term::{ANSIString, ANSIStrings}; +//! use nu_ansi_term::Color::Red; +//! use nu_ansi_term::{AnsiString, AnsiStrings}; //! //! let some_value = format!("{:b}", 42); -//! let strings: &[ANSIString<'static>] = &[ +//! let strings: &[AnsiString<'static>] = &[ //! Red.paint("["), //! Red.bold().paint(some_value), //! Red.paint("]"), //! ]; //! -//! println!("Value: {}", ANSIStrings(strings)); +//! println!("Value: {}", AnsiStrings(strings)); //! ``` //! //! There are several things to note here. Firstly, the [`paint`] method can take -//! *either* an owned [`String`] or a borrowed [`&str`]. Internally, an [`ANSIString`] +//! *either* an owned [`String`] or a borrowed [`&str`]. Internally, an [`AnsiString`] //! holds a copy-on-write ([`Cow`]) string value to deal with both owned and //! borrowed strings at the same time. This is used here to display a `String`, //! the result of the `format!` call, using the same mechanism as some -//! statically-available `&str` slices. Secondly, that the [`ANSIStrings`] value +//! statically-available `&str` slices. Secondly, that the [`AnsiStrings`] value //! works in the same way as its singular counterpart, with a [`Display`] //! implementation that only performs the formatting when required. //! @@ -176,25 +176,25 @@ //! //! This library also supports formatting `\[u8]` byte strings; this supports //! applications working with text in an unknown encoding. [`Style`] and -//! [`Colour`] support painting `\[u8]` values, resulting in an [`ANSIByteString`]. +//! [`Color`] support painting `\[u8]` values, resulting in an [`AnsiByteString`]. //! This type does not implement [`Display`], as it may not contain UTF-8, but //! it does provide a method [`write_to`] to write the result to any value that //! implements [`Write`]: //! //! ``` -//! use ansi_term::Colour::Green; +//! use nu_ansi_term::Color::Green; //! //! Green.paint("user data".as_bytes()).write_to(&mut std::io::stdout()).unwrap(); //! ``` //! -//! Similarly, the type [`ANSIByteStrings`] supports writing a list of -//! [`ANSIByteString`] values with minimal escape sequences: +//! Similarly, the type [`AnsiByteStrings`] supports writing a list of +//! [`AnsiByteString`] values with minimal escape sequences: //! //! ``` -//! use ansi_term::Colour::Green; -//! use ansi_term::ANSIByteStrings; +//! use nu_ansi_term::Color::Green; +//! use nu_ansi_term::AnsiByteStrings; //! -//! ANSIByteStrings(&[ +//! AnsiByteStrings(&[ //! Green.paint("user data 1\n".as_bytes()), //! Green.bold().paint("user data 2\n".as_bytes()), //! ]).write_to(&mut std::io::stdout()).unwrap(); @@ -209,14 +209,14 @@ //! [`Style`]: struct.Style.html //! [`Style::new()`]: struct.Style.html#method.new //! [`Color`]: enum.Color.html -//! [`Colour`]: enum.Colour.html -//! [`ANSIString`]: type.ANSIString.html -//! [`ANSIStrings`]: type.ANSIStrings.html -//! [`ANSIByteString`]: type.ANSIByteString.html -//! [`ANSIByteStrings`]: type.ANSIByteStrings.html -//! [`write_to`]: type.ANSIByteString.html#method.write_to -//! [`paint`]: type.ANSIByteString.html#method.write_to -//! [`normal`]: enum.Colour.html#method.normal +//! [`Color`]: enum.Color.html +//! [`AnsiString`]: type.AnsiString.html +//! [`AnsiStrings`]: type.AnsiStrings.html +//! [`AnsiByteString`]: type.AnsiByteString.html +//! [`AnsiByteStrings`]: type.AnsiByteStrings.html +//! [`write_to`]: type.AnsiByteString.html#method.write_to +//! [`paint`]: type.AnsiByteString.html#method.write_to +//! [`normal`]: enum.Color.html#method.normal //! //! [`bold`]: struct.Style.html#method.bold //! [`dimmed`]: struct.Style.html#method.dimmed @@ -229,16 +229,14 @@ //! [`fg`]: struct.Style.html#method.fg //! [`on`]: struct.Style.html#method.on -#![crate_name = "ansi_term"] +#![crate_name = "nu_ansi_term"] #![crate_type = "rlib"] -#![crate_type = "dylib"] - #![warn(missing_copy_implementations)] -#![warn(missing_docs)] +// #![warn(missing_docs)] #![warn(trivial_casts, trivial_numeric_casts)] -#![warn(unused_extern_crates, unused_qualifications)] +// #![warn(unused_extern_crates, unused_qualifications)] -#[cfg(target_os="windows")] +#[cfg(target_os = "windows")] extern crate winapi; #[cfg(test)] #[macro_use] @@ -247,14 +245,11 @@ extern crate doc_comment; #[cfg(test)] doctest!("../README.md"); -mod ansi; -pub use ansi::{Prefix, Infix, Suffix}; +pub mod ansi; +pub use ansi::{Infix, Prefix, Suffix}; mod style; -pub use style::{Colour, Style}; - -/// Color is a type alias for `Colour`. -pub use Colour as Color; +pub use style::{Color, Style}; mod difference; mod display; @@ -269,3 +264,9 @@ mod util; pub use util::*; mod debug; + +pub mod gradient; +pub use gradient::*; + +mod rgb; +pub use rgb::*; diff --git a/vendor/nu-ansi-term/src/rgb.rs b/vendor/nu-ansi-term/src/rgb.rs new file mode 100644 index 000000000..19475c36b --- /dev/null +++ b/vendor/nu-ansi-term/src/rgb.rs @@ -0,0 +1,173 @@ +// Code liberally borrowed from here +// https://github.com/navierr/coloriz +use std::ops; +use std::u32; +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Rgb { + /// Red + pub r: u8, + /// Green + pub g: u8, + /// Blue + pub b: u8, +} + +impl Rgb { + /// Creates a new [Rgb] color + #[inline] + pub const fn new(r: u8, g: u8, b: u8) -> Self { + Self { r, g, b } + } + + /// Creates a new [Rgb] color with a hex code + #[inline] + pub const fn from_hex(hex: u32) -> Self { + Self::new((hex >> 16) as u8, (hex >> 8) as u8, hex as u8) + } + + pub fn from_hex_string(hex: String) -> Self { + if hex.chars().count() == 8 && hex.starts_with("0x") { + // eprintln!("hex:{:?}", hex); + let (_, value_string) = hex.split_at(2); + // eprintln!("value_string:{:?}", value_string); + let int_val = u64::from_str_radix(value_string, 16); + match int_val { + Ok(num) => Self::new( + ((num & 0xff0000) >> 16) as u8, + ((num & 0xff00) >> 8) as u8, + (num & 0xff) as u8, + ), + // Don't fail, just make the color black + // Should we fail? + _ => Self::new(0, 0, 0), + } + } else { + // Don't fail, just make the color black. + // Should we fail? + Self::new(0, 0, 0) + } + } + + /// Creates a new [Rgb] color with three [f32] values + pub fn from_f32(r: f32, g: f32, b: f32) -> Self { + Self::new( + (r.clamp(0.0, 1.0) * 255.0) as u8, + (g.clamp(0.0, 1.0) * 255.0) as u8, + (b.clamp(0.0, 1.0) * 255.0) as u8, + ) + } + + /// Creates a grayscale [Rgb] color + #[inline] + pub const fn gray(x: u8) -> Self { + Self::new(x, x, x) + } + + /// Creates a grayscale [Rgb] color with a [f32] value + pub fn gray_f32(x: f32) -> Self { + Self::from_f32(x, x, x) + } + + /// Creates a new [Rgb] color from a [HSL] color + // pub fn from_hsl(hsl: HSL) -> Self { + // if hsl.s == 0.0 { + // return Self::gray_f32(hsl.l); + // } + + // let q = if hsl.l < 0.5 { + // hsl.l * (1.0 + hsl.s) + // } else { + // hsl.l + hsl.s - hsl.l * hsl.s + // }; + // let p = 2.0 * hsl.l - q; + // let h2c = |t: f32| { + // let t = t.clamp(0.0, 1.0); + // if 6.0 * t < 1.0 { + // p + 6.0 * (q - p) * t + // } else if t < 0.5 { + // q + // } else if 1.0 < 1.5 * t { + // p + 6.0 * (q - p) * (1.0 / 1.5 - t) + // } else { + // p + // } + // }; + + // Self::from_f32(h2c(hsl.h + 1.0 / 3.0), h2c(hsl.h), h2c(hsl.h - 1.0 / 3.0)) + // } + + /// Computes the linear interpolation between `self` and `other` for `t` + pub fn lerp(&self, other: Self, t: f32) -> Self { + let t = t.clamp(0.0, 1.0); + self * (1.0 - t) + other * t + } +} + +impl From<(u8, u8, u8)> for Rgb { + fn from((r, g, b): (u8, u8, u8)) -> Self { + Self::new(r, g, b) + } +} + +impl From<(f32, f32, f32)> for Rgb { + fn from((r, g, b): (f32, f32, f32)) -> Self { + Self::from_f32(r, g, b) + } +} + +use crate::ANSIColorCode; +use crate::TargetGround; +impl ANSIColorCode for Rgb { + fn ansi_color_code(&self, target: TargetGround) -> String { + format!("{};2;{};{};{}", target.code() + 8, self.r, self.g, self.b) + } +} + +overload::overload!( + (lhs: ?Rgb) + (rhs: ?Rgb) -> Rgb { + Rgb::new( + lhs.r.saturating_add(rhs.r), + lhs.g.saturating_add(rhs.g), + lhs.b.saturating_add(rhs.b) + ) + } +); + +overload::overload!( + (lhs: ?Rgb) - (rhs: ?Rgb) -> Rgb { + Rgb::new( + lhs.r.saturating_sub(rhs.r), + lhs.g.saturating_sub(rhs.g), + lhs.b.saturating_sub(rhs.b) + ) + } +); + +overload::overload!( + (lhs: ?Rgb) * (rhs: ?f32) -> Rgb { + Rgb::new( + (lhs.r as f32 * rhs.clamp(0.0, 1.0)) as u8, + (lhs.g as f32 * rhs.clamp(0.0, 1.0)) as u8, + (lhs.b as f32 * rhs.clamp(0.0, 1.0)) as u8 + ) + } +); + +overload::overload!( + (lhs: ?f32) * (rhs: ?Rgb) -> Rgb { + Rgb::new( + (rhs.r as f32 * lhs.clamp(0.0, 1.0)) as u8, + (rhs.g as f32 * lhs.clamp(0.0, 1.0)) as u8, + (rhs.b as f32 * lhs.clamp(0.0, 1.0)) as u8 + ) + } +); + +overload::overload!( + -(rgb: ?Rgb) -> Rgb { + Rgb::new( + 255 - rgb.r, + 255 - rgb.g, + 255 - rgb.b) + } +); diff --git a/vendor/nu-ansi-term/src/style.rs b/vendor/nu-ansi-term/src/style.rs new file mode 100644 index 000000000..3d47a79f7 --- /dev/null +++ b/vendor/nu-ansi-term/src/style.rs @@ -0,0 +1,629 @@ +/// A style is a collection of properties that can format a string +/// using ANSI escape codes. +/// +/// # Examples +/// +/// ``` +/// use nu_ansi_term::{Style, Color}; +/// +/// let style = Style::new().bold().on(Color::Black); +/// println!("{}", style.paint("Bold on black")); +/// ``` +#[derive(PartialEq, Clone, Copy)] +#[cfg_attr( + feature = "derive_serde_style", + derive(serde::Deserialize, serde::Serialize) +)] +pub struct Style { + /// The style's foreground color, if it has one. + pub foreground: Option<Color>, + + /// The style's background color, if it has one. + pub background: Option<Color>, + + /// Whether this style is bold. + pub is_bold: bool, + + /// Whether this style is dimmed. + pub is_dimmed: bool, + + /// Whether this style is italic. + pub is_italic: bool, + + /// Whether this style is underlined. + pub is_underline: bool, + + /// Whether this style is blinking. + pub is_blink: bool, + + /// Whether this style has reverse colors. + pub is_reverse: bool, + + /// Whether this style is hidden. + pub is_hidden: bool, + + /// Whether this style is struckthrough. + pub is_strikethrough: bool, +} + +impl Style { + /// Creates a new Style with no properties set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Style; + /// + /// let style = Style::new(); + /// println!("{}", style.paint("hi")); + /// ``` + pub fn new() -> Style { + Style::default() + } + + /// Returns a `Style` with the bold property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Style; + /// + /// let style = Style::new().bold(); + /// println!("{}", style.paint("hey")); + /// ``` + pub fn bold(&self) -> Style { + Style { + is_bold: true, + ..*self + } + } + + /// Returns a `Style` with the dimmed property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Style; + /// + /// let style = Style::new().dimmed(); + /// println!("{}", style.paint("sup")); + /// ``` + pub fn dimmed(&self) -> Style { + Style { + is_dimmed: true, + ..*self + } + } + + /// Returns a `Style` with the italic property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Style; + /// + /// let style = Style::new().italic(); + /// println!("{}", style.paint("greetings")); + /// ``` + pub fn italic(&self) -> Style { + Style { + is_italic: true, + ..*self + } + } + + /// Returns a `Style` with the underline property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Style; + /// + /// let style = Style::new().underline(); + /// println!("{}", style.paint("salutations")); + /// ``` + pub fn underline(&self) -> Style { + Style { + is_underline: true, + ..*self + } + } + + /// Returns a `Style` with the blink property set. + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Style; + /// + /// let style = Style::new().blink(); + /// println!("{}", style.paint("wazzup")); + /// ``` + pub fn blink(&self) -> Style { + Style { + is_blink: true, + ..*self + } + } + + /// Returns a `Style` with the reverse property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Style; + /// + /// let style = Style::new().reverse(); + /// println!("{}", style.paint("aloha")); + /// ``` + pub fn reverse(&self) -> Style { + Style { + is_reverse: true, + ..*self + } + } + + /// Returns a `Style` with the hidden property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Style; + /// + /// let style = Style::new().hidden(); + /// println!("{}", style.paint("ahoy")); + /// ``` + pub fn hidden(&self) -> Style { + Style { + is_hidden: true, + ..*self + } + } + + /// Returns a `Style` with the strikethrough property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Style; + /// + /// let style = Style::new().strikethrough(); + /// println!("{}", style.paint("yo")); + /// ``` + pub fn strikethrough(&self) -> Style { + Style { + is_strikethrough: true, + ..*self + } + } + + /// Returns a `Style` with the foreground color property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::{Style, Color}; + /// + /// let style = Style::new().fg(Color::Yellow); + /// println!("{}", style.paint("hi")); + /// ``` + pub fn fg(&self, foreground: Color) -> Style { + Style { + foreground: Some(foreground), + ..*self + } + } + + /// Returns a `Style` with the background color property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::{Style, Color}; + /// + /// let style = Style::new().on(Color::Blue); + /// println!("{}", style.paint("eyyyy")); + /// ``` + pub fn on(&self, background: Color) -> Style { + Style { + background: Some(background), + ..*self + } + } + + /// Return true if this `Style` has no actual styles, and can be written + /// without any control characters. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Style; + /// + /// assert_eq!(true, Style::default().is_plain()); + /// assert_eq!(false, Style::default().bold().is_plain()); + /// ``` + pub fn is_plain(self) -> bool { + self == Style::default() + } +} + +impl Default for Style { + /// Returns a style with *no* properties set. Formatting text using this + /// style returns the exact same text. + /// + /// ``` + /// use nu_ansi_term::Style; + /// assert_eq!(None, Style::default().foreground); + /// assert_eq!(None, Style::default().background); + /// assert_eq!(false, Style::default().is_bold); + /// assert_eq!("txt", Style::default().paint("txt").to_string()); + /// ``` + fn default() -> Style { + Style { + foreground: None, + background: None, + is_bold: false, + is_dimmed: false, + is_italic: false, + is_underline: false, + is_blink: false, + is_reverse: false, + is_hidden: false, + is_strikethrough: false, + } + } +} + +// ---- colors ---- + +/// A color is one specific type of ANSI escape code, and can refer +/// to either the foreground or background color. +/// +/// These use the standard numeric sequences. +/// See <http://invisible-island.net/xterm/ctlseqs/ctlseqs.html> +#[derive(PartialEq, Clone, Copy, Debug)] +#[cfg_attr( + feature = "derive_serde_style", + derive(serde::Deserialize, serde::Serialize) +)] +pub enum Color { + /// Color #0 (foreground code `30`, background code `40`). + /// + /// This is not necessarily the background color, and using it as one may + /// render the text hard to read on terminals with dark backgrounds. + Black, + + /// Color #0 (foreground code `90`, background code `100`). + DarkGray, + + /// Color #1 (foreground code `31`, background code `41`). + Red, + + /// Color #1 (foreground code `91`, background code `101`). + LightRed, + + /// Color #2 (foreground code `32`, background code `42`). + Green, + + /// Color #2 (foreground code `92`, background code `102`). + LightGreen, + + /// Color #3 (foreground code `33`, background code `43`). + Yellow, + + /// Color #3 (foreground code `93`, background code `103`). + LightYellow, + + /// Color #4 (foreground code `34`, background code `44`). + Blue, + + /// Color #4 (foreground code `94`, background code `104`). + LightBlue, + + /// Color #5 (foreground code `35`, background code `45`). + Purple, + + /// Color #5 (foreground code `95`, background code `105`). + LightPurple, + + /// Color #5 (foreground code `35`, background code `45`). + Magenta, + + /// Color #5 (foreground code `95`, background code `105`). + LightMagenta, + + /// Color #6 (foreground code `36`, background code `46`). + Cyan, + + /// Color #6 (foreground code `96`, background code `106`). + LightCyan, + + /// Color #7 (foreground code `37`, background code `47`). + /// + /// As above, this is not necessarily the foreground color, and may be + /// hard to read on terminals with light backgrounds. + White, + + /// Color #7 (foreground code `97`, background code `107`). + LightGray, + + /// A color number from 0 to 255, for use in 256-color terminal + /// environments. + /// + /// - colors 0 to 7 are the `Black` to `White` variants respectively. + /// These colors can usually be changed in the terminal emulator. + /// - colors 8 to 15 are brighter versions of the eight colors above. + /// These can also usually be changed in the terminal emulator, or it + /// could be configured to use the original colors and show the text in + /// bold instead. It varies depending on the program. + /// - colors 16 to 231 contain several palettes of bright colors, + /// arranged in six squares measuring six by six each. + /// - colors 232 to 255 are shades of grey from black to white. + /// + /// It might make more sense to look at a [color chart][cc]. + /// + /// [cc]: https://upload.wikimedia.org/wikipedia/commons/1/15/Xterm_256color_chart.svg + Fixed(u8), + + /// A 24-bit Rgb color, as specified by ISO-8613-3. + Rgb(u8, u8, u8), + + /// The default color (foreground code `39`, background codr `49`). + Default, +} + +impl Default for Color { + fn default() -> Self { + Color::White + } +} + +impl Color { + /// Returns a `Style` with the foreground color set to this color. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Color; + /// + /// let style = Color::Red.normal(); + /// println!("{}", style.paint("hi")); + /// ``` + pub fn normal(self) -> Style { + Style { + foreground: Some(self), + ..Style::default() + } + } + + /// Returns a `Style` with the foreground color set to this color and the + /// bold property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Color; + /// + /// let style = Color::Green.bold(); + /// println!("{}", style.paint("hey")); + /// ``` + pub fn bold(self) -> Style { + Style { + foreground: Some(self), + is_bold: true, + ..Style::default() + } + } + + /// Returns a `Style` with the foreground color set to this color and the + /// dimmed property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Color; + /// + /// let style = Color::Yellow.dimmed(); + /// println!("{}", style.paint("sup")); + /// ``` + pub fn dimmed(self) -> Style { + Style { + foreground: Some(self), + is_dimmed: true, + ..Style::default() + } + } + + /// Returns a `Style` with the foreground color set to this color and the + /// italic property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Color; + /// + /// let style = Color::Blue.italic(); + /// println!("{}", style.paint("greetings")); + /// ``` + pub fn italic(self) -> Style { + Style { + foreground: Some(self), + is_italic: true, + ..Style::default() + } + } + + /// Returns a `Style` with the foreground color set to this color and the + /// underline property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Color; + /// + /// let style = Color::Purple.underline(); + /// println!("{}", style.paint("salutations")); + /// ``` + pub fn underline(self) -> Style { + Style { + foreground: Some(self), + is_underline: true, + ..Style::default() + } + } + + /// Returns a `Style` with the foreground color set to this color and the + /// blink property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Color; + /// + /// let style = Color::Cyan.blink(); + /// println!("{}", style.paint("wazzup")); + /// ``` + pub fn blink(self) -> Style { + Style { + foreground: Some(self), + is_blink: true, + ..Style::default() + } + } + + /// Returns a `Style` with the foreground color set to this color and the + /// reverse property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Color; + /// + /// let style = Color::Black.reverse(); + /// println!("{}", style.paint("aloha")); + /// ``` + pub fn reverse(self) -> Style { + Style { + foreground: Some(self), + is_reverse: true, + ..Style::default() + } + } + + /// Returns a `Style` with the foreground color set to this color and the + /// hidden property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Color; + /// + /// let style = Color::White.hidden(); + /// println!("{}", style.paint("ahoy")); + /// ``` + pub fn hidden(self) -> Style { + Style { + foreground: Some(self), + is_hidden: true, + ..Style::default() + } + } + + /// Returns a `Style` with the foreground color set to this color and the + /// strikethrough property set. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Color; + /// + /// let style = Color::Fixed(244).strikethrough(); + /// println!("{}", style.paint("yo")); + /// ``` + pub fn strikethrough(self) -> Style { + Style { + foreground: Some(self), + is_strikethrough: true, + ..Style::default() + } + } + + /// Returns a `Style` with the foreground color set to this color and the + /// background color property set to the given color. + /// + /// # Examples + /// + /// ``` + /// use nu_ansi_term::Color; + /// + /// let style = Color::Rgb(31, 31, 31).on(Color::White); + /// println!("{}", style.paint("eyyyy")); + /// ``` + pub fn on(self, background: Color) -> Style { + Style { + foreground: Some(self), + background: Some(background), + ..Style::default() + } + } +} + +impl From<Color> for Style { + /// You can turn a `Color` into a `Style` with the foreground color set + /// with the `From` trait. + /// + /// ``` + /// use nu_ansi_term::{Style, Color}; + /// let green_foreground = Style::default().fg(Color::Green); + /// assert_eq!(green_foreground, Color::Green.normal()); + /// assert_eq!(green_foreground, Color::Green.into()); + /// assert_eq!(green_foreground, Style::from(Color::Green)); + /// ``` + fn from(color: Color) -> Style { + color.normal() + } +} + +#[cfg(test)] +#[cfg(feature = "derive_serde_style")] +mod serde_json_tests { + use super::{Color, Style}; + + #[test] + fn color_serialization() { + let colors = &[ + Color::Red, + Color::Blue, + Color::Rgb(123, 123, 123), + Color::Fixed(255), + ]; + + assert_eq!( + serde_json::to_string(&colors).unwrap(), + String::from("[\"Red\",\"Blue\",{\"Rgb\":[123,123,123]},{\"Fixed\":255}]") + ); + } + + #[test] + fn color_deserialization() { + let colors = [ + Color::Red, + Color::Blue, + Color::Rgb(123, 123, 123), + Color::Fixed(255), + ]; + + for color in colors { + let serialized = serde_json::to_string(&color).unwrap(); + let deserialized: Color = serde_json::from_str(&serialized).unwrap(); + + assert_eq!(color, deserialized); + } + } + + #[test] + fn style_serialization() { + let style = Style::default(); + + assert_eq!(serde_json::to_string(&style).unwrap(), "{\"foreground\":null,\"background\":null,\"is_bold\":false,\"is_dimmed\":false,\"is_italic\":false,\"is_underline\":false,\"is_blink\":false,\"is_reverse\":false,\"is_hidden\":false,\"is_strikethrough\":false}".to_string()); + } +} diff --git a/vendor/ansi_term/src/util.rs b/vendor/nu-ansi-term/src/util.rs index ba0f12a02..a35020137 100644 --- a/vendor/ansi_term/src/util.rs +++ b/vendor/nu-ansi-term/src/util.rs @@ -1,27 +1,30 @@ -use display::*; +use crate::display::{AnsiString, AnsiStrings}; use std::ops::Deref; -/// Return a substring of the given ANSIStrings sequence, while keeping the formatting. -pub fn sub_string<'a>(start: usize, len: usize, strs: &ANSIStrings<'a>) -> Vec<ANSIString<'static>> { +/// Return a substring of the given AnsiStrings sequence, while keeping the formatting. +pub fn sub_string<'a>( + start: usize, + len: usize, + strs: &AnsiStrings<'a>, +) -> Vec<AnsiString<'static>> { let mut vec = Vec::new(); let mut pos = start; let mut len_rem = len; for i in strs.0.iter() { - let fragment = i.deref(); - let frag_len = fragment.len(); + let frag_len = i.string.len(); if pos >= frag_len { pos -= frag_len; continue; } - if len_rem <= 0 { + if len_rem == 0 { break; } let end = pos + len_rem; let pos_end = if end >= frag_len { frag_len } else { end }; - vec.push(i.style_ref().paint(String::from(&fragment[pos..pos_end]))); + vec.push(i.style_ref().paint(String::from(&i.string[pos..pos_end]))); if end <= frag_len { break; @@ -35,30 +38,29 @@ pub fn sub_string<'a>(start: usize, len: usize, strs: &ANSIStrings<'a>) -> Vec<A } /// Return a concatenated copy of `strs` without the formatting, as an allocated `String`. -pub fn unstyle(strs: &ANSIStrings) -> String { +pub fn unstyle(strs: &AnsiStrings) -> String { let mut s = String::new(); for i in strs.0.iter() { - s += &i.deref(); + s += i.string.deref(); } s } -/// Return the unstyled length of ANSIStrings. This is equaivalent to `unstyle(strs).len()`. -pub fn unstyled_len(strs: &ANSIStrings) -> usize { +/// Return the unstyled length of AnsiStrings. This is equaivalent to `unstyle(strs).len()`. +pub fn unstyled_len(strs: &AnsiStrings) -> usize { let mut l = 0; for i in strs.0.iter() { - l += i.deref().len(); + l += i.string.len(); } l } #[cfg(test)] mod test { - use Colour::*; - use display::*; use super::*; + use crate::Color::*; #[test] fn test() { @@ -67,15 +69,11 @@ mod test { Red.paint("-second"), White.paint("-third"), ]; - let a = ANSIStrings(&l); + let a = AnsiStrings(&l); assert_eq!(unstyle(&a), "first-second-third"); assert_eq!(unstyled_len(&a), 18); - let l2 = [ - Black.paint("st"), - Red.paint("-second"), - White.paint("-t"), - ]; - assert_eq!(sub_string(3, 11, &a).as_slice(), &l2); + let l2 = [Black.paint("st"), Red.paint("-second"), White.paint("-t")]; + assert_eq!(sub_string(3, 11, &a), l2); } } diff --git a/vendor/ansi_term/src/windows.rs b/vendor/nu-ansi-term/src/windows.rs index fcf02ecf6..828e35573 100644 --- a/vendor/ansi_term/src/windows.rs +++ b/vendor/nu-ansi-term/src/windows.rs @@ -25,7 +25,8 @@ pub fn enable_ansi_support() -> Result<(), u32> { unsafe { // ref: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew // Using `CreateFileW("CONOUT$", ...)` to retrieve the console handle works correctly even if STDOUT and/or STDERR are redirected - let console_out_name: Vec<u16> = OsStr::new("CONOUT$").encode_wide().chain(once(0)).collect(); + let console_out_name: Vec<u16> = + OsStr::new("CONOUT$").encode_wide().chain(once(0)).collect(); let console_handle = CreateFileW( console_out_name.as_ptr(), GENERIC_READ | GENERIC_WRITE, @@ -35,27 +36,27 @@ pub fn enable_ansi_support() -> Result<(), u32> { 0, null_mut(), ); - if console_handle == INVALID_HANDLE_VALUE - { + if console_handle == INVALID_HANDLE_VALUE { return Err(GetLastError()); } // ref: https://docs.microsoft.com/en-us/windows/console/getconsolemode let mut console_mode: u32 = 0; - if 0 == GetConsoleMode(console_handle, &mut console_mode) - { + if 0 == GetConsoleMode(console_handle, &mut console_mode) { return Err(GetLastError()); } // VT processing not already enabled? if console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING == 0 { // https://docs.microsoft.com/en-us/windows/console/setconsolemode - if 0 == SetConsoleMode(console_handle, console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING) - { + if 0 == SetConsoleMode( + console_handle, + console_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING, + ) { return Err(GetLastError()); } } } - return Ok(()); + Ok(()) } diff --git a/vendor/ansi_term/src/write.rs b/vendor/nu-ansi-term/src/write.rs index 65a64feb2..552771918 100644 --- a/vendor/ansi_term/src/write.rs +++ b/vendor/nu-ansi-term/src/write.rs @@ -1,40 +1,37 @@ use std::fmt; use std::io; - pub trait AnyWrite { - type wstr: ?Sized; + type Wstr: ?Sized; type Error; fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<(), Self::Error>; - fn write_str(&mut self, s: &Self::wstr) -> Result<(), Self::Error>; + fn write_str(&mut self, s: &Self::Wstr) -> Result<(), Self::Error>; } - -impl<'a> AnyWrite for fmt::Write + 'a { - type wstr = str; +impl<'a> AnyWrite for dyn fmt::Write + 'a { + type Wstr = str; type Error = fmt::Error; fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<(), Self::Error> { fmt::Write::write_fmt(self, fmt) } - fn write_str(&mut self, s: &Self::wstr) -> Result<(), Self::Error> { + fn write_str(&mut self, s: &Self::Wstr) -> Result<(), Self::Error> { fmt::Write::write_str(self, s) } } - -impl<'a> AnyWrite for io::Write + 'a { - type wstr = [u8]; +impl<'a> AnyWrite for dyn io::Write + 'a { + type Wstr = [u8]; type Error = io::Error; fn write_fmt(&mut self, fmt: fmt::Arguments) -> Result<(), Self::Error> { io::Write::write_fmt(self, fmt) } - fn write_str(&mut self, s: &Self::wstr) -> Result<(), Self::Error> { + fn write_str(&mut self, s: &Self::Wstr) -> Result<(), Self::Error> { io::Write::write_all(self, s) } } |