From b27899a223c0f4f0dc1a5a8aad09df1de30b7f07 Mon Sep 17 00:00:00 2001 From: Atsukoro1 Date: Mon, 5 Dec 2022 22:42:19 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Enabling=20ansii=20suppor?= =?UTF-8?q?t=20handle=20on=20my=20won?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 2 +- src/helpers/colors.rs | 8 +++----- src/helpers/console.rs | 26 ++++++++++++++++++++++++++ src/helpers/mod.rs | 3 ++- 4 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 src/helpers/console.rs diff --git a/Cargo.toml b/Cargo.toml index fef10f7..bd0b4ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,4 @@ include = [ ] [dependencies] -ansi_term = "0.12.1" \ No newline at end of file +kernel32-sys = "0.2.2" \ No newline at end of file diff --git a/src/helpers/colors.rs b/src/helpers/colors.rs index b3ddd54..0b3f0b1 100644 --- a/src/helpers/colors.rs +++ b/src/helpers/colors.rs @@ -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] = [ ("black", "\u{001b}[30m"), ("red", "\u{001b}[31m"), @@ -42,10 +43,7 @@ pub fn print(text: &str, inline: bool, color: &str) { ).unwrap().1; #[cfg(windows)] - { - use ansi_term::enable_ansi_support; - enable_ansi_support().unwrap(); - } + enable_ansi_support(); if inline { print!("{}{}", color, text); diff --git a/src/helpers/console.rs b/src/helpers/console.rs new file mode 100644 index 0000000..af2208e --- /dev/null +++ b/src/helpers/console.rs @@ -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; + } + } +} \ No newline at end of file diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 2ad3c2f..7952e51 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -3,4 +3,5 @@ pub mod file; pub mod colors; pub mod print; pub mod paths; -pub mod ponies; \ No newline at end of file +pub mod ponies; +pub mod console; \ No newline at end of file