Skip to content

added disabling color functionality #2

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions lib/colored.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
module Colored
extend self

@@color_enabled = true

COLORS = {
'black' => 30,
'red' => 31,
Expand Down Expand Up @@ -66,6 +68,7 @@ module Colored
end

def colorize(string, options = {})
return string unless @@color_enabled
colored = [color(options[:foreground]), color("on_#{options[:background]}"), extra(options[:extra])].compact * ''
colored << string
colored << extra(:clear)
Expand All @@ -86,6 +89,15 @@ def color(color_name)
return unless color_name && COLORS[color_name]
"\e[#{COLORS[color_name] + (background ? 10 : 0)}m"
end

def disable_color
@@color_enabled = false
end

def enable_color
@@color_enabled = true
end

end unless Object.const_defined? :Colored

String.send(:include, Colored)