This repository has been archived by the owner on Oct 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
winapi_color.rs
191 lines (153 loc) · 7.27 KB
/
winapi_color.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! This is a `WinApi` specific implementation for styling related action.
//! This module is used for non supporting `ANSI` Windows terminals.
use std::sync::Once;
use winapi::um::wincon;
use crossterm_utils::Result;
use crossterm_winapi::{Console, Handle, HandleType, ScreenBuffer};
use crate::{Color, Colored, ITerminalColor};
const FG_GREEN: u16 = wincon::FOREGROUND_GREEN;
const FG_RED: u16 = wincon::FOREGROUND_RED;
const FG_BLUE: u16 = wincon::FOREGROUND_BLUE;
const FG_INTENSITY: u16 = wincon::FOREGROUND_INTENSITY;
const BG_GREEN: u16 = wincon::BACKGROUND_GREEN;
const BG_RED: u16 = wincon::BACKGROUND_RED;
const BG_BLUE: u16 = wincon::BACKGROUND_BLUE;
const BG_INTENSITY: u16 = wincon::BACKGROUND_INTENSITY;
/// This struct is a WinApi implementation for color related actions.
pub struct WinApiColor;
impl WinApiColor {
pub fn new() -> Box<WinApiColor> {
Box::from(WinApiColor)
}
}
impl ITerminalColor for WinApiColor {
fn set_fg(&self, fg_color: Color) -> Result<()> {
// init the original color in case it is not set.
init_console_color()?;
let color_value = color_value(Colored::Fg(fg_color));
let screen_buffer = ScreenBuffer::current()?;
let csbi = screen_buffer.info()?;
// Notice that the color values are stored in wAttribute.
// So we need to use bitwise operators to check if the values exists or to get current console colors.
let mut color: u16;
let attrs = csbi.attributes();
let bg_color = attrs & 0x0070;
color = color_value.parse::<u16>()? | bg_color;
// background intensity is a separate value in attrs,
// wee need to check if this was applied to the current bg color.
if (attrs & wincon::BACKGROUND_INTENSITY as u16) != 0 {
color = color | wincon::BACKGROUND_INTENSITY as u16;
}
Console::from(**screen_buffer.handle()).set_text_attribute(color)?;
Ok(())
}
fn set_bg(&self, bg_color: Color) -> Result<()> {
// init the original color in case it is not set.
init_console_color()?;
let color_value = color_value(Colored::Bg(bg_color));
let screen_buffer = ScreenBuffer::current()?;
let csbi = screen_buffer.info()?;
// Notice that the color values are stored in wAttribute.
// So wee need to use bitwise operators to check if the values exists or to get current console colors.
let mut color: u16;
let attrs = csbi.attributes();
let fg_color = attrs & 0x0007;
color = fg_color | color_value.parse::<u16>()?;
// Foreground intensity is a separate value in attrs,
// So we need to check if this was applied to the current fg color.
if (attrs & wincon::FOREGROUND_INTENSITY as u16) != 0 {
color = color | wincon::FOREGROUND_INTENSITY as u16;
}
Console::from(**screen_buffer.handle()).set_text_attribute(color)?;
Ok(())
}
fn reset(&self) -> Result<()> {
// init the original color in case it is not set.
let original_color = original_console_color();
Console::from(Handle::new(HandleType::CurrentOutputHandle)?)
.set_text_attribute(original_color)?;
Ok(())
}
}
/// This will get the winapi color value from the Color and ColorType struct
fn color_value(color: Colored) -> String {
let winapi_color: u16;
match color {
Colored::Fg(color) => {
winapi_color = match color {
Color::Black => 0,
Color::DarkGrey => FG_INTENSITY,
Color::Red => FG_INTENSITY | FG_RED,
Color::DarkRed => FG_RED,
Color::Green => FG_INTENSITY | FG_GREEN,
Color::DarkGreen => FG_GREEN,
Color::Yellow => FG_INTENSITY | FG_GREEN | FG_RED,
Color::DarkYellow => FG_GREEN | FG_RED,
Color::Blue => FG_INTENSITY | FG_BLUE,
Color::DarkBlue => FG_BLUE,
Color::Magenta => FG_INTENSITY | FG_RED | FG_BLUE,
Color::DarkMagenta => FG_RED | FG_BLUE,
Color::Cyan => FG_INTENSITY | FG_GREEN | FG_BLUE,
Color::DarkCyan => FG_GREEN | FG_BLUE,
Color::White => FG_RED | FG_GREEN | FG_BLUE,
Color::Grey => FG_INTENSITY | FG_RED | FG_GREEN | FG_BLUE,
Color::Reset => {
// init the original color in case it is not set.
let mut original_color = original_console_color();
const REMOVE_BG_MASK: u16 = BG_INTENSITY | BG_RED | BG_GREEN | BG_BLUE;
// remove all background values from the original color, we don't want to reset those.
original_color &= !(REMOVE_BG_MASK);
original_color
}
/* WinApi will be used for systems that do not support ANSI, those are windows version less then 10. RGB and 255 (AnsiBValue) colors are not supported in that case.*/
Color::Rgb { r: _, g: _, b: _ } => 0,
Color::AnsiValue(_val) => 0,
};
}
Colored::Bg(color) => {
winapi_color = match color {
Color::Black => 0,
Color::DarkGrey => BG_INTENSITY,
Color::Red => BG_INTENSITY | BG_RED,
Color::DarkRed => BG_RED,
Color::Green => BG_INTENSITY | BG_GREEN,
Color::DarkGreen => BG_GREEN,
Color::Yellow => BG_INTENSITY | BG_GREEN | BG_RED,
Color::DarkYellow => BG_GREEN | BG_RED,
Color::Blue => BG_INTENSITY | BG_BLUE,
Color::DarkBlue => BG_BLUE,
Color::Magenta => BG_INTENSITY | BG_RED | BG_BLUE,
Color::DarkMagenta => BG_RED | BG_BLUE,
Color::Cyan => BG_INTENSITY | BG_GREEN | BG_BLUE,
Color::DarkCyan => BG_GREEN | BG_BLUE,
Color::White => BG_INTENSITY | BG_RED | BG_GREEN | BG_BLUE,
Color::Grey => BG_RED | BG_GREEN | BG_BLUE,
Color::Reset => {
// init the original color in case it is not set.
let mut original_color = original_console_color();
const REMOVE_FG_MASK: u16 = FG_INTENSITY | FG_RED | FG_GREEN | FG_BLUE;
// remove all foreground values from the original color, we don't want to reset those.
original_color &= !(REMOVE_FG_MASK);
original_color
}
/* WinApi will be used for systems that do not support ANSI, those are windows version less then 10. RGB and 255 (AnsiBValue) colors are not supported in that case.*/
Color::Rgb { r: _, g: _, b: _ } => 0,
Color::AnsiValue(_val) => 0,
};
}
};
winapi_color.to_string()
}
fn init_console_color() -> Result<()> {
let screen_buffer = ScreenBuffer::current()?;
let attr = screen_buffer.info()?.attributes();
GET_ORIGINAL_CONSOLE_COLOR.call_once(|| {
unsafe { ORIGINAL_CONSOLE_COLOR = attr };
});
Ok(())
}
fn original_console_color() -> u16 {
return unsafe { ORIGINAL_CONSOLE_COLOR };
}
static GET_ORIGINAL_CONSOLE_COLOR: Once = Once::new();
static mut ORIGINAL_CONSOLE_COLOR: u16 = 0;