rainbow 2.2.1 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,38 +1,100 @@
1
- module Rainbow
1
+ # frozen_string_literal: true
2
2
 
3
+ module Rainbow
3
4
  class NullPresenter < ::String
5
+ def color(*_values)
6
+ self
7
+ end
8
+
9
+ def background(*_values)
10
+ self
11
+ end
12
+
13
+ def reset
14
+ self
15
+ end
16
+
17
+ def bright
18
+ self
19
+ end
20
+
21
+ def faint
22
+ self
23
+ end
24
+
25
+ def italic
26
+ self
27
+ end
28
+
29
+ def underline
30
+ self
31
+ end
32
+
33
+ def blink
34
+ self
35
+ end
36
+
37
+ def inverse
38
+ self
39
+ end
40
+
41
+ def hide
42
+ self
43
+ end
4
44
 
5
- def color(*values); self; end
6
- def background(*values); self; end
7
- def reset; self; end
8
- def bright; self; end
9
- def italic; self; end
10
- def underline; self; end
11
- def blink; self; end
12
- def inverse; self; end
13
- def hide; self; end
14
-
15
- def black; self; end
16
- def red; self; end
17
- def green; self; end
18
- def yellow; self; end
19
- def blue; self; end
20
- def magenta; self; end
21
- def cyan; self; end
22
- def white; self; end
23
-
24
- def method_missing(method_name,*args)
25
- if Color::X11Named.color_names.include? method_name and args.empty? then
45
+ def cross_out
46
+ self
47
+ end
48
+
49
+ def black
50
+ self
51
+ end
52
+
53
+ def red
54
+ self
55
+ end
56
+
57
+ def green
58
+ self
59
+ end
60
+
61
+ def yellow
62
+ self
63
+ end
64
+
65
+ def blue
66
+ self
67
+ end
68
+
69
+ def magenta
70
+ self
71
+ end
72
+
73
+ def cyan
74
+ self
75
+ end
76
+
77
+ def white
78
+ self
79
+ end
80
+
81
+ def method_missing(method_name, *args)
82
+ if Color::X11Named.color_names.include?(method_name) && args.empty?
26
83
  self
27
84
  else
28
85
  super
29
86
  end
30
87
  end
31
88
 
32
- alias_method :foreground, :color
33
- alias_method :fg, :color
34
- alias_method :bg, :background
89
+ def respond_to_missing?(method_name, *args)
90
+ Color::X11Named.color_names.include?(method_name) && args.empty? || super
91
+ end
35
92
 
93
+ alias foreground color
94
+ alias fg color
95
+ alias bg background
96
+ alias bold bright
97
+ alias dark faint
98
+ alias strike cross_out
36
99
  end
37
-
38
100
  end
@@ -1,35 +1,37 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'string_utils'
2
4
  require_relative 'x11_color_names'
3
5
  require_relative 'color'
4
6
 
5
7
  module Rainbow
6
-
7
8
  class Presenter < ::String
8
-
9
9
  TERM_EFFECTS = {
10
- reset: 0,
11
- bright: 1,
12
- italic: 3,
10
+ reset: 0,
11
+ bright: 1,
12
+ faint: 2,
13
+ italic: 3,
13
14
  underline: 4,
14
- blink: 5,
15
- inverse: 7,
16
- hide: 8,
17
- }
15
+ blink: 5,
16
+ inverse: 7,
17
+ hide: 8,
18
+ cross_out: 9
19
+ }.freeze
18
20
 
19
21
  # Sets color of this text.
20
22
  def color(*values)
21
23
  wrap_with_sgr(Color.build(:foreground, values).codes)
22
24
  end
23
25
 
24
- alias_method :foreground, :color
25
- alias_method :fg, :color
26
+ alias foreground color
27
+ alias fg color
26
28
 
27
29
  # Sets background color of this text.
28
30
  def background(*values)
29
31
  wrap_with_sgr(Color.build(:background, values).codes)
