simplecolor 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e29e32da09d330fd55cc5a21481c4b2cd697f15f
4
- data.tar.gz: adb0f95981bb5a0d8508303199c99d09313712ec
3
+ metadata.gz: 7bfa2f004182adc7590fe70ab6a17c4234dbdfe9
4
+ data.tar.gz: 2a0ae6533b7ccf85397d9dd0cf4722403eb8c4d9
5
5
  SHA512:
6
- metadata.gz: e2280f08ed62a8abcba4241bc4119aab09ffc386abc98e5dbf154097ec9402bd8f268af2806ac5e226275a2457665a25a67f7b20bb4dd440e00287bdda91caba
7
- data.tar.gz: 5065a32ee415dfc0e9dbba91cf1cc5a6de425bd8bb956ffc9ae81b4e0b8693981beaa31169a3f18182bbee5c088c70ad38b3710b69a0b2d60cf09abdc070b049
6
+ metadata.gz: 7d1fc1b1e74ef5eeea41bab02e75399a551a709230752e6d5816a8b9e518933033040ec58a034afa14fb20c014c2948653d1ca3302512bba68b51d53a9777458
7
+ data.tar.gz: 9252b19aa9917848bcef1291a1f35c5af8cc80a816e27691eae258816fe85b208a7657ef977940c1c412c9d06e2578cfa194d413c82d1573ae0ae2648ac9fb9b
data/.gitignore CHANGED
@@ -1,3 +1,6 @@
1
- .yardoc/
2
- doc/
3
- pkg/
1
+ /.bundle
2
+ /.yardoc/
3
+ /Gemfile.lock
4
+ /doc/
5
+ /pkg/
6
+ /vendor/cache/*.gem
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ ---
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ - 2.3.3
6
+ - 2.2.6
7
+ - 2.1.10
8
+ #- ruby-head
9
+ #- ruby-head-clang
10
+ script: rake test
data/.yardopts CHANGED
@@ -1 +1,6 @@
1
- --markup markdown --title "simplecolor Documentation" --protected
1
+ --markup markdown
2
+ --title "simplecolor Documentation"
3
+ --protected
4
+ -
5
+ ChangeLog.md
6
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'kramdown'
7
+ end
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Damien Robert
1
+ Copyright © 2013–2016 Damien Robert
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,9 +1,12 @@
1
1
  # simplecolor
2
2
 
3
- * [Homepage](https://rubygems.org/gems/simplecolor)
4
- * [Documentation](http://rubydoc.info/gems/simplecolor/frames)
3
+ * [Homepage](https://github.com/DamienRobert/simplecolor)
4
+ * [Documentation](http://rubydoc.info/gems/simplecolor)
5
5
  * [Email](mailto:Damien.Olivier.Robert+gems@gmail.com)
6
6
 
7
+ [![Gem Version](https://img.shields.io/gem/v/simplecolor.svg)](https://rubygems.org/gems/simplecolor)
8
+ [![Build Status](https://travis-ci.org/DamienRobert/simplecolor.svg?branch=master)](https://travis-ci.org/DamienRobert/simplecolor)
9
+
7
10
  ## Description
8
11
 
9
12
  [rainbow]: https://github.com/sickill/rainbow
@@ -35,15 +38,32 @@ definitions.
35
38
 
36
39
  ## Examples
37
40
 
38
- require 'simplecolor'
41
+ ~~~ ruby
42
+ require 'simplecolor'
43
+
44
+ SimpleColor.color("blue", :blue, :bold)
45
+ SimpleColor.color(:blue,:bold) { "blue" }
46
+ SimpleColor.color(:blue,:bold) << "blue" << SimpleColor.color(:clear)
47
+
48
+ SimpleColor.mix_in_string
49
+ "blue".color(:blue,:bold)
50
+ "\e[34m\e[1mblue\e[0m".uncolor
51
+ ~~~
52
+
53
+ ## Usage
39
54
 
40
- SimpleColor.color("blue", :blue, :bold)
41
- SimpleColor.color(:blue,:bold) { "blue" }
42
- SimpleColor.color(:blue,:bold) << "blue" << SimpleColor.color(:clear)
55
+ There is a global switch `SimpleColor.enabled` to select the color mode.
56
+ The possible values are
43
57
 
44
- SimpleColor.mix_in_string
45
- "blue".color(:blue,:bold)
46
- "\e[34m\e[1mblue\e[0m".uncolor
58
+ - `true`: activate color output
59
+ - `false`: desactivate all color output
60
+ - `:shell`: activate color output for use in zsh prompt
61
+
62
+ When using a color escape sequence in a prompt in zsh, zsh will count the
63
+ escape sequences as part of the length of a prompt. To mark them as non
64
+ printable, one has to wrap them around `%{` and `%}`.
65
+ With `SimpleColor.enabled=:shell` this is done automatically by
66
+ `SimpleColor`.
47
67
 
48
68
  ## Requirements
49
69
 
@@ -51,16 +71,19 @@ None.
51
71
 
52
72
  ## Install
53
73
 
54
- $ gem install simplecolor
74
+ ~~~ sh
75
+ $ gem install simplecolor
76
+ ~~~
77
+
78
+ This installs `v0.1.0`, the current `master` version may not work.
55
79
 
56
80
  ## Todo
57
81
 
58
82
  - Support 256 colors
59
83
  - Tests
60
84
 
61
-
62
85
  ## Copyright
63
86
 
64
- Copyright (c) 2013 Damien Robert
87
+ Copyright © 2013–2016 Damien Robert
65
88
 
66
- MIT License. See {file:LICENSE.txt} for details.
89
+ MIT License. See [LICENSE.txt](./LICENSE.txt) for details.
data/Rakefile CHANGED
@@ -1,36 +1,29 @@
1
- # encoding: utf-8
2
-
3
- require 'rubygems'
4
1
  require 'rake'
5
2
 
6
3
  begin
7
- gem 'yard', '~> 0.8'
8
- require 'yard'
9
-
10
- YARD::Rake::YardocTask.new
11
- rescue LoadError => e
12
- task :yard do
13
- abort "Please run `gem install yard` to install YARD."
14
- end
15
- end
16
- task :doc => :yard
17
-
18
- begin
19
- gem 'rubygems-tasks', '~> 0.2'
20
4
  require 'rubygems/tasks'
21
-
22
- Gem::Tasks.new(sign: {checksum: true, pgp: true},
23
- build: {tar: true}, scm: {status: true}) do |tasks|
24
- tasks.console.command = 'pry'
25
- end
5
+ Gem::Tasks.new(sign: {checksum: true, pgp: true},
6
+ scm: {status: true}) do |tasks|
7
+ tasks.console.command = 'pry'
8
+ end
26
9
  rescue LoadError => e
27
10
  warn e.message
28
- warn "Run `gem install rubygems-tasks` to install Gem::Tasks."
29
11
  end
30
12
 
31
13
  require 'rake/testtask'
14
+ Rake::TestTask.new do |test|
15
+ test.libs << 'test'
16
+ test.pattern = 'test/**/test_*.rb'
17
+ test.verbose = true
18
+ end
32
19
 
