clicolors-control-1.0.1/.gitignore010064400007650000024000000000221353004621500153600ustar0000000000000000target Cargo.lock clicolors-control-1.0.1/Cargo.toml.orig010064400007650000024000000012571353004622400162720ustar0000000000000000[package] name = "clicolors-control" description = "A common utility library to control CLI colorization" version = "1.0.1" authors = ["Armin Ronacher "] keywords = ["cli", "clicolor", "clicolors", "colors"] license = "MIT" repository = "https://github.com/mitsuhiko/clicolors-control" documentation = "https://docs.rs/clicolors-control" readme = "README.md" [features] default = ["terminal_autoconfig"] terminal_autoconfig = [] [dependencies] lazy_static = "1" [target.'cfg(windows)'.dependencies] winapi = { version = "0.3", features = ["winbase","handleapi","consoleapi","processenv"] } atty = "0.2.11" [target.'cfg(unix)'.dependencies] libc = "0.2" clicolors-control-1.0.1/Cargo.toml0000644000000023610000000000000125410ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "clicolors-control" version = "1.0.1" authors = ["Armin Ronacher "] description = "A common utility library to control CLI colorization" documentation = "https://docs.rs/clicolors-control" readme = "README.md" keywords = ["cli", "clicolor", "clicolors", "colors"] license = "MIT" repository = "https://github.com/mitsuhiko/clicolors-control" [dependencies.lazy_static] version = "1" [features] default = ["terminal_autoconfig"] terminal_autoconfig = [] [target."cfg(unix)".dependencies.libc] version = "0.2" [target."cfg(windows)".dependencies.atty] version = "0.2.11" [target."cfg(windows)".dependencies.winapi] version = "0.3" features = ["winbase", "handleapi", "consoleapi", "processenv"] clicolors-control-1.0.1/Cargo.toml.orig0000644000000023620000000000000135010ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "clicolors-control" version = "1.0.1" authors = ["Armin Ronacher "] description = "A common utility library to control CLI colorization" documentation = "https://docs.rs/clicolors-control" readme = "README.md" keywords = ["cli", "clicolor", "clicolors", "colors"] license = "MIT" repository = "https://github.com/mitsuhiko/clicolors-control" [dependencies.lazy_static] version = "1" [features] default = ["terminal_autoconfig"] terminal_autoconfig = [] [target."cfg(unix)".dependencies.libc] version = "0.2" [target."cfg(windows)".dependencies.atty] version = "0.2.11" [target."cfg(windows)".dependencies.winapi] version = "0.3" features = ["winbase", "handleapi", "consoleapi", "processenv"] clicolors-control-1.0.1/CHANGELOG.md010064400007650000024000000000551353004624600152130ustar0000000000000000# Changelog ## 1.0.1 * Added WASM support. clicolors-control-1.0.1/README.md010064400007650000024000000001671353004621500146610ustar0000000000000000# clicolors-control A utility library for Rust that acts as a common place to control the colorization for CLI tools. clicolors-control-1.0.1/src/common.rs010064400007650000024000000042061353004621500160250ustar0000000000000000use std::env; use std::sync::atomic::{AtomicBool, Ordering}; #[cfg(unix)] pub use unix::*; #[cfg(windows)] pub use windows::*; #[cfg(all(not(unix), not(windows)))] pub use generic::*; /// Returns the default value for `colors_enabled`. pub fn enable_colors_by_default() -> bool { (is_a_color_terminal() && &env::var("CLICOLOR").unwrap_or("1".into()) != "0") || &env::var("CLICOLOR_FORCE").unwrap_or("0".into()) != "0" } lazy_static! { static ref ENABLE_COLORS: AtomicBool = AtomicBool::new(enable_colors_by_default()); } /// Returns `true` if colors should be enabled. /// /// This honors the [clicolors spec](http://bixense.com/clicolors/). /// /// * `CLICOLOR != 0`: ANSI colors are supported and should be used when the program isn't piped. /// * `CLICOLOR == 0`: Don't output ANSI color escape codes. /// * `CLICOLOR_FORCE != 0`: ANSI colors should be enabled no matter what. pub fn colors_enabled() -> bool { ENABLE_COLORS.load(Ordering::Relaxed) } /// Forces colorization on or off. /// /// This overrides the default for the current process and changes the return value of the /// `colors_enabled` function. pub fn set_colors_enabled(val: bool) { ENABLE_COLORS.store(val, Ordering::Relaxed) } /// Configures the terminal for ANSI color support. /// /// This is not needed on UNIX systems and normally automatically happens on windows /// the first time `colors_enabled()` is called. This automatic behavior however /// can be disabled by removing the `terminal_autoconfig` feature flag. /// /// When this function is called and the terminal was reconfigured, changes from /// `set_colors_enabled` are reverted. /// /// It returns `true` if the terminal supports colors after configuration or /// `false` if not. pub fn configure_terminal() -> bool { #[cfg(windows)] { if enable_ansi_mode() { // if the terminal is configured we override the cached colors value // with the default as otherwise we might have a wrong value. set_colors_enabled(enable_colors_by_default()); true } else { false } } #[cfg(not(windows))] { true } } clicolors-control-1.0.1/src/generic.rs010064400007650000024000000001411353004621500161430ustar0000000000000000pub fn is_a_terminal() -> bool { false } pub fn is_a_color_terminal() -> bool { false } clicolors-control-1.0.1/src/lib.rs010064400007650000024000000033361353004621500153060ustar0000000000000000//! This library implements basic [clicolor](http://bixense.com/clicolors/) control for //! other rust libraries. The idea is that other crates can depend on this to have a //! central source of truth for the colorization of command line applications. //! //! it follows the cli color specification: //! //! * `CLICOLOR != 0`: ANSI colors are supported and should be used when the program isn't piped. //! * `CLICOLOR == 0`: Don't output ANSI color escape codes. //! * `CLICOLOR_FORCE != 0`: ANSI colors should be enabled no matter what. //! //! ## Example Usage //! //! ```rust //! extern crate clicolors_control; //! //! pub fn main() { //! if clicolors_control::colors_enabled() { //! println!("\x1b[36mThis is colored text.\x1b[0m"); //! } else { //! println!("Someone turned off the colors :()") //! } //! } //! ``` //! //! ## Controlling Colors //! //! Colors can be turned on and off for the current process with `set_colors_enabled`. //! //! ## Windows 10 Console //! //! The default behavior of this crate is to reconfigure the windows console to enable the //! VT100 emulation when available the first time colors are requested. This will only work //! on recent Windows 10 versions. This feature can be disabled by removing the default //! `terminal_autoconfig` feature. //! //! The terminal can be manually configured for colors by calling `configure_terminal()` #[cfg(windows)] extern crate atty; #[cfg(unix)] extern crate libc; #[cfg(windows)] extern crate winapi; #[macro_use] extern crate lazy_static; mod common; pub mod terminfo; #[cfg(unix)] mod unix; #[cfg(windows)] mod windows; #[cfg(all(not(unix), not(windows)))] mod generic; pub use common::{colors_enabled, configure_terminal, set_colors_enabled}; clicolors-control-1.0.1/src/terminfo.rs010064400007650000024000000007401353004621500163570ustar0000000000000000//! Auxiliary terminal information. //! //! These are internal functions exported to come to the same conclusions as //! clicolors-control about terminal and color support if that is wanted. use common; /// Returns `true` if colors are supported by this terminal. pub fn supports_colors() -> bool { common::is_a_color_terminal() } /// Returns `true` if a terminal is connected. /// /// This uses a best effort check pub fn is_atty() -> bool { common::is_a_terminal() } clicolors-control-1.0.1/src/unix.rs010064400007650000024000000003061353004621500155150ustar0000000000000000use libc; #[inline(always)] pub fn is_a_terminal() -> bool { unsafe { libc::isatty(libc::STDOUT_FILENO) == 1 } } #[inline(always)] pub fn is_a_color_terminal() -> bool { is_a_terminal() } clicolors-control-1.0.1/src/windows.rs010064400007650000024000000057151353004621500162350ustar0000000000000000use atty; use std::env; use std::mem; use std::slice; use winapi::ctypes::c_void; use winapi::shared::minwindef::MAX_PATH; use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode}; use winapi::um::fileapi::FILE_NAME_INFO; use winapi::um::handleapi::INVALID_HANDLE_VALUE; use winapi::um::minwinbase::FileNameInfo; use winapi::um::processenv::GetStdHandle; use winapi::um::winbase::GetFileInformationByHandleEx; use winapi::um::winbase::{STD_ERROR_HANDLE, STD_OUTPUT_HANDLE}; use winapi::um::winnt::WCHAR; const ENABLE_VIRTUAL_TERMINAL_PROCESSING: u32 = 0x4; pub fn is_a_terminal() -> bool { atty::is(atty::Stream::Stdout) } #[cfg(feature = "terminal_autoconfig")] pub fn is_a_color_terminal() -> bool { if !is_a_terminal() { return false; } if msys_tty_on_stdout() { return msys_color_check(); } enable_ansi_mode() } #[cfg(not(feature = "terminal_autoconfig"))] pub fn is_a_color_terminal() -> bool { if msys_tty_on_stdout() { return msys_color_check(); } false } fn msys_color_check() -> bool { match env::var("TERM") { Ok(term) => term != "dumb", Err(_) => true, } } fn enable_ansi_on(handle: u32) -> bool { unsafe { let handle = GetStdHandle(handle); if handle == INVALID_HANDLE_VALUE { return false; } let mut dw_mode = 0; if GetConsoleMode(handle, &mut dw_mode) == 0 { return false; } dw_mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; if SetConsoleMode(handle, dw_mode) == 0 { return false; } true } } pub fn enable_ansi_mode() -> bool { enable_ansi_on(STD_OUTPUT_HANDLE) || enable_ansi_on(STD_ERROR_HANDLE) } pub fn msys_tty_on_stdout() -> bool { unsafe { let handle = GetStdHandle(STD_OUTPUT_HANDLE); let size = mem::size_of::(); let mut name_info_bytes = vec![0u8; size + MAX_PATH * mem::size_of::()]; let res = GetFileInformationByHandleEx( handle as *mut _, FileNameInfo, &mut *name_info_bytes as *mut _ as *mut c_void, name_info_bytes.len() as u32, ); if res == 0 { return false; } let name_info: &FILE_NAME_INFO = &*(name_info_bytes.as_ptr() as *const FILE_NAME_INFO); let s = slice::from_raw_parts( name_info.FileName.as_ptr(), name_info.FileNameLength as usize / 2, ); let name = String::from_utf16_lossy(s); // This checks whether 'pty' exists in the file name, which indicates that // a pseudo-terminal is attached. To mitigate against false positives // (e.g., an actual file name that contains 'pty'), we also require that // either the strings 'msys-' or 'cygwin-' are in the file name as well.) let is_msys = name.contains("msys-") || name.contains("cygwin-"); let is_pty = name.contains("-pty"); is_msys && is_pty } } clicolors-control-1.0.1/.cargo_vcs_info.json0000644000000001120000000000000145330ustar00{ "git": { "sha1": "3b3876b9bac7509c2049c29fa4c1a8d11b0aa0b3" } } clicolors-control-1.0.1/Cargo.lock0000644000000047050000000000000125220ustar00# This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] name = "atty" version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "clicolors-control" version = "1.0.1" dependencies = [ "atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "lazy_static" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "libc" version = "0.2.62" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "winapi" version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "1803c647a3ec87095e7ae7acfca019e98de5ec9a7d01343f611cf3152ed71a90" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" "checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba" "checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"