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
#[cfg(windows)]
pub fn try_init() -> Result<(), ()> {
use winapi::shared::minwindef::DWORD;
use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode};
use winapi::um::processenv::GetStdHandle;
use winapi::um::winbase::STD_OUTPUT_HANDLE;
use winapi::um::wincon::{DISABLE_NEWLINE_AUTO_RETURN, ENABLE_VIRTUAL_TERMINAL_PROCESSING};
let console_out = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) };
let mut state: DWORD = 0;
let mut ret: Result<(), _> = Ok(());
unsafe {
let mut e: DWORD = 0;
if GetConsoleMode(console_out, &mut state) == 0 {
ret = Err(());
}
if ret.is_ok() {
state |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
state &= !DISABLE_NEWLINE_AUTO_RETURN;
if SetConsoleMode(console_out, state) == 0 {
ret = Err(());
}
}
}
return ret;
}
#[cfg(windows)]
pub fn init() {
assert_eq!(try_init().is_ok(), true);
}
#[cfg(not(windows))]
pub fn try_init() -> Result<(), ()> {
Ok(())
}
#[cfg(not(windows))]
pub fn init() {
;
}
#[cfg(test)]
mod tests {
#[test]
fn activate_vt100() {
crate::init();
}
}