mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-04-14 00:58:43 +02:00
wincolor: migrate to winapi 0.3
This commit is contained in:
parent
f8162d2707
commit
0d03145293
3
Cargo.lock
generated
3
Cargo.lock
generated
@ -395,8 +395,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
name = "wincolor"
|
||||
version = "0.1.4"
|
||||
dependencies = [
|
||||
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[metadata]
|
||||
|
@ -17,5 +17,4 @@ name = "wincolor"
|
||||
bench = false
|
||||
|
||||
[dependencies]
|
||||
kernel32-sys = "0.2.2"
|
||||
winapi = "0.2.8"
|
||||
winapi = { version = "0.3", features = ["minwindef", "processenv", "winbase", "wincon"] }
|
||||
|
@ -23,8 +23,6 @@ println!("This text will be normal.");
|
||||
|
||||
#![deny(missing_docs)]
|
||||
|
||||
#[cfg(windows)]
|
||||
extern crate kernel32;
|
||||
#[cfg(windows)]
|
||||
extern crate winapi;
|
||||
|
||||
|
@ -1,20 +1,21 @@
|
||||
use std::io;
|
||||
use std::mem;
|
||||
|
||||
use kernel32;
|
||||
use winapi::{DWORD, WORD};
|
||||
use winapi::winbase::{STD_ERROR_HANDLE, STD_OUTPUT_HANDLE};
|
||||
use winapi::wincon::{
|
||||
use winapi::shared::minwindef::{DWORD, WORD};
|
||||
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: DWORD = FG_BLUE | FG_GREEN;
|
||||
const FG_MAGENTA: DWORD = FG_BLUE | FG_RED;
|
||||
const FG_YELLOW: DWORD = FG_GREEN | FG_RED;
|
||||
const FG_WHITE: DWORD = FG_BLUE | FG_GREEN | FG_RED;
|
||||
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.
|
||||
///
|
||||
@ -40,8 +41,8 @@ impl Console {
|
||||
fn create_for_stream(handle_id: DWORD) -> io::Result<Console> {
|
||||
let mut info = unsafe { mem::zeroed() };
|
||||
let res = unsafe {
|
||||
let handle = kernel32::GetStdHandle(handle_id);
|
||||
kernel32::GetConsoleScreenBufferInfo(handle, &mut info)
|
||||
let handle = processenv::GetStdHandle(handle_id);
|
||||
wincon::GetConsoleScreenBufferInfo(handle, &mut info)
|
||||
};
|
||||
if res == 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
@ -72,8 +73,8 @@ impl Console {
|
||||
fn set(&mut self) -> io::Result<()> {
|
||||
let attr = self.cur_attr.to_word();
|
||||
let res = unsafe {
|
||||
let handle = kernel32::GetStdHandle(self.handle_id);
|
||||
kernel32::SetConsoleTextAttribute(handle, attr)
|
||||
let handle = processenv::GetStdHandle(self.handle_id);
|
||||
wincon::SetConsoleTextAttribute(handle, attr)
|
||||
};
|
||||
if res == 0 {
|
||||
return Err(io::Error::last_os_error());
|
||||
@ -132,16 +133,15 @@ impl TextAttributes {
|
||||
w |= self.fg_intense.to_fg();
|
||||
w |= self.bg_color.to_bg();
|
||||
w |= self.bg_intense.to_bg();
|
||||
w as WORD
|
||||
w
|
||||
}
|
||||
|
||||
fn from_word(word: WORD) -> TextAttributes {
|
||||
let attr = word as DWORD;
|
||||
TextAttributes {
|
||||
fg_color: Color::from_fg(attr),
|
||||
fg_intense: Intense::from_fg(attr),
|
||||
bg_color: Color::from_bg(attr),
|
||||
bg_intense: Intense::from_bg(attr),
|
||||
fg_color: Color::from_fg(word),
|
||||
fg_intense: Intense::from_fg(word),
|
||||
bg_color: Color::from_bg(word),
|
||||
bg_intense: Intense::from_bg(word),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -155,22 +155,22 @@ pub enum Intense {
|
||||
}
|
||||
|
||||
impl Intense {
|
||||
fn to_bg(&self) -> DWORD {
|
||||
fn to_bg(&self) -> WORD {
|
||||
self.to_fg() << 4
|
||||
}
|
||||
|
||||
fn from_bg(word: DWORD) -> Intense {
|
||||
fn from_bg(word: WORD) -> Intense {
|
||||
Intense::from_fg(word >> 4)
|
||||
}
|
||||
|
||||
fn to_fg(&self) -> DWORD {
|
||||
fn to_fg(&self) -> WORD {
|
||||
match *self {
|
||||
Intense::No => 0,
|
||||
Intense::Yes => FG_INTENSITY,
|
||||
}
|
||||
}
|
||||
|
||||
fn from_fg(word: DWORD) -> Intense {
|
||||
fn from_fg(word: WORD) -> Intense {
|
||||
if word & FG_INTENSITY > 0 {
|
||||
Intense::Yes
|
||||
} else {
|
||||
@ -194,15 +194,15 @@ pub enum Color {
|
||||
}
|
||||
|
||||
impl Color {
|
||||
fn to_bg(&self) -> DWORD {
|
||||
fn to_bg(&self) -> WORD {
|
||||
self.to_fg() << 4
|
||||
}
|
||||
|
||||
fn from_bg(word: DWORD) -> Color {
|
||||
fn from_bg(word: WORD) -> Color {
|
||||
Color::from_fg(word >> 4)
|
||||
}
|
||||
|
||||
fn to_fg(&self) -> DWORD {
|
||||
fn to_fg(&self) -> WORD {
|
||||
match *self {
|
||||
Color::Black => 0,
|
||||
Color::Blue => FG_BLUE,
|
||||
@ -215,7 +215,7 @@ impl Color {
|
||||
}
|
||||
}
|
||||
|
||||
fn from_fg(word: DWORD) -> Color {
|
||||
fn from_fg(word: WORD) -> Color {
|
||||
match word & 0b111 {
|
||||
FG_BLUE => Color::Blue,
|
||||
FG_GREEN => Color::Green,
|
||||
|
Loading…
x
Reference in New Issue
Block a user