colored2 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a23ca7f2255c16d188e9666fe9ef292129470141
4
+ data.tar.gz: c242e7fa809669503778e851d6cd68a8079c3830
5
+ SHA512:
6
+ metadata.gz: 9be8019ddb511d36eb5021459700946d514430744e75ffacce4976c41ce920847ac27aa6bb973c672be89cf1939073617802b01712c265bf8801d18d7afd1d76
7
+ data.tar.gz: 220144edad3f117953a5abfb390dd0404b71dce699a2156d1de5213a4ec5dd276e25c594a6769935cf891a202331917cd8a578f84d0efe534a9305620566f481
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Konstantin Gredeskoul
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ [![Gem Version](https://badge.fury.io/rb/colored2.svg)](https://badge.fury.io/rb/colored2)
2
+ [![Build Status](https://travis-ci.org/kigster/colored2.svg?branch=master)](https://travis-ci.org/kigster/colored2)
3
+ [![Code Climate](https://codeclimate.com/repos/56dff58d80b254164b003d80/badges/176e4c39b20bfed90478/gpa.svg)](https://codeclimate.com/repos/56dff58d80b254164b003d80/feed)
4
+ [![Test Coverage](https://codeclimate.com/repos/56dff58d80b254164b003d80/badges/176e4c39b20bfed90478/coverage.svg)](https://codeclimate.com/repos/56dff58d80b254164b003d80/coverage)
5
+ [![Issue Count](https://codeclimate.com/repos/56dff58d80b254164b003d80/badges/176e4c39b20bfed90478/issue_count.svg)](https://codeclimate.com/repos/56dff58d80b254164b003d80/feed)
6
+
7
+ ## Colored2
8
+
9
+ This is a fork of Chris (defunkt) Wanstrath's colored gem, which appears to be no longer supported.
10
+
11
+ This fork comes with a slightly spruced up syntax and rspecs.
12
+
13
+ ## Usage
14
+
15
+ In addition to the simple syntax of the original gem, which affected only the string to the left of the method call,
16
+ the "bang" syntax affects a string to the right. If the block or a method argument is provided,
17
+ the contents is wrapped in the color, and the color is then reset back. If no block
18
+ or argument is provided, the color is left open-ended, and must be explicitly reset – when using the 'bang' notation.
19
+
20
+ ![](doc/colored2-session1.png)
21
+
22
+ ## Usage in Other Classes
23
+
24
+ You can decorate and color not just strings.
25
+
26
+ To color numbers, require the following file, which automatically decorates `Fixnum` and `Float`. You can also add color methods
27
+ to the `Object`. Finally, you can add the methods to any custom class by including the `Colored2` Module.
28
+
29
+ Below is an `IRB` session that shows a slightly more advanced usage.
30
+
31
+ ![](doc/colored2-session2.png)
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/clean'
4
+
5
+ CLEAN.include %w(pkg coverage *.gem)
6
+
7
+ begin
8
+ require 'rspec/core/rake_task'
9
+ RSpec::Core::RakeTask.new(:spec)
10
+ rescue LoadError
11
+ end
12
+
13
+ task :default => [:spec]
@@ -0,0 +1,77 @@
1
+ require 'colored2/constants'
2
+ require 'colored2/ascii_decorator'
3
+
4
+ module Colored2
5
+ def self.decorate(a_class)
6
+ a_class.send(:include, Colored2)
7
+ end
8
+
9
+ def self.included(from_class)
10
+ from_class.class_eval do
11
+
12
+ def surround_with_color(color_or_effect, color_self, string = nil, &block)
13
+ color_type = if Colored2.background_next?
14
+ Colored2.foreground_next!
15
+ :background
16
+ else
17
+ :foreground
18
+ end
19
+
20
+ if color_self then
21
+ opts = { beginning: :on, end: :off, color_type => color_or_effect, effect: color_or_effect }
22
+ colored = Colored2::AsciiDecorator.new(self).decorate(opts)
23
+ if string || block
24
+ arg = "#{string}#{block.call if block}"
25
+ colored << Colored2::AsciiDecorator.new(arg).decorate(opts) if arg.length > 0
26
+ end
27
+ else
28
+ opts = { end: :on, color_type => color_or_effect, effect: color_or_effect}
29
+ colored = Colored2::AsciiDecorator.new(self).decorate(opts)
30
+ if string || block
31
+ arg = "#{string}#{block.call if block}"
32
+ colored << Colored2::AsciiDecorator.new(arg).decorate(opts.merge(end: :off)) if arg.length > 0
33
+ end
34
+ end
35
+ colored
36
+ end
37
+
38
+ def on
39
+ Colored2.background_next!
40
+ self
41
+ end
42
+ end
43
+
44
+ from_class.instance_eval do
45
+ COLORS.keys.each do |color|
46
+ define_method(color) do |string = nil, &block|
47
+ surround_with_color(color, true, string, &block)
48
+ end
49
+
50
+ define_method("#{color}!".to_sym) do |string = nil, &block|
51
+ surround_with_color(color, false, string, &block)
52
+ end
53
+ end
54
+
55
+ EFFECTS.keys.each do |effect|
56
+ next if effect == 'clear'
57
+ define_method(effect) do |string = nil, &block|
58
+ surround_with_color(effect, true, string, &block)
59
+ end
60
+
61
+ define_method("#{effect}!".to_sym) do |string = nil, &block|
62
+ surround_with_color(effect, false, string, &block)
63
+ end
64
+ end
65
+
66
+ define_method(:to_eol) do
67
+ tmp = sub(/^(\e\[[\[\e0-9;m]+m)/, "\\1\e[2K")
68
+ if tmp == self
69
+ return "\e[2K" << self
70
+ end
71
+ tmp
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ require 'colored2/strings'
@@ -0,0 +1,101 @@
1
+ require_relative 'constants'
2
+ require 'forwardable'
3
+
4
+ module Colored2
5
+ def self.enable!
6
+ Colored2::AsciiDecorator.enable!
7
+ end
8
+ def self.disable!
9
+ Colored2::AsciiDecorator.disable!
10
+ end
11
+ def self.background_next!
12
+ Colored2::AsciiDecorator.background_next!
13
+ end
14
+ def self.foreground_next!
15
+ Colored2::AsciiDecorator.foreground_next!
16
+ end
17
+ def self.background_next?
18
+ Colored2::AsciiDecorator.background_next?
19
+ end
20
+
21
+ class AsciiDecorator
22
+ @__background_next = false
23
+ @__colors_disabled = false
24
+ class << self
25
+ attr_accessor :__background_next, :__colors_disabled
26
+ def enable!
27
+ self.__colors_disabled = false
28
+ end
29
+ def enabled?
30
+ !self.__colors_disabled
31
+ end
32
+ def disable!
33
+ self.__colors_disabled = true
34
+ end
35
+ def background_next!
36
+ self.__background_next = true
37
+ end
38
+ def foreground_next!
39
+ self.__background_next = false
40
+ end
41
+ def background_next?
42
+ self.__background_next
43
+ end
44
+
45
+ def clear
46
+ effect(:clear)
47
+ end
48
+
49
+ def colors
50
+ @colors ||= COLORS.keys.sort
51
+ end
52
+
53
+ def effect(effect_name)
54
+ "\e[#{EFFECTS[effect_name]}m" if effect_name && EFFECTS[effect_name.to_sym]
55
+ end
56
+
57
+ def color(color_name:, type:)
58
+ background_code = (type == :background) ? 10 : 0
59
+ if color_name && COLORS[color_name.to_sym]
60
+ "\e[#{COLORS[color_name.to_sym] + background_code}m"
61
+ end
62
+ end
63
+ end
64
+
65
+ extend Forwardable
66
+ def_delegators :@my_class, :enable!, :disable!, :clear, :colors, :effect, :color
67
+
68
+ attr_accessor :string, :my_class
69
+
70
+ def initialize(a_string)
71
+ self.string = a_string.instance_of?(Object) ? '' : a_string.to_s
72
+ self.my_class = self.class
73
+ end
74
+
75
+ # options[:start] = :color
76
+ # options[:end] = :color | :clear
77
+ def decorate(options = {})
78
+
79
+ return string if !self.class.enabled? || string.length == 0
80
+ escape_sequence = [
81
+ color(color_name: options[:foreground], type: :foreground),
82
+ color(color_name: options[:background], type: :background),
83
+ effect(options[:effect])
84
+ ].compact.join
85
+
86
+ colored = ''
87
+ colored << escape_sequence if options[:beginning] == :on
88
+ colored << string
89
+ if options[:end]
90
+ colored << clear if options[:end] == :off && !colored.end_with?(clear)
91
+ colored << escape_sequence if options[:end] == :on
92
+ end
93
+ colored
94
+ end
95
+
96
+ def un_decorate
97
+ string.gsub(%r{\e\[\d+(;\d+)*m}, '')
98
+ end
99
+
100
+ end
101
+ end
@@ -0,0 +1,23 @@
1
+ module Colored2
2
+ COLORS = {
3
+ black: 30,
4
+ red: 31,
5
+ green: 32,
6
+ yellow: 33,
7
+ blue: 34,
8
+ magenta: 35,
9
+ cyan: 36,
10
+ white: 37
11
+ }
12
+ EFFECTS = {
13
+ clear: 0,
14
+ bold: 1,
15
+ dark: 2,
16
+ italic: 3,
17
+ underlined: 4,
18
+ reversed: 7,
19
+ plain: 21, # non-bold
20
+ normal: 22
21
+ }
22
+ ESCAPES = COLORS.merge(EFFECTS)
23
+ end
@@ -0,0 +1,3 @@
1
+ require 'colored2'
2
+ Colored2.decorate(Fixnum)
3
+ Colored2.decorate(Float)
@@ -0,0 +1,2 @@
1
+ require 'colored2'
2
+ Colored2.decorate(Object)
@@ -0,0 +1,2 @@
1
+ require 'colored2'
2
+ Colored2.decorate(String)
@@ -0,0 +1,3 @@
1
+ module Colored2
2
+ VERSION = '2.0.0'
3
+ end
@@ -0,0 +1,19 @@
1
+ require_relative 'spec_helper'
2
+ require_relative '../lib/colored2/numbers'
3
+ require_relative '../lib/colored2/strings'
4
+
5
+ RSpec.describe Fixnum do
6
+ describe 'with foreground and background colors' do
7
+ it 'should work with one color' do
8
+ expect(32.red).to eql('32'.red)
9
+ end
10
+ end
11
+ end
12
+
13
+ RSpec.describe Float do
14
+ describe 'with foreground and background colors' do
15
+ it 'should add two colors chained' do
16
+ expect((32.5).blue.on.red).to eql('32.5'.blue.on.red)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ require_relative 'spec_helper'
2
+ require_relative '../lib/colored2/object'
3
+
4
+ subject1 = red('hello')
5
+ subject2 = red('blue').on.blue
6
+ subject3 = on.yellow('on yellow')
7
+
8
+ RSpec.describe Object do
9
+
10
+ describe 'with foreground and background colors' do
11
+ it 'should work with one color' do
12
+ expect(subject1).to eql('hello'.red)
13
+ end
14
+
15
+ it 'should work with color on color' do
16
+ expect(subject2).to eql('blue'.red.on.blue)
17
+ end
18
+
19
+ it 'should add background color using on_<color>' do
20
+ expect(subject3).to eql('on yellow'.on.yellow)
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+ require 'colored2/strings'
3
+
4
+ RSpec.describe Colored2 do
5
+ describe 'global enable and disable' do
6
+ before do
7
+ Colored2.disable!
8
+ end
9
+ after do
10
+ Colored2.enable!
11
+ end
12
+ let(:sample) { 'sample string' }
13
+
14
+ describe 'colors' do
15
+ subject { sample.red.on.blue }
16
+ it { should eql(sample) }
17
+ end
18
+ describe 'effects' do
19
+ subject { sample.bold.on.red }
20
+ it { should eql(sample) }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,77 @@
1
+ require_relative 'spec_helper'
2
+ require_relative '../lib/colored2/strings'
3
+
4
+ RSpec.describe String do
5
+ before do
6
+ Colored2.decorate(String)
7
+ end
8
+
9
+ describe 'with foreground and background colors' do
10
+ it 'should work with one color' do
11
+ expect('red'.red).to eql("\e[31mred\e[0m")
12
+ end
13
+
14
+ it 'should add two colors chained' do
15
+ expect('blue'.red.blue).to eql("\e[34m\e[31mblue\e[0m")
16
+ end
17
+
18
+ it 'should add background color using on_<color>' do
19
+ expect('on yellow'.on.yellow).to eql("\e[43mon yellow\e[0m")
20
+ end
21
+
22
+ it 'should work with <color>_on_<color> syntax' do
23
+ expect('red on blue'.red.on.blue).to eql("\e[44m\e[31mred on blue\e[0m")
24
+ end
25
+ end
26
+
27
+ describe 'with effects' do
28
+ it 'should add a bold modifier' do
29
+ expect('way bold'.bold).to eql("\e[1mway bold\e[0m")
30
+ end
31
+
32
+ it 'should let modifiers stack' do
33
+ expect('underlinedd bold'.bold.underlined).to eql("\e[4m\e[1munderlinedd bold\e[0m")
34
+ end
35
+
36
+ it 'should let modifiers stack with colors' do
37
+ expect('cyan underlinedd bold'.bold.underlined.cyan).to eql("\e[36m\e[4m\e[1mcyan underlinedd bold\e[0m")
38
+ end
39
+ end
40
+
41
+ describe 'new block syntax' do
42
+ it 'should defined block syntax nested colors' do
43
+ expect('No Color, then'.blue!('blue inside')).to eql('No Color, then' + 'blue inside'.blue)
44
+ end
45
+
46
+ it 'should defined block syntax nested colors two levels deep' do
47
+ expect('regular here'.blue! + 'blue here'.clear!).to eql('regular here' << 'blue here'.blue)
48
+ end
49
+
50
+ it 'should defined block syntax nested colors two levels deep' do
51
+ expect('regular here'.blue! { 'something else'.red!('red riding hood') }).to eql('regular here'.blue! << 'something else'.red! << 'red riding hood'.clear!)
52
+ end
53
+
54
+ it 'should defined block syntax nested colors two levels deep' do
55
+ expectation = 'this is regular, but '.red! do
56
+ 'this is red '.yellow! do
57
+ ' and yellow'.clear!
58
+ end
59
+ end
60
+ expect(expectation).to eql('this is regular, but '.red! << 'this is red '.yellow! << ' and yellow'.clear!)
61
+ end
62
+ end
63
+
64
+ describe 'end of line' do
65
+ it 'should work with eol' do
66
+ expect('nothing to see here really.'.to_eol).to eql("\e[2Knothing to see here really.")
67
+ end
68
+
69
+ it 'should work with eol_with_with_two_colors' do
70
+ expect('blue'.red.blue.to_eol).to eql("\e[34m\e[31m\e[2Kblue\e[0m")
71
+ end
72
+
73
+ it 'should work with eol_with_modifiers_stack_with_colors' do
74
+ expect('cyan underlinedd bold'.bold.underlined.cyan.to_eol).to eql("\e[36m\e[4m\e[1m\e[2Kcyan underlinedd bold\e[0m")
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,5 @@
1
+ require 'codeclimate-test-reporter'
2
+ CodeClimate::TestReporter.start
3
+
4
+ require 'rspec/core'
5
+
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: colored2
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Konstantin Gredeskoul
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: codeclimate-test-reporter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: |+
56
+ This is a heavily modified fork of http://github.com/defunkt/colored gem, with many
57
+ sensible pull requests combined. Since the authors of the original gem no longer support it,
58
+ this might, perhaps, be considered a good alternative.
59
+
60
+ Simple gem that adds various color methods to String class, and can be used as follows:
61
+
62
+ require 'colored2'
63
+
64
+ puts 'this is red'.red
65
+ puts 'this is red with a yellow background'.red.on.yellow
66
+ puts 'this is red with and italic'.red.italic
67
+ puts 'this is green bold'.green.bold << ' and regular'.green
68
+ puts 'this is really bold blue on white but reversed'.bold.blue.on.white.reversed
69
+ puts 'this is regular, but '.red! << 'this is red '.yellow! << ' and yellow.'.clear!
70
+ puts ('this is regular, but '.red! do
71
+ 'this is red '.yellow! do
72
+ ' and yellow.'.clear!
73
+ end
74
+ end)
75
+
76
+ email: kig@reinvent.one
77
+ executables: []
78
+ extensions: []
79
+ extra_rdoc_files: []
80
+ files:
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
84
+ - lib/colored2.rb
85
+ - lib/colored2/ascii_decorator.rb
86
+ - lib/colored2/constants.rb
87
+ - lib/colored2/numbers.rb
88
+ - lib/colored2/object.rb
89
+ - lib/colored2/strings.rb
90
+ - lib/colored2/version.rb
91
+ - spec/colored2_numbers_spec.rb
92
+ - spec/colored2_object_spec.rb
93
+ - spec/colored2_spec.rb
94
+ - spec/colored2_string_spec.rb
95
+ - spec/spec_helper.rb
96
+ homepage: http://github.com/kigster/colored
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: 2.0.0
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 1.3.6
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.5.1
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Add even more color to your life.
120
+ test_files:
121
+ - spec/colored2_numbers_spec.rb
122
+ - spec/colored2_object_spec.rb
123
+ - spec/colored2_spec.rb
124
+ - spec/colored2_string_spec.rb
125
+ - spec/spec_helper.rb