From 083e4b59f663342a7c45b3d65e44f868d8691a20 Mon Sep 17 00:00:00 2001 From: Tyler Rick Date: Tue, 15 Oct 2024 12:02:48 -0700 Subject: [PATCH] Add block methods `Rainbow.with_enabled(enabled)`/`new_with_enabled(enabled)` to temporarily enable/disable Rainbow --- Changelog.md | 2 ++ README.markdown | 6 ++++++ lib/rainbow/global.rb | 27 +++++++++++++++++++++++++++ spec/integration/instance_spec.rb | 15 +++++++++++++++ spec/integration/rainbow_spec.rb | 7 +++++++ 5 files changed, 57 insertions(+) diff --git a/Changelog.md b/Changelog.md index 85dc369..014dfd7 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,8 @@ ## Unreleased +- Added block methods `Rainbow.with_enabled(enabled)`/`new_with_enabled(enabled)` to temporarily +- enable/disable Rainbow - Development: Drop `rbx` section in Gemfile. No continued support effort for Rubinius. ## 3.1.1 (2022-01-11) diff --git a/README.markdown b/README.markdown index fd040c7..8d7d7db 100644 --- a/README.markdown +++ b/README.markdown @@ -145,6 +145,12 @@ When disabled all the methods return an unmodified string It's enabled by default, unless STDOUT/STDERR is not a TTY or a terminal is dumb. +You can temporarily enable/disable Rainbow by using the block form: + +```ruby +Rainbow.with_enabled(true) { Rainbow('not blue').blue } # => 'not blue' +``` + ### Advanced usage `Rainbow()` and `Rainbow.enabled` operate on the global Rainbow wrapper diff --git a/lib/rainbow/global.rb b/lib/rainbow/global.rb index 08599bf..66407b0 100644 --- a/lib/rainbow/global.rb +++ b/lib/rainbow/global.rb @@ -11,10 +11,37 @@ def self.enabled global.enabled end + # Globally enables/disables Rainbow def self.enabled=(value) global.enabled = value end + # Globally enables/disables Rainbow for the duration of the block + # + # @example + # Rainbow.with_enabled(true) { Rainbow('blue').blue } # => "\e[34mblue\e[0m" + # Rainbow.with_enabled(false) { Rainbow('not blue').blue } # => "not blue" + # + def self.with_enabled(enabled) + orig_enabled, Rainbow.enabled = Rainbow.enabled, enabled # rubocop:disable Style/ParallelAssignment + yield + ensure + Rainbow.enabled = orig_enabled + end + + # Yields a new Rainbow instance that is enabled/disabled + # + # @example + # Rainbow.new_with_enabled(true) { |rainbow| rainbow.wrap('blue').blue } # => "\e[34mblue\e[0m" + # Rainbow.new_with_enabled(false) { |rainbow| rainbow.wrap('not blue').blue } # => "not blue" + # + def self.new_with_enabled(enabled) + instance = Rainbow.new + instance.enabled = enabled + yield instance if block_given? + instance + end + def self.uncolor(string) StringUtils.uncolor(string) end diff --git a/spec/integration/instance_spec.rb b/spec/integration/instance_spec.rb index a957326..cfd3392 100644 --- a/spec/integration/instance_spec.rb +++ b/spec/integration/instance_spec.rb @@ -7,6 +7,21 @@ expect(Rainbow.new.enabled).to eq(:yep) end + it 'Rainbow.new_with_enabled creates instance with different enabled state' do + Rainbow.enabled = :ignored + Rainbow.new_with_enabled(true) do |rainbow| + expect(rainbow.enabled).to eq(true) + end + + rainbow = Rainbow.new_with_enabled(false) do |rainbow| + expect(rainbow.enabled).to eq(false) + end + expect(rainbow.enabled).to eq(false) + + rainbow = Rainbow.new_with_enabled(true) + expect(rainbow.enabled).to eq(true) + end + it 'tracks its own state separately from the global instance' do Rainbow.enabled = :yep rainbow = Rainbow.new diff --git a/spec/integration/rainbow_spec.rb b/spec/integration/rainbow_spec.rb index 59900e0..ae25725 100644 --- a/spec/integration/rainbow_spec.rb +++ b/spec/integration/rainbow_spec.rb @@ -167,4 +167,11 @@ expect(result).to eq('hello') end end + + it 'can temporarily enabled/disabled by using the block form' do + Rainbow.with_enabled(false) do + expect(Rainbow.enabled).to eq(false) + end + expect(Rainbow.enabled).to eq(true) + end end