output_vt100-0.1.3/.cargo_vcs_info.json0000644000000001360000000000100133270ustar { "git": { "sha1": "65466dd0c6be479acf9cb76822f22435a4b026a4" }, "path_in_vcs": "" }output_vt100-0.1.3/.drone.yml000064400000000000000000000005130072674642500140560ustar 00000000000000kind: pipeline type: docker name: rust-stable steps: - name: stable image: rust:latest commands: - cargo build --verbose - cargo test --- kind: pipeline type: docker name: rust-nightly steps: - name: nightly image: rustlang/rust:nightly commands: - cargo build --verbose - cargo test output_vt100-0.1.3/.gitignore000064400000000000000000000000350072674642500141350ustar 00000000000000/target **/*.rs.bk Cargo.lockoutput_vt100-0.1.3/Cargo.lock0000644000000015610000000000100113050ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "output_vt100" version = "0.1.3" dependencies = [ "winapi", ] [[package]] name = "winapi" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" output_vt100-0.1.3/Cargo.toml0000644000000016550000000000100113340ustar # 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 are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "output_vt100" version = "0.1.3" authors = ["Phuntsok Drak-pa "] description = "Utility to activate escape codes in Windows' CMD and PowerShell" readme = "README.md" keywords = ["vt100", "console", "ansi"] categories = ["development-tools"] license = "MIT" repository = "https://github.com/Phundrak/output-vt100-rs" [dependencies.winapi] version = "0.3.6" features = ["winuser", "winbase", "consoleapi", "processenv"] output_vt100-0.1.3/Cargo.toml.orig000064400000000000000000000007420072674642500150410ustar 00000000000000[package] name = "output_vt100" version = "0.1.3" authors = ["Phuntsok Drak-pa "] edition = "2018" description = "Utility to activate escape codes in Windows' CMD and PowerShell" repository = "https://github.com/Phundrak/output-vt100-rs" keywords = ["vt100", "console", "ansi"] readme = "README.md" license = "MIT" categories = ["development-tools"] [dependencies] winapi = { version = "0.3.6", features = ["winuser", "winbase", "consoleapi", "processenv"] } output_vt100-0.1.3/LICENSE000064400000000000000000000021130072674642500131510ustar 00000000000000The MIT License (MIT) Copyright (c) 2016 rust-derive-builder contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. output_vt100-0.1.3/README.md000064400000000000000000000033210072674642500134250ustar 00000000000000[![crates.io](https://img.shields.io/crates/v/output_vt100.svg?style=flat)](https://crates.io/crates/output_vt100) [![Released API docs](https://docs.rs/output_vt100/badge.svg)](https://docs.rs/output_vt100) [![Downloads](https://img.shields.io/crates/d/output_vt100.svg?style=flat)](https://crates.io/crates/output_vt100) [![MIT Licensed](https://img.shields.io/crates/l/output_vt100.svg?style=flat)](https://crates.io/crates/output_vt100) [![AppVeyor CI](https://img.shields.io/appveyor/ci/Phundrak/output-vt100-rs.svg?style=flat)](https://ci.appveyor.com/project/Phundrak/output-vt100-rs) [![Build Status](https://drone.phundrak.com/api/badges/phundrak/output-vt100-rs/status.svg)](https://drone.phundrak.com/phundrak/output-vt100-rs) # Output-VT100 This simple crates allows developers to enable ANSI escape characters in Windows' console, be it CMD or PowerShell. Its usage is very simple, as shown below: ```rust extern crate output_vt100; fn main() { output_vt100::init(); println!("\x1b[31mThis text is red!\x1b[0m"); } ``` If you wish to ensure the `output_vt100::init()` function is only ran once, you can use the crate [ctor](https://crates.io/crates/ctor). Be aware though it might not be suited for every use case, as explained on the crate’s presentation. ```rust extern crate output_vt100; extern crate ctor; use ctor::*; #[ctor] fn init_term() { output_vt100::init(); } fn main() { println!("\x1b[31mThis text is red!\x1b[0m"); } ``` Not that init panics on error, if you do not wish to panic, use `output_vt100::try_init` which returns a `Result<(), ()>` # Acknowledgements A big thank you to [nbouteme](https://github.com/nbouteme) who helped me a lot during the development of this create. output_vt100-0.1.3/appveyor.yml000064400000000000000000000035560072674642500145500ustar 00000000000000# Appveyor configuration template for Rust using rustup for Rust installation # https://github.com/starkat99/appveyor-rust ## Operating System (VM environment) ## # Rust needs at least Visual Studio 2013 Appveyor OS for MSVC targets. os: Visual Studio 2015 ## Build Matrix ## environment: matrix: ### MSVC Toolchains ### # Stable 64-bit MSVC - channel: stable target: x86_64-pc-windows-msvc # Stable 32-bit MSVC - channel: stable target: i686-pc-windows-msvc # Beta 64-bit MSVC - channel: beta target: x86_64-pc-windows-msvc # Beta 32-bit MSVC - channel: beta target: i686-pc-windows-msvc # Nightly 64-bit MSVC - channel: nightly target: x86_64-pc-windows-msvc # Nightly 32-bit MSVC - channel: nightly target: i686-pc-windows-msvc ### GNU Toolchains ### # Stable 64-bit GNU - channel: stable target: x86_64-pc-windows-gnu # Stable 32-bit GNU - channel: stable target: i686-pc-windows-gnu # Beta 64-bit GNU - channel: beta target: x86_64-pc-windows-gnu # Beta 32-bit GNU - channel: beta target: i686-pc-windows-gnu # Nightly 64-bit GNU - channel: nightly target: x86_64-pc-windows-gnu # Nightly 32-bit GNU - channel: nightly target: i686-pc-windows-gnu ## Install Script ## install: - appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe - rustup-init -yv --default-toolchain %channel% --default-host %target% - set PATH=%PATH%;%USERPROFILE%\.cargo\bin - rustc -vV - cargo -vV ## Build Script ## # This prevents the "directory does not contain a project or solution file" # error. build: false # Uses 'cargo build' to build. AppVeyor cannot run tests on this crate as it # redirects the console's output, and its state then cannot be retrieved, let # alone modified. test_script: - cargo build --verbose %cargoflags% output_vt100-0.1.3/examples/red-hello.rs000064400000000000000000000001660072674642500162110ustar 00000000000000extern crate output_vt100; fn main() { output_vt100::init(); println!("\x1b[31mThis text is red!\x1b[0m"); } output_vt100-0.1.3/src/lib.rs000064400000000000000000000047730072674642500140650ustar 00000000000000//! # Output-VT100 //! //! When you write terminal-based crates, sometimes you might want to use the //! standard ANSI escaped characters, to display some colors, to display text //! as bold, italic or whatever. However, you’ve just discovered all your //! pretty displays that worked like a charm on Linux and Mac look terrible //! on Windows, because the escaped characters do not work. Rather, they are //! not activated by default. Then you discover you have to do system calls to //! Windows directly to activate them in order to get your beautiful text back. //! What a pain! //! And this is where this crate comes in action! Simply add it as a dependency //! for your own crate, and use it like this: //! ```rust //! extern crate output_vt100; //! //! fn main() { //! output_vt100::init(); //! println!("\x1b[31mThis text is red!\x1b[0m"); //! } //! ``` //! And that’s it! By calling it once, you have now activated PowerShell’s and //! CMD’s support for ANSI’s escaped characters on your Windows builds! And //! you can leave this line in your Unix builds too, it will simply do nothing. use std::fmt; #[derive(Debug)] pub struct InitError; impl fmt::Display for InitError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "output-vt100-rs: Could not initialize") } } impl std::error::Error for InitError {} #[cfg(windows)] pub fn try_init() -> Result<(), InitError> { 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 { if GetConsoleMode(console_out, &mut state) == 0 { ret = Err(InitError); } if ret.is_ok() { state |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; state &= !DISABLE_NEWLINE_AUTO_RETURN; if SetConsoleMode(console_out, state) == 0 { ret = Err(InitError); } } } return ret; } #[cfg(windows)] pub fn init() { assert_eq!(try_init().is_ok(), true); } #[cfg(not(windows))] pub fn try_init() -> Result<(), InitError> { Ok(()) } #[cfg(not(windows))] pub fn init() {} #[cfg(test)] mod tests { #[test] fn activate_vt100() { crate::init(); } }