diff --git a/Cargo.toml b/Cargo.toml index 6775f20..ed9d439 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ license = "EPL-2.0" [dependencies] serde = "1" serde_json = "1" -yansi = "0.5" +yansi = "1" [dev-dependencies] serde = { version = "1", features = ["derive"] } diff --git a/examples/color.rs b/examples/color.rs index 05dc633..9c4bd80 100644 --- a/examples/color.rs +++ b/examples/color.rs @@ -3,9 +3,6 @@ use colored_json; use colored_json::prelude::*; fn main() -> Result<(), Box> { - #[cfg(windows)] - let _enabled = colored_json::enable_ansi_support(); - println!( "{}", r#"{ diff --git a/src/lib.rs b/src/lib.rs index d567454..a858557 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,5 @@ //!colored_json crate to output colored serde json with ANSI terminal escape codes //! -//!**Note for Windows 10+ users:** On Windows 10+, the application must enable ANSI support first: -//! -//!```rust -//!#[cfg(windows)] -//!let enabled = colored_json::enable_ansi_support(); -//!``` -//! //!# Examples //! //!For everything, which implements `AsRef` @@ -74,12 +67,12 @@ //! "#.to_colored_json_with_styler( //! ColorMode::default().eval(), //! Styler { -//! key: Style::new(Color::Green), -//! string_value: Style::new(Color::Blue).bold(), -//! integer_value: Style::new(Color::Magenta).bold(), -//! float_value: Style::new(Color::Magenta).italic(), -//! object_brackets: Style::new(Color::Yellow).bold(), -//! array_brackets: Style::new(Color::Cyan).bold(), +//! key: Color::Green.foreground(), +//! string_value: Color::Blue.bold(), +//! integer_value: Color::Magenta.bold(), +//! float_value: Color::Magenta.italic(), +//! object_brackets: Color::Yellow.bold(), +//! array_brackets: Color::Cyan.bold(), //! ..Default::default() //! })? //! ); @@ -97,8 +90,8 @@ //! let f = ColoredFormatter::with_styler( //! CompactFormatter {}, //! Styler { -//! key: Style::new(Color::Green), -//! string_value: Style::new(Color::Blue).bold(), +//! key: Color::Green.foreground(), +//! string_value: Color::Blue.bold(), //! ..Default::default() //! }, //! ); @@ -131,28 +124,7 @@ pub use serde_json::ser::{CompactFormatter, PrettyFormatter}; use serde_json::value::Value; use std::io::{self, IsTerminal}; -pub use yansi::{Color, Style}; - -/// Enable ANSI support (on Windows). -/// -/// On Windows, the terminal needs to be put into an "ANSI mode" so that it will render colors. -/// This is not enabled by default, but this function will enable it for you. -/// -/// The function is also available on other platforms, but is a no-op in this case. So you can call -/// this function in any case. -/// -/// You can also directly call the function [`yansi::Paint::enable_windows_ascii`], or use any other means -/// of enabling the virtual ANSI console in Windows. Maybe some other part of your application -/// already does that. -#[allow(clippy::result_unit_err)] -#[inline] -pub fn enable_ansi_support() -> Result<(), ()> { - #[cfg(windows)] - if !yansi::Paint::enable_windows_ascii() { - return Err(()); - } - Ok(()) -} +pub use yansi::{Color, Paint, Style}; pub mod prelude { pub use crate::ColorMode; @@ -188,15 +160,15 @@ pub struct Styler { impl Default for Styler { fn default() -> Styler { Styler { - object_brackets: Style::default().bold(), - object_colon: Style::default(), - array_brackets: Style::default().bold(), - key: Style::default().fg(Color::Blue).bold(), - string_value: Style::default().fg(Color::Green), - integer_value: Style::default(), - float_value: Style::default(), - bool_value: Style::default(), - nil_value: Style::default(), + object_brackets: Style::new().bold(), + object_colon: Style::new(), + array_brackets: Style::new().bold(), + key: Color::Blue.bold(), + string_value: Color::Green.foreground(), + integer_value: Style::new(), + float_value: Style::new(), + bool_value: Style::new(), + nil_value: Style::new(), string_include_quotation: true, } } @@ -283,7 +255,7 @@ where if !w.is_empty() { let s = String::from_utf8_lossy(&w); - writer.write_all(style.paint(s).to_string().as_bytes())?; + writer.write_all(s.paint(style).to_string().as_bytes())?; } Ok(()) diff --git a/tests/test.rs b/tests/test.rs index cfe7e34..621628c 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -6,9 +6,6 @@ use std::result::Result; #[test] fn test_display_json_value() -> Result<(), Box> { - #[cfg(windows)] - let _res = enable_ansi_support(); - let data = json!({ "name": "John Doe", "age": 43, @@ -26,9 +23,6 @@ fn test_display_json_value() -> Result<(), Box> { #[test] fn test_trait() -> Result<(), Box> { - #[cfg(windows)] - let _res = enable_ansi_support(); - println!( "\n{}", r#"{ @@ -48,9 +42,6 @@ fn test_trait() -> Result<(), Box> { #[test] fn test_trait_err() -> Result<(), Box> { - #[cfg(windows)] - let _res = enable_ansi_support(); - eprintln!( "\n{}", r#"{ @@ -70,9 +61,6 @@ fn test_trait_err() -> Result<(), Box> { #[test] fn test_trait_color_off() -> Result<(), Box> { - #[cfg(windows)] - let _res = enable_ansi_support(); - println!( "\n{}", r#"{ @@ -92,9 +80,6 @@ fn test_trait_color_off() -> Result<(), Box> { #[test] fn test_trait_styler() -> Result<(), Box> { - #[cfg(windows)] - let _res = enable_ansi_support(); - println!( "\n{}", r#"{ @@ -110,12 +95,12 @@ fn test_trait_styler() -> Result<(), Box> { .to_colored_json_with_styler( ColorMode::default().eval(), Styler { - key: Style::new(Color::Green), - string_value: Style::new(Color::Blue).bold(), - integer_value: Style::new(Color::Magenta).bold(), - float_value: Style::new(Color::Magenta).italic(), - object_brackets: Style::new(Color::Yellow).bold(), - array_brackets: Style::new(Color::Cyan).bold(), + key: Color::Green.foreground(), + string_value: Color::Blue.bold(), + integer_value: Color::Magenta.bold(), + float_value: Color::Magenta.italic(), + object_brackets: Color::Yellow.bold(), + array_brackets: Color::Cyan.bold(), ..Default::default() }, )? @@ -125,9 +110,6 @@ fn test_trait_styler() -> Result<(), Box> { #[test] fn test_trait_styler_color_off() -> Result<(), Box> { - #[cfg(windows)] - let _res = enable_ansi_support(); - println!( "\n{}", r#"{ @@ -143,12 +125,12 @@ fn test_trait_styler_color_off() -> Result<(), Box> { .to_colored_json_with_styler( ColorMode::Off, Styler { - key: Style::new(Color::Green), - string_value: Style::new(Color::Blue).bold(), - integer_value: Style::new(Color::Magenta).bold(), - float_value: Style::new(Color::Magenta).italic(), - object_brackets: Style::new(Color::Yellow).bold(), - array_brackets: Style::new(Color::Cyan).bold(), + key: Color::Green.foreground(), + string_value: Color::Blue.bold(), + integer_value: Color::Magenta.bold(), + float_value: Color::Magenta.italic(), + object_brackets: Color::Yellow.bold(), + array_brackets: Color::Cyan.bold(), ..Default::default() }, )? @@ -158,9 +140,6 @@ fn test_trait_styler_color_off() -> Result<(), Box> { #[test] fn test_writer() -> Result<(), Box> { - #[cfg(windows)] - let _res = enable_ansi_support(); - let data = json!({ "name": "John Doe", "age": 43, @@ -181,18 +160,15 @@ fn test_writer() -> Result<(), Box> { #[test] fn test_styler() -> Result<(), Box> { - #[cfg(windows)] - let _res = enable_ansi_support(); - let f = ColoredFormatter::with_styler( PrettyFormatter::new(), Styler { - key: Style::new(Color::Green), - string_value: Style::new(Color::Blue).bold(), - integer_value: Style::new(Color::Magenta).bold(), - float_value: Style::new(Color::Magenta).italic(), - object_brackets: Style::new(Color::Yellow).bold(), - array_brackets: Style::new(Color::Cyan).bold(), + key: Color::Green.foreground(), + string_value: Color::Blue.bold(), + integer_value: Color::Magenta.bold(), + float_value: Color::Magenta.italic(), + object_brackets: Color::Yellow.bold(), + array_brackets: Color::Cyan.bold(), ..Default::default() }, ); @@ -222,18 +198,15 @@ fn test_styler() -> Result<(), Box> { #[test] fn test_styler_no_color() -> Result<(), Box> { - #[cfg(windows)] - let _res = enable_ansi_support(); - let f = ColoredFormatter::with_styler( PrettyFormatter::new(), Styler { - key: Style::new(Color::Green), - string_value: Style::new(Color::Blue).bold(), - integer_value: Style::new(Color::Magenta).bold(), - float_value: Style::new(Color::Magenta).italic(), - object_brackets: Style::new(Color::Yellow).bold(), - array_brackets: Style::new(Color::Cyan).bold(), + key: Color::Green.foreground(), + string_value: Color::Blue.bold(), + integer_value: Color::Magenta.bold(), + float_value: Color::Magenta.italic(), + object_brackets: Color::Yellow.bold(), + array_brackets: Color::Cyan.bold(), ..Default::default() }, ); @@ -266,15 +239,12 @@ fn test_styler_no_color() -> Result<(), Box> { #[test] fn test_styler_compact() -> Result<(), Box> { - #[cfg(windows)] - let _res = enable_ansi_support(); - let f = ColoredFormatter::with_styler( CompactFormatter {}, Styler { - key: Style::new(Color::Green), - string_value: Style::new(Color::Blue).bold(), - integer_value: Style::new(Color::Blue).bold(), + key: Color::Green.foreground(), + string_value: Color::Blue.bold(), + integer_value: Color::Blue.bold(), ..Default::default() }, ); @@ -303,9 +273,6 @@ fn test_styler_compact() -> Result<(), Box> { #[test] fn test_serializes() -> Result<(), Box> { - #[cfg(windows)] - let _res = enable_ansi_support(); - #[derive(serde::Serialize)] struct Data<'a> { name: &'a str,