33
- Rake::TestTask.new do |t|
34
- t.libs << "test"
35
- t.test_files = FileList['test/**/test_*.rb']
20
+ begin
21
+ require 'yard'
22
+ YARD::Rake::YardocTask.new
23
+ rescue LoadError => e
24
+ task :yard do
25
+ warn e.message
26
+ end
36
27
  end
28
+ task :doc => :yard
29
+
data/gemspec.yml CHANGED
@@ -9,8 +9,10 @@ description: "A simple library for coloring text output. Heavily inspired
9
9
  license: MIT
10
10
  authors: Damien Robert
11
11
  email: Damien.Olivier.Robert+gems@gmail.com
12
- homepage: https://rubygems.org/gems/simplecolor
12
+ homepage: https://github.com/DamienRobert/simplecolor
13
13
 
14
14
  development_dependencies:
15
- rubygems-tasks: ~> 0.2
16
- yard: ~> 0.8
15
+ minitest: "~> 5.0"
16
+ rake: "~> 10"
17
+ rubygems-tasks: "~> 0.2"
18
+ yard: "~> 0.8"
@@ -1,17 +1,15 @@
1
1
  module SimpleColor
2
2
 
3
- # Regular expression to scan if there is a clear ANSI effect at end of string
4
- CLEAR_REGEXP = /\e\[0m$/
5
- # Regular expression to find the end of the current ANSI sequence at
6
- # beginning of string, to wrap around in the correct order.
7
- COLOR_REGEXP = /^(\e\[([\d;]+)m)*/
8
- # Regular expression that is used to scan for ANSI-sequences while
9
- # uncoloring strings.
10
- COLORED_REGEXP = /\e\[(?:(?:[349]|10)[0-7]|[0-9])?m/
11
-
12
- #Stolen from paint gem
13
- #http://en.wikipedia.org/wiki/ANSI_escape_code
14
-
3
+ # Regular expression to scan if there is a clear ANSI effect
4
+ CLEAR_REGEXP = /\e\[0m/
5
+ # Regular expression that is used to scan for ANSI-sequences
6
+ ANSICOLOR_REGEXP = /\e\[(?:[\d;]*)m/
7
+ COLOR_REGEXP = /#{ANSICOLOR_REGEXP}+/
8
+ COLORMATCH_REGEXP = /#{ANSICOLOR_REGEXP}*/
9
+
10
+ #Stolen from the paint gem.
11
+ #See alosa http://en.wikipedia.org/wiki/ANSI_escape_code
12
+
15
13
  # Basic colors (often, the color differs when using the bright effect)
16
14
  # Final color will be 30 + value for foreground and 40 + value for background
17
15
  # 90+value for intense foreground, 100+value for intense background
@@ -94,19 +92,18 @@ module SimpleColor
94
92
  }
95
93
 
96
94
  ANSI_COLORS_INTENSE_BACKGROUND = {
97
- :on_instance_black => 100,
98
- :on_instance_red => 101,
99
- :on_instance_green => 102,
100
- :on_instance_yellow => 103,
101
- :on_instance_blue => 104,
102
- :on_instance_magenta => 105,
103
- :on_instance_cyan => 106,
104
- :on_instance_white => 107,
105
- :on_instance_default => 109,
95
+ :on_intense_black => 100,
96
+ :on_intense_red => 101,
97
+ :on_intense_green => 102,
98
+ :on_intense_yellow => 103,
99
+ :on_intense_blue => 104,
100
+ :on_intense_magenta => 105,
101
+ :on_intense_cyan => 106,
102
+ :on_intense_white => 107,
103
+ :on_intense_default => 109,
106
104
  }
107
105
 
108
106
  #attributes that can be specified to the color method
109
107
  COLORS = [ANSI_EFFECTS,ANSI_COLORS_FOREGROUND, ANSI_COLORS_BACKGROUND, ANSI_COLORS_INTENSE_FOREGROUND, ANSI_COLORS_INTENSE_BACKGROUND].inject({}){ |a,b| a.merge(b) }
110
108
 
111
-
112
109
  end
@@ -1,4 +1,4 @@
1
1
  module SimpleColor
2
2
  # simplecolor version
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
data/lib/simplecolor.rb CHANGED
@@ -1,3 +1,181 @@
1
1
  require 'simplecolor/version'
2
2
  require 'simplecolor/colors'
3
- require 'simplecolor/simplecolor'
3
+
4
+ #Usage:
5
+ #
6
+ #@example
7
+ # class Foo
8
+ # include SimpleColor::Mixin
9
+ # def to_str
10
+ # ...
11
+ # end
12
+ # end
13
+ # foo=Foo.new()
14
+ # foo.color(:red)
15
+
16
+ #after SimpleColor.mix_in_string, one can do
17
+ #`"blue".color(:blue,:bold)`
18
+ module SimpleColor
19
+ # The Colorer module handle all color outputs
20
+ module Colorer
21
+ extend self
22
+ WrongColor=Class.new(StandardError)
23
+ def color_attributes(*args, mode: :text)
24
+ accu=[]
25
+ buffer=""
26
+ flush=lambda {r=accu.join(";"); accu=[]; r.empty? || r="\e["+r+"m"; buffer<<r}
27
+ args.each do |col|
28
+ case col
29
+ when Symbol
30
+ raise WrongColor.new(col) unless COLORS.key?(col)
31
+ accu<<COLORS[col]
32
+ when COLOR_REGEXP
33
+ flush.call
34
+ buffer<<col
35
+ else
36
+ raise WrongColor.new(col)
37
+ end
38
+ end
39
+ flush.call
40
+ case mode
41
+ when :shell
42
+ "%{"+buffer+"%}"
43
+ when :disabled
44
+ ""
45
+ else
46
+ buffer
47
+ end
48
+ end
49
+
50
+ def colorer(s,*attributes,**kwds)
51
+ if s.nil?
52
+ color_attributes(*attributes,**kwds)
53
+ elsif s.empty?
54
+ s
55
+ else
56
+ matched = s.match(COLORMATCH_REGEXP)
57
+ attributes=color_attributes(*attributes,**kwds)
58
+ s.insert(matched.end(0), attributes)
59
+ s.concat(color_attributes(:clear,**kwds)) unless s =~ /#{CLEAR_REGEXP}$/ or attributes.empty?
60
+ s
61
+ end
62
+ end
63
+
64
+ def uncolorer(s)
65
+ s.to_str.gsub!(COLOR_REGEXP, '') || s.to_str
66
+ rescue ArgumentError #rescue from "invalid byte sequence in UTF-8"
67
+ s.to_str
68
+ end
69
+
70
+ def colored?(s)
71
+ !! (s =~ COLOR_REGEXP)
72
+ end
73
+ end
74
+
75
+ # Wraps around Colorer to provide useful instance methods
76
+ module ColorWrapper
77
+ extend self
78
+
79
+ # Returns an uncolored version of the string, that is all
80
+ # ANSI-sequences are stripped from the string.
81
+ # @see: color
82
+ def uncolor!(string = nil)
83
+ arg=if block_given?
84
+ yield.to_s
85
+ elsif string.respond_to?(:to_str)
86
+ string.to_str
87
+ elsif respond_to?(:to_str)
88
+ self.to_str
89
+ else
90
+ ''
91
+ end
92
+ Colorer.uncolorer(arg)
93
+ end
94
+
95
+ # wrap self or the first argument with colors
96
+ # @example ploum
97
+ # SimpleColor.color("blue", :blue, :bold)
98
+ # SimpleColor.color(:blue,:bold) { "blue" }
99
+ # SimpleColor.color(:blue,:bold) << "blue" << SimpleColor.color(:clear)
100
+ def color!(*args)
101
+ arg=if block_given?
102
+ yield.to_s
103
+ elsif respond_to?(:to_str)
104
+ self.to_str
105
+ elsif args.first.respond_to?(:to_str)
106
+ args.shift.to_str
107
+ else
108
+ nil
109
+ end
110
+ Colorer.colorer(arg,*args)
111
+ end
112
+
113
+ [:color,:uncolor].each do |m|
114
+ define_method m do |*args,&b|
115
+ self.dup.send :"#{m}!",*args,&b
116
+ end
117
+ end
118
+
119
+ def color?(*args)
120
+ arg=if block_given?
121
+ yield.to_s
122
+ elsif respond_to?(:to_str)
123
+ self.to_str
124
+ elsif args.first.respond_to?(:to_str)
125
+ args.shift.to_str
126
+ else
127
+ nil
128
+ end
129
+ Colorer.colored?(arg)
130
+ end
131
+ end
132
+
133
+ include ColorWrapper
134
+ extend self
135
+
136
+ # enabled can be set to true, false, or :shell
137
+ # :shell means that the color escape sequence will be quoted.
138
+ # This is meant to be used in the shell prompt, so that the escape
139
+ # sequence will not count in the length of the prompt.
140
+ attr_accessor :enabled
141
+ self.enabled=true
142
+
143
+ [:color,:color!].each do |m|
144
+ define_method m do |*args, mode: (SimpleColor.enabled || :disabled), &b|
145
+ super(*args, mode: mode, &b)
146
+ end
147
+ end
148
+
149
+ module Helpers
150
+ def mix_in(klass)
151
+ klass.send :include, SimpleColor
152
+ end
153
+ def mix_in_string
154
+ mix_in(String)
155
+ end
156
+
157
+ def attributes_from_colors(s)
158
+ s.scan(/#{ANSICOLOR_REGEXP}/).flat_map do |a|
159
+ next :reset if a=="\e[m"
160
+ a[/\e\[(.*)m/,1].split(';').map {|c| COLORS.key(c.to_i)}
161
+ end
162
+ end
163
+ #get the ansi sequences on s (assume the whole line is colored)
164
+ def current_colors(s)
165
+ m=s.match(/^(#{COLORMATCH_REGEXP})(.*?)(#{COLORMATCH_REGEXP})$/)
166
+ [m[1],m[3],m[2]]
167
+ rescue ArgumentError #rescue from "invalid byte sequence in UTF-8"
168
+ ["","",s]
169
+ end
170
+ #copy the colors from s to t
171
+ def copy_colors(s,t)
172
+ b,e=current_colors(s)
173
+ b+t+e
174
+ end
175
+ #split the line into characters and ANSII color sequences
176
+ def color_entities(l)
177
+ l.split(/(#{COLOR_REGEXP})/).flat_map {|c| color?(c) ? [c] : c.split('') }
178
+ end
179
+ end
180
+ extend Helpers
181
+ end
data/simplecolor.gemspec CHANGED
@@ -24,6 +24,16 @@ Gem::Specification.new do |gem|
24
24
  glob = lambda { |patterns| gem.files & Dir[*patterns] }
25
25
 
26
26
  gem.files = `git ls-files`.split($/)
27
+
28
+ `git submodule --quiet foreach --recursive pwd`.split($/).each do |submodule|
29
+ submodule.sub!("#{Dir.pwd}/",'')
30
+
31
+ Dir.chdir(submodule) do
32
+ `git ls-files`.split($/).map do |subpath|
33
+ gem.files << File.join(submodule,subpath)
34
+ end
35
+ end
36
+ end
27
37
  gem.files = glob[gemspec['files']] if gemspec['files']
28
38
 
29
39
  gem.executables = gemspec.fetch('executables') do
@@ -38,7 +48,7 @@ Gem::Specification.new do |gem|
38
48
  %w[ext lib].select { |dir| File.directory?(dir) }
39
49
  })
40
50
 
41
- gem.requirements = gemspec['requirements']
51
+ gem.requirements = Array(gemspec['requirements'])
42
52
  gem.required_ruby_version = gemspec['required_ruby_version']
43
53
  gem.required_rubygems_version = gemspec['required_rubygems_version']
44
54
  gem.post_install_message = gemspec['post_install_message']
@@ -56,4 +66,6 @@ Gem::Specification.new do |gem|
56
66
  gem.add_development_dependency(name,split[versions])
57
67
  end
58
68
  end
69
+
70
+ gem.metadata['yard.run']='yri'
59
71
  end
data/test/helper.rb CHANGED
@@ -1,7 +1,13 @@
1
- require 'rubygems'
2
1
  require 'minitest/autorun'
3
2
 
4
- class MiniTest::Test
5
- end
3
+ ## Uncomment to launch pry on a failure
4
+ #require 'pry-rescue/minitest'
6
5
 
7
- MiniTest.autorun
6
+ begin
7
+ require 'minitest/reporters'
8
+ Minitest::Reporters.use! Minitest::Reporters::DefaultReporter.new
9
+ #Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
10
+ #Minitest::Reporters.use! Minitest::Reporters::ProgressReporter.new
11
+ rescue LoadError => error
12
+ warn "minitest/reporters not found, not changing minitest reporter: #{error}"
13
+ end
@@ -3,10 +3,152 @@ require 'simplecolor'
3
3
 
4
4
  class TestSimpleColor < MiniTest::Test
5
5
 
6
- def test_version
7
- version = SimpleColor.const_get('VERSION')
6
+ def test_version
7
+ version = SimpleColor.const_get('VERSION')
8
8
 
9
- assert(!version.empty?, 'should have a VERSION constant')
10
- end
9
+ assert(!version.empty?, 'should have a VERSION constant')
10
+ end
11
+
12
+ end
13
+
14
+ describe SimpleColor do
15
+ before do
16
+ SimpleColor.enabled=true
17
+ end
18
+
19
+ it "Can be used directly" do
20
+ SimpleColor.color("red",:red).must_equal "\e[31mred\e[0m"
21
+ end
22
+
23
+ it "Can specify several colors" do
24
+ SimpleColor.color("red",:red,:bold).must_equal "\e[31;1mred\e[0m"
25
+ end
26
+
27
+ it "Accepts ANSI sequences" do
28
+ SimpleColor.color("red","\e[31;1m").must_equal "\e[31;1mred\e[0m"
29
+ end
30
+
31
+ it "Can mix ANSI sequences and attributes" do
32
+ SimpleColor.color("red",:inverse,"\e[31m",:blue,:bold).must_equal "\e[7m\e[31m\e[34;1mred\e[0m"
33
+ end
34
+
35
+ it "Can take a block" do
36
+ SimpleColor.color(:red) { "red" }.must_equal "\e[31mred\e[0m"
37
+ end
38
+
39
+ it "Can provide only color values" do
40
+ SimpleColor.color(:red).must_equal "\e[31m"
41
+ end
42
+
43
+ it "Does not change the string when there is no color attributes" do
44
+ SimpleColor.color("foo").must_equal "foo"
45
+ end
46
+
47
+ it "can uncolor" do
48
+ SimpleColor.uncolor(SimpleColor.color("red",:red)).must_equal "red"
49
+ end
50
+
51
+ it "uncolor does not change uncolored strings" do
52
+ SimpleColor.uncolor("red").must_equal "red"
53
+ end
54
+
55
+ it "Can detect colored strings" do
56
+ SimpleColor.color?("red").must_equal false
57
+ SimpleColor.color?(SimpleColor.color("red",:red)).must_equal true
58
+ end
59
+
60
+
61
+ describe "Shell mode" do
62
+ before do
63
+ SimpleColor.enabled=:shell
64
+ end
65
+
66
+ it "Wraps color into shell escapes" do
67
+ SimpleColor.color("red",:red).must_equal "%{\e[31m%}red%{\e[0m%}"
68
+ end
69
+ end
70
+
71
+ describe "It can be mixed in strings" do
72
+ before do
73
+ SimpleColor.mix_in_string
74
+ end
75
+
76
+ it "Works on strings" do
77
+ "red".color(:red).must_equal "\e[31mred\e[0m"
78
+ end
79
+
80
+ it "Uncolor works on strings" do
81
+ "red".color(:red).uncolor.must_equal "red"
82
+ end
83
+
84
+ it "color does not affects the string" do
85
+ s="red"
86
+ s.color(:red)
87
+ s.must_equal "red"
88
+ end
89
+
90
+ it "color! does affects the string" do
91
+ s="red"
92
+ s.color!(:red)
93
+ s.must_equal "\e[31mred\e[0m"
94
+ end
95
+
96
+ it "uncolor does not affects the string" do
97
+ s="\e[31mred\e[0m"
98
+ s.uncolor
99
+ s.must_equal "\e[31mred\e[0m"
100
+ end
101
+
102
+ it "uncolor! does not affects the string" do
103
+ s="\e[31mred\e[0m"
104
+ s.uncolor!
105
+ s.must_equal "red"
106
+ end
107
+
108
+ end
109
+
110
+
111
+ describe "It can be disabled" do
112
+ before do
113
+ SimpleColor.enabled=false
114
+ end
115
+
116
+ it "When disabled color should be a noop" do
117
+ word="red"
118
+ SimpleColor.color(word,:red).must_equal word
119
+ end
120
+
121
+ it "When disabled uncolor should still work" do
122
+ word="\e[31mred\e[0m"
123
+ SimpleColor.uncolor(word).must_equal "red"
124
+ end
125
+ end
126
+
127
+ describe "It has colors helpers utilities" do
128
+ it "It can copy one color" do
129
+ SimpleColor.copy_colors(SimpleColor.color("red",:red),"blue").must_equal "\e[31mblue\e[0m"
130
+ end
131
+
132
+ it "Copy several colors" do
133
+ SimpleColor.copy_colors(SimpleColor.color("red",:red,:bold),"blue").must_equal "\e[31;1mblue\e[0m"
134
+ end
135
+
136
+ it "Can recover colors" do
137
+ b,e,l=SimpleColor.current_colors(SimpleColor.color("red",:red,:bold))
138
+ b.must_equal "\e[31;1m"
139
+ e.must_equal "\e[0m"
140
+ l.must_equal "red"
141
+ end
142
+
143
+ it "Can get colors attributes" do
144
+ b,e=SimpleColor.current_colors(SimpleColor.color("red",:red,:bold))
145
+ SimpleColor.attributes_from_colors("\e[m"+b+e).must_equal([:reset,:red,:bright,:reset])
146
+ end
147
+
148
+ it "Can split a string into color entities" do
149
+ SimpleColor.color_entities(SimpleColor.color("red",:red,:bold)+SimpleColor.color("blue",:blue)).must_equal(["\e[31;1m", "r", "e", "d", "\e[0m\e[34m", "b", "l", "u", "e", "\e[0m"])
150
+ SimpleColor.color_entities("blue "+SimpleColor.color("red",:red,:bold)+" green").must_equal(["b", "l", "u", "e", " ", "\e[31;1m", "r", "e", "d", "\e[0m", " ", "g", "r", "e", "e", "n"])
151
+ end
152
+ end
11
153
 
12
154
  end
metadata CHANGED
@@ -1,41 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simplecolor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damien Robert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-23 00:00:00.000000000 Z
11
+ date: 2017-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10'
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: rubygems-tasks
15
43
  requirement: !ruby/object:Gem::Requirement
16
44
  requirements:
17
- - - ~>
45
+ - - "~>"
18
46
  - !ruby/object:Gem::Version
19
47
  version: '0.2'
20
48
  type: :development
21
49
  prerelease: false
22
50
  version_requirements: !ruby/object:Gem::Requirement
23
51
  requirements:
24
- - - ~>
52
+ - - "~>"
25
53
  - !ruby/object:Gem::Version
26
54
  version: '0.2'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: yard
29
57
  requirement: !ruby/object:Gem::Requirement
30
58
  requirements:
31
- - - ~>
59
+ - - "~>"
32
60
  - !ruby/object:Gem::Version
33
61
  version: '0.8'
34
62
  type: :development
35
63
  prerelease: false
36
64
  version_requirements: !ruby/object:Gem::Requirement
37
65
  requirements:
38
- - - ~>
66
+ - - "~>"
39
67
  - !ruby/object:Gem::Version
40
68
  version: '0.8'
41
69
  description: 'A simple library for coloring text output. Heavily inspired from term-ansicolor
@@ -51,42 +79,43 @@ extra_rdoc_files:
51
79
  - LICENSE.txt
52
80
  - README.md
53
81
  files:
54
- - .document
55
- - .gitignore
56
- - .yardopts
82
+ - ".gitignore"
83
+ - ".travis.yml"
84
+ - ".yardopts"
57
85
  - ChangeLog.md
86
+ - Gemfile
58
87
  - LICENSE.txt
59
88
  - README.md
60
89
  - Rakefile
61
90
  - gemspec.yml
62
91
  - lib/simplecolor.rb
63
92
  - lib/simplecolor/colors.rb
64
- - lib/simplecolor/simplecolor.rb
65
93
  - lib/simplecolor/version.rb
66
94
  - simplecolor.gemspec
67
95
  - test/helper.rb
68
96
  - test/test_simplecolor.rb
69
- homepage: https://rubygems.org/gems/simplecolor
97
+ homepage: https://github.com/DamienRobert/simplecolor
70
98
  licenses:
71
99
  - MIT
72
- metadata: {}
100
+ metadata:
101
+ yard.run: yri
73
102
  post_install_message:
74
103
  rdoc_options: []
75
104
  require_paths:
76
105
  - lib
77
106
  required_ruby_version: !ruby/object:Gem::Requirement
78
107
  requirements:
79
- - - '>='
108
+ - - ">="
80
109
  - !ruby/object:Gem::Version
81
110
  version: '0'
82
111
  required_rubygems_version: !ruby/object:Gem::Requirement
83
112
  requirements:
84
- - - '>='
113
+ - - ">="
85
114
  - !ruby/object:Gem::Version
86
115
  version: '0'
87
116
  requirements: []
88
117
  rubyforge_project:
89
- rubygems_version: 2.0.3
118
+ rubygems_version: 2.6.8
90
119
  signing_key:
91
120
  specification_version: 4
92
121
  summary: Simple color library
data/.document DELETED
@@ -1,3 +0,0 @@
1
- -
2
- ChangeLog.md
3
- LICENSE.txt
@@ -1,82 +0,0 @@
1
- module SimpleColor
2
- class << self; attr_accessor :enabled; end
3
- self.enabled=true
4
-
5
- def colorattributes(*args)
6
- if SimpleColor.enabled
7
- result=args.map {|col| "\e[#{COLORS[col]}m" }.inject(:+)
8
- if SimpleColor.enabled == :shell
9
- return "%{"+result+"%}"
10
- else
11
- return result
12
- end
13
- else
14
- return ''
15
- end
16
- end
17
-
18
- #Usage:
19
- #class Foo
20
- # include SimpleColor::Mixin
21
- # def to_str
22
- # ...
23
- # end
24
- #end
25
- #foo=Foo.new()
26
- #foo.color(:red)
27
- module Mixin
28
-
29
- # Returns an uncolored version of the string, that is all
30
- # ANSI-sequences are stripped from the string.
31
- def uncolor(string = nil)
32
- if block_given?
33
- yield.to_str.gsub(COLORED_REGEXP, '')
34
- elsif string.respond_to?(:to_str)
35
- string.to_str.gsub(COLORED_REGEXP, '')
36
- elsif respond_to?(:to_str)
37
- to_str.gsub(COLORED_REGEXP, '')
38
- else
39
- ''
40
- end
41
- end
42
-
43
- #wrap self or the first argument with colors
44
- #Examples:
45
- # SimpleColor.color("blue", :blue, :bold)
46
- # SimpleColor.color(:blue,:bold) { "blue" }
47
- # SimpleColor.color(:blue,:bold) << "blue" << SimpleColor.color(:clear)
48
- #pareil pour uncolored
49
- def color(*args)
50
- if respond_to?(:to_str)
51
- arg=self.dup
52
- elsif block_given?
53
- arg = yield
54
- elsif args.first.respond_to?(:to_str)
55
- arg=args.shift
56
- else
57
- arg=nil
58
- end
59
- return arg unless SimpleColor.enabled
60
-
61
- if arg.nil?
62
- return SimpleColor.colorattributes(*args)
63
- elsif arg.empty?
64
- return arg
65
- else
66
- matched = arg.match(SimpleColor::COLOR_REGEXP)
67
- arg.insert(matched.end(0), SimpleColor.colorattributes(*args))
68
- arg.concat(SimpleColor.colorattributes(:clear)) unless arg =~ SimpleColor::CLEAR_REGEXP
69
- return arg
70
- end
71
- end
72
- end
73
-
74
- #after SimpleColor.mix_in_string, one can do
75
- #"blue".color(:blue,:bold)
76
- include SimpleColor::Mixin
77
- def mix_in_string
78
- String.class_eval {include SimpleColor::Mixin}
79
- end
80
-
81
- extend self
82
- end