dye 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.rdoc +4 -1
  2. data/Rakefile +1 -1
  3. data/VERSION +1 -1
  4. data/lib/dye.rb +25 -28
  5. metadata +4 -4
data/README.rdoc CHANGED
@@ -38,6 +38,9 @@ Easy ANSI code coloring for strings in libraries.
38
38
  # and use it on the instance
39
39
  any_instance.dye "any string", :any_style
40
40
 
41
+ # alternative no-color string
42
+ Dye.dye ' reversed ', '=== reversed ===', :reversed
43
+
41
44
  # back to plain text
42
45
  plain_text = Dye.strip_ansi(ansi_string)
43
46
 
@@ -114,7 +117,7 @@ so Dye will output non strict ansi by default. If you want to have strict ansi y
114
117
 
115
118
  Dye.strict_ansi = true
116
119
 
117
- or you can set the DYE_ANSI_STRICT environment variable for a system wide setting.
120
+ or you can set the DYE_STRICT_ANSI environment variable for a system wide setting.
118
121
 
119
122
  === Color
120
123
 
data/Rakefile CHANGED
@@ -20,7 +20,7 @@ task :install, :force do |t, args|
20
20
  File.open('VERSION', 'w') {|f| f.puts version }
21
21
  gem_name = "#{name}-#{version}.gem"
22
22
  sh %(gem build #{name}.gemspec)
23
- sh %(gem install #{gem_name} --local)
23
+ sh %(gem install #{gem_name} --local --no-rdoc --no-ri)
24
24
  puts <<-EOS.gsub(/^ {6}/, '')
25
25
 
26
26
  *******************************************************************************
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/lib/dye.rb CHANGED
@@ -1,20 +1,9 @@
1
- class Object
1
+ class Module
2
2
 
3
3
  def define_dye_method(custom_styles={})
4
- define_method(:dye) do |string, *names|
5
- return string unless Dye.color?
6
- codes = names.map do |name|
7
- case
8
- when custom_styles.has_key?(name)
9
- Dye.sgr_names_to_codes *custom_styles[name]
10
- when Dye::BASIC_SGR.has_key?(name)
11
- Dye::BASIC_SGR[name]
12
- else
13
- raise Dye::UnknownSgrCode.new(name)
14
- end
15
- end.flatten
16
- Dye.apply_codes string, *codes
17
- end
4
+ define_method(:dye) do |*args|
5
+ Dye.send :apply_styles, custom_styles, *args
6
+ end
18
7
  end
19
8
 
20
9
  end
@@ -66,26 +55,33 @@ module Dye
66
55
  @strict_ansi = !!ENV['DYE_STRICT_ANSI']
67
56
  attr_accessor :strict_ansi
68
57
 
69
- def dye(string, *sgr_names)
70
- return string unless color?
71
- apply_codes string, sgr_names_to_codes(*sgr_names)
58
+ def dye(*args)
59
+ apply_styles( {}, *args )
72
60
  end
73
61
 
74
62
  def strip_ansi(string)
75
63
  string.gsub(/\e\[[\d;]+m/, '')
76
64
  end
77
65
 
78
- def sgr_names_to_codes(*names)
79
- names.map do |n|
80
- next if n.nil?
81
- code = n.is_a?(Symbol) ? BASIC_SGR[n] : n
82
- raise UnknownSgrCode.new(n) unless code.is_a?(Integer) && (0..109).include?(code)
83
- code
84
- end
85
- end
66
+ private
86
67
 
87
- def apply_codes(string, *codes)
88
- codes.compact!
68
+ def apply_styles(custom_styles, *args)
69
+ if args[1].is_a?(String)
70
+ string, no_color_string, *names = args
71
+ else
72
+ string, *names = args
73
+ end
74
+ return no_color_string||string unless color?
75
+ codes = names.map do |name|
76
+ sgr = custom_styles.has_key?(name) ? custom_styles[name] : name
77
+ sgr = [sgr] unless sgr.is_a?(Array)
78
+ sgr.map do |n|
79
+ next if n.nil?
80
+ code = n.is_a?(Symbol) ? BASIC_SGR[n] : n
81
+ raise UnknownSgrCode.new(n) unless code.is_a?(Integer) && (0..109).include?(code)
82
+ code
83
+ end
84
+ end.flatten.compact
89
85
  return string if codes.empty?
90
86
  if strict_ansi
91
87
  string.match(/^\e\[[\d;]+m.*\e\[0m$/m) ?
@@ -97,4 +93,5 @@ module Dye
97
93
  sprintf("\e[0m%s%s\e[0m", codes.map{|c| "\e[#{c}m" }.join, string)
98
94
  end
99
95
  end
96
+
100
97
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dye
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Domizio Demichelis
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-31 00:00:00 -04:00
18
+ date: 2011-02-01 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency