From cbbb5f97379faa3b99b992328f600c29221d659d Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 7 Jun 2024 10:35:57 +0200 Subject: [PATCH] Remove color if not tty --- CHANGELOG.md | 3 +++ Gemfile.lock | 1 + lib/specdiff/config.rb | 21 ++++++++++++++------- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90e25a3..56ca548 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Add option to disable color with environment variable "NO_COLOR" +- Default coloring to true if $stdout is terminal + ## [0.3.0] - 2024-04-16 ### Changed diff --git a/Gemfile.lock b/Gemfile.lock index daf278d..5a35bf3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -62,6 +62,7 @@ GEM unicode-display_width (2.5.0) PLATFORMS + arm64-darwin-22 x86_64-linux DEPENDENCIES diff --git a/lib/specdiff/config.rb b/lib/specdiff/config.rb index 3887f2f..e800318 100644 --- a/lib/specdiff/config.rb +++ b/lib/specdiff/config.rb @@ -9,8 +9,20 @@ class << self attr_reader :config end - DEFAULT = Config.new(colorize: true).freeze - @config = DEFAULT.dup + # Generates the default configuration + def self.default_configuration + config = Config.new(colorize: true) + + if !ENV["NO_COLOR"].nil? && !ENV["NO_COLOR"].empty? + config.colorize = false + else + config.colorize = $stdout.isatty + end + + config.freeze + end + + @config = default_configuration.dup # private, used for testing def self._set_config(new_config) @@ -22,9 +34,4 @@ def self.configure yield(@config) @config end - - # Generates the default configuration - def self.default_configuration - DEFAULT - end end