colored2-3.1.2/0000755000175000017500000000000013617304130012214 5ustar georggeorgcolored2-3.1.2/lib/0000755000175000017500000000000013617304130012762 5ustar georggeorgcolored2-3.1.2/lib/colored2.rb0000644000175000017500000000475013617304130015026 0ustar georggeorgrequire 'colored2/codes' require 'colored2/ascii_decorator' module Colored2 def self.decorate(a_class) a_class.send(:include, Colored2) end def self.included(from_class) from_class.class_eval do def surround_with_color(color: nil, effect: nil, color_self: nil, string: nil, &block) color_type = if Colored2.background_next? && effect.nil? Colored2.foreground_next! :background else :foreground end opts = {} opts.merge!(color_type => color) if color opts.merge!(effect: effect) if effect if color_self then opts.merge!( beginning: :on, end: :off) colored = Colored2::AsciiDecorator.new(self).decorate(opts) if string || block arg = "#{string}#{block.call if block}" colored << Colored2::AsciiDecorator.new(arg).decorate(opts) if arg.length > 0 end else opts.merge!( end: :on ) colored = Colored2::AsciiDecorator.new(self).decorate(opts) if string || block arg = "#{string}#{block.call if block}" colored << Colored2::AsciiDecorator.new(arg).decorate(opts.merge(end: :off)) if arg.length > 0 end end colored end def on Colored2.background_next! self end end from_class.instance_eval do COLORS.keys.each do |color| define_method(color) do |string = nil, &block| surround_with_color(color: color, color_self: true, string: string, &block) end define_method("#{color}!".to_sym) do |string = nil, &block| surround_with_color(color: color, color_self: false, string: string, &block) end end EFFECTS.keys.each do |effect| next if effect == 'no_color' define_method(effect) do |string = nil, &block| surround_with_color(effect: effect, color_self: true, string: string, &block) end define_method("#{effect}!".to_sym) do |string = nil, &block| surround_with_color(effect: effect, color_self: false, string: string, &block) end end define_method(:to_eol) do tmp = sub(/^(\e\[[\[\e0-9;m]+m)/, "\\1\e[2K") if tmp == self return "\e[2K" << self end tmp end define_method(:to_bol) do "#{self}\033[#{length}D\033[0D" end end end end require 'colored2/strings' colored2-3.1.2/lib/colored2/0000755000175000017500000000000013617304130014473 5ustar georggeorgcolored2-3.1.2/lib/colored2/codes.rb0000644000175000017500000000204113617304130016112 0ustar georggeorgmodule Colored2 COLORS = { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37 } EFFECTS = { no_color: 0, bold: 1, dark: 2, italic: 3, underlined: 4, reversed: 7, plain: 21, # non-bold normal: 22 } class Code attr_accessor :name, :escape def initialize(name) @name = name return if name.nil? @escape = codes[name.to_sym] raise ArgumentError.new("No color or effect named #{name} exists for #{self.class}.") if @escape.nil? end def value(shift = nil) escape_code = escape escape_code += shift if shift && escape_code name && escape ? "\e[#{escape_code}m" : '' end def to_s value end end class Effect < Code def codes EFFECTS end end class TextColor < Code def codes COLORS end end class BackgroundColor < TextColor def value super 10 end end end colored2-3.1.2/lib/colored2/object.rb0000644000175000017500000000015213617304130016264 0ustar georggeorgrequire 'colored2' unless defined?(Colored2) && Colored2.respond_to?(:decorate) Colored2.decorate(Object) colored2-3.1.2/lib/colored2/ascii_decorator.rb0000644000175000017500000000413013617304130020150 0ustar georggeorgrequire 'colored2/codes' require 'forwardable' module Colored2 def self.enable! Colored2::AsciiDecorator.enable! end def self.disable! Colored2::AsciiDecorator.disable! end def self.background_next! Colored2::AsciiDecorator.background_next! end def self.foreground_next! Colored2::AsciiDecorator.foreground_next! end def self.background_next? Colored2::AsciiDecorator.background_next? end class AsciiDecorator @__background_next = false @__colors_disabled = false class << self attr_accessor :__background_next, :__colors_disabled def enable! self.__colors_disabled = false end def enabled? !self.__colors_disabled end def disable! self.__colors_disabled = true end def background_next! self.__background_next = true end def foreground_next! self.__background_next = false end def background_next? self.__background_next end end extend Forwardable def_delegators :@my_class, :enable!, :disable! attr_accessor :string, :my_class def initialize(a_string) self.string = a_string.instance_of?(Object) ? '' : a_string.to_s self.my_class = self.class end # options[:start] = :color # options[:end] = :color | :no_color def decorate(options = {}) return string if !self.class.enabled? || string.length == 0 escape_sequence = [ Colored2::TextColor.new(options[:foreground]), Colored2::BackgroundColor.new(options[:background]), Colored2::Effect.new(options[:effect]) ].compact.join colored = '' colored << escape_sequence if options[:beginning] == :on colored << string if options[:end] colored << no_color if options[:end] == :off && !colored.end_with?(no_color) colored << escape_sequence if options[:end] == :on end colored end def un_decorate string.gsub(%r{\e\[\d+(;\d+)*m}, '') end private def no_color @no_color ||= Colored2::Effect.new(:no_color).to_s end end end colored2-3.1.2/lib/colored2/numbers.rb0000644000175000017500000000050313617304130016471 0ustar georggeorgrequire 'colored2' unless defined?(Colored2) && Colored2.respond_to?(:decorate) module Colored2 def self.integer_class major, minor = RUBY_VERSION.split(/\./).map(&:to_i) major >= 2 && minor >= 4 ? Integer : Kernel.const_get(:Fixnum) end end Colored2.decorate(Colored2.integer_class) Colored2.decorate(Float) colored2-3.1.2/lib/colored2/strings.rb0000644000175000017500000000015213617304130016507 0ustar georggeorgrequire 'colored2' unless defined?(Colored2) && Colored2.respond_to?(:decorate) Colored2.decorate(String) colored2-3.1.2/lib/colored2/version.rb0000644000175000017500000000005013617304130016500 0ustar georggeorgmodule Colored2 VERSION = '3.1.2' end colored2-3.1.2/Rakefile0000644000175000017500000000034313617304130013661 0ustar georggeorgrequire 'bundler' require 'bundler/gem_tasks' require 'rake/clean' CLEAN.include %w(pkg coverage *.gem) begin require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) rescue LoadError end task :default => [:spec] colored2-3.1.2/README.md0000644000175000017500000000741613617304130013503 0ustar georggeorg[![Gem Version](https://badge.fury.io/rb/colored2.svg)](https://badge.fury.io/rb/colored2) [![Downloads](http://ruby-gem-downloads-badge.herokuapp.com/colored2?type=total)](https://rubygems.org/gems/colored2) [![Gitter](https://img.shields.io/gitter/room/gitterHQ/gitter.svg)](https://gitter.im/colored2) [![Build Status](https://travis-ci.org/kigster/colored2.svg?branch=master)](https://travis-ci.org/kigster/colored2) [![Test Coverage](https://codeclimate.com/github/kigster/colored2/badges/coverage.svg)](https://codeclimate.com/github/kigster/colored2/coverage) [![Code Climate](https://codeclimate.com/github/kigster/colored2/badges/gpa.svg)](https://codeclimate.com/github/kigster/colored2) [![Issue Count](https://codeclimate.com/github/kigster/colored2/badges/issue_count.svg)](https://codeclimate.com/github/kigster/colored2) ## Colored2 This is an actively maintained fork of Chris (defunkt) Wanstrath's gem [colored](https://github.com/defunkt/colored), which appears to be no longer supported. This fork comes with a slightly spruced up syntax, some additional features, and a test suite written in [RSpec](http://rspec.info/). ## Usage In addition to the simple syntax of the original gem, which affected only the string to the left of the method call, the new "bang" syntax affects a string to the right. If the block or a method argument is provided, the contents is wrapped in the color, and the color is then reset back. If no block or argument is provided, the color is left open-ended, and must be explicitly reset – when using the 'bang' notation. ![](doc/colored2-session1.png) ### Complete set of colors: * black * red * green * yellow * blue * magenta * cyan * white ### Complete Set of Effects > Note: previous versions used method name `clear` instead of `no_color`, which clashed with many 3rd party frameworks that defined similarly named method in the global namespace. > This highlights the dangers of introducing so many words into the `String` namespace. * no_color * bold * dark * italic * underlined * reversed * plain * normal ## Usage in Other Classes With this gem you can add color to not just strings, but to any other class. `String` class is automatically decorated as soon as `require 'colored2'` is parsed by the ruby interpreter. Note that future versions may refrain from auto-requiring `colored2/strings`, and rely on explicitly requiring components they need colorized, eg `require 'colored2/numbers'`. To color numbers, require the following file, which automatically decorates `Integer` and `Float`. You can also add color methods to the `Object`. Finally, you can add the methods to any custom class by including the `Colored2` Module. Below is an `IRB` — session that shows a slightly more advanced usage. ![](doc/colored2-session2.png) ## Additional Helpers There are several additional helpers tucked onto the `String` class. * `#to_bol` (to beginning of the line) will rewind the cursor back to the beginning of the current line. * `#to_eol` (to end of line) ## Installation Add this line to your application's Gemfile: gem 'colored2' And then execute: $ bundle Or install it yourself as: $ gem install colored2 ## Development To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). ## Contributing Bug reports and pull requests are welcome on GitHub at [https://github.com/kigster/colored2](https://github.com/kigster/colored2). ## License The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). colored2-3.1.2/spec/0000755000175000017500000000000013617304130013146 5ustar georggeorgcolored2-3.1.2/spec/spec_helper.rb0000644000175000017500000000007313617304130015764 0ustar georggeorgrequire 'simplecov' SimpleCov.start require 'rspec/core' colored2-3.1.2/spec/colored2_spec.rb0000644000175000017500000000071613617304130016222 0ustar georggeorgrequire 'spec_helper' require 'colored2/strings' RSpec.describe Colored2 do describe 'global enable and disable' do before do Colored2.disable! end after do Colored2.enable! end let(:sample) { 'sample string' } describe 'colors' do subject { sample.red.on.blue } it { should eql(sample) } end describe 'effects' do subject { sample.bold.on.red } it { should eql(sample) } end end end colored2-3.1.2/spec/colored2/0000755000175000017500000000000013617304130014657 5ustar georggeorgcolored2-3.1.2/spec/colored2/numbers_spec.rb0000644000175000017500000000127613617304130017677 0ustar georggeorgrequire File.expand_path('spec/spec_helper') require 'colored2/numbers' require 'colored2/strings' RSpec.describe Colored2.integer_class do describe 'with foreground and background colors' do it 'should work with one color' do expect(32.red).to eql('32'.red) end it 'should insert escape sequences' do expect(32.red).to eql("\e[31m32\e[0m") end end end RSpec.describe Float do describe 'with foreground and background colors' do it 'should add two colors chained' do expect((32.5).blue.on.red).to eql('32.5'.blue.on.red) end it 'should insert escape sequences' do expect((32.5).blue.on.red).to eql("\e[41m\e[34m32.5\e[0m") end end end colored2-3.1.2/spec/colored2/object_spec.rb0000644000175000017500000000106513617304130017466 0ustar georggeorgrequire File.expand_path('spec/spec_helper') require 'colored2/object' subject1 = red('hello') subject2 = red('blue').on.blue subject3 = on.yellow('on yellow') RSpec.describe Object do describe 'with foreground and background colors' do it 'should work with one color' do expect(subject1).to eql('hello'.red) end it 'should work with color on color' do expect(subject2).to eql('blue'.red.on.blue) end it 'should add background color using on_' do expect(subject3).to eql('on yellow'.on.yellow) end end end colored2-3.1.2/spec/colored2/strings_spec.rb0000644000175000017500000000507113617304130017712 0ustar georggeorgrequire File.expand_path('spec/spec_helper') require 'colored2/strings' RSpec.describe String do before do Colored2.decorate(String) end describe 'with foreground and background colors' do it 'should work with one color' do expect('red'.red).to eql("\e[31mred\e[0m") end it 'should add two colors chained' do expect('blue'.red.blue).to eql("\e[34m\e[31mblue\e[0m") end it 'should add background color using on_' do expect('on yellow'.on.yellow).to eql("\e[43mon yellow\e[0m") end it 'should work with _on_ syntax' do expect('red on blue'.red.on.blue).to eql("\e[44m\e[31mred on blue\e[0m") end end describe 'with effects' do it 'should add a bold modifier' do expect('way bold'.bold).to eql("\e[1mway bold\e[0m") end it 'should let modifiers stack' do expect('underlinedd bold'.bold.underlined).to eql("\e[4m\e[1munderlinedd bold\e[0m") end it 'should let modifiers stack with colors' do expect('cyan underlinedd bold'.bold.underlined.cyan).to eql("\e[36m\e[4m\e[1mcyan underlinedd bold\e[0m") end end describe 'new block syntax' do it 'should defined block syntax nested colors' do expect('No Color, then'.blue!('blue inside')).to eql('No Color, then' + 'blue inside'.blue) end it 'should defined block syntax nested colors two levels deep' do expect('regular here'.blue! + 'blue here'.no_color!).to eql('regular here' << 'blue here'.blue) end it 'should defined block syntax nested colors two levels deep' do expect('regular here'.blue! { 'something else'.red!('red riding hood') }).to eql('regular here'.blue! << 'something else'.red! << 'red riding hood'.no_color!) end it 'should defined block syntax nested colors two levels deep' do expectation = 'this is regular, but '.red! do 'this is red '.yellow! do ' and yellow'.no_color! end end expect(expectation).to eql('this is regular, but '.red! << 'this is red '.yellow! << ' and yellow'.no_color!) end end describe 'end of line' do it 'should work with eol' do expect('nothing to see here really.'.to_eol).to eql("\e[2Knothing to see here really.") end it 'should work with eol_with_with_two_colors' do expect('blue'.red.blue.to_eol).to eql("\e[34m\e[31m\e[2Kblue\e[0m") end it 'should work with eol_with_modifiers_stack_with_colors' do expect('cyan underlinedd bold'.bold.underlined.cyan.to_eol).to eql("\e[36m\e[4m\e[1m\e[2Kcyan underlinedd bold\e[0m") end end end colored2-3.1.2/colored2.gemspec0000644000175000017500000000614113617304130015274 0ustar georggeorg######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: colored2 3.1.2 ruby lib Gem::Specification.new do |s| s.name = "colored2".freeze s.version = "3.1.2" s.required_rubygems_version = Gem::Requirement.new(">= 1.3.6".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["Chris Wanstrath".freeze, "Konstantin Gredeskoul".freeze] s.date = "2017-02-14" s.description = "This is a heavily modified fork of http://github.com/defunkt/colored gem, with many\nsensible pull requests combined. Since the authors of the original gem no longer support it,\nthis might, perhaps, be considered a good alternative.\n\nSimple gem that adds various color methods to String class, and can be used as follows:\n\n require 'colored2'\n\n puts 'this is red'.red\n puts 'this is red with a yellow background'.red.on.yellow\n puts 'this is red with and italic'.red.italic\n puts 'this is green bold'.green.bold << ' and regular'.green\n puts 'this is really bold blue on white but reversed'.bold.blue.on.white.reversed\n puts 'this is regular, but '.red! << 'this is red '.yellow! << ' and yellow.'.no_color!\n puts ('this is regular, but '.red! do\n 'this is red '.yellow! do\n ' and yellow.'.no_color!\n end\n end)\n\n".freeze s.email = "kigster@gmail.com".freeze s.files = ["LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "lib/colored2.rb".freeze, "lib/colored2/ascii_decorator.rb".freeze, "lib/colored2/codes.rb".freeze, "lib/colored2/numbers.rb".freeze, "lib/colored2/object.rb".freeze, "lib/colored2/strings.rb".freeze, "lib/colored2/version.rb".freeze, "spec/colored2/numbers_spec.rb".freeze, "spec/colored2/object_spec.rb".freeze, "spec/colored2/strings_spec.rb".freeze, "spec/colored2_spec.rb".freeze, "spec/spec_helper.rb".freeze] s.homepage = "http://github.com/kigster/colored2".freeze s.licenses = ["MIT".freeze] s.required_ruby_version = Gem::Requirement.new(">= 2.0.0".freeze) s.rubygems_version = "2.7.6.2".freeze s.summary = "Add even more color to your life.".freeze s.test_files = ["spec/colored2/numbers_spec.rb".freeze, "spec/colored2/object_spec.rb".freeze, "spec/colored2/strings_spec.rb".freeze, "spec/colored2_spec.rb".freeze, "spec/spec_helper.rb".freeze] if s.respond_to? :specification_version then s.specification_version = 4 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, ["~> 10.0"]) s.add_development_dependency(%q.freeze, ["~> 3.4"]) else s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, ["~> 10.0"]) s.add_dependency(%q.freeze, ["~> 3.4"]) end else s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, ["~> 10.0"]) s.add_dependency(%q.freeze, ["~> 3.4"]) end end colored2-3.1.2/LICENSE0000644000175000017500000000213113617304130013216 0ustar georggeorgCopyright (c) 2010 Chris Wanstrath Copyright (c) 2016 Konstantin Gredeskoul MIT License 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.