ansi256 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -15,7 +15,7 @@ Features:
15
15
  Basic usage
16
16
  -----------
17
17
 
18
- ### 256-colors
18
+ ### Numeric 256-color codes
19
19
 
20
20
  ```ruby
21
21
  require 'ansi256'
@@ -87,14 +87,32 @@ ansi256 executable
87
87
  Ansi256 comes with `ansi256` script which can be used as follows
88
88
 
89
89
  ```bash
90
- > ansi256
91
90
  usage: ansi256 [-u] <[fg][/bg]> [mesage]
92
91
 
93
- > ansi256 232/226 "Hello world"
92
+ # Numeric color codes
93
+ ansi256 232 "Hello world"
94
+ ansi256 /226 "Hello world"
95
+ ansi256 232/226 "Hello world"
94
96
 
95
- > ls | ansi256 -u /226
97
+ # Named color codes
98
+ ansi256 yellow "Hello world"
99
+ ansi256 /blue "Hello world"
100
+ ansi256 yellow/blue "Hello world"
96
101
 
97
- > ansi256 30 "Say '$(ansi256 230/75 "Hello $(ansi256 -u 232/226 World)")'"
102
+ # Mixed color codes
103
+ ansi256 yellow/232 "Hello world"
104
+
105
+ # Bold yellow
106
+ ansi256 yellow/232 -b "Hello world"
107
+
108
+ # With underline
109
+ ansi256 yellow/232 -b -u "Hello world"
110
+
111
+ # Colorizing STDIN
112
+ find / | ansi256 -u /226
113
+
114
+ # Nesting
115
+ ansi256 30 "Say '$(ansi256 230/75 "Hello $(ansi256 -u 232/226 World)")'"
98
116
  ```
99
117
 
100
118
  Color chart
data/bin/ansi256 CHANGED
@@ -2,29 +2,35 @@
2
2
  require 'rubygems'
3
3
  require 'ansi256'
4
4
 
5
+ bold = ARGV.delete '-b'
5
6
  underline = ARGV.delete '-u'
6
7
 
7
8
  if ARGV.length < 1
8
- puts 'usage: ansi256 [-u] <[fg][/bg]> [message]'
9
+ puts 'usage: ansi256 [-b] [-u] <[fg][/bg]> [message]'
9
10
  exit 1
10
11
  end
11
12
 
12
13
  fgbg, *msg = ARGV
13
- fg, bg = fgbg.split '/'
14
+ fg, bg = fgbg.split('/').map { |e|
15
+ if !e.to_s.empty?
16
+ (e =~ /^\d+$/) ? e.to_i : e.downcase.to_sym
17
+ end
18
+ }
14
19
 
15
- def output msg, fg, bg, ul
20
+ def output msg, fg, bg, ul, b
16
21
  msg = msg.fg(fg) if fg
17
22
  msg = msg.bg(bg) if bg
18
23
  msg = msg.underline if ul
24
+ msg = msg.bold if b
19
25
  puts msg
20
26
  end
21
27
 
22
28
  if !msg.empty?
23
29
  msg.each do |m|
24
- output m, fg, bg, underline
30
+ output m, fg, bg, underline, bold
25
31
  end
26
32
  else
27
33
  while msg = $stdin.gets
28
- output msg.chomp, fg, bg, underline
34
+ output msg.chomp, fg, bg, underline, bold
29
35
  end
30
36
  end
data/lib/ansi256.rb CHANGED
@@ -1,23 +1,31 @@
1
- require "ansi256/version"
2
- require "ansi256/code"
3
- require "ansi256/mixin"
4
1
  require 'set'
2
+ require 'ansi256/version'
3
+ require 'ansi256/code'
4
+ require 'ansi256/mixin'
5
5
 
6
6
  module Ansi256
7
7
  class << self
8
8
  def fg code, str = nil
9
9
  if str
10
10
  wrap str, Ansi256.fg(code)
11
- else
11
+ elsif NAMED_COLORS.include?(code)
12
+ "\e[#{CODE[code]}m"
13
+ elsif code.is_a?(Fixnum) && (0..255).include?(code)
12
14
  "\e[38;5;#{code}m"
15
+ else
16
+ raise ArgumentError, "Invalid color code: #{code}"
13
17
  end
14
18
  end
15
19
 
16
20
  def bg code, str = nil
17
21
  if str
18
22
  wrap str, Ansi256.bg(code)
19
- else
23
+ elsif NAMED_COLORS.include?(code)
24
+ "\e[#{CODE[code] + 10}m"
25
+ elsif code.is_a?(Fixnum) && (0..255).include?(code)
20
26
  "\e[48;5;#{code}m"
27
+ else
28
+ raise ArgumentError, "Invalid color code: #{code}"
21
29
  end
22
30
  end
23
31
 
@@ -36,9 +44,9 @@ module Ansi256
36
44
  end
37
45
 
38
46
  private
39
- PATTERN = /\e\[[0-9;]+m/
40
- MULTI_PATTERN = /(?:\e\[[0-9;]+m)+/
41
- EMPTY_TRIPLE = [nil, nil, Set.new]
47
+ PATTERN = /\e\[[0-9;]+m/.freeze
48
+ MULTI_PATTERN = /(?:\e\[[0-9;]+m)+/.freeze
49
+ EMPTY_TRIPLE = [nil, nil, Set.new].freeze
42
50
 
43
51
  def ansify prev, curr
44
52
  nums = []
data/lib/ansi256/code.rb CHANGED
@@ -21,5 +21,16 @@ module Ansi256
21
21
  :on_magenta => 45,
22
22
  :on_cyan => 46,
23
23
  :on_white => 47,
24
- }
24
+ }.freeze
25
+
26
+ NAMED_COLORS = Set[
27
+ :black,
28
+ :red,
29
+ :green,
30
+ :yellow,
31
+ :blue,
32
+ :magenta,
33
+ :cyan,
34
+ :white,
35
+ ].freeze
25
36
  end
@@ -1,3 +1,3 @@
1
1
  module Ansi256
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/test/test_ansi256.rb CHANGED
@@ -1,51 +1,142 @@
1
1
  $VERBOSE = true
2
2
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
3
  require 'ansi256'
4
+ require 'minitest/autorun'
4
5
 
5
- def cfmt col
6
- col.to_s.rjust(5).fg(232).bg(col)
7
- end
6
+ class TestAnsi256 < MiniTest::Unit::TestCase
7
+ def cfmt col
8
+ col.to_s.rjust(5).fg(232).bg(col)
9
+ end
8
10
 
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
11
+ # TODO: assertions :p
14
12
 
15
- # Wrap
13
+ def test_256_color_table
14
+ puts (0..7).map { |col| cfmt col }.join
15
+ puts (8..15).map { |col| cfmt col }.join
16
+ (16..255).each_slice(6) do |slice|
17
+ puts slice.map { |col| cfmt col }.join
18
+ end
19
+ end
16
20
 
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
-
29
- # Nesting 2
30
- puts world = "World".bg(226).blue.underline
31
- puts hello = "Hello #{world} Hello".white.bold
32
- puts say_hello_world = "Say '#{hello}'".fg(30).underline
33
- puts say_hello_world.plain.fg(27)
34
-
35
- # Nesting 3
36
- puts world = "World".blue.underline
37
- puts hello = "Hello #{world} Hello".blue.bold
38
- puts say_hello_world = "Say '#{hello}'".fg(30).underline
39
- puts say_hello_world.plain.fg(27)
40
-
41
- # Named colors
42
- s = "Colorize me"
43
- puts [ s.black, s.black.bold, s.white.bold.on_black ].join ' '
44
- puts [ s.red, s.red.bold, s.white.bold.on_red ].join ' '
45
- puts [ s.green, s.green.bold, s.white.bold.on_green ].join ' '
46
- puts [ s.yellow, s.yellow.bold, s.white.bold.on_yellow ].join ' '
47
- puts [ s.blue, s.blue.bold, s.white.bold.on_blue ].join ' '
48
- puts [ s.magenta, s.magenta.bold, s.white.bold.on_magenta ].join ' '
49
- puts [ s.cyan, s.cyan.bold, s.white.bold.on_cyan ].join ' '
50
- puts [ s.white, s.white.bold, s.white.bold.on_white ].join ' '
21
+ def test_nesting
22
+ a = ' '
23
+ (16..60).each do |i|
24
+ a = "<#{a}>".bg(i).fg(i * 2).underline
25
+ end
26
+ puts a
27
+ end
28
+
29
+ def test_nesting_hello_world
30
+ # Nesting
31
+ puts world = "World".bg(226).fg(232).underline
32
+ puts hello = "Hello #{world} !".fg(230).bg(75)
33
+ puts say_hello_world = "Say '#{hello}'".fg(30)
34
+ puts say_hello_world.plain.fg(27)
35
+ end
36
+
37
+ def test_nesting_hello_world2
38
+ puts world = "World".bg(226).blue.underline
39
+ puts hello = "Hello #{world} Hello".white.bold
40
+ puts say_hello_world = "Say '#{hello}'".fg(30).underline
41
+ puts say_hello_world.plain.fg(27)
42
+ end
43
+
44
+ def test_nesting_hello_world3
45
+ puts world = "World".blue.underline
46
+ puts hello = "Hello #{world} Hello".blue.bold
47
+ puts say_hello_world = "Say '#{hello}'".fg(30).underline
48
+ puts say_hello_world.plain.fg(27)
49
+ end
50
+
51
+ def test_named_colors
52
+ s = "Colorize me"
53
+ puts [ s.black, s.black.bold, s.white.bold.on_black ].join ' '
54
+ puts [ s.red, s.red.bold, s.white.bold.on_red ].join ' '
55
+ puts [ s.green, s.green.bold, s.white.bold.on_green ].join ' '
56
+ puts [ s.yellow, s.yellow.bold, s.white.bold.on_yellow ].join ' '
57
+ puts [ s.blue, s.blue.bold, s.white.bold.on_blue ].join ' '
58
+ puts [ s.magenta, s.magenta.bold, s.white.bold.on_magenta ].join ' '
59
+ puts [ s.cyan, s.cyan.bold, s.white.bold.on_cyan ].join ' '
60
+ puts [ s.white, s.white.bold, s.white.bold.on_white ].join ' '
61
+ end
62
+
63
+ def test_named_color_code_with_fg_bg
64
+ puts "Colorize me".fg(:green).bg(:red).bold.underline
65
+ end
66
+
67
+ def test_just_code
68
+ assert_equal "\e[0m", Ansi256.reset
69
+ assert_equal "\e[1m", Ansi256.bold
70
+ assert_equal "\e[4m", Ansi256.underline
51
71
 
72
+ assert_equal "\e[30m", Ansi256.black
73
+ assert_equal "\e[31m", Ansi256.red
74
+ assert_equal "\e[32m", Ansi256.green
75
+ assert_equal "\e[33m", Ansi256.yellow
76
+ assert_equal "\e[34m", Ansi256.blue
77
+ assert_equal "\e[35m", Ansi256.magenta
78
+ assert_equal "\e[36m", Ansi256.cyan
79
+ assert_equal "\e[37m", Ansi256.white
80
+
81
+ assert_equal "\e[40m", Ansi256.on_black
82
+ assert_equal "\e[41m", Ansi256.on_red
83
+ assert_equal "\e[42m", Ansi256.on_green
84
+ assert_equal "\e[43m", Ansi256.on_yellow
85
+ assert_equal "\e[44m", Ansi256.on_blue
86
+ assert_equal "\e[45m", Ansi256.on_magenta
87
+ assert_equal "\e[46m", Ansi256.on_cyan
88
+ assert_equal "\e[47m", Ansi256.on_white
89
+
90
+ assert_equal "\e[30m", Ansi256.fg(:black)
91
+ assert_equal "\e[31m", Ansi256.fg(:red)
92
+ assert_equal "\e[32m", Ansi256.fg(:green)
93
+ assert_equal "\e[33m", Ansi256.fg(:yellow)
94
+ assert_equal "\e[34m", Ansi256.fg(:blue)
95
+ assert_equal "\e[35m", Ansi256.fg(:magenta)
96
+ assert_equal "\e[36m", Ansi256.fg(:cyan)
97
+ assert_equal "\e[37m", Ansi256.fg(:white)
98
+
99
+ assert_equal "\e[40m", Ansi256.bg(:black)
100
+ assert_equal "\e[41m", Ansi256.bg(:red)
101
+ assert_equal "\e[42m", Ansi256.bg(:green)
102
+ assert_equal "\e[43m", Ansi256.bg(:yellow)
103
+ assert_equal "\e[44m", Ansi256.bg(:blue)
104
+ assert_equal "\e[45m", Ansi256.bg(:magenta)
105
+ assert_equal "\e[46m", Ansi256.bg(:cyan)
106
+ assert_equal "\e[47m", Ansi256.bg(:white)
107
+ end
108
+
109
+ def test_invalid_color_code
110
+ assert_raises(ArgumentError) { Ansi256.fg(:none) }
111
+ assert_raises(ArgumentError) { Ansi256.fg(:on_green) }
112
+ assert_raises(ArgumentError) { Ansi256.bg(:none) }
113
+ assert_raises(ArgumentError) { Ansi256.bg(:on_green) }
114
+ assert_raises(ArgumentError) { Ansi256.fg(-1) }
115
+ assert_raises(ArgumentError) { Ansi256.fg(300) }
116
+ assert_raises(ArgumentError) { Ansi256.bg(-1) }
117
+ assert_raises(ArgumentError) { Ansi256.bg(300) }
118
+ end
119
+
120
+ def test_fg_bg_underline
121
+ assert_equal "\e[38;5;100mHello\e[0m", 'Hello'.fg(100)
122
+ assert_equal "\e[48;5;100mHello\e[0m", 'Hello'.bg(100)
123
+
124
+ assert_equal "\e[31mHello\e[0m", 'Hello'.red
125
+ assert_equal "\e[41mHello\e[0m", 'Hello'.on_red
126
+
127
+ assert_equal "\e[38;5;100mHello\e[38;5;200m world\e[0m", "#{'Hello'.fg(100)} world".fg(200)
128
+ assert_equal "\e[38;5;200;4mWow \e[38;5;100mhello\e[38;5;200m world\e[0m",
129
+ "Wow #{'hello'.fg(100)} world".fg(200).underline
130
+ assert_equal "\e[38;5;200mWow \e[38;5;100;4mhello\e[0m\e[38;5;200m world\e[0m",
131
+ "Wow #{'hello'.fg(100).underline} world".fg(200)
132
+ assert_equal "\e[38;5;200mWow \e[38;5;100;48;5;50;4mhello\e[0m\e[38;5;200m world\e[0m",
133
+ "Wow #{'hello'.fg(100).underline.bg(50)} world".fg(200)
134
+ assert_equal "\e[38;5;200;48;5;250mWow \e[38;5;100;48;5;50;4mhello\e[0m\e[38;5;200;48;5;250m world\e[0m",
135
+ "Wow #{'hello'.fg(100).underline.bg(50)} world".fg(200).bg(250)
136
+ assert_equal "Wow hello world",
137
+ "Wow #{'hello'.fg(100).underline.bg(50)} world".fg(200).bg(250).plain
138
+
139
+ assert_equal "\e[32;48;5;200;1mWow \e[38;5;100;44;1;4mhello\e[0m\e[32;48;5;200;1m world\e[0m",
140
+ "Wow #{'hello'.fg(100).underline.on_blue} world".green.bold.bg(200)
141
+ end
142
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ansi256
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-10 00:00:00.000000000 Z
12
+ date: 2013-05-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -78,7 +78,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
78
  version: '0'
79
79
  segments:
80
80
  - 0
81
- hash: 2355104005458200198
81
+ hash: -3827977892707263520
82
82
  required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  version: '0'
88
88
  segments:
89
89
  - 0
90
- hash: 2355104005458200198
90
+ hash: -3827977892707263520
91
91
  requirements: []
92
92
  rubyforge_project:
93
93
  rubygems_version: 1.8.25