From 0f4b13e8c44a17142aa264bc4f8beb38e6f7eb3e Mon Sep 17 00:00:00 2001 From: Mark Zaidelman Date: Sun, 22 Jun 2025 02:04:52 +0300 Subject: [PATCH] feat(ui): respect NO_COLOR environment variable (#804) * feat(ui) Respect NO_COLOR environment variable * Add NO_COLOR to man and readme * chore: generate completions & manpage * Really update manpage this time * Add `enpty` color scheme to readme * Rename "empty" color theme to "none" This seems a better wording for what this theme is. However, empty is still supported for backwards compatibility (though not mentioned in readme and manpage, but it wasn't mentioned before anyway) Since `ColorTheme::empty` is private, I felt it's okay to rename too to follow suite. * Add details to related readme paragraph --------- Co-authored-by: Skim bot Co-authored-by: LoricAndre <57358788+LoricAndre@users.noreply.github.com> --- README.md | 3 +++ man/man1/sk.1 | 5 +++++ skim/src/options.rs | 5 +++++ skim/src/theme.rs | 21 ++++++++++++--------- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f0bdac80..ef9ef921 100644 --- a/README.md +++ b/README.md @@ -319,6 +319,8 @@ limited to the default appearance - Skim supports comprehensive customization of --color=[BASE_SCHEME][,COLOR:ANSI] ``` +Skim also respects the `NO_COLOR` environment variable. Set it to anything and `sk` (and many other terminal apps) will disable all colored output. See [no-color.org](https://no-color.org/) for more details. + ### Available Base Color Schemes Skim comes with several built-in color schemes that you can use as a starting point: @@ -328,6 +330,7 @@ sk --color=dark # Default dark theme (256 colors) sk --color=light # Light theme (256 colors) sk --color=16 # Simple 16-color theme sk --color=bw # Minimal black & white theme (no colors, just styles) +sk --color=none # Minimal black & white theme (no colors, no styles) sk --color=molokai # Molokai-inspired theme (256 colors) ``` diff --git a/man/man1/sk.1 b/man/man1/sk.1 index 80c3fce0..26effac2 100644 --- a/man/man1/sk.1 +++ b/man/man1/sk.1 @@ -12,6 +12,10 @@ sk is a general purpose command\-line fuzzy finder. .PP ENVIRONMENT VARIABLES .PP +NO_COLOR +.PP +If set and not empty, sk will not use any colors in the output. +.PP SKIM_DEFAULT_COMMAND .PP Default command to use when input is tty. On *nix systems, sk runs the command with sh \-c, so make sure that it\*(Aqs POSIX\-compliant. @@ -426,6 +430,7 @@ Base Color Schemes \- light: 256\-color light theme \- 16: Basic 16\-color theme \- bw: Minimal black & white theme (no colors, just styles) +\- none: Minimal black & white theme (no colors, no styles). Default when NO_COLOR is set \- molokai: Molokai\-inspired 256\-color theme Color Customization diff --git a/skim/src/options.rs b/skim/src/options.rs index e06de3e9..c4842978 100644 --- a/skim/src/options.rs +++ b/skim/src/options.rs @@ -19,6 +19,10 @@ use crate::{CaseMatching, FuzzyAlgorithm, Selector}; /// /// # ENVIRONMENT VARIABLES /// +/// ## NO_COLOR +/// +/// If set and not empty, sk will not use any colors in the output. +/// /// ## SKIM_DEFAULT_COMMAND /// /// Default command to use when input is tty. On *nix systems, sk runs the command with sh -c, so make sure that @@ -468,6 +472,7 @@ pub struct SkimOptions { /// - **light**: 256-color light theme /// - **16**: Basic 16-color theme /// - **bw**: Minimal black & white theme (no colors, just styles) + /// - **none**: Minimal black & white theme (no colors, no styles). Default when NO_COLOR is set /// - **molokai**: Molokai-inspired 256-color theme /// /// ### Color Customization diff --git a/skim/src/theme.rs b/skim/src/theme.rs index 8f0822c8..453ce019 100644 --- a/skim/src/theme.rs +++ b/skim/src/theme.rs @@ -1,5 +1,5 @@ //! Handle the color theme -use std::sync::LazyLock; +use std::{env, sync::LazyLock}; use crate::options::SkimOptions; use skim_tuikit::prelude::*; @@ -52,11 +52,14 @@ impl ColorTheme { if let Some(color) = options.color.clone() { ColorTheme::from_options(&color) } else { - ColorTheme::dark256() + match env::var_os("NO_COLOR") { + Some(no_color) if !no_color.is_empty() => ColorTheme::none(), + _ => ColorTheme::dark256(), + } } } - fn empty() -> Self { + fn none() -> Self { ColorTheme { fg: Color::Default, bg: Color::Default, @@ -88,7 +91,7 @@ impl ColorTheme { matched_effect: Effect::UNDERLINE, current_effect: Effect::REVERSE, current_match_effect: Effect::UNDERLINE | Effect::REVERSE, - ..ColorTheme::empty() + ..ColorTheme::none() } } @@ -107,7 +110,7 @@ impl ColorTheme { selected: Color::MAGENTA, header: Color::CYAN, border: Color::LIGHT_BLACK, - ..ColorTheme::empty() + ..ColorTheme::none() } } @@ -126,7 +129,7 @@ impl ColorTheme { selected: Color::AnsiValue(168), header: Color::AnsiValue(109), border: Color::AnsiValue(59), - ..ColorTheme::empty() + ..ColorTheme::none() } } @@ -145,7 +148,7 @@ impl ColorTheme { selected: Color::AnsiValue(168), header: Color::AnsiValue(109), border: Color::AnsiValue(59), - ..ColorTheme::empty() + ..ColorTheme::none() } } @@ -164,7 +167,7 @@ impl ColorTheme { selected: Color::AnsiValue(168), header: Color::AnsiValue(31), border: Color::AnsiValue(145), - ..ColorTheme::empty() + ..ColorTheme::none() } } @@ -179,7 +182,7 @@ impl ColorTheme { "light" => ColorTheme::light256(), "16" => ColorTheme::default16(), "bw" => ColorTheme::bw(), - "empty" => ColorTheme::empty(), + "none" | "empty" => ColorTheme::none(), "dark" | "default" | _ => ColorTheme::dark256(), }; continue;