ansi256 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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +99 -0
- data/Rakefile +7 -0
- data/ansi256.gemspec +23 -0
- data/bin/ansi256 +30 -0
- data/lib/ansi256/mixin.rb +20 -0
- data/lib/ansi256/version.rb +3 -0
- data/lib/ansi256.rb +87 -0
- data/test/test_ansi256.rb +28 -0
- metadata +97 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Junegunn Choi
|
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,99 @@
|
|
1
|
+
# Ansi256
|
2
|
+
|
3
|
+
Decorate text with 256-color ANSI codes.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
$ gem install ansi256
|
8
|
+
|
9
|
+
Basic usage
|
10
|
+
-----------
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
require 'ansi256'
|
14
|
+
|
15
|
+
# Foreground color
|
16
|
+
puts "Colorize me".fg(111)
|
17
|
+
|
18
|
+
# With background color
|
19
|
+
puts "Colorize me".fg(111).bg(226)
|
20
|
+
|
21
|
+
# Also with underline
|
22
|
+
puts "Colorize me".fg(111).bg(226).underline
|
23
|
+
|
24
|
+
# Strip ANSI codes
|
25
|
+
puts "Colorize me".fg(111).bg(226).underline.plain
|
26
|
+
```
|
27
|
+
|
28
|
+

|
29
|
+
|
30
|
+
Added methods (`fg`, `bg`, `underline`, and `plain`)
|
31
|
+
return new String object and do not modify the original String.
|
32
|
+
|
33
|
+
256-color table
|
34
|
+
---------------
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
require 'ansi256'
|
38
|
+
|
39
|
+
def cfmt col
|
40
|
+
col.to_s.rjust(5).fg(232).bg(col)
|
41
|
+
end
|
42
|
+
|
43
|
+
puts (0..7).map { |col| cfmt col }.join
|
44
|
+
puts (8..15).map { |col| cfmt col }.join
|
45
|
+
(16..255).each_slice(6) do |slice|
|
46
|
+
puts slice.map { |col| cfmt col }.join
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
Nesting
|
51
|
+
-------
|
52
|
+
|
53
|
+
Unlike the other similar gems, Ansi256 allows you to nest colored text.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
require 'ansi256'
|
57
|
+
|
58
|
+
puts world = "World".bg(226).fg(232).underline
|
59
|
+
puts hello_world = "Hello #{world} !".fg(230).bg(75)
|
60
|
+
puts say_hello_world = "Say '#{hello_world}'".fg(30)
|
61
|
+
puts say_hello_world.plain.fg(27)
|
62
|
+
```
|
63
|
+
|
64
|
+