30
32
  end
31
33
 
32
- alias_method :bg, :background
34
+ alias bg background
33
35
 
34
36
  # Resets terminal to default colors/backgrounds.
35
37
  #
@@ -44,7 +46,15 @@ module Rainbow
44
46
  wrap_with_sgr(TERM_EFFECTS[:bright])
45
47
  end
46
48
 
47
- alias_method :bold, :bright
49
+ alias bold bright
50
+
51
+ # Turns on faint/dark for this text (not well supported by terminal
52
+ # emulators).
53
+ def faint
54
+ wrap_with_sgr(TERM_EFFECTS[:faint])
55
+ end
56
+
57
+ alias dark faint
48
58
 
49
59
  # Turns on italic style for this text (not well supported by terminal
50
60
  # emulators).
@@ -73,6 +83,12 @@ module Rainbow
73
83
  wrap_with_sgr(TERM_EFFECTS[:hide])
74
84
  end
75
85
 
86
+ def cross_out
87
+ wrap_with_sgr(TERM_EFFECTS[:cross_out])
88
+ end
89
+
90
+ alias strike cross_out
91
+
76
92
  def black
77
93
  color(:black)
78
94
  end
@@ -107,20 +123,22 @@ module Rainbow
107
123
 
108
124
  # We take care of X11 color method call here.
109
125
  # Such as #aqua, #ghostwhite.
110
- def method_missing(method_name,*args)
111
- if Color::X11Named.color_names.include? method_name and args.empty? then
126
+ def method_missing(method_name, *args)
127
+ if Color::X11Named.color_names.include?(method_name) && args.empty?
112
128
  color(method_name)
113
129
  else
114
130
  super
115
131
  end
116
132
  end
117
133
 
134
+ def respond_to_missing?(method_name, *args)
135
+ Color::X11Named.color_names.include?(method_name) && args.empty? || super
136
+ end
137
+
118
138
  private
119
139
 
120
140
  def wrap_with_sgr(codes) #:nodoc:
121
141
  self.class.new(StringUtils.wrap_with_sgr(self, [*codes]))
122
142
  end
123
-
124
143
  end
125
-
126
144
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'presenter'
4
+ require_relative 'global'
5
+
6
+ module Rainbow
7
+ refine String do
8
+ Presenter.instance_methods(false).each do |method_name|
9
+ define_method(method_name) do |*args|
10
+ ::Rainbow.global.wrap(self).send(method_name, *args)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,20 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rainbow
2
4
  class StringUtils
3
-
4
5
  def self.wrap_with_sgr(string, codes)
5
6
  return string if codes.empty?
6
7
 
7
8
  seq = "\e[" + codes.join(";") + "m"
