jamescook-pow 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/README +17 -0
- data/lib/pow.rb +155 -0
- data/pow.rb +1 -0
- data/test/pow_test.rb +41 -0
- metadata +59 -0
data/.gitignore
ADDED
data/README
ADDED
@@ -0,0 +1,17 @@
|
|
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. )
|
2
|
+
|
3
|
+
Override your 'puts' for fun shell coloring.
|
4
|
+
|
5
|
+
Usage:
|
6
|
+
require "pow"
|
7
|
+
|
8
|
+
Examples:
|
9
|
+
puts.yellow "Hello world in yellow"
|
10
|
+
puts.red "Hello world in red"
|
11
|
+
puts.red! "Hello world in red, but with boldness"
|
12
|
+
puts.red_ "Hello world in red, but with strikethrough"
|
13
|
+
puts.rainbow "Hellow world, but more silly."
|
14
|
+
|
15
|
+
|
16
|
+
TODO:
|
17
|
+
- Figure out if you can bold a 'dark' color
|
data/lib/pow.rb
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
module Pow
|
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
|
+
end
|
6
|
+
CODES = {
|
7
|
+
:clear => 0,
|
8
|
+
:reset => 0, #clear
|
9
|
+
:bold => 1,
|
10
|
+
:dark => 2,
|
11
|
+
:italic => 3,
|
12
|
+
:underline => 4,
|
13
|
+
:underscore => 4,
|
14
|
+
:blink => 5,
|
15
|
+
:rapid_blink => 6,
|
16
|
+
:negative => 7,
|
17
|
+
:concealed => 8,
|
18
|
+
:strikethrough => 9,
|
19
|
+
:black => 30,
|
20
|
+
:red => 31,
|
21
|
+
:green => 32,
|
22
|
+
:yellow => 33,
|
23
|
+
:blue => 34,
|
24
|
+
:magenta => 35,
|
25
|
+
:purple => 35,
|
26
|
+
:cyan => 36,
|
27
|
+
:white => 37,
|
28
|
+
:on_black => 40,
|
29
|
+
:on_red => 41,
|
30
|
+
:on_green => 42,
|
31
|
+
:on_yellow => 43,
|
32
|
+
:on_blue => 44,
|
33
|
+
:on_magenta => 45,
|
34
|
+
:on_purple => 45,
|
35
|
+
:on_cyan => 46,
|
36
|
+
:on_white => 47,
|
37
|
+
:brown => [:yellow, :dark],
|
38
|
+
:navy => [:blue, :dark],
|
39
|
+
:dark_red => [:red, :dark],
|
40
|
+
:dark_blue => [:blue, :dark],
|
41
|
+
:dark_cyan => [:cyan, :dark],
|
42
|
+
:dark_green => [:green, :dark],
|
43
|
+
:dark_magenta => [:magenta, :dark],
|
44
|
+
:dark_purple => [:purple, :dark],
|
45
|
+
:gray => [:white, :dark],
|
46
|
+
:grey => [:white, :dark]
|
47
|
+
}.freeze
|
48
|
+
|
49
|
+
class Puts
|
50
|
+
attr_accessor :writer
|
51
|
+
def initialize(*args)
|
52
|
+
options = args[0].is_a?(String) ? {:text => args[0] } : (args[0] || {})
|
53
|
+
CODES.keys.each do |key|
|
54
|
+
# Color
|
55
|
+
self.class.send(:define_method, key.to_sym) { |*args| Puts.new({:color => key.to_sym, :text => args[0], :misc => args[1]}).out! }
|
56
|
+
# Bold
|
57
|
+
self.class.send(:define_method, "#{key}!".to_sym) { |*args| Puts.new({:color => key.to_sym, :text => args[0], :bold => true, :misc => args[1]}).out! }
|
58
|
+
# Strikethrough
|
59
|
+
self.class.send(:define_method, "#{key}_".to_sym) { |*args| Puts.new({:color => key.to_sym, :text => args[0], :strikethrough => true, :misc => args[1]}).out! }
|
60
|
+
end
|
61
|
+
# FIXME make this part a method or less fug
|
62
|
+
if options[:misc] && options[:misc].is_a?(Hash)
|
63
|
+
options[:bold] ||= options[:misc][:bold]
|
64
|
+
options[:underline] ||= options[:misc][:underline]
|
65
|
+
options[:background] ||= (options[:misc][:background] || options[:misc][:on])
|
66
|
+
options[:strikethrough] ||= options[:misc][:strikethrough]
|
67
|
+
end
|
68
|
+
@writer = options[:writer] || STDOUT
|
69
|
+
@formatted_text = format_string(options)
|
70
|
+
out!(@formatted_text)
|
71
|
+
end
|
72
|
+
|
73
|
+
# Write the coloured text to IO
|
74
|
+
def out!(string=nil)
|
75
|
+
if string && string != ""
|
76
|
+
printf(writer, string)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def rainbow(*args)
|
81
|
+
text = args[0]
|
82
|
+
out!(text.to_s.split("").inject(""){|m, word| m+= format_string(:text => word, :bold => true, :newline => "", :color => rainbow_keys.sort_by{|k| rand}[0]) + " " } + "\n")
|
83
|
+
end
|
84
|
+
|
85
|
+
# Feel the painbow
|
86
|
+
def painbow(*args)
|
87
|
+
text = args[0]
|
88
|
+
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")
|
89
|
+
end
|
90
|
+
|
91
|
+
def inspect
|
92
|
+
nil
|
93
|
+
end
|
94
|
+
|
95
|
+
protected
|
96
|
+
def painbow_keys
|
97
|
+
[:red, :gray, :white].freeze
|
98
|
+
end
|
99
|
+
|
100
|
+
def rainbow_keys
|
101
|
+
[:red, :blue, :yellow, :green, :magenta, :cyan, :dark_purple].freeze
|
102
|
+
end
|
103
|
+
|
104
|
+
def format_string(options={})
|
105
|
+
newline = options.fetch(:newline){ "\n" }
|
106
|
+
color = options.fetch(:color){ :white }
|
107
|
+
text = options.fetch(:text){ '' }
|
108
|
+
bold = options.fetch(:bold){ false }
|
109
|
+
underline = options.fetch(:underline){ false }
|
110
|
+
background = options.fetch(:background){ false }
|
111
|
+
concealed = options.fetch(:concealed){ false }
|
112
|
+
strikethrough = options.fetch(:strikethrough){ false }
|
113
|
+
|
114
|
+
if text != ""
|
115
|
+
result = [escape_sequence(color), text, escape_sequence(:reset), newline].join
|
116
|
+
end
|
117
|
+
|
118
|
+
if bold
|
119
|
+
result.insert(0, escape_sequence(:bold))
|
120
|
+
end
|
121
|
+
|
122
|
+
if underline
|
123
|
+
result.insert(0, escape_sequence(:underline))
|
124
|
+
end
|
125
|
+
|
126
|
+
if background
|
127
|
+
result.insert(0, escape_sequence("on_#{background}".to_sym))
|
128
|
+
end
|
129
|
+
|
130
|
+
if concealed
|
131
|
+
result.insert(0, escape_sequence(:concealed))
|
132
|
+
end
|
133
|
+
|
134
|
+
if strikethrough
|
135
|
+
result.insert(0, escape_sequence(:strikethrough))
|
136
|
+
end
|
137
|
+
|
138
|
+
return result
|
139
|
+
end
|
140
|
+
|
141
|
+
def escape_sequence(code)
|
142
|
+
if CODES[code].is_a?(Array)
|
143
|
+
sequence = ""
|
144
|
+
CODES[code].each do |key|
|
145
|
+
sequence << escape_sequence(key)
|
146
|
+
end
|
147
|
+
return sequence
|
148
|
+
else
|
149
|
+
return "\e[#{CODES[code]}m"
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
include Pow
|
data/pow.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "lib/pow"
|
data/test/pow_test.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require "stringio"
|
3
|
+
require File.join( File.dirname(__FILE__), "..", "pow")
|
4
|
+
|
5
|
+
class PowTest < Test::Unit::TestCase
|
6
|
+
include Pow
|
7
|
+
def setup
|
8
|
+
@puts = Pow::Puts
|
9
|
+
@writer = StringIO.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_puts
|
13
|
+
@puts.new(:text => "TEST", :writer => @writer).out!
|
14
|
+
@writer.rewind
|
15
|
+
assert_equal "\e[37mTEST\e[0m\n", @writer.gets # White text
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_puts_with_red
|
19
|
+
@puts.new(:text => "TEST", :color => :red, :writer => @writer).out!
|
20
|
+
@writer.rewind
|
21
|
+
assert_equal "\e[31mTEST\e[0m\n", @writer.gets
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_puts_with_dark_color #brown = yellow + dark
|
25
|
+
@puts.new(:text => "TEST", :color => :brown, :writer => @writer).out!
|
26
|
+
@writer.rewind
|
27
|
+
assert_equal "\e[33m\e[2mTEST\e[0m\n", @writer.gets
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_puts_with_bold
|
31
|
+
@puts.new(:text => "TEST", :color => :bold, :writer => @writer).out!
|
32
|
+
@writer.rewind
|
33
|
+
assert_equal "\e[1mTEST\e[0m\n", @writer.gets
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_puts_with_strikethrough
|
37
|
+
@puts.new(:text => "TEST", :strikethrough => :true, :writer => @writer).out!
|
38
|
+
@writer.rewind
|
39
|
+
assert_equal "\e[9m\e[37mTEST\e[0m\n", @writer.gets
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jamescook-pow
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Cook
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-19 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: "'puts' with shell colors."
|
17
|
+
email: jamecook@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- .gitignore
|
26
|
+
- README
|
27
|
+
- lib/pow.rb
|
28
|
+
- pow.rb
|
29
|
+
- test/pow_test.rb
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: http://github.com/jamescook/pow
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- --charset=UTF-8
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.3.5
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: puts with colors
|
58
|
+
test_files:
|
59
|
+
- test/pow_test.rb
|