fix: 🐛 Enabling ansii support handle on my won

This commit is contained in:
Atsukoro1 2022-12-05 22:42:19 +01:00
parent 58b059bcbf
commit b27899a223
4 changed files with 32 additions and 7 deletions

View file

@ -13,4 +13,4 @@ include = [
] ]
[dependencies] [dependencies]
ansi_term = "0.12.1" kernel32-sys = "0.2.2"

View file

@ -1,4 +1,5 @@
// #[cfg(any(target_os = "linux", target_os = "macos"))] use crate::helpers::console::enable_ansi_support;
pub const COLORS : [(&str, &str); 34] = [ pub const COLORS : [(&str, &str); 34] = [
("black", "\u{001b}[30m"), ("black", "\u{001b}[30m"),
("red", "\u{001b}[31m"), ("red", "\u{001b}[31m"),
@ -42,10 +43,7 @@ pub fn print(text: &str, inline: bool, color: &str) {
).unwrap().1; ).unwrap().1;
#[cfg(windows)] #[cfg(windows)]
{ enable_ansi_support();
use ansi_term::enable_ansi_support;
enable_ansi_support().unwrap();
}
if inline { if inline {
print!("{}{}", color, text); print!("{}{}", color, text);

26
src/helpers/console.rs Normal file
View file

@ -0,0 +1,26 @@
#[cfg(target_os = "windows")]
pub fn enable_ansi_support() {
use std::os::windows::prelude::AsRawHandle;
const ENABLE_V_TERM_PROCESSING: u32 = 0x0004;
let stdout = std::io::stdout();
let handle = stdout.lock();
let mut mode = 0;
unsafe {
let handle = handle.as_raw_handle();
let result = kernel32::GetConsoleMode(handle, &mut mode);
if result == 0 {
return;
}
mode |= ENABLE_V_TERM_PROCESSING;
let result = kernel32::SetConsoleMode(handle, mode);
if result == 0 {
return;
}
}
}

View file

@ -4,3 +4,4 @@ pub mod colors;
pub mod print; pub mod print;
pub mod paths; pub mod paths;
pub mod ponies; pub mod ponies;
pub mod console;