Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bump yansi to 1, remove enable_ansi_support #10

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
3 changes: 0 additions & 3 deletions examples/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ use colored_json;
use colored_json::prelude::*;

fn main() -> Result<(), Box<dyn ::std::error::Error>> {
#[cfg(windows)]
let _enabled = colored_json::enable_ansi_support();

println!(
"{}",
r#"{
Expand Down
66 changes: 19 additions & 47 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<str>`
Expand Down Expand Up @@ -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()
//! })?
//! );
Expand All @@ -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()
//! },
//! );
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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(())
Expand Down
87 changes: 27 additions & 60 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ use std::result::Result;

#[test]
fn test_display_json_value() -> Result<(), Box<dyn Error>> {
#[cfg(windows)]
let _res = enable_ansi_support();

let data = json!({
"name": "John Doe",
"age": 43,
Expand All @@ -26,9 +23,6 @@ fn test_display_json_value() -> Result<(), Box<dyn Error>> {

#[test]
fn test_trait() -> Result<(), Box<dyn Error>> {
#[cfg(windows)]
let _res = enable_ansi_support();

println!(
"\n{}",
r#"{
Expand All @@ -48,9 +42,6 @@ fn test_trait() -> Result<(), Box<dyn Error>> {

#[test]
fn test_trait_err() -> Result<(), Box<dyn Error>> {
#[cfg(windows)]
let _res = enable_ansi_support();

eprintln!(
"\n{}",
r#"{
Expand All @@ -70,9 +61,6 @@ fn test_trait_err() -> Result<(), Box<dyn Error>> {

#[test]
fn test_trait_color_off() -> Result<(), Box<dyn Error>> {
#[cfg(windows)]
let _res = enable_ansi_support();

println!(
"\n{}",
r#"{
Expand All @@ -92,9 +80,6 @@ fn test_trait_color_off() -> Result<(), Box<dyn Error>> {

#[test]
fn test_trait_styler() -> Result<(), Box<dyn Error>> {
#[cfg(windows)]
let _res = enable_ansi_support();

println!(
"\n{}",
r#"{
Expand All @@ -110,12 +95,12 @@ fn test_trait_styler() -> Result<(), Box<dyn Error>> {
.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()
},
)?
Expand All @@ -125,9 +110,6 @@ fn test_trait_styler() -> Result<(), Box<dyn Error>> {

#[test]
fn test_trait_styler_color_off() -> Result<(), Box<dyn Error>> {
#[cfg(windows)]
let _res = enable_ansi_support();

println!(
"\n{}",
r#"{
Expand All @@ -143,12 +125,12 @@ fn test_trait_styler_color_off() -> Result<(), Box<dyn Error>> {
.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()
},
)?
Expand All @@ -158,9 +140,6 @@ fn test_trait_styler_color_off() -> Result<(), Box<dyn Error>> {

#[test]
fn test_writer() -> Result<(), Box<dyn Error>> {
#[cfg(windows)]
let _res = enable_ansi_support();

let data = json!({
"name": "John Doe",
"age": 43,
Expand All @@ -181,18 +160,15 @@ fn test_writer() -> Result<(), Box<dyn Error>> {

#[test]
fn test_styler() -> Result<(), Box<dyn Error>> {
#[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()
},
);
Expand Down Expand Up @@ -222,18 +198,15 @@ fn test_styler() -> Result<(), Box<dyn Error>> {

#[test]
fn test_styler_no_color() -> Result<(), Box<dyn Error>> {
#[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()
},
);
Expand Down Expand Up @@ -266,15 +239,12 @@ fn test_styler_no_color() -> Result<(), Box<dyn Error>> {

#[test]
fn test_styler_compact() -> Result<(), Box<dyn Error>> {
#[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()
},
);
Expand Down Expand Up @@ -303,9 +273,6 @@ fn test_styler_compact() -> Result<(), Box<dyn Error>> {

#[test]
fn test_serializes() -> Result<(), Box<dyn Error>> {
#[cfg(windows)]
let _res = enable_ansi_support();

#[derive(serde::Serialize)]
struct Data<'a> {
name: &'a str,
Expand Down
Loading