Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository = "https://github.com/BurntSushi/termcolor"
readme = "README.md"
keywords = ["windows", "win", "color", "ansi", "console"]
license = "Unlicense OR MIT"
edition = "2018"
edition = "2021"

[lib]
name = "termcolor"
Expand Down
85 changes: 38 additions & 47 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ except it is augmented with methods for coloring by the `WriteColor` trait.
For example, to write some green text:

```rust,no_run
# fn test() -> Result<(), Box<::std::error::Error>> {
# fn test() -> Result<(), Box<dyn (::std::error::Error)>> {
use std::io::Write;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

Expand All @@ -66,7 +66,7 @@ implements `io::Write` and `io::WriteColor`.
This example shows how to print some green text to stderr.

```rust,no_run
# fn test() -> Result<(), Box<::std::error::Error>> {
# fn test() -> Result<(), Box<dyn (::std::error::Error)>> {
use std::io::Write;
use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};

Expand Down Expand Up @@ -198,45 +198,45 @@ pub trait WriteColor: io::Write {
}
}

impl<'a, T: ?Sized + WriteColor> WriteColor for &'a mut T {
impl<T: ?Sized + WriteColor> WriteColor for &mut T {
fn supports_color(&self) -> bool {
(&**self).supports_color()
(**self).supports_color()
}
fn supports_hyperlinks(&self) -> bool {
(&**self).supports_hyperlinks()
(**self).supports_hyperlinks()
}
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
(&mut **self).set_color(spec)
(**self).set_color(spec)
}
fn set_hyperlink(&mut self, link: &HyperlinkSpec) -> io::Result<()> {
(&mut **self).set_hyperlink(link)
(**self).set_hyperlink(link)
}
fn reset(&mut self) -> io::Result<()> {
(&mut **self).reset()
(**self).reset()
}
fn is_synchronous(&self) -> bool {
(&**self).is_synchronous()
(**self).is_synchronous()
}
}

impl<T: ?Sized + WriteColor> WriteColor for Box<T> {
fn supports_color(&self) -> bool {
(&**self).supports_color()
(**self).supports_color()
}
fn supports_hyperlinks(&self) -> bool {
(&**self).supports_hyperlinks()
(**self).supports_hyperlinks()
}
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
(&mut **self).set_color(spec)
(**self).set_color(spec)
}
fn set_hyperlink(&mut self, link: &HyperlinkSpec) -> io::Result<()> {
(&mut **self).set_hyperlink(link)
(**self).set_hyperlink(link)
}
fn reset(&mut self) -> io::Result<()> {
(&mut **self).reset()
(**self).reset()
}
fn is_synchronous(&self) -> bool {
(&**self).is_synchronous()
(**self).is_synchronous()
}
}

Expand Down Expand Up @@ -381,7 +381,6 @@ impl fmt::Display for ColorChoiceParseError {
/// `std::io` implements `Stdout` and `Stderr` (and their `Lock` variants) as
/// separate types, which makes it difficult to abstract over them. We use
/// some simple internal enum types to work around this.

enum StandardStreamType {
Stdout,
Stderr,
Expand Down Expand Up @@ -1658,7 +1657,6 @@ impl<W: io::Write> Ansi<W> {
Color::White => write_intense!("15"),
Color::Ansi256(c) => write_custom!(c),
Color::Rgb(r, g, b) => write_custom!(r, g, b),
Color::__Nonexhaustive => unreachable!(),
}
} else {
match *c {
Expand All @@ -1672,7 +1670,6 @@ impl<W: io::Write> Ansi<W> {
Color::White => write_normal!("7"),
Color::Ansi256(c) => write_custom!(c),
Color::Rgb(r, g, b) => write_custom!(r, g, b),
Color::__Nonexhaustive => unreachable!(),
}
}
}
Expand Down Expand Up @@ -2068,6 +2065,7 @@ impl ColorSpec {
/// Hexadecimal numbers are written with a `0x` prefix.
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Color {
Black,
Blue,
Expand All @@ -2079,8 +2077,6 @@ pub enum Color {
White,
Ansi256(u8),
Rgb(u8, u8, u8),
#[doc(hidden)]
__Nonexhaustive,
}

impl Color {
Expand Down Expand Up @@ -2119,7 +2115,6 @@ impl Color {
Color::Ansi256(15) => return Some((Yes, wincon::Color::White)),
Color::Ansi256(_) => return None,
Color::Rgb(_, _, _) => return None,
Color::__Nonexhaustive => unreachable!(),
};
let intense = if intense { Yes } else { No };
Some((intense, color))
Expand All @@ -2134,31 +2129,27 @@ impl Color {
// by a comma corresponding to one of 256^3 colors.

fn parse_number(s: &str) -> Option<u8> {
use std::u8;

if s.starts_with("0x") {
u8::from_str_radix(&s[2..], 16).ok()
if let Some(hex) = s.strip_prefix("0x") {
u8::from_str_radix(hex, 16).ok()
} else {
u8::from_str_radix(s, 10).ok()
s.parse::<u8>().ok()
}
}

let codes: Vec<&str> = s.split(',').collect();
if codes.len() == 1 {
if let Some(n) = parse_number(&codes[0]) {
if let Some(n) = parse_number(codes[0]) {
Ok(Color::Ansi256(n))
} else if s.chars().all(|c| c.is_ascii_hexdigit()) {
Err(ParseColorError {
kind: ParseColorErrorKind::InvalidAnsi256,
given: s.to_string(),
})
} else {
if s.chars().all(|c| c.is_digit(16)) {
Err(ParseColorError {
kind: ParseColorErrorKind::InvalidAnsi256,
given: s.to_string(),
})
} else {
Err(ParseColorError {
kind: ParseColorErrorKind::InvalidName,
given: s.to_string(),
})
}
Err(ParseColorError {
kind: ParseColorErrorKind::InvalidName,
given: s.to_string(),
})
}
} else if codes.len() == 3 {
let mut v = vec![];
Expand Down Expand Up @@ -2505,14 +2496,14 @@ mod tests {

fn all_attributes() -> Vec<ColorSpec> {
let mut result = vec![];
for fg in vec![None, Some(Color::Red)] {
for bg in vec![None, Some(Color::Red)] {
for bold in vec![false, true] {
for underline in vec![false, true] {
for intense in vec![false, true] {
for italic in vec![false, true] {
for strikethrough in vec![false, true] {
for dimmed in vec![false, true] {
for fg in [None, Some(Color::Red)] {
for bg in [None, Some(Color::Red)] {
for bold in [false, true] {
for underline in [false, true] {
for intense in [false, true] {
for italic in [false, true] {
for strikethrough in [false, true] {
for dimmed in [false, true] {
let mut color = ColorSpec::new();
color.set_fg(fg);
color.set_bg(bg);
Expand Down Expand Up @@ -2552,7 +2543,7 @@ mod tests {
for color in all_attributes() {
let mut color1 = color.clone();
color1.clear();
assert!(color1.is_none(), "{:?} => {:?}", color, color1);
assert!(color1.is_none(), "{color:?} => {color1:?}");
}
}

Expand Down