color_string 0.0.2
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/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +2 -0
- data/color_string.gemspec +23 -0
- data/lib/color_string.rb +6 -0
- data/lib/color_string/colorize.rb +92 -0
- data/lib/color_string/version.rb +3 -0
- data/lib/string.rb +7 -0
- data/test/string_test.rb +26 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 850ed78834e9232847203cc34a0f157f45ebfac8
|
4
|
+
data.tar.gz: b1335183faf8bb792acfc0dad65781ee789e0cd0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4944d36586dd3bd97a444864b15f28ddb6c7b5d00437b24b4bfbd8e70cc489e11d51a6035f38281b857a62248f83ef694dbae42d0820fe19a5dd486b77b71537
|
7
|
+
data.tar.gz: 0465ecebbdb6dd82590d50d462209b893b173d56b805252d17551c688f992f76764e05249ce7eecd93327cd6a712a47eea7009959193688c8c2083a7b425e95d
|
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/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Sean Doyle
|
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,37 @@
|
|
1
|
+
# ColorString
|
2
|
+
|
3
|
+
`color_string` is a Ruby library for outputting colored strings to a console
|
4
|
+
using a simple inline syntax in your string to specify the color to print as.
|
5
|
+
|
6
|
+
For example, the string `[blue]hello [red]world` would output the text `hello
|
7
|
+
world` in two colors.
|
8
|
+
|
9
|
+
Inspired by Mitchell Hashimoto's [`colorstring`](https://github.com/mitchellh/colorstring)
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'color_string'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install color_string
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
"[red]Hello [blue]World!".color # => "\e[31mHello \e[0m\e[34mWorld\e[0m"
|
29
|
+
```
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it ( https://github.com/[my-github-username]/color_string/fork )
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'color_string/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "color_string"
|
8
|
+
spec.version = ColorString::VERSION
|
9
|
+
spec.authors = ["Sean Doyle"]
|
10
|
+
spec.email = ["sean.p.doyle24@gmail.com"]
|
11
|
+
spec.summary = %q{Color your shell strings}
|
12
|
+
spec.description = %q{Color your shell strings}
|
13
|
+
spec.homepage = "https://github.com/seanpdoyle/color_string"
|
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
|
+
end
|
data/lib/color_string.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
module ColorString
|
2
|
+
class Colorize
|
3
|
+
REGEX = %r{\[[a-z0-9_-]+\]}i
|
4
|
+
CODES = {
|
5
|
+
"default" => "39",
|
6
|
+
|
7
|
+
# Attributes
|
8
|
+
"bold" => "1",
|
9
|
+
"dim" => "2",
|
10
|
+
"underline" => "4",
|
11
|
+
"blink_slow" => "5",
|
12
|
+
"blink_fast" => "6",
|
13
|
+
"invert" => "7",
|
14
|
+
"hidden" => "8",
|
15
|
+
|
16
|
+
# Reset to reset everything to their defaults
|
17
|
+
"reset" => "0",
|
18
|
+
"reset_bold" => "21",
|
19
|
+
|
20
|
+
# Foreground Colors
|
21
|
+
"black" => "30",
|
22
|
+
"red" => "31",
|
23
|
+
"green" => "32",
|
24
|
+
"yellow" => "33",
|
25
|
+
"blue" => "34",
|
26
|
+
"magenta" => "35",
|
27
|
+
"cyan" => "36",
|
28
|
+
"light_gray" => "37",
|
29
|
+
"dark_gray" => "90",
|
30
|
+
"light_red" => "91",
|
31
|
+
"light_green" => "92",
|
32
|
+
"light_yellow" => "93",
|
33
|
+
"light_blue" => "94",
|
34
|
+
"light_magenta" => "95",
|
35
|
+
"light_cyan" => "96",
|
36
|
+
"white" => "97",
|
37
|
+
}.freeze
|
38
|
+
|
39
|
+
def initialize(string)
|
40
|
+
@string = string
|
41
|
+
end
|
42
|
+
|
43
|
+
def color
|
44
|
+
return string if matches.empty?
|
45
|
+
|
46
|
+
colored = string.match(REGEX).pre_match
|
47
|
+
matches.each_with_index do |match, index|
|
48
|
+
next_match =
|
49
|
+
if match.length > index + 1
|
50
|
+
matches[index + 1]
|
51
|
+
end
|
52
|
+
|
53
|
+
color = match.delete("[]")
|
54
|
+
code = code_for(color)
|
55
|
+
|
56
|
+
offset = string.index(match)
|
57
|
+
start_of_next =
|
58
|
+
if next_match
|
59
|
+
string.index(next_match)
|
60
|
+
else
|
61
|
+
offset + match.length
|
62
|
+
end
|
63
|
+
words =
|
64
|
+
if next_match
|
65
|
+
string[offset...start_of_next]
|
66
|
+
else
|
67
|
+
string[offset...string.length]
|
68
|
+
end
|
69
|
+
|
70
|
+
if code
|
71
|
+
words_without_code = words.gsub(match, '')
|
72
|
+
colored << "\033[#{code}m#{words_without_code}\033[0m"
|
73
|
+
else
|
74
|
+
colored << words
|
75
|
+
end
|
76
|
+
end
|
77
|
+
colored
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
attr_reader :string
|
83
|
+
|
84
|
+
def matches
|
85
|
+
string.scan(REGEX)
|
86
|
+
end
|
87
|
+
|
88
|
+
def code_for(color)
|
89
|
+
CODES.fetch(color, nil)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/lib/string.rb
ADDED
data/test/string_test.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
|
2
|
+
|
3
|
+
require "minitest/autorun"
|
4
|
+
require "color_string"
|
5
|
+
|
6
|
+
class StringTest < Minitest::Test
|
7
|
+
def test_leaves_uncolored_text_unchanged
|
8
|
+
assert_equal "Hello World",
|
9
|
+
"Hello World".color
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_colors_single_words
|
13
|
+
assert_equal "\033[34mHello\033[0m",
|
14
|
+
"[blue]Hello".color
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_ignores_unknown_codes
|
18
|
+
assert_equal "say [hi]Hello \033[31mWorld\033[0m",
|
19
|
+
"say [hi]Hello [red]World".color
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_colors_phrases
|
23
|
+
assert_equal "\033[34mHello \033[0m\033[31mWorld\033[0m",
|
24
|
+
"[blue]Hello [red]World".color
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: color_string
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sean Doyle
|
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
|
+
description: Color your shell strings
|
42
|
+
email:
|
43
|
+
- sean.p.doyle24@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- color_string.gemspec
|
54
|
+
- lib/color_string.rb
|
55
|
+
- lib/color_string/colorize.rb
|
56
|
+
- lib/color_string/version.rb
|
57
|
+
- lib/string.rb
|
58
|
+
- test/string_test.rb
|
59
|
+
homepage: https://github.com/seanpdoyle/color_string
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.3.0
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Color your shell strings
|
83
|
+
test_files:
|
84
|
+
- test/string_test.rb
|
85
|
+
has_rdoc:
|