Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add block methods Rainbow.with_enabled / new_with_enabled to temporarily enable/disable Rainbow #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions lib/rainbow/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions spec/integration/instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions spec/integration/rainbow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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