From 159215f4cf2070b02959be49f1ec64af29374544 Mon Sep 17 00:00:00 2001 From: Tyler Rick Date: Tue, 15 Oct 2024 11:24:11 -0700 Subject: [PATCH] Allow `enabled` option to be passed to `Rainbow.new` Wrapper.new already allows this, so we simply have to pass it on. --- Changelog.md | 1 + README.markdown | 4 +--- lib/rainbow.rb | 4 ++-- spec/integration/instance_spec.rb | 6 ++++++ 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Changelog.md b/Changelog.md index 85dc369..9603106 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,7 @@ ## Unreleased +- Allow `enabled` option to be passed to `Rainbow.new` - 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..1736080 100644 --- a/README.markdown +++ b/README.markdown @@ -153,11 +153,9 @@ parts of your application you can get a new Rainbow wrapper instance for each of them and control the state of coloring during the runtime. ```ruby -rainbow_one = Rainbow.new +rainbow_one = Rainbow.new(false) rainbow_two = Rainbow.new -rainbow_one.enabled = false - Rainbow("hello").red # => "\e[31mhello\e[0m" ("hello" if not on TTY) rainbow_one.wrap("hello").red # => "hello" rainbow_two.wrap("hello").red # => "\e[31mhello\e[0m" ("hello" if not on TTY) diff --git a/lib/rainbow.rb b/lib/rainbow.rb index 7fa371f..6a0bdbe 100644 --- a/lib/rainbow.rb +++ b/lib/rainbow.rb @@ -3,8 +3,8 @@ require_relative 'rainbow/global' module Rainbow - def self.new - Wrapper.new(global.enabled) + def self.new(enabled = global.enabled) + Wrapper.new(enabled) end self.enabled = false unless STDOUT.tty? && STDERR.tty? diff --git a/spec/integration/instance_spec.rb b/spec/integration/instance_spec.rb index a957326..be444ac 100644 --- a/spec/integration/instance_spec.rb +++ b/spec/integration/instance_spec.rb @@ -17,6 +17,12 @@ expect(rainbow.enabled).to eq(:nope) end + it 'can be initialized with a different enabled state' do + Rainbow.enabled = :yep + rainbow = Rainbow.new(false) + expect(rainbow.enabled).to eq(false) + end + it 'wraps string with escape codes when enabled' do rainbow = Rainbow.new rainbow.enabled = true