|
65
|
+
|
66
|
+
_"Just gimme the code"_
|
67
|
+
-----------------------
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
Ansi256.fg(232)
|
71
|
+
Ansi256.bg(226)
|
72
|
+
Ansi256.underline
|
73
|
+
Ansi256.reset
|
74
|
+
```
|
75
|
+
|
76
|
+
|
77
|
+
ansi256 executable
|
78
|
+
------------------
|
79
|
+
|
80
|
+
Ansi256 comes with ansi256 script which can be used as follows
|
81
|
+
|
82
|
+
```bash
|
83
|
+
> ansi256
|
84
|
+
usage: ansi256 [-u] <[fg][/bg]> [mesage]
|
85
|
+
|
86
|
+
> ansi256 232/226 "Hello world"
|
87
|
+
|
88
|
+
> ls | ansi256 -u /226
|
89
|
+
|
90
|
+
> ansi256 30 "Say '$(ansi256 230/75 "Hello $(ansi256 -u 232/226 World)")'"
|
91
|
+
```
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
1. Fork it
|
96
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
97
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
98
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
99
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/ansi256.gemspec
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 'ansi256/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ansi256"
|
8
|
+
spec.version = Ansi256::VERSION
|
9
|
+
spec.authors = ["Junegunn Choi"]
|
10
|
+
spec.email = ["junegunn.c@gmail.com"]
|
11
|
+
spec.description = %q{Colorize text using 256-color ANSI codes}
|
12
|
+
spec.summary = %q{Colorize text using 256-color ANSI codes}
|
13
|
+
spec.homepage = "https://github.com/junegunn/ansi256"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/).reject { |f| File.extname(f) == '.png' }
|
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.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/bin/ansi256
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'ansi256'
|
4
|
+
|
5
|
+
underline = ARGV.delete '-u'
|
6
|
+
|
7
|
+
if ARGV.length < 1
|
8
|
+
puts 'usage: ansi256 [-u] <[fg][/bg]> [message]'
|
9
|
+
exit 1
|
10
|
+
end
|
11
|
+
|
12
|
+
fgbg, *msg = ARGV
|
13
|
+
fg, bg = fgbg.split '/'
|
14
|
+
|
15
|
+
def output msg, fg, bg, ul
|
16
|
+
msg = msg.fg(fg) if fg
|
17
|
+
msg = msg.bg(bg) if bg
|
18
|
+
msg = msg.underline if ul
|
19
|
+
puts msg
|
20
|
+
end
|
21
|
+
|
22
|
+
if !msg.empty?
|
23
|
+
msg.each do |m|
|
24
|
+
output m, fg, bg, underline
|
25
|
+
end
|
26
|
+
else
|
27
|
+
while msg = $stdin.gets
|
28
|
+
output msg.chomp, fg, bg, underline
|
29
|
+
end
|
30
|
+
end
|
data/lib/ansi256.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require "ansi256/version"
|
2
|
+
require "ansi256/mixin"
|
3
|
+
|
4
|
+
module Ansi256
|
5
|
+
RESET = "\e[0m"
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def fg code, str = nil
|
9
|
+
if str
|
10
|
+
wrap str, Ansi256.fg(code)
|
11
|
+
else
|
12
|
+
"\e[38;5;#{code}m"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def bg code, str = nil
|
17
|
+
if str
|
18
|
+
wrap str, Ansi256.bg(code)
|
19
|
+
else
|
20
|
+
"\e[48;5;#{code}m"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def underline str = nil
|
25
|
+
if str
|
26
|
+
wrap str, Ansi256.underline
|
27
|
+
else
|
28
|
+
"\e[4m"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def reset
|
33
|
+
RESET
|
34
|
+
end
|
35
|
+
|
36
|
+
def plain str
|
37
|
+
str.gsub(PATTERN, '')
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
PATTERN = /\e\[(?:[34]8;5;)?[0-9]+m/
|
42
|
+
MULTI_PATTERN = /(?:\e\[(?:[34]8;5;)?[0-9]+m)+/
|
43
|
+
|
44
|
+
def wrap str, color
|
45
|
+
current = [nil, nil, nil]
|
46
|
+
|
47
|
+
(color + str.gsub(PATTERN) { |m|
|
48
|
+
if m =~ /\e\[0m/
|
49
|
+
m + color
|
50
|
+
else
|
51
|
+
m
|
52
|
+
end
|
53
|
+
} << RESET).gsub(MULTI_PATTERN) { |codes|
|
54
|
+
prev = current.dup
|
55
|
+
codes.split(/(?<=m)/).each do |code|
|
56
|
+
case code
|
57
|
+
when /\e\[38/
|
58
|
+
current[0] = code
|
59
|
+
when /\e\[48/
|
60
|
+
current[1] = code
|
61
|
+
when /\e\[0m/
|
62
|
+
current = [nil, nil, nil]
|
63
|
+
else
|
64
|
+
current[2] = code
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
if current == prev
|
69
|
+
''
|
70
|
+
elsif current == [nil, nil, nil]
|
71
|
+
RESET
|
72
|
+
else
|
73
|
+
if (0..2).any? { |i| prev[i] && !current[i] }
|
74
|
+
RESET
|
75
|
+
else
|
76
|
+
''
|
77
|
+
end + current.join
|
78
|
+
end
|
79
|
+
}
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class String
|
85
|
+
include Ansi256::Mixin
|
86
|
+
end
|
87
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
$VERBOSE = true
|
2
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
3
|
+
require 'ansi256'
|
4
|
+
|
5
|
+
def cfmt col
|
6
|
+
col.to_s.rjust(5).fg(232).bg(col)
|
7
|
+
end
|
8
|
+
|
9
|
+
puts (0..7).map { |col| cfmt col }.join
|
10
|
+
puts (8..15).map { |col| cfmt col }.join
|
11
|
+
(16..255).each_slice(6) do |slice|
|
12
|
+
puts slice.map { |col| cfmt col }.join
|
13
|
+
end
|
14
|
+
|
15
|
+
# Wrap
|
16
|
+
|
17
|
+
a = ' '
|
18
|
+
(16..60).each do |i|
|
19
|
+
a = "<#{a}>".bg(i).fg(i * 2).underline
|
20
|
+
end
|
21
|
+
puts a
|
22
|
+
|
23
|
+
# Nesting
|
24
|
+
puts world = "World".bg(226).fg(232).underline
|
25
|
+
puts hello = "Hello #{world} !".fg(230).bg(75)
|
26
|
+
puts say_hello_world = "Say '#{hello}'".fg(30)
|
27
|
+
puts say_hello_world.plain.fg(27)
|
28
|
+
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ansi256
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Junegunn Choi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Colorize text using 256-color ANSI codes
|
47
|
+
email:
|
48
|
+
- junegunn.c@gmail.com
|
49
|
+
executables:
|
50
|
+
- ansi256
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- ansi256.gemspec
|
60
|
+
- bin/ansi256
|
61
|
+
- lib/ansi256.rb
|
62
|
+
- lib/ansi256/mixin.rb
|
63
|
+
- lib/ansi256/version.rb
|
64
|
+
- test/test_ansi256.rb
|
65
|
+
homepage: https://github.com/junegunn/ansi256
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
hash: 863938328980395766
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
hash: 863938328980395766
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.8.25
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: Colorize text using 256-color ANSI codes
|
96
|
+
test_files:
|
97
|
+
- test/test_ansi256.rb
|