ansi_chameleon 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog CHANGED
@@ -1,3 +1,7 @@
1
+ 0.1.0 (February 2, 2012)
2
+ Added support for xterm 256 colors.
3
+ Made minor code improvements.
4
+
1
5
  0.0.2 (January 30, 2012)
2
6
  Fixed a typo in the LICENSE file.
3
7
 
data/README.md CHANGED
@@ -90,7 +90,7 @@ Tag names in a style sheet can be written as either :symbols or "strings".
90
90
  * `:foreground_color` (aliases: `:foreground`, `:fg_color`)
91
91
  * `:background_color` (aliases: `:background`, `:bg_color`)
92
92
 
93
- Available color values: `:black`, `:red`, `:green`, `:yellow`, `:blue`, `:magenta`, `:cyan`, `:white`.
93
+ Available color values: `:black`, `:red`, `:green`, `:yellow`, `:blue`, `:magenta`, `:cyan`, `:white` and a number/string between 0 and 255 that represents a color in xterm 256 color scale.
94
94
 
95
95
  * `:effect`
96
96
 
@@ -1,32 +1,58 @@
1
1
  module AnsiChameleon
2
2
  module SequenceGenerator
3
3
 
4
- class UnknownColorName < ArgumentError; end
5
- class UnknownEffectName < ArgumentError; end
4
+ class InvalidColorValueError < ArgumentError; end
5
+ class InvalidEffectValueError < ArgumentError; end
6
6
 
7
7
  COLORS = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
8
8
  EFFECTS = { :none => 0, :bright => 1, :underline => 4, :blink => 5, :reverse => 7 }
9
9
 
10
- def self.effect_code(name)
11
- EFFECTS[name.to_sym] or raise UnknownEffectName.new("Unknown effect name #{name.inspect}")
10
+ def self.generate(effect_value, foreground_color_value=nil, background_color_value=nil)
11
+ if effect_value.to_sym == :reset
12
+ "\033[0m"
13
+ else
14
+ "#{effect_sequence(effect_value)}#{foreground_color_sequence(foreground_color_value)}#{background_color_sequence(background_color_value)}"
15
+ end
12
16
  end
13
17
 
14
- def self.foreground_color_code(name)
15
- index = COLORS.index(name.to_sym) or raise UnknownColorName.new("Unknown foreground color name #{name.inspect}")
16
- 30 + index
18
+ def self.effect_sequence(value)
19
+ if EFFECTS.include?(value.to_sym)
20
+ "\033[#{EFFECTS[value.to_sym]}m"
21
+
22
+ else
23
+ raise InvalidEffectValueError.new("Invalid effect value #{value.inspect}")
24
+
25
+ end
17
26
  end
18
27
 
19
- def self.background_color_code(name)
20
- index = COLORS.index(name.to_sym) or raise UnknownColorName.new("Unknown background color name #{name.inspect}")
21
- 40 + index
28
+ def self.foreground_color_sequence(value)
29
+ if number_from_0_to_255?(value)
30
+ "\033[38;5;#{value}m"
31
+
32
+ elsif COLORS.include?(value.to_s.to_sym)
33
+ "\033[#{COLORS.index(value.to_sym) + 30}m"
34
+
35
+ else
36
+ raise InvalidColorValueError.new("Invalid foreground color value #{value.inspect}")
37
+
38
+ end
22
39
  end
23
40
 
24
- def self.generate(effect_name, foreground_color_name=nil, background_color_name=nil)
25
- if effect_name.to_sym == :reset
26
- "\033[0m"
41
+ def self.background_color_sequence(value)
42
+ if number_from_0_to_255?(value)
43
+ "\033[48;5;#{value}m"
44
+
45
+ elsif COLORS.include?(value.to_s.to_sym)
46
+ "\033[#{COLORS.index(value.to_sym) + 40}m"
47
+
27
48
  else
28
- "\033[#{effect_code(effect_name)};#{foreground_color_code(foreground_color_name)};#{background_color_code(background_color_name)}m"
49
+ raise InvalidColorValueError.new("Invalid background color value #{value.inspect}")
50
+
29
51
  end
30
52
  end
53
+
54
+ def self.number_from_0_to_255?(value)
55
+ value.to_s =~ /^\d+$/ && (0..255).member?(value.to_i)
56
+ end
31
57
  end
32
58
  end
@@ -1,3 +1,3 @@
1
1
  module AnsiChameleon
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ansi_chameleon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-30 00:00:00.000000000Z
12
+ date: 2012-02-02 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &80598820 !ruby/object:Gem::Requirement
16
+ requirement: &79655270 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '2.0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *80598820
24
+ version_requirements: *79655270
25
25
  description: Colorizes text terminal output by converting custom HTML-like tags into
26
26
  color ANSI escape sequences.
27
27
  email: jacekmikrut.software@gmail.com