colcolor 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +103 -0
- data/Rakefile +7 -0
- data/colcolor.gemspec +24 -0
- data/lib/colcolor.rb +44 -0
- data/lib/colcolor/version.rb +3 -0
- data/spec/colcolor_spec.rb +68 -0
- data/spec/spec_helper.rb +2 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5347ef03ca37dd6d366489635ccd5f6d4a8ab378
|
4
|
+
data.tar.gz: 33257625f3db686621035ade7d45e1c8da50e94a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 78221ccecdadf899e29ac7c77a6cae5458604f69cbb10dafbf71f93b4e77b83da0f3a52c2ea09041a70c36af359d7bb9df2c1cfafde90b4ecb41f5202eb60de4
|
7
|
+
data.tar.gz: 06628df1c918fe35a53a4a0df5e27e9f54128370032030876c3ad668dcdd7a64efaa89a2909389622a9a88fb64dc519e5ddfad97c3fd4129d7b36a9b2786b16e
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
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
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 kyoendo
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# Colcolor
|
2
|
+
|
3
|
+
Easily colorize terminal text by each column.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'colcolor'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install colcolor
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
You want to colorize terminal outputs in which each column(separated by whitespaces) have different color.
|
22
|
+
|
23
|
+
It can be easily achieved with `String#colco` method as follows:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require "colcolor"
|
27
|
+
|
28
|
+
list = <<-EOS
|
29
|
+
Charlie\t21\tprogrammer
|
30
|
+
Bill\t43\tdoctor
|
31
|
+
Liz\t18\tstudent
|
32
|
+
EOS
|
33
|
+
|
34
|
+
list.each_line do |line|
|
35
|
+
puts line.colco(:green, :yellow, :blue)
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Output:
|
40
|
+
|
41
|
+
![sample1](https://github.com/melborne/colcolor/raw/screenshot/sample1.png)
|
42
|
+
|
43
|
+
If you want to have background colors or some decorations onto them, chain the names with underscores:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
list.each_line do |line|
|
47
|
+
puts line.colco(:green, :red_yellow, :blue_underline)
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
Output:
|
52
|
+
|
53
|
+
![sample2](https://github.com/melborne/colcolor/raw/screenshot/sample2.png)
|
54
|
+
|
55
|
+
If you prefer ages(second column) without color, pass `nil` for it:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
list.each_line do |line|
|
59
|
+
puts line.colco(:green, nil, :blue_underline)
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
Output:
|
64
|
+
|
65
|
+
![sample3](https://github.com/melborne/colcolor/raw/screenshot/sample3.png)
|
66
|
+
|
67
|
+
You have full names in the list and want to apply bgcolor for them, `regexp` option might helps you.
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
list = <<-EOS
|
71
|
+
Charlie Brown\t21\tprogrammer
|
72
|
+
Bill Clinton\t43\tdoctor
|
73
|
+
Liz Taylor\t18\tstudent
|
74
|
+
EOS
|
75
|
+
|
76
|
+
# less appropriate example:
|
77
|
+
|
78
|
+
list.each_line do |line|
|
79
|
+
puts line.colco(:bg_green, :bg_green, :yellow, :blue_underline)
|
80
|
+
end
|
81
|
+
|
82
|
+
puts
|
83
|
+
|
84
|
+
# example with regexp option:
|
85
|
+
|
86
|
+
re = /^.*?(?=\t)|\S+/ # match any characters before the first tab or non whitespaces
|
87
|
+
|
88
|
+
list.each_line do |line|
|
89
|
+
puts line.colco(:bg_green, :yellow, :blue_underline, regexp:re)
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
Output:
|
94
|
+
|
95
|
+
![sample4](https://github.com/melborne/colcolor/raw/screenshot/sample4.png)
|
96
|
+
|
97
|
+
## Contributing
|
98
|
+
|
99
|
+
1. Fork it ( https://github.com/[my-github-username]/colcolor/fork )
|
100
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
101
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
102
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
103
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/colcolor.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'colcolor/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "colcolor"
|
8
|
+
spec.version = Colcolor::VERSION
|
9
|
+
spec.authors = ["kyoendo"]
|
10
|
+
spec.email = ["postagie@gmail.com"]
|
11
|
+
spec.summary = %q{Easily colorize terminal text by each column.}
|
12
|
+
spec.description = %q{Easily colorize terminal text by each column.}
|
13
|
+
spec.homepage = "https://github.com/melborne/colcolor"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/colcolor.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require "colcolor/version"
|
2
|
+
|
3
|
+
module Colcolor
|
4
|
+
COLORS = Hash[
|
5
|
+
%i( black red green yellow blue magenta cyan white )
|
6
|
+
.zip(30..37)
|
7
|
+
.map { |k,v| [k, "\e[#{v}m"] }
|
8
|
+
].freeze
|
9
|
+
|
10
|
+
BGCOLORS = Hash[
|
11
|
+
%i( black red green yellow blue magenta cyan white )
|
12
|
+
.zip(40..47)
|
13
|
+
.map { |k,v| [k, "\e[#{v}m"] }
|
14
|
+
].freeze
|
15
|
+
|
16
|
+
EXTRA = Hash[
|
17
|
+
%i( clear bold underline blink reverse )
|
18
|
+
.zip([0, 1, 4, 5, 7])
|
19
|
+
.map { |k,v| [k, "\e[#{v}m"] }
|
20
|
+
].freeze
|
21
|
+
|
22
|
+
def colco(*colors, regexp:/\S+/)
|
23
|
+
cs = colors.dup
|
24
|
+
self.gsub(regexp) do
|
25
|
+
color = build_color_tag(cs.shift)
|
26
|
+
color + $& + EXTRA[:clear]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def build_color_tag(name)
|
32
|
+
if tag = COLORS[name] || BGCOLORS["bg_#{name}".intern] || EXTRA[name]
|
33
|
+
tag
|
34
|
+
else
|
35
|
+
fore, *ext = name.to_s.split('_').map(&:intern)
|
36
|
+
tag = "#{COLORS[fore]}"
|
37
|
+
ext = ext.inject("") { |t, ex|
|
38
|
+
ex = BGCOLORS[ex] || EXTRA[ex]; "#{t}#{ex}" }
|
39
|
+
tag << ext
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
String.send(:include, Colcolor)
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Colcolor do
|
4
|
+
it 'has a version number' do
|
5
|
+
expect(Colcolor::VERSION).not_to be nil
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "String#c" do
|
9
|
+
before do
|
10
|
+
@str = "George 18 guitarist"
|
11
|
+
end
|
12
|
+
|
13
|
+
context "match words and colors sizes" do
|
14
|
+
it "colors each words with given colors" do
|
15
|
+
expect = "\e[32mGeorge\e[0m \e[33m18\e[0m \e[31mguitarist\e[0m"
|
16
|
+
color_set = [:green, :yellow, :red]
|
17
|
+
expect(@str.colco(*color_set)).to eq expect
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "two or more color attributes to a word" do
|
22
|
+
it "colors a word with two attrs" do
|
23
|
+
str = "Charlie is great!"
|
24
|
+
expect = "\e[32mCharlie\e[0m is\e[0m \e[31m\e[4mgreat!\e[0m"
|
25
|
+
color_set = [:green, nil, :red_underline]
|
26
|
+
expect(str.colco(*color_set)).to eq expect
|
27
|
+
end
|
28
|
+
|
29
|
+
it "colors a word with three attrs" do
|
30
|
+
str = "Charlie is great!"
|
31
|
+
expect = "\e[32mCharlie\e[0m is\e[0m \e[31m\e[43m\e[4mgreat!\e[0m"
|
32
|
+
color_set = [:green, nil, :red_yellow_underline]
|
33
|
+
expect(str.colco(*color_set)).to eq expect
|
34
|
+
end
|
35
|
+
|
36
|
+
it "colors a word with color attrs but no foreground" do
|
37
|
+
str = "Charlie is great!"
|
38
|
+
expect = "\e[32mCharlie\e[0m is\e[0m \e[43m\e[4mgreat!\e[0m"
|
39
|
+
color_set = [:green, nil, :_yellow_underline]
|
40
|
+
expect(str.colco(*color_set)).to eq expect
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "short colors" do
|
45
|
+
it "colors a word white for missing color" do
|
46
|
+
expect = "\e[32mGeorge\e[0m \e[33m18\e[0m guitarist\e[0m"
|
47
|
+
color_set = [:green, :yellow]
|
48
|
+
expect(@str.colco(*color_set)).to eq expect
|
49
|
+
end
|
50
|
+
|
51
|
+
it "colors a word white at intermidiate" do
|
52
|
+
expect = "\e[32mGeorge\e[0m 18\e[0m \e[31mguitarist\e[0m"
|
53
|
+
color_set = [:green, nil, :red]
|
54
|
+
expect(@str.colco(*color_set)).to eq expect
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "irregular pattern" do
|
59
|
+
it "gets regexp to define the chunks" do
|
60
|
+
str = "George + 18 guitarist"
|
61
|
+
expect = "\e[32mGeorge\e[0m \e[33m+ 18\e[0m \e[31mguitarist\e[0m"
|
62
|
+
color_set = [:green, :yellow, :red]
|
63
|
+
re = /\+\s*\d+|\S+/
|
64
|
+
expect(str.colco(*color_set, regexp:re)).to eq expect
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: colcolor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kyoendo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
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: Easily colorize terminal text by each column.
|
56
|
+
email:
|
57
|
+
- postagie@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- colcolor.gemspec
|
70
|
+
- lib/colcolor.rb
|
71
|
+
- lib/colcolor/version.rb
|
72
|
+
- spec/colcolor_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: https://github.com/melborne/colcolor
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.3.0
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Easily colorize terminal text by each column.
|
98
|
+
test_files:
|
99
|
+
- spec/colcolor_spec.rb
|
100
|
+
- spec/spec_helper.rb
|