mirror of
https://github.com/openharmony/third_party_rust_termcolor.git
synced 2026-06-30 21:37:57 -04:00
style: use rustfmt
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
max_width = 79
|
||||
use_small_heuristics = "max"
|
||||
+252
-136
@@ -85,9 +85,9 @@ use std::error;
|
||||
use std::fmt;
|
||||
use std::io::{self, Write};
|
||||
use std::str::FromStr;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
#[cfg(windows)]
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
/// This trait describes the behavior of writers that support colored output.
|
||||
pub trait WriteColor: io::Write {
|
||||
@@ -126,21 +126,33 @@ pub trait WriteColor: io::Write {
|
||||
}
|
||||
|
||||
impl<'a, T: ?Sized + WriteColor> WriteColor for &'a mut T {
|
||||
fn supports_color(&self) -> bool { (&**self).supports_color() }
|
||||
fn supports_color(&self) -> bool {
|
||||
(&**self).supports_color()
|
||||
}
|
||||
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
|
||||
(&mut **self).set_color(spec)
|
||||
}
|
||||
fn reset(&mut self) -> io::Result<()> { (&mut **self).reset() }
|
||||
fn is_synchronous(&self) -> bool { (&**self).is_synchronous() }
|
||||
fn reset(&mut self) -> io::Result<()> {
|
||||
(&mut **self).reset()
|
||||
}
|
||||
fn is_synchronous(&self) -> bool {
|
||||
(&**self).is_synchronous()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + WriteColor> WriteColor for Box<T> {
|
||||
fn supports_color(&self) -> bool { (&**self).supports_color() }
|
||||
fn supports_color(&self) -> bool {
|
||||
(&**self).supports_color()
|
||||
}
|
||||
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
|
||||
(&mut **self).set_color(spec)
|
||||
}
|
||||
fn reset(&mut self) -> io::Result<()> { (&mut **self).reset() }
|
||||
fn is_synchronous(&self) -> bool { (&**self).is_synchronous() }
|
||||
fn reset(&mut self) -> io::Result<()> {
|
||||
(&mut **self).reset()
|
||||
}
|
||||
fn is_synchronous(&self) -> bool {
|
||||
(&**self).is_synchronous()
|
||||
}
|
||||
}
|
||||
|
||||
/// ColorChoice represents the color preferences of an end user.
|
||||
@@ -168,12 +180,10 @@ impl ColorChoice {
|
||||
ColorChoice::Always => true,
|
||||
ColorChoice::AlwaysAnsi => true,
|
||||
ColorChoice::Never => false,
|
||||
ColorChoice::Auto => {
|
||||
match env::var("TERM") {
|
||||
Err(_) => false,
|
||||
Ok(k) => k != "dumb",
|
||||
}
|
||||
}
|
||||
ColorChoice::Auto => match env::var("TERM") {
|
||||
Err(_) => false,
|
||||
Ok(k) => k != "dumb",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,12 +194,10 @@ impl ColorChoice {
|
||||
ColorChoice::Always => true,
|
||||
ColorChoice::AlwaysAnsi => true,
|
||||
ColorChoice::Never => false,
|
||||
ColorChoice::Auto => {
|
||||
match env::var("TERM") {
|
||||
Err(_) => true,
|
||||
Ok(k) => k != "dumb",
|
||||
}
|
||||
}
|
||||
ColorChoice::Auto => match env::var("TERM") {
|
||||
Err(_) => true,
|
||||
Ok(k) => k != "dumb",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -347,7 +355,10 @@ enum WriterInner<W> {
|
||||
NoColor(NoColor<W>),
|
||||
Ansi(Ansi<W>),
|
||||
#[cfg(windows)]
|
||||
Windows { wtr: W, console: Mutex<wincolor::Console> },
|
||||
Windows {
|
||||
wtr: W,
|
||||
console: Mutex<wincolor::Console>,
|
||||
},
|
||||
}
|
||||
|
||||
/// WriterInnerLock is a (limited) generic representation of a writer. It is
|
||||
@@ -362,7 +373,10 @@ enum WriterInnerLock<'a, W> {
|
||||
#[allow(dead_code)]
|
||||
Unreachable(::std::marker::PhantomData<&'a ()>),
|
||||
#[cfg(windows)]
|
||||
Windows { wtr: W, console: MutexGuard<'a, wincolor::Console> },
|
||||
Windows {
|
||||
wtr: W,
|
||||
console: MutexGuard<'a, wincolor::Console>,
|
||||
},
|
||||
}
|
||||
|
||||
impl StandardStream {
|
||||
@@ -449,10 +463,8 @@ impl BufferedStandardStream {
|
||||
/// The specific color/style settings can be configured when writing via
|
||||
/// the `WriteColor` trait.
|
||||
pub fn stdout(choice: ColorChoice) -> BufferedStandardStream {
|
||||
let wtr = WriterInner::create(
|
||||
StandardStreamType::StdoutBuffered,
|
||||
choice,
|
||||
);
|
||||
let wtr =
|
||||
WriterInner::create(StandardStreamType::StdoutBuffered, choice);
|
||||
BufferedStandardStream { wtr: LossyStandardStream::new(wtr) }
|
||||
}
|
||||
|
||||
@@ -465,10 +477,8 @@ impl BufferedStandardStream {
|
||||
/// The specific color/style settings can be configured when writing via
|
||||
/// the `WriteColor` trait.
|
||||
pub fn stderr(choice: ColorChoice) -> BufferedStandardStream {
|
||||
let wtr = WriterInner::create(
|
||||
StandardStreamType::StderrBuffered,
|
||||
choice,
|
||||
);
|
||||
let wtr =
|
||||
WriterInner::create(StandardStreamType::StderrBuffered, choice);
|
||||
BufferedStandardStream { wtr: LossyStandardStream::new(wtr) }
|
||||
}
|
||||
}
|
||||
@@ -504,9 +514,10 @@ impl WriterInner<IoStandardStream> {
|
||||
StandardStreamType::StdoutBuffered => wincolor::Console::stdout(),
|
||||
StandardStreamType::StderrBuffered => wincolor::Console::stderr(),
|
||||
};
|
||||
let is_console_virtual = con.as_mut().map(|con| {
|
||||
con.set_virtual_terminal_processing(true).is_ok()
|
||||
}).unwrap_or(false);
|
||||
let is_console_virtual = con
|
||||
.as_mut()
|
||||
.map(|con| con.set_virtual_terminal_processing(true).is_ok())
|
||||
.unwrap_or(false);
|
||||
if choice.should_attempt_color() {
|
||||
if choice.should_ansi() || is_console_virtual {
|
||||
WriterInner::Ansi(Ansi(IoStandardStream::new(sty)))
|
||||
@@ -526,15 +537,21 @@ impl WriterInner<IoStandardStream> {
|
||||
|
||||
impl io::Write for StandardStream {
|
||||
#[inline]
|
||||
fn write(&mut self, b: &[u8]) -> io::Result<usize> { self.wtr.write(b) }
|
||||
fn write(&mut self, b: &[u8]) -> io::Result<usize> {
|
||||
self.wtr.write(b)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> io::Result<()> { self.wtr.flush() }
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
self.wtr.flush()
|
||||
}
|
||||
}
|
||||
|
||||
impl WriteColor for StandardStream {
|
||||
#[inline]
|
||||
fn supports_color(&self) -> bool { self.wtr.supports_color() }
|
||||
fn supports_color(&self) -> bool {
|
||||
self.wtr.supports_color()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
|
||||
@@ -542,23 +559,33 @@ impl WriteColor for StandardStream {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn reset(&mut self) -> io::Result<()> { self.wtr.reset() }
|
||||
fn reset(&mut self) -> io::Result<()> {
|
||||
self.wtr.reset()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_synchronous(&self) -> bool { self.wtr.is_synchronous() }
|
||||
fn is_synchronous(&self) -> bool {
|
||||
self.wtr.is_synchronous()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> io::Write for StandardStreamLock<'a> {
|
||||
#[inline]
|
||||
fn write(&mut self, b: &[u8]) -> io::Result<usize> { self.wtr.write(b) }
|
||||
fn write(&mut self, b: &[u8]) -> io::Result<usize> {
|
||||
self.wtr.write(b)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> io::Result<()> { self.wtr.flush() }
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
self.wtr.flush()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> WriteColor for StandardStreamLock<'a> {
|
||||
#[inline]
|
||||
fn supports_color(&self) -> bool { self.wtr.supports_color() }
|
||||
fn supports_color(&self) -> bool {
|
||||
self.wtr.supports_color()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
|
||||
@@ -566,23 +593,33 @@ impl<'a> WriteColor for StandardStreamLock<'a> {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn reset(&mut self) -> io::Result<()> { self.wtr.reset() }
|
||||
fn reset(&mut self) -> io::Result<()> {
|
||||
self.wtr.reset()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_synchronous(&self) -> bool { self.wtr.is_synchronous() }
|
||||
fn is_synchronous(&self) -> bool {
|
||||
self.wtr.is_synchronous()
|
||||
}
|
||||
}
|
||||
|
||||
impl io::Write for BufferedStandardStream {
|
||||
#[inline]
|
||||
fn write(&mut self, b: &[u8]) -> io::Result<usize> { self.wtr.write(b) }
|
||||
fn write(&mut self, b: &[u8]) -> io::Result<usize> {
|
||||
self.wtr.write(b)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> io::Result<()> { self.wtr.flush() }
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
self.wtr.flush()
|
||||
}
|
||||
}
|
||||
|
||||
impl WriteColor for BufferedStandardStream {
|
||||
#[inline]
|
||||
fn supports_color(&self) -> bool { self.wtr.supports_color() }
|
||||
fn supports_color(&self) -> bool {
|
||||
self.wtr.supports_color()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
|
||||
@@ -593,10 +630,14 @@ impl WriteColor for BufferedStandardStream {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn reset(&mut self) -> io::Result<()> { self.wtr.reset() }
|
||||
fn reset(&mut self) -> io::Result<()> {
|
||||
self.wtr.reset()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_synchronous(&self) -> bool { self.wtr.is_synchronous() }
|
||||
fn is_synchronous(&self) -> bool {
|
||||
self.wtr.is_synchronous()
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: io::Write> io::Write for WriterInner<W> {
|
||||
@@ -662,7 +703,7 @@ impl<W: io::Write> WriteColor for WriterInner<W> {
|
||||
WriterInner::NoColor(_) => false,
|
||||
WriterInner::Ansi(_) => false,
|
||||
#[cfg(windows)]
|
||||
WriterInner::Windows {..} => true,
|
||||
WriterInner::Windows { .. } => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -733,7 +774,7 @@ impl<'a, W: io::Write> WriteColor for WriterInnerLock<'a, W> {
|
||||
WriterInnerLock::NoColor(_) => false,
|
||||
WriterInnerLock::Ansi(_) => false,
|
||||
#[cfg(windows)]
|
||||
WriterInnerLock::Windows {..} => true,
|
||||
WriterInnerLock::Windows { .. } => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -787,10 +828,12 @@ impl BufferWriter {
|
||||
StandardStreamType::Stderr => wincolor::Console::stderr(),
|
||||
StandardStreamType::StdoutBuffered => wincolor::Console::stdout(),
|
||||
StandardStreamType::StderrBuffered => wincolor::Console::stderr(),
|
||||
}.ok();
|
||||
let is_console_virtual = con.as_mut().map(|con| {
|
||||
con.set_virtual_terminal_processing(true).is_ok()
|
||||
}).unwrap_or(false);
|
||||
}
|
||||
.ok();
|
||||
let is_console_virtual = con
|
||||
.as_mut()
|
||||
.map(|con| con.set_virtual_terminal_processing(true).is_ok())
|
||||
.unwrap_or(false);
|
||||
// If we can enable ANSI on Windows, then we don't need the console
|
||||
// anymore.
|
||||
if is_console_virtual {
|
||||
@@ -879,7 +922,9 @@ impl BufferWriter {
|
||||
BufferInner::Windows(ref b) => {
|
||||
// We guarantee by construction that we have a console here.
|
||||
// Namely, a BufferWriter is the only way to produce a Buffer.
|
||||
let console_mutex = self.console.as_ref()
|
||||
let console_mutex = self
|
||||
.console
|
||||
.as_ref()
|
||||
.expect("got Windows buffer but have no Console");
|
||||
let mut console = console_mutex.lock().unwrap();
|
||||
b.print(&mut *console, &mut stream)?;
|
||||
@@ -1088,16 +1133,24 @@ pub struct NoColor<W>(W);
|
||||
impl<W: Write> NoColor<W> {
|
||||
/// Create a new writer that satisfies `WriteColor` but drops all color
|
||||
/// information.
|
||||
pub fn new(wtr: W) -> NoColor<W> { NoColor(wtr) }
|
||||
pub fn new(wtr: W) -> NoColor<W> {
|
||||
NoColor(wtr)
|
||||
}
|
||||
|
||||
/// Consume this `NoColor` value and return the inner writer.
|
||||
pub fn into_inner(self) -> W { self.0 }
|
||||
pub fn into_inner(self) -> W {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Return a reference to the inner writer.
|
||||
pub fn get_ref(&self) -> &W { &self.0 }
|
||||
pub fn get_ref(&self) -> &W {
|
||||
&self.0
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the inner writer.
|
||||
pub fn get_mut(&mut self) -> &mut W { &mut self.0 }
|
||||
pub fn get_mut(&mut self) -> &mut W {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: io::Write> io::Write for NoColor<W> {
|
||||
@@ -1114,16 +1167,24 @@ impl<W: io::Write> io::Write for NoColor<W> {
|
||||
|
||||
impl<W: io::Write> WriteColor for NoColor<W> {
|
||||
#[inline]
|
||||
fn supports_color(&self) -> bool { false }
|
||||
fn supports_color(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_color(&mut self, _: &ColorSpec) -> io::Result<()> { Ok(()) }
|
||||
fn set_color(&mut self, _: &ColorSpec) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn reset(&mut self) -> io::Result<()> { Ok(()) }
|
||||
fn reset(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_synchronous(&self) -> bool { false }
|
||||
fn is_synchronous(&self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Satisfies `WriteColor` using standard ANSI escape sequences.
|
||||
@@ -1132,16 +1193,24 @@ pub struct Ansi<W>(W);
|
||||
impl<W: Write> Ansi<W> {
|
||||
/// Create a new writer that satisfies `WriteColor` using standard ANSI
|
||||
/// escape sequences.
|
||||
pub fn new(wtr: W) -> Ansi<W> { Ansi(wtr) }
|
||||
pub fn new(wtr: W) -> Ansi<W> {
|
||||
Ansi(wtr)
|
||||
}
|
||||
|
||||
/// Consume this `Ansi` value and return the inner writer.
|
||||
pub fn into_inner(self) -> W { self.0 }
|
||||
pub fn into_inner(self) -> W {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Return a reference to the inner writer.
|
||||
pub fn get_ref(&self) -> &W { &self.0 }
|
||||
pub fn get_ref(&self) -> &W {
|
||||
&self.0
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the inner writer.
|
||||
pub fn get_mut(&mut self) -> &mut W { &mut self.0 }
|
||||
pub fn get_mut(&mut self) -> &mut W {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: io::Write> io::Write for Ansi<W> {
|
||||
@@ -1158,7 +1227,9 @@ impl<W: io::Write> io::Write for Ansi<W> {
|
||||
|
||||
impl<W: io::Write> WriteColor for Ansi<W> {
|
||||
#[inline]
|
||||
fn supports_color(&self) -> bool { true }
|
||||
fn supports_color(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
|
||||
@@ -1189,7 +1260,9 @@ impl<W: io::Write> WriteColor for Ansi<W> {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_synchronous(&self) -> bool { false }
|
||||
fn is_synchronous(&self) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: io::Write> Ansi<W> {
|
||||
@@ -1210,7 +1283,7 @@ impl<W: io::Write> Ansi<W> {
|
||||
} else {
|
||||
self.write_str(concat!("\x1B[48;5;", $clr, "m"))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! write_normal {
|
||||
($clr:expr) => {
|
||||
@@ -1219,7 +1292,7 @@ impl<W: io::Write> Ansi<W> {
|
||||
} else {
|
||||
self.write_str(concat!("\x1B[4", $clr, "m"))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! write_var_ansi_code {
|
||||
($pre:expr, $($code:expr),+) => {{
|
||||
@@ -1339,10 +1412,7 @@ struct WindowsBuffer {
|
||||
impl WindowsBuffer {
|
||||
/// Create a new empty buffer for Windows console coloring.
|
||||
fn new() -> WindowsBuffer {
|
||||
WindowsBuffer {
|
||||
buf: vec![],
|
||||
colors: vec![],
|
||||
}
|
||||
WindowsBuffer { buf: vec![], colors: vec![] }
|
||||
}
|
||||
|
||||
/// Push the given color specification into this buffer.
|
||||
@@ -1399,7 +1469,9 @@ impl io::Write for WindowsBuffer {
|
||||
#[cfg(windows)]
|
||||
impl WriteColor for WindowsBuffer {
|
||||
#[inline]
|
||||
fn supports_color(&self) -> bool { true }
|
||||
fn supports_color(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
|
||||
@@ -1452,7 +1524,9 @@ impl ColorSpec {
|
||||
}
|
||||
|
||||
/// Get the foreground color.
|
||||
pub fn fg(&self) -> Option<&Color> { self.fg_color.as_ref() }
|
||||
pub fn fg(&self) -> Option<&Color> {
|
||||
self.fg_color.as_ref()
|
||||
}
|
||||
|
||||
/// Set the foreground color.
|
||||
pub fn set_fg(&mut self, color: Option<Color>) -> &mut ColorSpec {
|
||||
@@ -1461,7 +1535,9 @@ impl ColorSpec {
|
||||
}
|
||||
|
||||
/// Get the background color.
|
||||
pub fn bg(&self) -> Option<&Color> { self.bg_color.as_ref() }
|
||||
pub fn bg(&self) -> Option<&Color> {
|
||||
self.bg_color.as_ref()
|
||||
}
|
||||
|
||||
/// Set the background color.
|
||||
pub fn set_bg(&mut self, color: Option<Color>) -> &mut ColorSpec {
|
||||
@@ -1472,7 +1548,9 @@ impl ColorSpec {
|
||||
/// Get whether this is bold or not.
|
||||
///
|
||||
/// Note that the bold setting has no effect in a Windows console.
|
||||
pub fn bold(&self) -> bool { self.bold }
|
||||
pub fn bold(&self) -> bool {
|
||||
self.bold
|
||||
}
|
||||
|
||||
/// Set whether the text is bolded or not.
|
||||
///
|
||||
@@ -1485,7 +1563,9 @@ impl ColorSpec {
|
||||
/// Get whether this is italic or not.
|
||||
///
|
||||
/// Note that the italic setting has no effect in a Windows console.
|
||||
pub fn italic(&self) -> bool { self.italic }
|
||||
pub fn italic(&self) -> bool {
|
||||
self.italic
|
||||
}
|
||||
|
||||
/// Set whether the text is italicized or not.
|
||||
///
|
||||
@@ -1498,7 +1578,9 @@ impl ColorSpec {
|
||||
/// Get whether this is underline or not.
|
||||
///
|
||||
/// Note that the underline setting has no effect in a Windows console.
|
||||
pub fn underline(&self) -> bool { self.underline }
|
||||
pub fn underline(&self) -> bool {
|
||||
self.underline
|
||||
}
|
||||
|
||||
/// Set whether the text is underlined or not.
|
||||
///
|
||||
@@ -1515,7 +1597,9 @@ impl ColorSpec {
|
||||
/// settings are applied.
|
||||
///
|
||||
/// Note that the reset setting has no effect in a Windows console.
|
||||
pub fn reset(&self) -> bool { self.reset }
|
||||
pub fn reset(&self) -> bool {
|
||||
self.reset
|
||||
}
|
||||
|
||||
/// Set whether to reset the terminal whenever color settings are applied.
|
||||
///
|
||||
@@ -1541,7 +1625,9 @@ impl ColorSpec {
|
||||
///
|
||||
/// On Windows systems, this will output the ANSI escape sequence
|
||||
/// that will print a brighter version of the color specified.
|
||||
pub fn intense(&self) -> bool { self.intense }
|
||||
pub fn intense(&self) -> bool {
|
||||
self.intense
|
||||
}
|
||||
|
||||
/// Set whether the text is intense or not.
|
||||
///
|
||||
@@ -1558,9 +1644,12 @@ impl ColorSpec {
|
||||
|
||||
/// Returns true if this color specification has no colors or styles.
|
||||
pub fn is_none(&self) -> bool {
|
||||
self.fg_color.is_none() && self.bg_color.is_none()
|
||||
&& !self.bold && !self.underline
|
||||
&& !self.italic && !self.intense
|
||||
self.fg_color.is_none()
|
||||
&& self.bg_color.is_none()
|
||||
&& !self.bold
|
||||
&& !self.underline
|
||||
&& !self.italic
|
||||
&& !self.intense
|
||||
}
|
||||
|
||||
/// Clears this color specification so that it has no color/style settings.
|
||||
@@ -1636,7 +1725,7 @@ impl Color {
|
||||
self,
|
||||
intense: bool,
|
||||
) -> Option<(wincolor::Intense, wincolor::Color)> {
|
||||
use wincolor::Intense::{Yes, No};
|
||||
use wincolor::Intense::{No, Yes};
|
||||
|
||||
let color = match self {
|
||||
Color::Black => wincolor::Color::Black,
|
||||
@@ -1660,7 +1749,9 @@ impl Color {
|
||||
Color::Ansi256(10) => return Some((Yes, wincolor::Color::Green)),
|
||||
Color::Ansi256(11) => return Some((Yes, wincolor::Color::Yellow)),
|
||||
Color::Ansi256(12) => return Some((Yes, wincolor::Color::Blue)),
|
||||
Color::Ansi256(13) => return Some((Yes, wincolor::Color::Magenta)),
|
||||
Color::Ansi256(13) => {
|
||||
return Some((Yes, wincolor::Color::Magenta))
|
||||
}
|
||||
Color::Ansi256(14) => return Some((Yes, wincolor::Color::Cyan)),
|
||||
Color::Ansi256(15) => return Some((Yes, wincolor::Color::White)),
|
||||
Color::Ansi256(_) => return None,
|
||||
@@ -1709,11 +1800,9 @@ impl Color {
|
||||
} else if codes.len() == 3 {
|
||||
let mut v = vec![];
|
||||
for code in codes {
|
||||
let n = parse_number(code).ok_or_else(|| {
|
||||
ParseColorError {
|
||||
kind: ParseColorErrorKind::InvalidRgb,
|
||||
given: s.to_string(),
|
||||
}
|
||||
let n = parse_number(code).ok_or_else(|| ParseColorError {
|
||||
kind: ParseColorErrorKind::InvalidRgb,
|
||||
given: s.to_string(),
|
||||
})?;
|
||||
v.push(n);
|
||||
}
|
||||
@@ -1750,7 +1839,9 @@ enum ParseColorErrorKind {
|
||||
|
||||
impl ParseColorError {
|
||||
/// Return the string that couldn't be parsed as a valid color.
|
||||
pub fn invalid(&self) -> &str { &self.given }
|
||||
pub fn invalid(&self) -> &str {
|
||||
&self.given
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for ParseColorError {
|
||||
@@ -1768,22 +1859,26 @@ impl fmt::Display for ParseColorError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use self::ParseColorErrorKind::*;
|
||||
match self.kind {
|
||||
InvalidName => {
|
||||
write!(f, "unrecognized color name '{}'. Choose from: \
|
||||
black, blue, green, red, cyan, magenta, yellow, \
|
||||
white",
|
||||
self.given)
|
||||
}
|
||||
InvalidAnsi256 => {
|
||||
write!(f, "unrecognized ansi256 color number, \
|
||||
should be '[0-255]' (or a hex number), but is '{}'",
|
||||
self.given)
|
||||
}
|
||||
InvalidRgb => {
|
||||
write!(f, "unrecognized RGB color triple, \
|
||||
should be '[0-255],[0-255],[0-255]' (or a hex \
|
||||
triple), but is '{}'", self.given)
|
||||
}
|
||||
InvalidName => write!(
|
||||
f,
|
||||
"unrecognized color name '{}'. Choose from: \
|
||||
black, blue, green, red, cyan, magenta, yellow, \
|
||||
white",
|
||||
self.given
|
||||
),
|
||||
InvalidAnsi256 => write!(
|
||||
f,
|
||||
"unrecognized ansi256 color number, \
|
||||
should be '[0-255]' (or a hex number), but is '{}'",
|
||||
self.given
|
||||
),
|
||||
InvalidRgb => write!(
|
||||
f,
|
||||
"unrecognized RGB color triple, \
|
||||
should be '[0-255],[0-255],[0-255]' (or a hex \
|
||||
triple), but is '{}'",
|
||||
self.given
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1820,8 +1915,7 @@ impl<W: io::Write> LossyStandardStream<W> {
|
||||
|
||||
#[cfg(windows)]
|
||||
fn new(wtr: W) -> LossyStandardStream<W> {
|
||||
let is_console =
|
||||
wincolor::Console::stdout().is_ok()
|
||||
let is_console = wincolor::Console::stdout().is_ok()
|
||||
|| wincolor::Console::stderr().is_ok();
|
||||
LossyStandardStream { wtr: wtr, is_console: is_console }
|
||||
}
|
||||
@@ -1842,12 +1936,18 @@ impl<W: io::Write> LossyStandardStream<W> {
|
||||
}
|
||||
|
||||
impl<W: WriteColor> WriteColor for LossyStandardStream<W> {
|
||||
fn supports_color(&self) -> bool { self.wtr.supports_color() }
|
||||
fn supports_color(&self) -> bool {
|
||||
self.wtr.supports_color()
|
||||
}
|
||||
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
|
||||
self.wtr.set_color(spec)
|
||||
}
|
||||
fn reset(&mut self) -> io::Result<()> { self.wtr.reset() }
|
||||
fn is_synchronous(&self) -> bool { self.wtr.is_synchronous() }
|
||||
fn reset(&mut self) -> io::Result<()> {
|
||||
self.wtr.reset()
|
||||
}
|
||||
fn is_synchronous(&self) -> bool {
|
||||
self.wtr.is_synchronous()
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: io::Write> io::Write for LossyStandardStream<W> {
|
||||
@@ -1885,8 +1985,8 @@ fn write_lossy_utf8<W: io::Write>(mut w: W, buf: &[u8]) -> io::Result<usize> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
Ansi, Color, ParseColorError, ParseColorErrorKind, StandardStream,
|
||||
ColorSpec, WriteColor,
|
||||
Ansi, Color, ColorSpec, ParseColorError, ParseColorErrorKind,
|
||||
StandardStream, WriteColor,
|
||||
};
|
||||
|
||||
fn assert_is_send<T: Send>() {}
|
||||
@@ -1917,10 +2017,13 @@ mod tests {
|
||||
#[test]
|
||||
fn test_256_parse_err_out_of_range() {
|
||||
let color = "256".parse::<Color>();
|
||||
assert_eq!(color, Err(ParseColorError {
|
||||
kind: ParseColorErrorKind::InvalidAnsi256,
|
||||
given: "256".to_string(),
|
||||
}));
|
||||
assert_eq!(
|
||||
color,
|
||||
Err(ParseColorError {
|
||||
kind: ParseColorErrorKind::InvalidAnsi256,
|
||||
given: "256".to_string(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1941,25 +2044,34 @@ mod tests {
|
||||
#[test]
|
||||
fn test_rgb_parse_err_out_of_range() {
|
||||
let color = "0,0,256".parse::<Color>();
|
||||
assert_eq!(color, Err(ParseColorError {
|
||||
kind: ParseColorErrorKind::InvalidRgb,
|
||||
given: "0,0,256".to_string(),
|
||||
}));
|
||||
assert_eq!(
|
||||
color,
|
||||
Err(ParseColorError {
|
||||
kind: ParseColorErrorKind::InvalidRgb,
|
||||
given: "0,0,256".to_string(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rgb_parse_err_bad_format() {
|
||||
let color = "0,0".parse::<Color>();
|
||||
assert_eq!(color, Err(ParseColorError {
|
||||
kind: ParseColorErrorKind::InvalidRgb,
|
||||
given: "0,0".to_string(),
|
||||
}));
|
||||
assert_eq!(
|
||||
color,
|
||||
Err(ParseColorError {
|
||||
kind: ParseColorErrorKind::InvalidRgb,
|
||||
given: "0,0".to_string(),
|
||||
})
|
||||
);
|
||||
|
||||
let color = "not_a_color".parse::<Color>();
|
||||
assert_eq!(color, Err(ParseColorError {
|
||||
kind: ParseColorErrorKind::InvalidName,
|
||||
given: "not_a_color".to_string(),
|
||||
}));
|
||||
assert_eq!(
|
||||
color,
|
||||
Err(ParseColorError {
|
||||
kind: ParseColorErrorKind::InvalidName,
|
||||
given: "not_a_color".to_string(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -2026,8 +2138,13 @@ mod tests {
|
||||
#[test]
|
||||
fn test_is_none() {
|
||||
for (i, color) in all_attributes().iter().enumerate() {
|
||||
assert_eq!(i == 0, color.is_none(),
|
||||
"{:?} => {}", color, color.is_none())
|
||||
assert_eq!(
|
||||
i == 0,
|
||||
color.is_none(),
|
||||
"{:?} => {}",
|
||||
color,
|
||||
color.is_none()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2036,8 +2153,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-17
@@ -1,12 +1,9 @@
|
||||
use std::io;
|
||||
|
||||
use winapi::shared::minwindef::{WORD};
|
||||
use winapi::shared::minwindef::WORD;
|
||||
use winapi::um::wincon::{
|
||||
self,
|
||||
FOREGROUND_BLUE as FG_BLUE,
|
||||
FOREGROUND_GREEN as FG_GREEN,
|
||||
FOREGROUND_RED as FG_RED,
|
||||
FOREGROUND_INTENSITY as FG_INTENSITY,
|
||||
self, FOREGROUND_BLUE as FG_BLUE, FOREGROUND_GREEN as FG_GREEN,
|
||||
FOREGROUND_INTENSITY as FG_INTENSITY, FOREGROUND_RED as FG_RED,
|
||||
};
|
||||
use winapi_util as winutil;
|
||||
|
||||
@@ -55,11 +52,7 @@ impl Console {
|
||||
let h = kind.handle();
|
||||
let info = winutil::console::screen_buffer_info(&h)?;
|
||||
let attr = TextAttributes::from_word(info.attributes());
|
||||
Ok(Console {
|
||||
kind: kind,
|
||||
start_attr: attr,
|
||||
cur_attr: attr,
|
||||
})
|
||||
Ok(Console { kind: kind, start_attr: attr, cur_attr: attr })
|
||||
}
|
||||
|
||||
/// Create a new Console to stdout.
|
||||
@@ -135,12 +128,7 @@ impl Console {
|
||||
|
||||
let handle = self.kind.handle();
|
||||
let old_mode = winutil::console::mode(&handle)?;
|
||||
let new_mode =
|
||||
if yes {
|
||||
old_mode | vt
|
||||
} else {
|
||||
old_mode & !vt
|
||||
};
|
||||
let new_mode = if yes { old_mode | vt } else { old_mode & !vt };
|
||||
if old_mode == new_mode {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user