Paintbrush provides one simple configuration option, allowing you to enable or disable colorization either globally, per-invocation, or within a block.
Note that global configuration overrides block configuration, and block configuration overrides per-invocation configuration. i.e. any method-level configurations have no impact if a global configuration is set.
Disable colorization globally by adding the following code somewhere near the beginning of your application’s start-up process (e.g. in Rails, an initializer is a good place to put this):
# config/initializers/paintbrush.rb
Paintbrush.configure do |config|
config.colorize = false if Rails.env.production?
end
All calls to #paintbrush
will now return a regular, uncolorized string.
Paintbrush can be configured for the duration of a block by calling Paintbrush.with_configuration
. e.g. you may want to disable colorization in your tests to make testing output a bit easier. If you’re using RSpec you can add the following to spec/spec_helper.rb
:
# spec/spec_helper.rb
RSpec.configure do |config|
config.around { |example| Paintbrush.with_configuration(colorize: false) { example.call } }
end
Pass colorize: false
to paintbrush
directly to disable colorization for a single call. This is especially useful when generating a message in two or more contexts. For example, you may want to render the same message to a development log as well as returning that message in a web response, one with colorization and one without: