simplecolor 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e29e32da09d330fd55cc5a21481c4b2cd697f15f
4
+ data.tar.gz: adb0f95981bb5a0d8508303199c99d09313712ec
5
+ SHA512:
6
+ metadata.gz: e2280f08ed62a8abcba4241bc4119aab09ffc386abc98e5dbf154097ec9402bd8f268af2806ac5e226275a2457665a25a67f7b20bb4dd440e00287bdda91caba
7
+ data.tar.gz: 5065a32ee415dfc0e9dbba91cf1cc5a6de425bd8bb956ffc9ae81b4e0b8693981beaa31169a3f18182bbee5c088c70ad38b3710b69a0b2d60cf09abdc070b049
data/.document ADDED
@@ -0,0 +1,3 @@
1
+ -
2
+ ChangeLog.md
3
+ LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .yardoc/
2
+ doc/
3
+ pkg/
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown --title "simplecolor Documentation" --protected
data/ChangeLog.md ADDED
@@ -0,0 +1,4 @@
1
+ ### 0.1.0 / 2013-08-23
2
+
3
+ * Initial release:
4
+
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Damien Robert
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # simplecolor
2
+
3
+ * [Homepage](https://rubygems.org/gems/simplecolor)
4
+ * [Documentation](http://rubydoc.info/gems/simplecolor/frames)
5
+ * [Email](mailto:Damien.Olivier.Robert+gems@gmail.com)
6
+
7
+ ## Description
8
+
9
+ [rainbow]: https://github.com/sickill/rainbow
10
+ [term-ansicolor]: https://github.com/flori/term-ansicolor
11
+ [paint]: https://github.com/janlelis/paint
12
+
13
+ A simple library for coloring text output. Heavily inspired by [rainbow],
14
+ [term-ansicolor] and [paint]. I recommend using these gems for more complex
15
+ coloring needs.
16
+
17
+ By default this gem does not change the `String` class, unlike [rainbow]. The
18
+ reason I wrote this gem is that when mixed in `String` it only adds two
19
+ methods: `color` and `uncolor`. This is the main reason I don't use
20
+ [term-ansicolor] which is more powerful, but adds more methods when mixed in.
21
+ The gem [paint] is similar to this one, but with more powerful shortcuts
22
+ definitions.
23
+
24
+ ## Features
25
+
26
+ - No string extensions (suitable for library development)
27
+
28
+ - Mixing the library in `String` only add two methods: `color` and
29
+ `uncolor`.
30
+
31
+ - Supports setting any effects (although most terminals won’t support it)
32
+
33
+ - Simple to use
34
+
35
+
36
+ ## Examples
37
+
38
+ require 'simplecolor'
39
+
40
+ SimpleColor.color("blue", :blue, :bold)
41
+ SimpleColor.color(:blue,:bold) { "blue" }
42
+ SimpleColor.color(:blue,:bold) << "blue" << SimpleColor.color(:clear)
43
+
44
+ SimpleColor.mix_in_string
45
+ "blue".color(:blue,:bold)
46
+ "\e[34m\e[1mblue\e[0m".uncolor
47
+
48
+ ## Requirements
49
+
50
+ None.
51
+
52
+ ## Install
53
+
54
+ $ gem install simplecolor
55
+
56
+ ## Todo
57
+
58
+ - Support 256 colors
59
+ - Tests
60
+
61
+
62
+ ## Copyright
63
+
64
+ Copyright (c) 2013 Damien Robert
65
+
66
+ MIT License. See {file:LICENSE.txt} for details.
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+
6
+ 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
+ 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
26
+ rescue LoadError => e
27
+ warn e.message
28
+ warn "Run `gem install rubygems-tasks` to install Gem::Tasks."
29
+ end
30
+
31
+ require 'rake/testtask'
32
+
33
+ Rake::TestTask.new do |t|
34
+ t.libs << "test"
35
+ t.test_files = FileList['test/**/test_*.rb']
36
+ end
data/gemspec.yml ADDED
@@ -0,0 +1,16 @@
1
+ name: simplecolor
2
+ summary: "Simple color library"
3
+ description: "A simple library for coloring text output. Heavily inspired
4
+ from term-ansicolor and rainbow, I recommend using these gems for more
5
+ complex color needs. By default this gem does not do any core extension.
6
+ When mixed in String it only add two methods: color and uncolor. Paint
7
+ is another gem similar to this one, but with more powerful shortcuts
8
+ definitions."
9
+ license: MIT
10
+ authors: Damien Robert
11
+ email: Damien.Olivier.Robert+gems@gmail.com
12
+ homepage: https://rubygems.org/gems/simplecolor
13
+
14
+ development_dependencies:
15
+ rubygems-tasks: ~> 0.2
16
+ yard: ~> 0.8
@@ -0,0 +1,3 @@
1
+ require 'simplecolor/version'
2
+ require 'simplecolor/colors'
3
+ require 'simplecolor/simplecolor'
@@ -0,0 +1,112 @@
1
+ module SimpleColor
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
+
15
+ # Basic colors (often, the color differs when using the bright effect)
16
+ # Final color will be 30 + value for foreground and 40 + value for background
17
+ # 90+value for intense foreground, 100+value for intense background
18
+ ANSI_COLORS = {
19
+ :black => 0,
20
+ :red => 1,
21
+ :green => 2,
22
+ :yellow => 3,
23
+ :blue => 4,
24
+ :magenta => 5,
25
+ :cyan => 6,
26
+ :white => 7,
27
+ :default => 9,
28
+ }
29
+
30
+ ANSI_EFFECTS = {
31
+ :reset => 0, :nothing => 0, # usually supported
32
+ :clear => 0, :normal => 0, # usually supported
33
+ :bright => 1, :bold => 1, # usually supported
34
+ :faint => 2,
35
+ :italic => 3,
36
+ :underline => 4, # usually supported
37
+ :blink => 5, :slow_blink => 5,
38
+ :rapid_blink => 6,
39
+ :inverse => 7, :swap => 7, # usually supported
40
+ :conceal => 8, :hide => 9,
41
+ :default_font => 10,
42
+ :font0 => 10, :font1 => 11, :font2 => 12, :font3 => 13, :font4 => 14,
43
+ :font5 => 15, :font6 => 16, :font7 => 17, :font8 => 18, :font9 => 19,
44
+ :fraktur => 20,
45
+ :bright_off => 21, :bold_off => 21, :double_underline => 21,
46
+ :clean => 22,
47
+ :italic_off => 23, :fraktur_off => 23,
48
+ :underline_off => 24,
49
+ :blink_off => 25,
50
+ :inverse_off => 26, :positive => 26,
51
+ :conceal_off => 27, :show => 27, :reveal => 27,
52
+ :crossed_off => 29, :crossed_out_off => 29,
53
+ :frame => 51,
54
+ :encircle => 52,
55
+ :overline => 53,
56
+ :frame_off => 54, :encircle_off => 54,
57
+ :overline_off => 55,
58
+ }
59
+
60
+ ANSI_COLORS_FOREGROUND = {
61
+ :black => 30,
62
+ :red => 31,
63
+ :green => 32,
64
+ :yellow => 33,
65
+ :blue => 34,
66
+ :magenta => 35,
67
+ :cyan => 36,
68
+ :white => 37,
69
+ :default => 39,
70
+ }
71
+
72
+ ANSI_COLORS_BACKGROUND = {
73
+ :on_black => 40,
74
+ :on_red => 41,
75
+ :on_green => 42,
76
+ :on_yellow => 43,
77
+ :on_blue => 44,
78
+ :on_magenta => 45,
79
+ :on_cyan => 46,
80
+ :on_white => 47,
81
+ :on_default => 49,
82
+ }
83
+
84
+ ANSI_COLORS_INTENSE_FOREGROUND = {
85
+ :intense_black => 90,
86
+ :intense_red => 91,
87
+ :intense_green => 92,
88
+ :intense_yellow => 93,
89
+ :intense_blue => 94,
90
+ :intense_magenta => 95,
91
+ :intense_cyan => 96,
92
+ :intense_white => 97,
93
+ :intense_default => 99,
94
+ }
95
+
96
+ 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,
106
+ }
107
+
108
+ #attributes that can be specified to the color method
109
+ COLORS = [ANSI_EFFECTS,ANSI_COLORS_FOREGROUND, ANSI_COLORS_BACKGROUND, ANSI_COLORS_INTENSE_FOREGROUND, ANSI_COLORS_INTENSE_BACKGROUND].inject({}){ |a,b| a.merge(b) }
110
+
111
+
112
+ end
@@ -0,0 +1,82 @@
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
@@ -0,0 +1,4 @@
1
+ module SimpleColor
2
+ # simplecolor version
3
+ VERSION = "0.1.0"
4
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ require 'yaml'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gemspec = YAML.load_file('gemspec.yml')
7
+
8
+ gem.name = gemspec.fetch('name')
9
+ gem.version = gemspec.fetch('version') do
10
+ lib_dir = File.join(File.dirname(__FILE__),'lib')
11
+ $LOAD_PATH << lib_dir unless $LOAD_PATH.include?(lib_dir)
12
+
13
+ require 'simplecolor/version'
14
+ SimpleColor::VERSION
15
+ end
16
+
17
+ gem.summary = gemspec['summary']
18
+ gem.description = gemspec['description']
19
+ gem.licenses = Array(gemspec['license'])
20
+ gem.authors = Array(gemspec['authors'])
21
+ gem.email = gemspec['email']
22
+ gem.homepage = gemspec['homepage']
23
+
24
+ glob = lambda { |patterns| gem.files & Dir[*patterns] }
25
+
26
+ gem.files = `git ls-files`.split($/)
27
+ gem.files = glob[gemspec['files']] if gemspec['files']
28
+
29
+ gem.executables = gemspec.fetch('executables') do
30
+ glob['bin/*'].map { |path| File.basename(path) }
31
+ end
32
+ gem.default_executable = gem.executables.first if Gem::VERSION < '1.7.'
33
+
34
+ gem.extensions = glob[gemspec['extensions'] || 'ext/**/extconf.rb']
35
+ gem.extra_rdoc_files = glob[gemspec['extra_doc_files'] || '*.{txt,md}']
36
+
37
+ gem.require_paths = Array(gemspec.fetch('require_paths') {
38
+ %w[ext lib].select { |dir| File.directory?(dir) }
39
+ })
40
+
41
+ gem.requirements = gemspec['requirements']
42
+ gem.required_ruby_version = gemspec['required_ruby_version']
43
+ gem.required_rubygems_version = gemspec['required_rubygems_version']
44
+ gem.post_install_message = gemspec['post_install_message']
45
+
46
+ split = lambda { |string| string.split(/,\s*/) }
47
+
48
+ if gemspec['dependencies']
49
+ gemspec['dependencies'].each do |name,versions|
50
+ gem.add_dependency(name,split[versions])
51
+ end
52
+ end
53
+
54
+ if gemspec['development_dependencies']
55
+ gemspec['development_dependencies'].each do |name,versions|
56
+ gem.add_development_dependency(name,split[versions])
57
+ end
58
+ end
59
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+
4
+ class MiniTest::Test
5
+ end
6
+
7
+ MiniTest.autorun
@@ -0,0 +1,12 @@
1
+ require 'helper'
2
+ require 'simplecolor'
3
+
4
+ class TestSimpleColor < MiniTest::Test
5
+
6
+ def test_version
7
+ version = SimpleColor.const_get('VERSION')
8
+
9
+ assert(!version.empty?, 'should have a VERSION constant')
10
+ end
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simplecolor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Damien Robert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubygems-tasks
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.8'
41
+ description: 'A simple library for coloring text output. Heavily inspired from term-ansicolor
42
+ and rainbow, I recommend using these gems for more complex color needs. By default
43
+ this gem does not do any core extension. When mixed in String it only add two methods:
44
+ color and uncolor. Paint is another gem similar to this one, but with more powerful
45
+ shortcuts definitions.'
46
+ email: Damien.Olivier.Robert+gems@gmail.com
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files:
50
+ - ChangeLog.md
51
+ - LICENSE.txt
52
+ - README.md
53
+ files:
54
+ - .document
55
+ - .gitignore
56
+ - .yardopts
57
+ - ChangeLog.md
58
+ - LICENSE.txt
59
+ - README.md
60
+ - Rakefile
61
+ - gemspec.yml
62
+ - lib/simplecolor.rb
63
+ - lib/simplecolor/colors.rb
64
+ - lib/simplecolor/simplecolor.rb
65
+ - lib/simplecolor/version.rb
66
+ - simplecolor.gemspec
67
+ - test/helper.rb
68
+ - test/test_simplecolor.rb
69
+ homepage: https://rubygems.org/gems/simplecolor
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.0.3
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Simple color library
93
+ test_files: []