mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-05-13 21:26:27 +02:00
termcolor: moved to its own repository
We also move wincolor with it. Fixes #924
This commit is contained in:
parent
b64475aeac
commit
df469fe1b4
@ -1,3 +0,0 @@
|
|||||||
This project is dual-licensed under the Unlicense and MIT licenses.
|
|
||||||
|
|
||||||
You may use this code under the terms of either license.
|
|
@ -1,20 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "termcolor"
|
|
||||||
version = "0.3.6" #:version
|
|
||||||
authors = ["Andrew Gallant <jamslam@gmail.com>"]
|
|
||||||
description = """
|
|
||||||
A simple cross platform library for writing colored text to a terminal.
|
|
||||||
"""
|
|
||||||
documentation = "https://docs.rs/termcolor"
|
|
||||||
homepage = "https://github.com/BurntSushi/ripgrep/tree/master/termcolor"
|
|
||||||
repository = "https://github.com/BurntSushi/ripgrep/tree/master/termcolor"
|
|
||||||
readme = "README.md"
|
|
||||||
keywords = ["windows", "win", "color", "ansi", "console"]
|
|
||||||
license = "Unlicense/MIT"
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
name = "termcolor"
|
|
||||||
bench = false
|
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
|
||||||
wincolor = { version = "0.1.6", path = "../wincolor" }
|
|
@ -1,21 +0,0 @@
|
|||||||
The MIT License (MIT)
|
|
||||||
|
|
||||||
Copyright (c) 2015 Andrew Gallant
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
@ -1,86 +1,2 @@
|
|||||||
termcolor
|
termcolor has moved to its own repository:
|
||||||
=========
|
https://github.com/BurntSushi/termcolor
|
||||||
A simple cross platform library for writing colored text to a terminal. This
|
|
||||||
library writes colored text either using standard ANSI escape sequences or
|
|
||||||
by interacting with the Windows console. Several convenient abstractions
|
|
||||||
are provided for use in single-threaded or multi-threaded command line
|
|
||||||
applications.
|
|
||||||
|
|
||||||
[](https://travis-ci.org/BurntSushi/ripgrep)
|
|
||||||
[](https://ci.appveyor.com/project/BurntSushi/ripgrep)
|
|
||||||
[](https://crates.io/crates/termcolor)
|
|
||||||
|
|
||||||
Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org).
|
|
||||||
|
|
||||||
### Documentation
|
|
||||||
|
|
||||||
[https://docs.rs/termcolor](https://docs.rs/termcolor)
|
|
||||||
|
|
||||||
### Usage
|
|
||||||
|
|
||||||
Add this to your `Cargo.toml`:
|
|
||||||
|
|
||||||
```toml
|
|
||||||
[dependencies]
|
|
||||||
termcolor = "0.3"
|
|
||||||
```
|
|
||||||
|
|
||||||
and this to your crate root:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
extern crate termcolor;
|
|
||||||
```
|
|
||||||
|
|
||||||
### Organization
|
|
||||||
|
|
||||||
The `WriteColor` trait extends the `io::Write` trait with methods for setting
|
|
||||||
colors or resetting them.
|
|
||||||
|
|
||||||
`StandardStream` and `StandardStreamLock` both satisfy `WriteColor` and are
|
|
||||||
analogous to `std::io::Stdout` and `std::io::StdoutLock`, or `std::io::Stderr`
|
|
||||||
and `std::io::StderrLock`.
|
|
||||||
|
|
||||||
`Buffer` is an in memory buffer that supports colored text. In a parallel
|
|
||||||
program, each thread might write to its own buffer. A buffer can be printed to
|
|
||||||
stdout or stderr using a `BufferWriter`. The advantage of this design is that
|
|
||||||
each thread can work in parallel on a buffer without having to synchronize
|
|
||||||
access to global resources such as the Windows console. Moreover, this design
|
|
||||||
also prevents interleaving of buffer output.
|
|
||||||
|
|
||||||
`Ansi` and `NoColor` both satisfy `WriteColor` for arbitrary implementors of
|
|
||||||
`io::Write`. These types are useful when you know exactly what you need. An
|
|
||||||
analogous type for the Windows console is not provided since it cannot exist.
|
|
||||||
|
|
||||||
### Example: using `StandardStream`
|
|
||||||
|
|
||||||
The `StandardStream` type in this crate works similarly to `std::io::Stdout`,
|
|
||||||
except it is augmented with methods for coloring by the `WriteColor` trait.
|
|
||||||
For example, to write some green text:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
use std::io::Write;
|
|
||||||
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
|
|
||||||
|
|
||||||
let mut stdout = StandardStream::stdout(ColorChoice::Always);
|
|
||||||
stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
|
|
||||||
writeln!(&mut stdout, "green text!")?;
|
|
||||||
```
|
|
||||||
|
|
||||||
### Example: using `BufferWriter`
|
|
||||||
|
|
||||||
A `BufferWriter` can create buffers and write buffers to stdout or stderr. It
|
|
||||||
does *not* implement `io::Write` or `WriteColor` itself. Instead, `Buffer`
|
|
||||||
implements `io::Write` and `termcolor::WriteColor`.
|
|
||||||
|
|
||||||
This example shows how to print some green text to stderr.
|
|
||||||
|
|
||||||
```rust
|
|
||||||
use std::io::Write;
|
|
||||||
use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor};
|
|
||||||
|
|
||||||
let mut bufwtr = BufferWriter::stderr(ColorChoice::Always);
|
|
||||||
let mut buffer = bufwtr.buffer();
|
|
||||||
buffer.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
|
|
||||||
writeln!(&mut buffer, "green text!")?;
|
|
||||||
bufwtr.print(&buffer)?;
|
|
||||||
```
|
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
This is free and unencumbered software released into the public domain.
|
|
||||||
|
|
||||||
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
||||||
distribute this software, either in source code form or as a compiled
|
|
||||||
binary, for any purpose, commercial or non-commercial, and by any
|
|
||||||
means.
|
|
||||||
|
|
||||||
In jurisdictions that recognize copyright laws, the author or authors
|
|
||||||
of this software dedicate any and all copyright interest in the
|
|
||||||
software to the public domain. We make this dedication for the benefit
|
|
||||||
of the public at large and to the detriment of our heirs and
|
|
||||||
successors. We intend this dedication to be an overt act of
|
|
||||||
relinquishment in perpetuity of all present and future rights to this
|
|
||||||
software under copyright law.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
||||||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
||||||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
||||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
||||||
OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
For more information, please refer to <http://unlicense.org/>
|
|
1832
termcolor/src/lib.rs
1832
termcolor/src/lib.rs
File diff suppressed because it is too large
Load Diff
@ -1,3 +0,0 @@
|
|||||||
This project is dual-licensed under the Unlicense and MIT licenses.
|
|
||||||
|
|
||||||
You may use this code under the terms of either license.
|
|
@ -1,21 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "wincolor"
|
|
||||||
version = "0.1.6" #:version
|
|
||||||
authors = ["Andrew Gallant <jamslam@gmail.com>"]
|
|
||||||
description = """
|
|
||||||
A simple Windows specific API for controlling text color in a Windows console.
|
|
||||||
"""
|
|
||||||
documentation = "https://docs.rs/wincolor"
|
|
||||||
homepage = "https://github.com/BurntSushi/ripgrep/tree/master/wincolor"
|
|
||||||
repository = "https://github.com/BurntSushi/ripgrep/tree/master/wincolor"
|
|
||||||
readme = "README.md"
|
|
||||||
keywords = ["windows", "win", "color", "ansi", "console"]
|
|
||||||
license = "Unlicense/MIT"
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
name = "wincolor"
|
|
||||||
bench = false
|
|
||||||
|
|
||||||
[dependencies.winapi]
|
|
||||||
version = "0.3"
|
|
||||||
features = ["consoleapi", "minwindef", "processenv", "winbase", "wincon"]
|
|
@ -1,21 +0,0 @@
|
|||||||
The MIT License (MIT)
|
|
||||||
|
|
||||||
Copyright (c) 2015 Andrew Gallant
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
@ -1,44 +1,2 @@
|
|||||||
wincolor
|
wincolor has moved to the termcolor repository:
|
||||||
========
|
https://github.com/BurntSushi/termcolor
|
||||||
A simple Windows specific API for controlling text color in a Windows console.
|
|
||||||
The purpose of this crate is to expose the full inflexibility of the Windows
|
|
||||||
console without any platform independent abstraction.
|
|
||||||
|
|
||||||
[](https://ci.appveyor.com/project/BurntSushi/ripgrep)
|
|
||||||
[](https://crates.io/crates/wincolor)
|
|
||||||
|
|
||||||
Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org).
|
|
||||||
|
|
||||||
### Documentation
|
|
||||||
|
|
||||||
[https://docs.rs/wincolor](https://docs.rs/wincolor)
|
|
||||||
|
|
||||||
### Usage
|
|
||||||
|
|
||||||
Add this to your `Cargo.toml`:
|
|
||||||
|
|
||||||
```toml
|
|
||||||
[dependencies]
|
|
||||||
wincolor = "0.1"
|
|
||||||
```
|
|
||||||
|
|
||||||
and this to your crate root:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
extern crate wincolor;
|
|
||||||
```
|
|
||||||
|
|
||||||
### Example
|
|
||||||
|
|
||||||
This is a simple example that shows how to write text with a foreground color
|
|
||||||
of cyan and the intense attribute set:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
use wincolor::{Console, Color, Intense};
|
|
||||||
|
|
||||||
let mut con = Console::stdout().unwrap();
|
|
||||||
con.fg(Intense::Yes, Color::Cyan).unwrap();
|
|
||||||
println!("This text will be intense cyan.");
|
|
||||||
con.reset().unwrap();
|
|
||||||
println!("This text will be normal.");
|
|
||||||
```
|
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
This is free and unencumbered software released into the public domain.
|
|
||||||
|
|
||||||
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
||||||
distribute this software, either in source code form or as a compiled
|
|
||||||
binary, for any purpose, commercial or non-commercial, and by any
|
|
||||||
means.
|
|
||||||
|
|
||||||
In jurisdictions that recognize copyright laws, the author or authors
|
|
||||||
of this software dedicate any and all copyright interest in the
|
|
||||||
software to the public domain. We make this dedication for the benefit
|
|
||||||
of the public at large and to the detriment of our heirs and
|
|
||||||
successors. We intend this dedication to be an overt act of
|
|
||||||
relinquishment in perpetuity of all present and future rights to this
|
|
||||||
software under copyright law.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
||||||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
||||||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
||||||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
||||||
OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
For more information, please refer to <http://unlicense.org/>
|
|
@ -1,33 +0,0 @@
|
|||||||
/*!
|
|
||||||
This crate provides a safe and simple Windows specific API to control
|
|
||||||
text attributes in the Windows console. Text attributes are limited to
|
|
||||||
foreground/background colors, as well as whether to make colors intense or not.
|
|
||||||
|
|
||||||
Note that on non-Windows platforms, this crate is empty but will compile.
|
|
||||||
|
|
||||||
# Example
|
|
||||||
|
|
||||||
```no_run
|
|
||||||
# #[cfg(windows)]
|
|
||||||
# {
|
|
||||||
use wincolor::{Console, Color, Intense};
|
|
||||||
|
|
||||||
let mut con = Console::stdout().unwrap();
|
|
||||||
con.fg(Intense::Yes, Color::Cyan).unwrap();
|
|
||||||
println!("This text will be intense cyan.");
|
|
||||||
con.reset().unwrap();
|
|
||||||
println!("This text will be normal.");
|
|
||||||
# }
|
|
||||||
```
|
|
||||||
*/
|
|
||||||
|
|
||||||
#![deny(missing_docs)]
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
extern crate winapi;
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
pub use win::*;
|
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
mod win;
|
|
@ -1,266 +0,0 @@
|
|||||||
use std::io;
|
|
||||||
use std::mem;
|
|
||||||
|
|
||||||
use winapi::shared::minwindef::{DWORD, WORD};
|
|
||||||
use winapi::um::consoleapi;
|
|
||||||
use winapi::um::processenv;
|
|
||||||
use winapi::um::winbase::{STD_ERROR_HANDLE, STD_OUTPUT_HANDLE};
|
|
||||||
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,
|
|
||||||
};
|
|
||||||
|
|
||||||
const FG_CYAN: WORD = FG_BLUE | FG_GREEN;
|
|
||||||
const FG_MAGENTA: WORD = FG_BLUE | FG_RED;
|
|
||||||
const FG_YELLOW: WORD = FG_GREEN | FG_RED;
|
|
||||||
const FG_WHITE: WORD = FG_BLUE | FG_GREEN | FG_RED;
|
|
||||||
|
|
||||||
/// A Windows console.
|
|
||||||
///
|
|
||||||
/// This represents a very limited set of functionality available to a Windows
|
|
||||||
/// console. In particular, it can only change text attributes such as color
|
|
||||||
/// and intensity.
|
|
||||||
///
|
|
||||||
/// There is no way to "write" to this console. Simply write to
|
|
||||||
/// stdout or stderr instead, while interleaving instructions to the console
|
|
||||||
/// to change text attributes.
|
|
||||||
///
|
|
||||||
/// A common pitfall when using a console is to forget to flush writes to
|
|
||||||
/// stdout before setting new text attributes.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Console {
|
|
||||||
handle_id: DWORD,
|
|
||||||
start_attr: TextAttributes,
|
|
||||||
cur_attr: TextAttributes,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Console {
|
|
||||||
/// Get a console for a standard I/O stream.
|
|
||||||
fn create_for_stream(handle_id: DWORD) -> io::Result<Console> {
|
|
||||||
let mut info = unsafe { mem::zeroed() };
|
|
||||||
let res = unsafe {
|
|
||||||
let handle = processenv::GetStdHandle(handle_id);
|
|
||||||
wincon::GetConsoleScreenBufferInfo(handle, &mut info)
|
|
||||||
};
|
|
||||||
if res == 0 {
|
|
||||||
return Err(io::Error::last_os_error());
|
|
||||||
}
|
|
||||||
let attr = TextAttributes::from_word(info.wAttributes);
|
|
||||||
Ok(Console {
|
|
||||||
handle_id: handle_id,
|
|
||||||
start_attr: attr,
|
|
||||||
cur_attr: attr,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a new Console to stdout.
|
|
||||||
///
|
|
||||||
/// If there was a problem creating the console, then an error is returned.
|
|
||||||
pub fn stdout() -> io::Result<Console> {
|
|
||||||
Self::create_for_stream(STD_OUTPUT_HANDLE)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a new Console to stderr.
|
|
||||||
///
|
|
||||||
/// If there was a problem creating the console, then an error is returned.
|
|
||||||
pub fn stderr() -> io::Result<Console> {
|
|
||||||
Self::create_for_stream(STD_ERROR_HANDLE)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Applies the current text attributes.
|
|
||||||
fn set(&mut self) -> io::Result<()> {
|
|
||||||
let attr = self.cur_attr.to_word();
|
|
||||||
let res = unsafe {
|
|
||||||
let handle = processenv::GetStdHandle(self.handle_id);
|
|
||||||
wincon::SetConsoleTextAttribute(handle, attr)
|
|
||||||
};
|
|
||||||
if res == 0 {
|
|
||||||
return Err(io::Error::last_os_error());
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Apply the given intensity and color attributes to the console
|
|
||||||
/// foreground.
|
|
||||||
///
|
|
||||||
/// If there was a problem setting attributes on the console, then an error
|
|
||||||
/// is returned.
|
|
||||||
pub fn fg(&mut self, intense: Intense, color: Color) -> io::Result<()> {
|
|
||||||
self.cur_attr.fg_color = color;
|
|
||||||
self.cur_attr.fg_intense = intense;
|
|
||||||
self.set()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Apply the given intensity and color attributes to the console
|
|
||||||
/// background.
|
|
||||||
///
|
|
||||||
/// If there was a problem setting attributes on the console, then an error
|
|
||||||
/// is returned.
|
|
||||||
pub fn bg(&mut self, intense: Intense, color: Color) -> io::Result<()> {
|
|
||||||
self.cur_attr.bg_color = color;
|
|
||||||
self.cur_attr.bg_intense = intense;
|
|
||||||
self.set()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Reset the console text attributes to their original settings.
|
|
||||||
///
|
|
||||||
/// The original settings correspond to the text attributes on the console
|
|
||||||
/// when this `Console` value was created.
|
|
||||||
///
|
|
||||||
/// If there was a problem setting attributes on the console, then an error
|
|
||||||
/// is returned.
|
|
||||||
pub fn reset(&mut self) -> io::Result<()> {
|
|
||||||
self.cur_attr = self.start_attr;
|
|
||||||
self.set()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Toggle virtual terminal processing.
|
|
||||||
///
|
|
||||||
/// This method attempts to toggle virtual terminal processing for this
|
|
||||||
/// console. If there was a problem toggling it, then an error returned.
|
|
||||||
/// On success, the caller may assume that toggling it was successful.
|
|
||||||
///
|
|
||||||
/// When virtual terminal processing is enabled, characters emitted to the
|
|
||||||
/// console are parsed for VT100 and similar control character sequences
|
|
||||||
/// that control color and other similar operations.
|
|
||||||
pub fn set_virtual_terminal_processing(
|
|
||||||
&mut self,
|
|
||||||
yes: bool,
|
|
||||||
) -> io::Result<()> {
|
|
||||||
let vt = wincon::ENABLE_VIRTUAL_TERMINAL_PROCESSING;
|
|
||||||
|
|
||||||
let mut old_mode = 0;
|
|
||||||
let handle = unsafe { processenv::GetStdHandle(self.handle_id) };
|
|
||||||
if unsafe { consoleapi::GetConsoleMode(handle, &mut old_mode) } == 0 {
|
|
||||||
return Err(io::Error::last_os_error());
|
|
||||||
}
|
|
||||||
let new_mode =
|
|
||||||
if yes {
|
|
||||||
old_mode | vt
|
|
||||||
} else {
|
|
||||||
old_mode & !vt
|
|
||||||
};
|
|
||||||
if old_mode == new_mode {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
if unsafe { consoleapi::SetConsoleMode(handle, new_mode) } == 0 {
|
|
||||||
return Err(io::Error::last_os_error());
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A representation of text attributes for the Windows console.
|
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
||||||
struct TextAttributes {
|
|
||||||
fg_color: Color,
|
|
||||||
fg_intense: Intense,
|
|
||||||
bg_color: Color,
|
|
||||||
bg_intense: Intense,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TextAttributes {
|
|
||||||
fn to_word(&self) -> WORD {
|
|
||||||
let mut w = 0;
|
|
||||||
w |= self.fg_color.to_fg();
|
|
||||||
w |= self.fg_intense.to_fg();
|
|
||||||
w |= self.bg_color.to_bg();
|
|
||||||
w |= self.bg_intense.to_bg();
|
|
||||||
w
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_word(word: WORD) -> TextAttributes {
|
|
||||||
TextAttributes {
|
|
||||||
fg_color: Color::from_fg(word),
|
|
||||||
fg_intense: Intense::from_fg(word),
|
|
||||||
bg_color: Color::from_bg(word),
|
|
||||||
bg_intense: Intense::from_bg(word),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether to use intense colors or not.
|
|
||||||
#[allow(missing_docs)]
|
|
||||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
||||||
pub enum Intense {
|
|
||||||
Yes,
|
|
||||||
No,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Intense {
|
|
||||||
fn to_bg(&self) -> WORD {
|
|
||||||
self.to_fg() << 4
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_bg(word: WORD) -> Intense {
|
|
||||||
Intense::from_fg(word >> 4)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_fg(&self) -> WORD {
|
|
||||||
match *self {
|
|
||||||
Intense::No => 0,
|
|
||||||
Intense::Yes => FG_INTENSITY,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_fg(word: WORD) -> Intense {
|
|
||||||
if word & FG_INTENSITY > 0 {
|
|
||||||
Intense::Yes
|
|
||||||
} else {
|
|
||||||
Intense::No
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The set of available colors for use with a Windows console.
|
|
||||||
#[allow(missing_docs)]
|
|
||||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
||||||
pub enum Color {
|
|
||||||
Black,
|
|
||||||
Blue,
|
|
||||||
Green,
|
|
||||||
Red,
|
|
||||||
Cyan,
|
|
||||||
Magenta,
|
|
||||||
Yellow,
|
|
||||||
White,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Color {
|
|
||||||
fn to_bg(&self) -> WORD {
|
|
||||||
self.to_fg() << 4
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_bg(word: WORD) -> Color {
|
|
||||||
Color::from_fg(word >> 4)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_fg(&self) -> WORD {
|
|
||||||
match *self {
|
|
||||||
Color::Black => 0,
|
|
||||||
Color::Blue => FG_BLUE,
|
|
||||||
Color::Green => FG_GREEN,
|
|
||||||
Color::Red => FG_RED,
|
|
||||||
Color::Cyan => FG_CYAN,
|
|
||||||
Color::Magenta => FG_MAGENTA,
|
|
||||||
Color::Yellow => FG_YELLOW,
|
|
||||||
Color::White => FG_WHITE,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_fg(word: WORD) -> Color {
|
|
||||||
match word & 0b111 {
|
|
||||||
FG_BLUE => Color::Blue,
|
|
||||||
FG_GREEN => Color::Green,
|
|
||||||
FG_RED => Color::Red,
|
|
||||||
FG_CYAN => Color::Cyan,
|
|
||||||
FG_MAGENTA => Color::Magenta,
|
|
||||||
FG_YELLOW => Color::Yellow,
|
|
||||||
FG_WHITE => Color::White,
|
|
||||||
_ => Color::Black,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user