1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-04-24 17:12:16 +02:00

wincolor: migrate to winapi 0.3

This commit is contained in:
Steffen Butzer 2017-12-29 23:53:18 +01:00 committed by Andrew Gallant
parent f8162d2707
commit 0d03145293
4 changed files with 28 additions and 32 deletions

3
Cargo.lock generated
View File

@ -395,8 +395,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "wincolor" name = "wincolor"
version = "0.1.4" version = "0.1.4"
dependencies = [ dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[metadata] [metadata]

View File

@ -17,5 +17,4 @@ name = "wincolor"
bench = false bench = false
[dependencies] [dependencies]
kernel32-sys = "0.2.2" winapi = { version = "0.3", features = ["minwindef", "processenv", "winbase", "wincon"] }
winapi = "0.2.8"

View File

@ -23,8 +23,6 @@ println!("This text will be normal.");
#![deny(missing_docs)] #![deny(missing_docs)]
#[cfg(windows)]
extern crate kernel32;
#[cfg(windows)] #[cfg(windows)]
extern crate winapi; extern crate winapi;

View File

@ -1,20 +1,21 @@
use std::io; use std::io;
use std::mem; use std::mem;
use kernel32; use winapi::shared::minwindef::{DWORD, WORD};
use winapi::{DWORD, WORD}; use winapi::um::processenv;
use winapi::winbase::{STD_ERROR_HANDLE, STD_OUTPUT_HANDLE}; use winapi::um::winbase::{STD_ERROR_HANDLE, STD_OUTPUT_HANDLE};
use winapi::wincon::{ use winapi::um::wincon::{
self,
FOREGROUND_BLUE as FG_BLUE, FOREGROUND_BLUE as FG_BLUE,
FOREGROUND_GREEN as FG_GREEN, FOREGROUND_GREEN as FG_GREEN,
FOREGROUND_RED as FG_RED, FOREGROUND_RED as FG_RED,
FOREGROUND_INTENSITY as FG_INTENSITY, FOREGROUND_INTENSITY as FG_INTENSITY,
}; };
const FG_CYAN: DWORD = FG_BLUE | FG_GREEN; const FG_CYAN: WORD = FG_BLUE | FG_GREEN;
const FG_MAGENTA: DWORD = FG_BLUE | FG_RED; const FG_MAGENTA: WORD = FG_BLUE | FG_RED;
const FG_YELLOW: DWORD = FG_GREEN | FG_RED; const FG_YELLOW: WORD = FG_GREEN | FG_RED;
const FG_WHITE: DWORD = FG_BLUE | FG_GREEN | FG_RED; const FG_WHITE: WORD = FG_BLUE | FG_GREEN | FG_RED;
/// A Windows console. /// A Windows console.
/// ///
@ -40,8 +41,8 @@ impl Console {
fn create_for_stream(handle_id: DWORD) -> io::Result<Console> { fn create_for_stream(handle_id: DWORD) -> io::Result<Console> {
let mut info = unsafe { mem::zeroed() }; let mut info = unsafe { mem::zeroed() };
let res = unsafe { let res = unsafe {
let handle = kernel32::GetStdHandle(handle_id); let handle = processenv::GetStdHandle(handle_id);
kernel32::GetConsoleScreenBufferInfo(handle, &mut info) wincon::GetConsoleScreenBufferInfo(handle, &mut info)
}; };
if res == 0 { if res == 0 {
return Err(io::Error::last_os_error()); return Err(io::Error::last_os_error());
@ -72,8 +73,8 @@ impl Console {
fn set(&mut self) -> io::Result<()> { fn set(&mut self) -> io::Result<()> {
let attr = self.cur_attr.to_word(); let attr = self.cur_attr.to_word();
let res = unsafe { let res = unsafe {
let handle = kernel32::GetStdHandle(self.handle_id); let handle = processenv::GetStdHandle(self.handle_id);
kernel32::SetConsoleTextAttribute(handle, attr) wincon::SetConsoleTextAttribute(handle, attr)
}; };
if res == 0 { if res == 0 {
return Err(io::Error::last_os_error()); return Err(io::Error::last_os_error());
@ -132,16 +133,15 @@ impl TextAttributes {
w |= self.fg_intense.to_fg(); w |= self.fg_intense.to_fg();
w |= self.bg_color.to_bg(); w |= self.bg_color.to_bg();
w |= self.bg_intense.to_bg(); w |= self.bg_intense.to_bg();
w as WORD w
} }
fn from_word(word: WORD) -> TextAttributes { fn from_word(word: WORD) -> TextAttributes {
let attr = word as DWORD;
TextAttributes { TextAttributes {
fg_color: Color::from_fg(attr), fg_color: Color::from_fg(word),
fg_intense: Intense::from_fg(attr), fg_intense: Intense::from_fg(word),
bg_color: Color::from_bg(attr), bg_color: Color::from_bg(word),
bg_intense: Intense::from_bg(attr), bg_intense: Intense::from_bg(word),
} }
} }
} }
@ -155,22 +155,22 @@ pub enum Intense {
} }
impl Intense { impl Intense {
fn to_bg(&self) -> DWORD { fn to_bg(&self) -> WORD {
self.to_fg() << 4 self.to_fg() << 4
} }
fn from_bg(word: DWORD) -> Intense { fn from_bg(word: WORD) -> Intense {
Intense::from_fg(word >> 4) Intense::from_fg(word >> 4)
} }
fn to_fg(&self) -> DWORD { fn to_fg(&self) -> WORD {
match *self { match *self {
Intense::No => 0, Intense::No => 0,
Intense::Yes => FG_INTENSITY, Intense::Yes => FG_INTENSITY,
} }
} }
fn from_fg(word: DWORD) -> Intense { fn from_fg(word: WORD) -> Intense {
if word & FG_INTENSITY > 0 { if word & FG_INTENSITY > 0 {
Intense::Yes Intense::Yes
} else { } else {
@ -194,15 +194,15 @@ pub enum Color {
} }
impl Color { impl Color {
fn to_bg(&self) -> DWORD { fn to_bg(&self) -> WORD {
self.to_fg() << 4 self.to_fg() << 4
} }
fn from_bg(word: DWORD) -> Color { fn from_bg(word: WORD) -> Color {
Color::from_fg(word >> 4) Color::from_fg(word >> 4)
} }
fn to_fg(&self) -> DWORD { fn to_fg(&self) -> WORD {
match *self { match *self {
Color::Black => 0, Color::Black => 0,
Color::Blue => FG_BLUE, 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 { match word & 0b111 {
FG_BLUE => Color::Blue, FG_BLUE => Color::Blue,
FG_GREEN => Color::Green, FG_GREEN => Color::Green,