8
- match = string.match(/^(\e\[([\d;]+)m)*/)
9
- seq_pos = match.end(0)
10
- string = string[0...seq_pos] + seq + string[seq_pos..-1]
11
9
 
12
- unless string =~ /\e\[0m$/
13
- string = string + "\e[0m"
14
- end
10
+ string = string.sub(/^(\e\[([\d;]+)m)*/) { |m| m + seq }
15
11
 
16
- string
12
+ return string if string.end_with? "\e[0m"
13
+
14
+ string + "\e[0m"
17
15
  end
18
16
 
17
+ def self.uncolor(string)
18
+ # See http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
19
+ string.gsub(/\e\[[0-9;]*m/, '')
20
+ end
19
21
  end
20
22
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rainbow
2
- VERSION = "2.2.1".freeze
4
+ VERSION = "3.1.1"
3
5
  end
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'presenter'
2
4
  require_relative 'null_presenter'
3
5
 
4
6
  module Rainbow
5
-
6
7
  class Wrapper
7
8
  attr_accessor :enabled
8
9
 
@@ -18,5 +19,4 @@ module Rainbow
18
19
  end
19
20
  end
20
21
  end
21
-
22
22
  end
@@ -1,7 +1,8 @@
1
- module Rainbow
1
+ # frozen_string_literal: true
2
2
 
3
+ module Rainbow
3
4
  module X11ColorNames
4
- NAMES= {
5
+ NAMES = {
5
6
  aqua: [0, 255, 255],
6
7
  aquamarine: [127, 255, 212],
7
8
  mediumaquamarine: [102, 205, 170],
@@ -147,6 +148,6 @@ module Rainbow
147
148
  yellow: [255, 255, 0],
148
149
  lightyellow: [255, 255, 224],
149
150
  greenyellow: [173, 255, 47]
150
- }
151
+ }.freeze
151
152
  end
152
- end
153
+ end
data/lib/rainbow.rb CHANGED
@@ -1,8 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'rainbow/global'
2
- require_relative 'rainbow/legacy'
3
4
 
4
5
  module Rainbow
5
-
6
6
  def self.new
7
7
  Wrapper.new(global.enabled)
8
8
  end
@@ -10,15 +10,4 @@ module Rainbow
10
10
  self.enabled = false unless STDOUT.tty? && STDERR.tty?
11
11
  self.enabled = false if ENV['TERM'] == 'dumb'
12
12
  self.enabled = true if ENV['CLICOLOR_FORCE'] == '1'
13
-
14
- # On Windows systems, try to load the local ANSI support library if Ruby version < 2
15
- # Ruby 2.x on Windows includes ANSI support.
16
- if RUBY_PLATFORM =~ /mswin|cygwin|mingw/ && RUBY_VERSION.to_i < 2
17
- begin
18
- require 'Win32/Console/ANSI'
19
- rescue LoadError
20
- self.enabled = false
21
- end
22
- end
23
-
24
13
  end
metadata CHANGED
@@ -1,103 +1,78 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rainbow
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Kulik
8
- autorequire:
8
+ - Olle Jonsson
9
+ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2016-12-28 00:00:00.000000000 Z
12
+ date: 2022-01-11 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
15
16
  requirement: !ruby/object:Gem::Requirement
16
17
  requirements:
17
- - - ~>
18
+ - - ">="
18
19
  - !ruby/object:Gem::Version
19
20
  version: '1.3'
21
+ - - "<"
22
+ - !ruby/object:Gem::Version
23
+ version: '3'
20
24
  type: :development
21
25
  prerelease: false
22
26
  version_requirements: !ruby/object:Gem::Requirement
23
27
  requirements:
24
- - - ~>
28
+ - - ">="
25
29
  - !ruby/object:Gem::Version
26
30
  version: '1.3'
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '3'
27
34
  description: Colorize printed text on ANSI terminals
28
35
  email:
29
36
  - m@ku1ik.com
30
37
  executables: []
31
- extensions:
32
- - ext/mkrf_conf.rb
38
+ extensions: []
33
39
  extra_rdoc_files: []
34
40
  files:
35
- - .gitignore
36
- - .rubocop.yml
37
- - .travis.yml
38
41
  - Changelog.md
39
- - Gemfile
40
- - Guardfile
41
42
  - LICENSE
42
43
  - README.markdown
43
- - Rakefile
44
- - ext/mkrf_conf.rb
45
44
  - lib/rainbow.rb
46
45
  - lib/rainbow/color.rb
47
46
  - lib/rainbow/ext/string.rb
48
47
  - lib/rainbow/global.rb
49
- - lib/rainbow/legacy.rb
50
48
  - lib/rainbow/null_presenter.rb
51
49
  - lib/rainbow/presenter.rb
50
+ - lib/rainbow/refinement.rb
52
51
  - lib/rainbow/string_utils.rb
53
52
  - lib/rainbow/version.rb
54
53
  - lib/rainbow/wrapper.rb
55
54
  - lib/rainbow/x11_color_names.rb
56
- - rainbow.gemspec
57
- - spec/integration/instance_spec.rb
58
- - spec/integration/rainbow_spec.rb
59
- - spec/integration/string_spec.rb
60
- - spec/spec_helper.rb
61
- - spec/support/presenter_shared_examples.rb
62
- - spec/unit/color_spec.rb
63
- - spec/unit/namespace_spec.rb
64
- - spec/unit/null_presenter_spec.rb
65
- - spec/unit/presenter_spec.rb
66
- - spec/unit/string_utils_spec.rb
67
- - spec/unit/wrapper_spec.rb
68
55
  homepage: https://github.com/sickill/rainbow
69
56
  licenses:
70
57
  - MIT
71
58
  metadata: {}
72
- post_install_message:
59
+ post_install_message:
73
60
  rdoc_options: []
74
61
  require_paths:
75
62
  - lib
76
63
  required_ruby_version: !ruby/object:Gem::Requirement
77
64
  requirements:
78
- - - '>='
65
+ - - ">="
79
66
  - !ruby/object:Gem::Version
80
- version: 1.9.2
67
+ version: 2.3.0
81
68
  required_rubygems_version: !ruby/object:Gem::Requirement
82
69
  requirements:
83
- - - '>='
70
+ - - ">="
84
71
  - !ruby/object:Gem::Version
85
72
  version: '0'
86
73
  requirements: []
87
- rubyforge_project:
88
- rubygems_version: 2.0.14.1
89
- signing_key:
74
+ rubygems_version: 3.0.9
75
+ signing_key:
90
76
  specification_version: 4
91
77
  summary: Colorize printed text on ANSI terminals
92
- test_files:
93
- - spec/integration/instance_spec.rb
94
- - spec/integration/rainbow_spec.rb
95
- - spec/integration/string_spec.rb
96
- - spec/spec_helper.rb
97
- - spec/support/presenter_shared_examples.rb
98
- - spec/unit/color_spec.rb
99
- - spec/unit/namespace_spec.rb
100
- - spec/unit/null_presenter_spec.rb
101
- - spec/unit/presenter_spec.rb
102
- - spec/unit/string_utils_spec.rb
103
- - spec/unit/wrapper_spec.rb
78
+ test_files: []
data/.gitignore DELETED
@@ -1,17 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
data/.rubocop.yml DELETED
@@ -1,14 +0,0 @@
1
- Documentation:
2
- Enabled: false
3
-
4
- EmptyLinesAroundBody:
5
- Enabled: false
6
-
7
- HashSyntax:
8
- Enabled: false
9
-
10
- MethodName:
11
- Enabled: false
12
-
13
- StringLiterals:
14
- Enabled: false
data/.travis.yml DELETED
@@ -1,22 +0,0 @@
1
- language: ruby
2
- cache:
3
- - bundler
4
- before_install:
5
- - gem update --system
6
- - gem install bundler
7
- bundler_args: --without guard development
8
-
9
- matrix:
10
- include:
11
- - rvm: 2.0.0
12
- - rvm: 2.1.9
13
- - rvm: 2.2.6
14
- - rvm: 2.3.3
15
- - rvm: 2.4.0
16
- - rvm: jruby-9.1.6.0
17
- jdk: oraclejdk8
18
- env:
19
- - JRUBY_OPTS=--debug
20
- - rvm: 2.2.6
21
- install: true # This skips 'bundle install'
22
- script: gem build rainbow && gem install *.gem
data/Gemfile DELETED
@@ -1,23 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in rainbow.gemspec
4
- gemspec
5
-
6
- gem 'rake'
7
- gem 'coveralls', require: false
8
- gem 'mime-types', '< 2.0.0', platforms: [:ruby_18]
9
- gem 'rspec'
10
-
11
- group :development do
12
- gem 'mutant-rspec'
13
- end
14
-
15
- group :guard do
16
- gem 'guard'
17
- gem 'guard-rspec'
18
- end
19
-
20
- platform :rbx do
21
- gem 'json'
22
- gem 'rubysl'
23
- end
data/Guardfile DELETED
@@ -1,9 +0,0 @@
1
- # A sample Guardfile
2
- # More info at https://github.com/guard/guard#readme
3
-
4
- guard :rspec, cmd: 'rspec --color' do
5
- watch(%r{^spec/.+_spec\.rb$})
6
- watch(%r{^lib/(.+)\.rb$}) { "spec" }
7
- watch('spec/spec_helper.rb') { "spec" }
8
- end
9
-
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require 'rspec/core/rake_task'
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec
data/ext/mkrf_conf.rb DELETED
@@ -1,22 +0,0 @@
1
- require 'rubygems'
2
- require 'rubygems/command'
3
- require 'rubygems/dependency_installer'
4
-
5
- begin
6
- Gem::Command.build_args = ARGV
7
- rescue NoMethodError
8
- exit 1
9
- end
10
-
11
- installer = Gem::DependencyInstaller.new
12
-
13
- begin
14
- if RUBY_PLATFORM =~ /mswin|cygwin|mingw/ && RUBY_VERSION.to_i < 2
15
- installer.install('windows-pr')
16
- installer.install('win32console')
17
- end
18
- rescue
19
- exit 1
20
- end
21
-
22
- File.write(File.join(File.dirname(__FILE__), 'Rakefile'), "task :default\n")
@@ -1,15 +0,0 @@
1
- module Sickill
2
- module Rainbow
3
-
4
- def self.enabled=(value)
5
- STDERR.puts("Rainbow gem notice: Sickill::Rainbow.enabled= is " \
6
- "deprecated, use Rainbow.enabled= instead.")
7
- ::Rainbow.enabled = value
8
- end
9
-
10
- def self.enabled
11
- ::Rainbow.enabled
12
- end
13
-
14
- end
15
- end
data/rainbow.gemspec DELETED
@@ -1,24 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'rainbow/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "rainbow"
8
- spec.version = Rainbow::VERSION
9
- spec.authors = ["Marcin Kulik"]
10
- spec.email = ["m@ku1ik.com"]
11
- spec.description = %q(Colorize printed text on ANSI terminals)
12
- spec.summary = %q(Colorize printed text on ANSI terminals)
13
- spec.homepage = "https://github.com/sickill/rainbow"
14
- spec.license = "MIT"
15
- spec.required_ruby_version = '>= 1.9.2'
16
-
17
- spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
18
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
- spec.require_paths = ["lib"]
21
-
22
- spec.add_development_dependency "bundler", "~> 1.3"
23
- spec.extensions = ["ext/mkrf_conf.rb"]
24
- end
@@ -1,35 +0,0 @@
1
- require 'spec_helper'
2
- require 'rainbow'
3
-
4
- describe 'Custom Rainbow instance' do
5
-
6
- it 'inherits enabled state from the global instance' do
7
- Rainbow.enabled = :yep
8
- expect(Rainbow.new.enabled).to eq(:yep)
9
- end
10
-
11
- it 'tracks its own state separately from the global instance' do
12
- Rainbow.enabled = :yep
13
- rainbow = Rainbow.new
14
- expect(Rainbow.new.enabled).to eq(:yep)
15
-
16
- rainbow.enabled = :nope
17
- expect(Rainbow.enabled).to eq(:yep)
18
- expect(rainbow.enabled).to eq(:nope)
19
- end
20
-
21
- it 'wraps string with escape codes when enabled' do
22
- rainbow = Rainbow.new
23
- rainbow.enabled = true
24
-
25
- expect(rainbow.wrap('hello').green).to eq("\e[32mhello\e[0m")
26
- end
27
-
28
- it "doesn't wrap string with any escape code when disabled" do
29
- rainbow = Rainbow.new
30
- rainbow.enabled = false
31
-
32
- expect(rainbow.wrap('hello').green).to eq('hello')
33
- end
34
-
35
- end