Skip to content

Commit

Permalink
Upgrade yansi: 0.5.1 -> 1.0.1 (#389)
Browse files Browse the repository at this point in the history
- Adapt to `thing.paint(style)` API; was `style.paint(thing)`
- Remove `yansi::Paint::enable_windows_ascii()` in style usage decision;
  removed in yansi commit b186eb5bfb, which introduced "automatic"
  support for Windows: "If support is not available, styling is disabled
  and no styling sequences are emitted", fitting the `Auto` option
- Respect `--color=always` even if we know it won't work

---------

Co-authored-by: Niklas Mohrin <[email protected]>
  • Loading branch information
nc7s and niklasmohrin authored Nov 14, 2024
1 parent d44613c commit fb7492b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ serde = "1.0.21"
serde_derive = "1.0.21"
toml = "0.8.19"
walkdir = "2.0.1"
yansi = "0.5"
yansi = "1"
zip = { version = "2.1.6", default-features = false, features = ["deflate"] }

[target.'cfg(not(windows))'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl From<RawColor> for Color {
RawColor::Cyan => Self::Cyan,
RawColor::White => Self::White,
RawColor::Ansi(num) => Self::Fixed(num),
RawColor::Rgb { r, g, b } => Self::RGB(r, g, b),
RawColor::Rgb { r, g, b } => Self::Rgb(r, g, b),
}
}
}
Expand Down
14 changes: 5 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,16 @@ fn main() {
let args = Cli::parse();

// Determine the usage of styles
#[cfg(target_os = "windows")]
let ansi_support = yansi::Paint::enable_windows_ascii();
#[cfg(not(target_os = "windows"))]
let ansi_support = true;
let enable_styles = match args.color.unwrap_or_default() {
// Attempt to use styling if instructed
ColorOptions::Always => true,
ColorOptions::Always => {
yansi::enable(); // disable yansi's automatic detection for ANSI support on Windows
true
}
// Enable styling if:
// * There is `ansi_support`
// * NO_COLOR env var isn't set: https://no-color.org/
// * The output stream is stdout (not being piped)
ColorOptions::Auto => {
ansi_support && env::var_os("NO_COLOR").is_none() && io::stdout().is_terminal()
}
ColorOptions::Auto => env::var_os("NO_COLOR").is_none() && io::stdout().is_terminal(),
// Disable styling
ColorOptions::Never => false,
};
Expand Down
11 changes: 6 additions & 5 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::io::{self, BufRead, Write};

use anyhow::{Context, Result};
use yansi::Paint;

use crate::{
cache::PageLookupResult,
Expand Down Expand Up @@ -86,11 +87,11 @@ fn print_snippet(
use PageSnippet::*;

match snip {
CommandName(s) => write!(writer, "{}", style.command_name.paint(s)),
Variable(s) => write!(writer, "{}", style.example_variable.paint(s)),
NormalCode(s) => write!(writer, "{}", style.example_code.paint(s)),
Description(s) => writeln!(writer, " {}", style.description.paint(s)),
Text(s) => writeln!(writer, " {}", style.example_text.paint(s)),
CommandName(s) => write!(writer, "{}", s.paint(style.command_name)),
Variable(s) => write!(writer, "{}", s.paint(style.example_variable)),
NormalCode(s) => write!(writer, "{}", s.paint(style.example_code)),
Description(s) => writeln!(writer, " {}", s.paint(style.description)),
Text(s) => writeln!(writer, " {}", s.paint(style.example_text)),
Linebreak => writeln!(writer),
}
}
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use yansi::Color;
use yansi::{Color, Paint};

/// Print a warning to stderr. If `enable_styles` is true, then a yellow
/// message will be printed.
Expand All @@ -14,7 +14,7 @@ pub fn print_error(enable_styles: bool, error: &anyhow::Error) {

fn print_msg(enable_styles: bool, message: &str, prefix: &'static str, color: Color) {
if enable_styles {
eprintln!("{}{}", color.paint(prefix), color.paint(message));
eprintln!("{}{}", prefix.paint(color), message.paint(color));
} else {
eprintln!("{message}");
}
Expand Down

0 comments on commit fb7492b

Please sign in to comment.