rainbow-1.99.1/0000755000004100000410000000000012260324232013300 5ustar www-datawww-datarainbow-1.99.1/rainbow.gemspec0000644000004100000410000000177412260324232016317 0ustar www-datawww-data# coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'rainbow/version' Gem::Specification.new do |spec| spec.name = "rainbow" spec.version = Rainbow::VERSION spec.authors = ["Marcin Kulik"] spec.email = ["m@ku1ik.com"] spec.description = %q{Colorize printed text on ANSI terminals} spec.summary = %q{Colorize printed text on ANSI terminals} spec.homepage = "https://github.com/sickill/rainbow" spec.license = "MIT" spec.required_ruby_version = '>= 1.9.2' spec.files = `git ls-files`.split($/) spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "rake" spec.add_development_dependency "rspec" end rainbow-1.99.1/Rakefile0000644000004100000410000000016512260324232014747 0ustar www-datawww-datarequire "bundler/gem_tasks" require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) task :default => :spec rainbow-1.99.1/Gemfile0000644000004100000410000000044012260324232014571 0ustar www-datawww-datasource 'https://rubygems.org' # Specify your gem's dependencies in rainbow.gemspec gemspec gem 'coveralls', require: false gem 'mime-types', '< 2.0.0', platforms: [:ruby_18] group :guard do gem 'guard' gem 'guard-rspec' end platform :rbx do gem 'rubysl' gem 'rubysl-json' end rainbow-1.99.1/Changelog.md0000644000004100000410000000255112260324232015514 0ustar www-datawww-data# Rainbow changelog ## 1.0.0 (2008-7-21) * initial version ## 1.0.1 (2009-3-19) * Windows support ## 1.0.2 (2009-5-15) * improved support for ruby 1.8.6 and 1.9.1 ## 1.0.3 (2009-7-26) * rainbow methods don't mutate the original string object anymore ## 1.0.4 (2009-11-27) * support for 256 colors ## 1.1 (2010-6-7) * option for enabling/disabling of escape code wrapping * auto-disabling when STDOUT is not a TTY ## 1.1.1 (2011-2-7) * improved Windows support ## 1.1.2 (2011-11-13) * improved compatibility with MRI 1.9.3 ## 1.1.3 (2011-12-6) * improved compatibility with MRI 1.8.7 * fix for regression with regards to original string mutation ## 1.1.4 (2012-4-28) * option for forcing coloring even when STDOUT is not a TTY (CLICOLOR_FORCE env var) * fix for frozen strings ## 1.99.0 (2013-12-26) * preparation for dropping String monkey patching * `require "rainbow/string"` if you want to use monkey patched String * introduction of Rainbow() wrapper * support for MRI 1.8.7, 1.9.2, 1.9.3, 2.0 and 2.1, JRuby and Rubinius * deprecation of Sickill::Rainbow namespace (use Rainbow.enabled = true instead) ## 1.99.1 (2013-12-28) * drop support for ruby 1.8 * `require "rainbow/string"` -> `require "rainbow/ext/string"` * custom rainbow wrapper instances (with separate enabled/disabled state) * shortcut methods for changing text color (`Rainbow("foo").red`) rainbow-1.99.1/spec/0000755000004100000410000000000012260324232014232 5ustar www-datawww-datarainbow-1.99.1/spec/spec_helper.rb0000644000004100000410000000030212260324232017043 0ustar www-datawww-dataif ENV["CI"] && (!defined?(RUBY_ENGINE) || RUBY_ENGINE == "ruby") require 'coveralls' Coveralls.wear! end Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |file| require file } rainbow-1.99.1/spec/integration/0000755000004100000410000000000012260324232016555 5ustar www-datawww-datarainbow-1.99.1/spec/integration/rainbow_spec.rb0000644000004100000410000000664312260324232021566 0ustar www-datawww-datarequire 'spec_helper' require 'rainbow' describe 'Rainbow() wrapper' do before do Rainbow.enabled = true end it 'allows foreground coloring by color number' do result = Rainbow('hello').foreground(5) expect(result).to eq("\e[35mhello\e[0m") end it 'allows foreground coloring by color name' do result = Rainbow('hello').foreground(:red) expect(result).to eq("\e[31mhello\e[0m") end it 'allows foreground coloring directly by method name' do result = Rainbow('hello').red expect(result).to eq("\e[31mhello\e[0m") end it 'allows foreground coloring by color name (color alias)' do result = Rainbow('hello').color(:red) expect(result).to eq("\e[31mhello\e[0m") end it 'allows foreground coloring by color name (colour alias)' do result = Rainbow('hello').colour(:red) expect(result).to eq("\e[31mhello\e[0m") end it 'allows foreground coloring by color rgb' do result = Rainbow('hello').foreground(255, 128, 64) expect(result).to eq("\e[38;5;215mhello\e[0m") end it 'allows foreground coloring by color rgb (hex string)' do result = Rainbow('hello').foreground('ff8040') expect(result).to eq("\e[38;5;215mhello\e[0m") end it 'allows background coloring by color number' do result = Rainbow('hello').background(3) expect(result).to eq("\e[43mhello\e[0m") end it 'allows background coloring by color name' do result = Rainbow('hello').background(:green) expect(result).to eq("\e[42mhello\e[0m") end it 'allows background coloring by color rgb' do result = Rainbow('hello').background(255, 128, 64) expect(result).to eq("\e[48;5;215mhello\e[0m") end it 'allows background coloring by color rgb (hex string)' do result = Rainbow('hello').background('ff8040') expect(result).to eq("\e[48;5;215mhello\e[0m") end it 'allows making text bright' do result = Rainbow('hello').bright expect(result).to eq("\e[1mhello\e[0m") end it 'allows making text italic' do result = Rainbow('hello').italic expect(result).to eq("\e[3mhello\e[0m") end it 'allows making text underlined' do result = Rainbow('hello').underline expect(result).to eq("\e[4mhello\e[0m") end it 'allows making text blinking' do result = Rainbow('hello').blink expect(result).to eq("\e[5mhello\e[0m") end it 'allows making text inversed' do result = Rainbow('hello').inverse expect(result).to eq("\e[7mhello\e[0m") end it 'allows making text hidden' do result = Rainbow('hello').hide expect(result).to eq("\e[8mhello\e[0m") end it 'allows resetting all the attributes' do result = Rainbow('hello').reset expect(result).to eq("\e[0mhello\e[0m") end it 'allows chaining' do result = Rainbow('hello'). foreground(:red). bright. italic. background('#ff8040'). underline. color(:blue). blink. inverse. hide expect(result).to eq( "\e[31m\e[1m\e[3m\e[48;5;215m\e[4m\e[34m\e[5m\e[7m\e[8mhello\e[0m" ) end context "when Rainbow is disabled" do before do Rainbow.enabled = false end it "allows chaining but doesn't wrap with escape codes" do result = Rainbow('hello'). foreground(:red). bright. italic. background('#ff8040'). underline. color(:blue). blink. inverse. hide expect(result).to eq('hello') end end end rainbow-1.99.1/spec/integration/instance_spec.rb0000644000004100000410000000154012260324232021720 0ustar www-datawww-datarequire 'spec_helper' require 'rainbow' describe 'Custom Rainbow instance' do it 'inherits enabled state from the global instance' do Rainbow.enabled = :yep expect(Rainbow.new.enabled).to eq(:yep) end it 'tracks its own state separately from the global instance' do Rainbow.enabled = :yep rainbow = Rainbow.new expect(Rainbow.new.enabled).to eq(:yep) rainbow.enabled = :nope expect(Rainbow.enabled).to eq(:yep) expect(rainbow.enabled).to eq(:nope) end it 'wraps string with escape codes when enabled' do rainbow = Rainbow.new rainbow.enabled = true expect(rainbow.wrap('hello').green).to eq("\e[32mhello\e[0m") end it "doesn't wrap string with any escape code when disabled" do rainbow = Rainbow.new rainbow.enabled = false expect(rainbow.wrap('hello').green).to eq('hello') end end rainbow-1.99.1/spec/integration/string_spec.rb0000644000004100000410000000344012260324232021423 0ustar www-datawww-datarequire 'spec_helper' require 'rainbow/ext/string' describe 'String mixin' do before do Rainbow.enabled = true end it 'proxies foreground to Rainbow().foreground' do expect('hello'.foreground(:red)).to eq(Rainbow('hello').foreground(:red)) end it 'proxies color to Rainbow().color' do expect('hello'.color(:red)).to eq(Rainbow('hello').color(:red)) end it 'proxies colour to Rainbow().colour' do expect('hello'.colour(:red)).to eq(Rainbow('hello').colour(:red)) end it 'proxies background to Rainbow().background' do expect('hello'.background(:red)).to eq(Rainbow('hello').background(:red)) end it 'proxies bright to Rainbow().bright' do expect('hello'.bright).to eq(Rainbow('hello').bright) end it 'proxies italic to Rainbow().italic' do expect('hello'.italic).to eq(Rainbow('hello').italic) end it 'proxies underline to Rainbow().underline' do expect('hello'.underline).to eq(Rainbow('hello').underline) end it 'proxies blink to Rainbow().blink' do expect('hello'.blink).to eq(Rainbow('hello').blink) end it 'proxies inverse to Rainbow().inverse' do expect('hello'.inverse).to eq(Rainbow('hello').inverse) end it 'proxies hide to Rainbow().hide' do expect('hello'.hide).to eq(Rainbow('hello').hide) end it 'proxies reset to Rainbow().reset' do expect('hello'.reset).to eq(Rainbow('hello').reset) end context "when Rainbow is disabled" do before do Rainbow.enabled = false end it "allows chaining but doesn't wrap with escape codes" do result = 'hello'. foreground(:red). bright. italic. background('#ff8040'). underline. color(:blue). blink. inverse. hide expect(result).to eq('hello') end end end rainbow-1.99.1/spec/unit/0000755000004100000410000000000012260324232015211 5ustar www-datawww-datarainbow-1.99.1/spec/unit/presenter_spec.rb0000644000004100000410000000752012260324232020563 0ustar www-datawww-datarequire 'spec_helper' require 'rainbow/presenter' module Rainbow describe Presenter do let(:presenter) { described_class.new('hello') } shared_examples_for "rainbow string method" do it "wraps the text with a sgr sequence" do expect(subject).to eq('[hello]') end it "returns an instance of Rainbow::Presenter" do expect(subject).to be_kind_of(Rainbow::Presenter) end end shared_examples_for "text color method" do let(:color) { double('color', codes: [1, 2]) } before do allow(Color).to receive(:build). with(:foreground, [:arg1, 'arg2']) { color } end it_behaves_like "rainbow string method" it 'wraps with color codes' do subject expect(StringUtils).to have_received(:wrap_with_sgr). with('hello', [1, 2]) end end shared_examples_for "text background method" do let(:color) { double('color', codes: [1, 2]) } before do allow(Color).to receive(:build). with(:background, [:arg1, 'arg2']) { color } end it_behaves_like "rainbow string method" it 'wraps with color codes' do subject expect(StringUtils).to have_received(:wrap_with_sgr). with('hello', [1, 2]) end end before do allow(StringUtils).to receive(:wrap_with_sgr) { '[hello]' } end describe '#color' do subject { presenter.color(:arg1, 'arg2') } it_behaves_like "text color method" end describe '#foreground' do subject { presenter.foreground(:arg1, 'arg2') } it_behaves_like "text color method" end describe '#fg' do subject { presenter.fg(:arg1, 'arg2') } it_behaves_like "text color method" end describe '#background' do subject { presenter.background(:arg1, 'arg2') } it_behaves_like "text background method" end describe '#bg' do subject { presenter.bg(:arg1, 'arg2') } it_behaves_like "text background method" end describe '#reset' do subject { presenter.reset } it_behaves_like "rainbow string method" it 'wraps with 0 code' do subject expect(StringUtils).to have_received(:wrap_with_sgr).with('hello', [0]) end end describe '#bright' do subject { presenter.bright } it_behaves_like "rainbow string method" it 'wraps with 1 code' do subject expect(StringUtils).to have_received(:wrap_with_sgr).with('hello', [1]) end end describe '#italic' do subject { presenter.italic } it_behaves_like "rainbow string method" it 'wraps with 3 code' do subject expect(StringUtils).to have_received(:wrap_with_sgr). with('hello', [3]) end end describe '#underline' do subject { presenter.underline } it_behaves_like "rainbow string method" it 'wraps with 4 code' do subject expect(StringUtils).to have_received(:wrap_with_sgr).with('hello', [4]) end end describe '#blink' do subject { presenter.blink } it_behaves_like "rainbow string method" it 'wraps with 5 code' do subject expect(StringUtils).to have_received(:wrap_with_sgr).with('hello', [5]) end end describe '#inverse' do subject { presenter.inverse } it_behaves_like "rainbow string method" it 'wraps with 7 code' do subject expect(StringUtils).to have_received(:wrap_with_sgr).with('hello', [7]) end end describe '#hide' do subject { presenter.hide } it_behaves_like "rainbow string method" it 'wraps with 8 code' do subject expect(StringUtils).to have_received(:wrap_with_sgr).with('hello', [8]) end end it_behaves_like "presenter with shortcut color methods" end end rainbow-1.99.1/spec/unit/color_spec.rb0000644000004100000410000001166512260324232017677 0ustar www-datawww-datarequire 'spec_helper' require 'rainbow/color' module Rainbow describe Color do describe '.build' do subject { described_class.build(ground, values) } let(:ground) { :foreground } context "when single fixnum given" do let(:values) { [1] } it { should be_a(Color) } end context "when single symbol given" do let(:values) { [:red] } it { should be_a(Color) } end context "when single string given" do let(:values) { ['#dead00'] } it { should be_a(Color) } end context "when 2 elements" do let(:values) { [1, 2] } it 'raises ArgumentError' do expect { subject }.to raise_error(ArgumentError) end end context "when 3 elements given" do let(:values) { [1, 2, 3] } it { should be_a(Color) } end context "when 4 elements" do let(:values) { [1, 2, 3, 4] } it 'raises ArgumentError' do expect { subject }.to raise_error(ArgumentError) end end end end describe Color::Indexed do let(:color) { described_class.new(ground, 5) } describe '#codes' do subject { color.codes } context "when ground is :foreground" do let(:ground) { :foreground } it { should eq([35]) } end context "when ground is :background" do let(:ground) { :background } it { should eq([45]) } end end end describe Color::Named do let(:color) { described_class.new(ground, name) } describe '#codes' do subject { color.codes } context "when ground is :foreground" do let(:ground) { :foreground } context "and name is :black" do let(:name) { :black } it { should eq([30]) } end context "and name is :red" do let(:name) { :red } it { should eq([31]) } end context "and name is :green" do let(:name) { :green } it { should eq([32]) } end context "and name is :yellow" do let(:name) { :yellow } it { should eq([33]) } end context "and name is :blue" do let(:name) { :blue } it { should eq([34]) } end context "and name is :magenta" do let(:name) { :magenta } it { should eq([35]) } end context "and name is :cyan" do let(:name) { :cyan } it { should eq([36]) } end context "and name is :white" do let(:name) { :white } it { should eq([37]) } end context "and name is invalid" do let(:name) { :foo } it 'raises ArgumentError' do expect { subject }.to raise_error(ArgumentError) end end end context "when ground is :background" do let(:ground) { :background } context "and name is :black" do let(:name) { :black } it { should eq([40]) } end context "and name is :red" do let(:name) { :red } it { should eq([41]) } end context "and name is :green" do let(:name) { :green } it { should eq([42]) } end context "and name is :yellow" do let(:name) { :yellow } it { should eq([43]) } end context "and name is :blue" do let(:name) { :blue } it { should eq([44]) } end context "and name is :magenta" do let(:name) { :magenta } it { should eq([45]) } end context "and name is :cyan" do let(:name) { :cyan } it { should eq([46]) } end context "and name is :white" do let(:name) { :white } it { should eq([47]) } end context "and name is invalid" do let(:name) { :foo } it 'raises ArgumentError' do expect { subject }.to raise_error(ArgumentError) end end end end end describe Color::RGB do let(:color) { described_class.new(ground, r, g, b) } describe '#codes' do subject { color.codes } let(:ground) { :foreground } let(:r) { 0 } let(:g) { 128 } let(:b) { 255 } context "when ground is :foreground" do let(:ground) { :foreground } it { should eq([38, 5, 39]) } end context "when ground is :background" do let(:ground) { :background } it { should eq([48, 5, 39]) } end context "when a component is lower than 0" do let(:r) { -1 } it 'raises ArgumentError' do expect { subject }.to raise_error(ArgumentError) end end context "when a component is greater than 255" do let(:b) { 256 } it 'raises ArgumentError' do expect { subject }.to raise_error(ArgumentError) end end end end end rainbow-1.99.1/spec/unit/wrapper_spec.rb0000644000004100000410000000110012260324232020220 0ustar www-datawww-datarequire 'spec_helper' require 'rainbow/wrapper' module Rainbow describe Wrapper do let(:wrapper) { described_class.new(enabled) } describe '#wrap' do subject { wrapper.wrap('hello') } context "when wrapping is enabled" do let(:enabled) { true } it { should eq('hello') } it { should be_kind_of(Rainbow::Presenter) } end context "when wrapping is disabled" do let(:enabled) { false } it { should eq('hello') } it { should be_kind_of(Rainbow::NullPresenter) } end end end end rainbow-1.99.1/spec/unit/null_presenter_spec.rb0000644000004100000410000000402212260324232021607 0ustar www-datawww-datarequire 'spec_helper' require 'rainbow/null_presenter' module Rainbow describe NullPresenter do let(:presenter) { described_class.new('hello') } shared_examples_for "rainbow null string method" do it "doesn't wrap the text with any sgr sequence" do expect(subject).to eq('hello') end it "returns an instance of Rainbow::NullPresenter" do expect(subject).to be_kind_of(Rainbow::NullPresenter) end end describe '#color' do subject { presenter.color(:arg1, 'arg2') } it_behaves_like "rainbow null string method" end describe '#foreground' do subject { presenter.foreground(:arg1, 'arg2') } it_behaves_like "rainbow null string method" end describe '#fg' do subject { presenter.fg(:arg1, 'arg2') } it_behaves_like "rainbow null string method" end describe '#background' do subject { presenter.background(:arg1, 'arg2') } it_behaves_like "rainbow null string method" end describe '#bg' do subject { presenter.bg(:arg1, 'arg2') } it_behaves_like "rainbow null string method" end describe '#reset' do subject { presenter.reset } it_behaves_like "rainbow null string method" end describe '#bright' do subject { presenter.bright } it_behaves_like "rainbow null string method" end describe '#italic' do subject { presenter.italic } it_behaves_like "rainbow null string method" end describe '#underline' do subject { presenter.underline } it_behaves_like "rainbow null string method" end describe '#blink' do subject { presenter.blink } it_behaves_like "rainbow null string method" end describe '#inverse' do subject { presenter.inverse } it_behaves_like "rainbow null string method" end describe '#hide' do subject { presenter.hide } it_behaves_like "rainbow null string method" end it_behaves_like "presenter with shortcut color methods" end end rainbow-1.99.1/spec/unit/string_utils_spec.rb0000644000004100000410000000303712260324232021301 0ustar www-datawww-datarequire 'spec_helper' require 'rainbow/string_utils' module Rainbow describe StringUtils do describe '.wrap_with_sgr' do subject { described_class.wrap_with_sgr(string, codes) } let(:string) { 'hello' } let(:codes) { [1] } it "doesn't mutate original string" do string.freeze expect(subject).to eq("\e[1mhello\e[0m") expect(string).to eq('hello') end context "when subclass of String class given" do class Stringgg < ::String; end let(:string) { Stringgg.new('hello') } it { should eq("\e[1mhello\e[0m") } end context "when no codes given" do let(:codes) { [] } it "doesn't wrap the given string with any sgr sequence" do expect(subject).to eq("hello") end end context "when single code given" do let(:codes) { [1] } it "wraps the given string with sgr sequence for given codes" do expect(subject).to eq("\e[1mhello\e[0m") end end context "when multiple codes given" do let(:codes) { [1, 2] } it "wraps the given string with sgr sequence for given codes" do expect(subject).to eq("\e[1;2mhello\e[0m") end end context "when wrapping an already wrapped string" do let(:string) { "\e[1;2mhello\e[0m" } let(:codes) { [3, 4] } it "wraps the given string with sgr sequence for given codes" do expect(subject).to eq("\e[1;2m\e[3;4mhello\e[0m") end end end end end rainbow-1.99.1/spec/unit/namespace_spec.rb0000644000004100000410000000110012260324232020474 0ustar www-datawww-datarequire 'spec_helper' require 'rainbow' describe Sickill::Rainbow do describe '.enabled' do before do ::Rainbow.enabled = :nope end it 'returns ::Rainbow.enabled' do expect(Sickill::Rainbow.enabled).to eq(:nope) end end describe '.enabled=' do before do allow(STDERR).to receive(:puts) Sickill::Rainbow.enabled = :yep end it 'sets ::Rainbow.enabled=' do expect(::Rainbow.enabled).to eq(:yep) end it 'prints the deprecation notice' do expect(STDERR).to have_received(:puts) end end end rainbow-1.99.1/spec/support/0000755000004100000410000000000012260324232015746 5ustar www-datawww-datarainbow-1.99.1/spec/support/presenter_shared_examples.rb0000644000004100000410000000043212260324232023525 0ustar www-datawww-datashared_examples_for "presenter with shortcut color methods" do [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white].each do |name| describe "##{name}" do subject { presenter.public_send(name) } it { should eq(presenter.color(name)) } end end end rainbow-1.99.1/.travis.yml0000644000004100000410000000017112260324232015410 0ustar www-datawww-databundler_args: --without guard rvm: - 1.9.2 - 1.9.3 - 2.0.0 - 2.1.0 - rbx - jruby script: "bundle exec rake" rainbow-1.99.1/lib/0000755000004100000410000000000012260324232014046 5ustar www-datawww-datarainbow-1.99.1/lib/rainbow/0000755000004100000410000000000012260324232015507 5ustar www-datawww-datarainbow-1.99.1/lib/rainbow/presenter.rb0000644000004100000410000000415712260324232020052 0ustar www-datawww-datarequire 'rainbow/string_utils' require 'rainbow/color' module Rainbow class Presenter < ::String TERM_EFFECTS = { reset: 0, bright: 1, italic: 3, underline: 4, blink: 5, inverse: 7, hide: 8, } # Sets color of this text. def color(*values) wrap_with_sgr(Color.build(:foreground, values).codes) end alias_method :foreground, :color alias_method :fg, :color # Sets background color of this text. def background(*values) wrap_with_sgr(Color.build(:background, values).codes) end alias_method :bg, :background # Resets terminal to default colors/backgrounds. # # It shouldn't be needed to use this method because all methods # append terminal reset code to end of string. def reset wrap_with_sgr(TERM_EFFECTS[:reset]) end # Turns on bright/bold for this text. def bright wrap_with_sgr(TERM_EFFECTS[:bright]) end # Turns on italic style for this text (not well supported by terminal # emulators). def italic wrap_with_sgr(TERM_EFFECTS[:italic]) end # Turns on underline decoration for this text. def underline wrap_with_sgr(TERM_EFFECTS[:underline]) end # Turns on blinking attribute for this text (not well supported by terminal # emulators). def blink wrap_with_sgr(TERM_EFFECTS[:blink]) end # Inverses current foreground/background colors. def inverse wrap_with_sgr(TERM_EFFECTS[:inverse]) end # Hides this text (set its color to the same as background). def hide wrap_with_sgr(TERM_EFFECTS[:hide]) end def black color(:black) end def red color(:red) end def green color(:green) end def yellow color(:yellow) end def blue color(:blue) end def magenta color(:magenta) end def cyan color(:cyan) end def white color(:white) end private def wrap_with_sgr(codes) #:nodoc: self.class.new(StringUtils.wrap_with_sgr(self, [*codes])) end end end rainbow-1.99.1/lib/rainbow/wrapper.rb0000644000004100000410000000055112260324232017515 0ustar www-datawww-datarequire 'rainbow/presenter' require 'rainbow/null_presenter' module Rainbow class Wrapper attr_accessor :enabled def initialize(enabled = true) @enabled = enabled end def wrap(string) if enabled Rainbow::Presenter.new(string.to_s) else Rainbow::NullPresenter.new(string.to_s) end end end end rainbow-1.99.1/lib/rainbow/global.rb0000644000004100000410000000041212260324232017271 0ustar www-datawww-datarequire 'rainbow/wrapper' module Rainbow def self.global @global ||= Wrapper.new end def self.enabled global.enabled end def self.enabled=(value) global.enabled = value end end def Rainbow(string) Rainbow.global.wrap(string.to_s) end rainbow-1.99.1/lib/rainbow/string_utils.rb0000644000004100000410000000071512260324232020565 0ustar www-datawww-datamodule Rainbow class StringUtils def self.wrap_with_sgr(string, codes) return string if codes.empty? seq = "\e[" + codes.join(";") + "m" match = string.match(/^(\e\[([\d;]+)m)*/) if match seq_pos = match.end(0) string = string[0...seq_pos] + seq + string[seq_pos..-1] else string = seq + string end string = string + "\e[0m" unless string =~ /\e\[0m$/ string end end end rainbow-1.99.1/lib/rainbow/version.rb0000644000004100000410000000005712260324232017523 0ustar www-datawww-datamodule Rainbow VERSION = "1.99.1".freeze end rainbow-1.99.1/lib/rainbow/color.rb0000644000004100000410000000420112260324232017147 0ustar www-datawww-datamodule Rainbow class Color attr_reader :ground def self.build(ground, values) unless [1, 3].include?(values.size) fail ArgumentError, "Wrong number of arguments for color definition, should be 1 or 3" end color = values.size == 1 ? values.first : values case color when ::Fixnum Indexed.new(ground, color) when ::Symbol Named.new(ground, color) when ::Array RGB.new(ground, *color) when ::String RGB.new(ground, *parse_hex_color(color)) end end def self.parse_hex_color(hex) hex = hex.gsub('#', '') r = hex[0..1].to_i(16) g = hex[2..3].to_i(16) b = hex[4..5].to_i(16) [r, g, b] end class Indexed < Color attr_reader :num def initialize(ground, num) @ground = ground @num = num end def codes code = num + (ground == :foreground ? 30 : 40) [code] end end class Named < Indexed NAMES = { black: 0, red: 1, green: 2, yellow: 3, blue: 4, magenta: 5, cyan: 6, white: 7, default: 9, } def initialize(ground, name) unless color_names.include?(name) fail ArgumentError, "Unknown color name, valid names: #{color_names.join(', ')}" end super(ground, NAMES[name]) end private def color_names NAMES.keys end end class RGB < Indexed attr_reader :r, :g, :b def self.to_ansi_domain(value) (6 * (value / 256.0)).to_i end def initialize(ground, *values) if values.min < 0 || values.max > 255 fail ArgumentError, "RGB value outside 0-255 range" end super(ground, 8) @r, @g, @b = values end def codes super + [5, code_from_rgb] end private def code_from_rgb 16 + self.class.to_ansi_domain(r) * 36 + self.class.to_ansi_domain(g) * 6 + self.class.to_ansi_domain(b) end end end end rainbow-1.99.1/lib/rainbow/legacy.rb0000644000004100000410000000045612260324232017305 0ustar www-datawww-datamodule Sickill module Rainbow def self.enabled=(value) STDERR.puts("Rainbow gem notice: Sickill::Rainbow.enabled= is " \ "deprecated, use Rainbow.enabled= instead.") ::Rainbow.enabled = value end def self.enabled ::Rainbow.enabled end end end rainbow-1.99.1/lib/rainbow/null_presenter.rb0000644000004100000410000000115212260324232021074 0ustar www-datawww-datamodule Rainbow class NullPresenter < ::String def color(*values); self; end def background(*values); self; end def reset; self; end def bright; self; end def italic; self; end def underline; self; end def blink; self; end def inverse; self; end def hide; self; end def black; self; end def red; self; end def green; self; end def yellow; self; end def blue; self; end def magenta; self; end def cyan; self; end def white; self; end alias_method :foreground, :color alias_method :fg, :color alias_method :bg, :background end end rainbow-1.99.1/lib/rainbow/ext/0000755000004100000410000000000012260324232016307 5ustar www-datawww-datarainbow-1.99.1/lib/rainbow/ext/string.rb0000644000004100000410000000156612260324232020152 0ustar www-datawww-datarequire 'rainbow' module Rainbow module Ext module String module InstanceMethods def foreground(*color) Rainbow(self).foreground(*color) end alias_method :color, :foreground alias_method :colour, :foreground def background(*color) Rainbow(self).background(*color) end def reset Rainbow(self).reset end def bright Rainbow(self).bright end def italic Rainbow(self).italic end def underline Rainbow(self).underline end def blink Rainbow(self).blink end def inverse Rainbow(self).inverse end def hide Rainbow(self).hide end end end end end ::String.send(:include, Rainbow::Ext::String::InstanceMethods) rainbow-1.99.1/lib/rainbow.rb0000644000004100000410000000106012260324232016031 0ustar www-datawww-datarequire 'rainbow/global' require 'rainbow/legacy' require 'rainbow/ext/string' module Rainbow def self.new Wrapper.new(global.enabled) end self.enabled = false unless STDOUT.tty? && STDERR.tty? self.enabled = false if ENV['TERM'] == 'dumb' self.enabled = true if ENV['CLICOLOR_FORCE'] == '1' # On Windows systems, try to load the local ANSI support library require 'rbconfig' if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ begin require 'Win32/Console/ANSI' rescue LoadError self.enabled = false end end end rainbow-1.99.1/README.markdown0000644000004100000410000001217112260324232016003 0ustar www-datawww-data# Rainbow [![Build Status](https://travis-ci.org/sickill/rainbow.png?branch=master)](https://travis-ci.org/sickill/rainbow) [![Code Climate](https://codeclimate.com/github/sickill/rainbow.png)](https://codeclimate.com/github/sickill/rainbow) [![Coverage Status](https://coveralls.io/repos/sickill/rainbow/badge.png)](https://coveralls.io/r/sickill/rainbow) Rainbow is a ruby gem for colorizing printed text on ANSI terminals. It provides a string presenter object, which adds several methods to your strings for wrapping them in [ANSI escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code). These codes when printed in a terminal change text attributes like text color, background color, intensity etc. ## Usage To make your string colored wrap it with `Rainbow()` presenter and call `.color()` on it. ### Example ```ruby require 'rainbow' p Rainbow("this is red").red + " and " + Rainbow("this on yellow bg").bg(:yellow) + " and " + Rainbow("even bright underlined!").underline.bright # => "\e[31mthis is red\e[0m and \e[43mthis on yellow bg\e[0m and \e[4m\e[1meven bright underlined!\e[0m" ``` ### Rainbow presenter API Rainbow presenter adds the following methods to presented string: * `color(c)` (with `foreground`, and `fg` aliases) * `background(c)` (with `bg` alias) * `bright` * `underline` * `blink` * `inverse` * `hide` * `italic` (not well supported by terminal emulators). Text color can also be changed by calling a method named by a color: * `black` * `red` * `green` * `yellow` * `blue` * `magenta` * `cyan` * `white` All of the methods return `self` (the presenter object) so you can chain method calls: ```ruby Rainbow("hola!").blue.bright.underline ``` ### String mixin If you don't like wrapping every string you want to colorize with `Rainbow()` you can include all the rainbow presenter methods directly in a String class by requiring `rainbow/ext/string`: ```ruby require 'rainbow/ext/string' puts "this is red".color(:red) + " and " + "this on yellow bg".background(:yellow) + " and " + "even bright underlined!".underline.bright ``` This way of using Rainbow is not recommended though as it pollutes String's public interface with methods that are presentation specific. NOTE: the mixing doesn't include shortcut methods for changing text color, you should use "string".color(:blue) instead of "string".blue NOTE: the mixin is included in String by default in rainbow versions up to (and including) 1.99.x to not break backwards compatibility. It won't be included by default in rainbow 2.0. ### Color specification Both `color` and `background` accept color specified in any of the following ways: * color number (where 0 is black, 1 is red, 2 is green and so on): `Rainbow("hello").color(1)` * color name as a symbol (:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white): `Rainbow("hello").color(:yellow)`. This can be simplified to `Rainbow("hello").yellow` * RGB triplet as separate values in the range 0-255: `Rainbow("hello").color(115, 23, 98)` * RGB triplet as a hex string: `Rainbow("hello").color("FFC482")` or `Rainbow("hello").color("#FFC482")` When you specify a color with a RGB triplet rainbow finds the nearest match from 256 colors palette. Note that it requires a 256-colors capable terminal to display correctly. ### Configuration Rainbow can be enabled/disabled globally by setting: ```ruby Rainbow.enabled = true/false ``` When disabled all the methods return an unmodified string (`Rainbow("hello").red == "hello"`). It's enabled by default, unless STDOUT/STDERR is not a TTY or a terminal is dumb. ### Advanced usage `Rainbow()` and `Rainbow.enabled` operate on the global Rainbow wrapper instance. If you would like to selectively enable/disable coloring in separate 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_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) ``` By default each new instance inherits enabled/disabled state from the global `Rainbow.enabled`. This feature comes handy for example when you have multiple output formatters in your application and some of them print to a terminal but others write to a file. Normally rainbow would detect that STDIN/STDERR is a TTY and would colorize all the strings, even the ones that go through file writing formatters. You can easily solve that by disabling coloring for the Rainbow instances that are used by formatters with file output. ## Windows support For Windows support, you should install the following gems: ```ruby gem install windows-pr win32console ``` If the above gems aren't installed then all strings are returned unmodified. ## Installation Add it to your Gemfile: ```ruby gem 'rainbow' ``` Or just install it via rubygems: ```ruby gem install rainbow ``` ## Authors [Marcin Kulik](http://ku1ik.com/) and [great open-source contributors](https://github.com/sickill/rainbow/graphs/contributors). rainbow-1.99.1/.rubocop.yml0000644000004100000410000000024612260324232015554 0ustar www-datawww-dataDocumentation: Enabled: false EmptyLinesAroundBody: Enabled: false HashSyntax: Enabled: false MethodName: Enabled: false StringLiterals: Enabled: false rainbow-1.99.1/metadata.yml0000644000004100000410000000603612260324232015610 0ustar www-datawww-data--- !ruby/object:Gem::Specification name: rainbow version: !ruby/object:Gem::Version version: 1.99.1 platform: ruby authors: - Marcin Kulik autorequire: bindir: bin cert_chain: [] date: 2013-12-28 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: bundler requirement: !ruby/object:Gem::Requirement requirements: - - ~> - !ruby/object:Gem::Version version: '1.3' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - ~> - !ruby/object:Gem::Version version: '1.3' - !ruby/object:Gem::Dependency name: rake requirement: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' - !ruby/object:Gem::Dependency name: rspec requirement: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' type: :development prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' description: Colorize printed text on ANSI terminals email: - m@ku1ik.com executables: [] extensions: [] extra_rdoc_files: [] files: - .gitignore - .rubocop.yml - .travis.yml - Changelog.md - Gemfile - Guardfile - LICENSE - README.markdown - Rakefile - lib/rainbow.rb - lib/rainbow/color.rb - lib/rainbow/ext/string.rb - lib/rainbow/global.rb - lib/rainbow/legacy.rb - lib/rainbow/null_presenter.rb - lib/rainbow/presenter.rb - lib/rainbow/string_utils.rb - lib/rainbow/version.rb - lib/rainbow/wrapper.rb - rainbow.gemspec - spec/integration/instance_spec.rb - spec/integration/rainbow_spec.rb - spec/integration/string_spec.rb - spec/spec_helper.rb - spec/support/presenter_shared_examples.rb - spec/unit/color_spec.rb - spec/unit/namespace_spec.rb - spec/unit/null_presenter_spec.rb - spec/unit/presenter_spec.rb - spec/unit/string_utils_spec.rb - spec/unit/wrapper_spec.rb homepage: https://github.com/sickill/rainbow licenses: - MIT metadata: {} post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: 1.9.2 required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rubygems_version: 2.1.11 signing_key: specification_version: 4 summary: Colorize printed text on ANSI terminals test_files: - spec/integration/instance_spec.rb - spec/integration/rainbow_spec.rb - spec/integration/string_spec.rb - spec/spec_helper.rb - spec/support/presenter_shared_examples.rb - spec/unit/color_spec.rb - spec/unit/namespace_spec.rb - spec/unit/null_presenter_spec.rb - spec/unit/presenter_spec.rb - spec/unit/string_utils_spec.rb - spec/unit/wrapper_spec.rb rainbow-1.99.1/.gitignore0000644000004100000410000000023212260324232015265 0ustar www-datawww-data*.gem *.rbc .bundle .config .yardoc Gemfile.lock InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp rainbow-1.99.1/LICENSE0000644000004100000410000000203312260324232014303 0ustar www-datawww-dataCopyright (c) Marcin Kulik Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rainbow-1.99.1/checksums.yaml.gz0000444000004100000410000000041312260324232016564 0ustar www-datawww-dataUپRe1@}~`-`} `dbm/Ԝ^x>;*gB4qҎ,S[vD*tO>9(rW6%_N]9ۖmR[Н\vjKW3%-Y$S}W:yai .%uUsd L׿)Sf1!3ՋAx[ Vraʹ&<J7BujfnKi4֪XߟyUT+rainbow-1.99.1/Guardfile0000644000004100000410000000035412260324232015127 0ustar www-datawww-data# A sample Guardfile # More info at https://github.com/guard/guard#readme guard :rspec, cmd: 'rspec --color' do watch(%r{^spec/.+_spec\.rb$}) watch(%r{^lib/(.+)\.rb$}) { "spec" } watch('spec/spec_helper.rb') { "spec" } end