jamescook-pow 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,4 +1,4 @@
1
- P O W ! ( Not like prisoner of war, but from the movie Step Brothers where that guy from the Daily Show is saying "POW" repeatedly. )
1
+ P O W !
2
2
 
3
3
  Override your 'puts' for fun shell coloring.
4
4
 
@@ -13,12 +13,19 @@ Examples:
13
13
  puts.red "Hello world in red"
14
14
  puts.red! "Hello world in red, but with boldness"
15
15
  puts.red_ "Hello world in red, but with underscore"
16
- puts.rainbow "Hellow world, but more silly."
16
+ puts.rainbow "Hello world, but more silly."
17
+
18
+ puts_ "Hello world, underscored."
19
+ puts! "Hello world, bold."
20
+
21
+ puts "Hello world, and I can highlight matches.", :match => "can"
22
+ puts "Hello world, and I can highlight matches with a specific color.", :match => "can", :match_color => :purple
17
23
 
18
24
  p.red "Hello world, but less typing .. also red."
19
25
  p! "Hello world, in bold"
20
26
  p! "Mix and match", :color => :purple, :background => :black
21
27
 
22
28
 
23
- TODO:
24
- - Figure out if you can bold a 'dark' color
29
+ You can also set defaults in your ~/.irbrc or wherever.
30
+ require 'pow'
31
+ Pow.defaults = {:bold => true} # Now any puts will default to bold
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{jamescook-pow}
8
- s.version = "0.1.3"
8
+ s.version = "0.1.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["James Cook"]
12
- s.date = %q{2009-11-22}
13
- s.description = %q{'puts' with shell colors.}
12
+ s.date = %q{2009-11-23}
13
+ s.description = %q{Ruby 'puts' with shell colors.}
14
14
  s.email = %q{jamecook@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "README"
data/lib/pow.rb CHANGED
@@ -1,12 +1,25 @@
1
1
  module Pow
2
2
  # Override puts on include to allow coloring (but also retain existing function)
3
- def self.included(base)
4
- base.send(:define_method, :puts){ |*args| Puts.new(*args) }
5
- base.send(:define_method, :puts!){ |*args| opts=(args.detect{|a| a.is_a?(Hash)} || {}).merge(:misc => {:bold => true}); args.reject!{|a| a.is_a?(Hash)}; args = [args.push(opts)].flatten; Puts.new(*args) } # Now that's just self-explanatory ..
3
+ class << self
4
+ def included(base)
5
+ @@defaults = {}
6
+ base.send(:define_method, :puts){ |*args| Puts.new(*args) }
7
+ base.send(:define_method, :puts!){ |*args| opts=(args.detect{|a| a.is_a?(Hash)} || {}).merge(:misc => {:bold => true}); args.reject!{|a| a.is_a?(Hash)}; args = [args.push(opts)].flatten; Puts.new(*args) } # Now that's just self-explanatory ..
8
+ base.send(:define_method, :puts_){ |*args| opts=(args.detect{|a| a.is_a?(Hash)} || {}).merge(:misc => {:underline => true}); args.reject!{|a| a.is_a?(Hash)}; args = [args.push(opts)].flatten; Puts.new(*args) } # Now that's just self-explanatory ..
9
+
10
+ base.send(:alias_method, :p, :puts)
11
+ base.send(:alias_method, :p!, :puts!)
12
+ base.send(:alias_method, :p_, :puts_)
13
+ end
14
+ def defaults
15
+ @@defaults
16
+ end
6
17
 
7
- base.send(:alias_method, :p, :puts)
8
- base.send(:alias_method, :p!, :puts!)
18
+ def defaults=(val)
19
+ @@defaults.merge!(val)
20
+ end
9
21
  end
22
+
10
23
  CODES = {
11
24
  :clear => 0,
12
25
  :reset => 0, #clear
@@ -54,6 +67,7 @@ module Pow
54
67
  attr_accessor :writer
55
68
  def initialize(*args)
56
69
  options = args[0].is_a?(Hash) ? args[0] : {:text => args[0].to_s}.merge(args[1] || {})
70
+ @@match_color = :red
57
71
  CODES.keys.each do |key|
58
72
  # Color
59
73
  self.class.send(:define_method, key.to_sym) { |*args| Puts.new({:color => key.to_sym, :text => args[0], :misc => args[1]}).out! }
@@ -68,12 +82,30 @@ module Pow
68
82
  options[:negative] ||= options[:misc][:negative]
69
83
  options[:underline] ||= options[:misc][:underline]
70
84
  options[:background] ||= (options[:misc][:background] || options[:misc][:on])
85
+ options[:on] ||= options[:background]
71
86
  options[:strikethrough] ||= options[:misc][:strikethrough]
87
+ options[:match] ||= options[:misc][:match]
88
+ options[:match_color] ||= options[:misc][:match_color]
72
89
  end
73
- @writer = options[:writer] || STDOUT
90
+ @@color = options[:color] || :white
91
+ @writer = options[:writer] || STDOUT
74
92
  @formatted_text = format_string(options)
75
93
  out!(@formatted_text)
76
94
  end
95
+
96
+ class << self
97
+ def color
98
+ @@color
99
+ end
100
+
101
+ def match_color=(val)
102
+ @@match_color = val.to_sym
103
+ end
104
+
105
+ def match_color
106
+ @@match_color
107
+ end
108
+ end
77
109
 
78
110
  # Write the coloured text to IO
79
111
  def out!(string=nil)
@@ -93,9 +125,7 @@ module Pow
93
125
  out!(text.to_s.split("").inject(""){|m, word| m+= format_string(:text => word, :bold => true, :newline => "", :color => painbow_keys.sort_by{|k| rand}[0]) + " " } + "\n")
94
126
  end
95
127
 
96
- def inspect
97
- nil
98
- end
128
+ def inspect; ''; end
99
129
 
100
130
  protected
101
131
  def painbow_keys
@@ -110,19 +140,26 @@ module Pow
110
140
  newline = options.fetch(:newline){ "\n" }
111
141
  color = options.fetch(:color){ :white }
112
142
  text = options.fetch(:text){ '' }
113
- bold = options.fetch(:bold){ false }
114
- negative = options.fetch(:negative){ false }
115
- italic = options.fetch(:italic){ false }
116
- underline = options.fetch(:underline){ false }
117
- background = options.fetch(:background){ false }
143
+ bold = options.fetch(:bold){ Pow.defaults[:bold] }
144
+ negative = options.fetch(:negative){ Pow.defaults[:negative] }
145
+ italic = options.fetch(:italic){ Pow.defaults[:italic] }
146
+ underline = options.fetch(:underline){ Pow.defaults[:underline] }
147
+ background = options.fetch(:background){ Pow.defaults[:background] } || options.fetch(:on){ Pow.defaults[:on]}
118
148
  concealed = options.fetch(:concealed){ false }
119
- strikethrough = options.fetch(:strikethrough){ false }
120
- underscore = options.fetch(:underscore){ false }
149
+ strikethrough = options.fetch(:strikethrough){ Pow.defaults[:strikethrough] }
150
+ underscore = options.fetch(:underscore){ Pow.defaults[:underscore] }
151
+ match = options.fetch(:match){ false }
152
+
153
+ if options[:match_color]
154
+ Puts.match_color = options[:match_color]
155
+ end
121
156
 
122
157
  if text != ""
123
158
  result = [escape_sequence(color), text, escape_sequence(:reset), newline].join
124
159
  end
125
160
 
161
+ result ||= ""
162
+
126
163
  if bold
127
164
  result.insert(0, escape_sequence(:bold))
128
165
  end
@@ -151,7 +188,11 @@ module Pow
151
188
  result.insert(0, escape_sequence(:underscore))
152
189
  end
153
190
 
154
- return result
191
+ if match.is_a?(Regexp) || match.is_a?(String)
192
+ result = wrap_match(result, match, Puts.match_color, false)
193
+ end
194
+
195
+ return result + escape_sequence(:clear)
155
196
  end
156
197
 
157
198
  def escape_sequence(code)
@@ -165,6 +206,11 @@ module Pow
165
206
  return "\e[#{CODES[code]}m"
166
207
  end
167
208
  end
209
+
210
+ def wrap_match(text, match, match_color, negative=false)
211
+ #TODO use the negative sequence when the text color is the same as the match color.
212
+ text.gsub(match, [escape_sequence(match_color), match, escape_sequence(Puts.color)].join(''))
213
+ end
168
214
  end
169
215
  end
170
216
 
data/rakefile.rb CHANGED
@@ -3,9 +3,9 @@ begin
3
3
  require 'jeweler'
4
4
  Jeweler::Tasks.new do |gemspec|
5
5
  gemspec.name = "jamescook-pow"
6
- gemspec.version = "0.1.3"
6
+ gemspec.version = "0.1.4"
7
7
  gemspec.summary = "puts with colors"
8
- gemspec.description = "'puts' with shell colors."
8
+ gemspec.description = "Ruby 'puts' with shell colors."
9
9
  gemspec.email = "jamecook@gmail.com"
10
10
  gemspec.homepage = "http://github.com/jamescook/pow"
11
11
  gemspec.authors = ["James Cook"]
data/test/pow_test.rb CHANGED
@@ -15,10 +15,24 @@ class PowTest < Test::Unit::TestCase
15
15
  assert_equal "\e[37mTEST\e[0m\n", @writer.gets # White text
16
16
  end
17
17
 
18
+ def test_puts_with_bold_default
19
+ Pow.defaults = {:bold => true}
20
+ @puts.new(:text => "TEST", :writer => @writer).out!
21
+ @writer.rewind
22
+ Pow.defaults = {:bold => false}
23
+ assert_equal "\e[1m\e[37mTEST\e[0m\n", @writer.gets
24
+ end
25
+
18
26
  def test_puts_with_non_string
19
27
  @puts.new(:text => 1, :writer => @writer).out!
20
28
  @writer.rewind
21
- assert_equal "\e[37m1\e[0m\n", @writer.gets # White text
29
+ assert_equal "\e[37m1\e[0m\n", @writer.gets
30
+ end
31
+
32
+ def test_puts_with_match
33
+ @puts.new(:text => "TEST", :match => 'E', :writer => @writer).out!
34
+ @writer.rewind
35
+ assert_equal "\e[37mT\e[31mE\e[37mST\e[0m\n", @writer.gets
22
36
  end
23
37
 
24
38
  def test_puts_with_red
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jamescook-pow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Cook
@@ -9,11 +9,11 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-22 00:00:00 -06:00
12
+ date: 2009-11-23 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: "'puts' with shell colors."
16
+ description: Ruby 'puts' with shell colors.
17
17
  email: jamecook@gmail.com
18
18
  executables: []
19
19