shiny 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- = shiny
1
+ = Shiny
2
2
 
3
3
  Shiny extends the ruby core string class with two methods, ansi and html. These two are proxy methods to provide a bunch of color and text formating methods.
4
4
  Over ansi there are the commen known ansi escape sequences available, bold, blink, underline and the whole colorful format things. The method html serves the same but in the html way, it makes the
@@ -10,6 +10,10 @@ standart w3c color names available and also a few other formats like overline, u
10
10
 
11
11
  == Usage
12
12
 
13
+ First require it:
14
+
15
+ require 'shiny'
16
+
13
17
  Some ansi color examples:
14
18
 
15
19
  puts "magenta".ansi.magenta
@@ -41,7 +45,9 @@ Some other html effect examples:
41
45
  "overline".html.overline
42
46
  "line-through".html.line_through
43
47
 
44
- Some terminal helper examples: (include Shiny::Helpers)
48
+ Some terminal helper examples:
49
+
50
+ include Shiny::Helpers
45
51
 
46
52
  say("green day?", :color => :green)
47
53
  say("fail!", :type => :error)
@@ -1,7 +1,7 @@
1
1
  module Shiny
2
- # The ansi class serve a great list of color and a few format
3
- # methods. To get an overview of all the generetad method, just look
4
- # at the CODES hash
2
+ # The ansi class serves a large list of color and a few format
3
+ # methods. To get an overview of all the generated methods, just look
4
+ # at the CODES hash.
5
5
  #
6
6
  # Some color examples:
7
7
  #
@@ -53,7 +53,6 @@ module Shiny
53
53
  :on_bright_magenta => "\e[105m",
54
54
  :on_bright_cyan => "\e[106m",
55
55
  :on_bright_white => "\e[107m",
56
- :reset => "\e[0m",
57
56
  :bold => "\e[1m",
58
57
  :underline => "\e[4m",
59
58
  :negative => "\e[7m",
@@ -69,16 +68,12 @@ module Shiny
69
68
  # Generate color instance methods
70
69
  # See CODES for a list of the created methods
71
70
  CODES.each do |code, value|
72
- next if code == :reset
73
- reset = CODES[:reset]
74
- class_eval <<-DEF
75
- def #{code.to_s}
76
- @string = "#{value}" + @string + "#{reset}"
77
- self
78
- end
79
- DEF
71
+ define_method code do
72
+ @string = "#{value}#{@string}\e[0m"
73
+ self
74
+ end
80
75
  end
81
-
76
+
82
77
  # Remove all ansi escape sequences from the string
83
78
  #
84
79
  # @return [String] from ansi escape sequences cleaned
@@ -51,7 +51,13 @@ module Shiny
51
51
  # @param [String] sign ('#') The character around the message
52
52
  def wrap(message, sign="#")
53
53
  sign = sign[0] if sign.length > 1
54
- around = sign * (message.length + 4) + "\n"
54
+
55
+ to_ignore = 0
56
+ message.gsub(/\e\[[0-9]+m/) do |m|
57
+ to_ignore += m.length
58
+ end
59
+
60
+ around = sign * ((message.length - to_ignore) + 4) + "\n"
55
61
 
56
62
  output = ""
57
63
  output << around
@@ -2,46 +2,46 @@ module Shiny
2
2
  class HTML < Basic
3
3
  # List of the 17 w3c supported color names
4
4
  # @see http://www.w3.org/TR/CSS21/syndata.html#value-def-color
5
- FORMATS = {
6
- :black => '<span style="color: black;">',
7
- :silver => '<span style="color: silver;">',
8
- :gray => '<span style="color: gray;">,',
9
- :white => '<span style="color: white;">',
10
- :maroon => '<span style="color: maroon;">',
11
- :red => '<span style="color: red;">',
12
- :purple => '<span style="color: purple;">',
13
- :fuchsia => '<span style="color: fuchsia;">',
14
- :green => '<span style="color: green;">',
15
- :lime => '<span style="color: lime;">',
16
- :olive => '<span style="color: olive;">',
17
- :yellow => '<span style="color: yellow;">',
18
- :navy => '<span style="color: navy;">',
19
- :blue => '<span style="color: blue;">',
20
- :teal => '<span style="color: teal;">',
21
- :aqua => '<span style="color: aqua;">',
22
- :orange => '<span style="color: orange;">',
23
- :on_black => '<span style="background-color: black;">',
24
- :on_silver => '<span style="background-color: silver;">',
25
- :on_gray => '<span style="background-color: gray;">',
26
- :on_white => '<span style="background-color: white;">',
27
- :on_maroon => '<span style="background-color: maroon;">',
28
- :on_red => '<span style="background-color: red;">',
29
- :on_purple => '<span style="background-color: purple;">',
30
- :on_fuchsia => '<span style="background-color: fuchsia;">',
31
- :on_green => '<span style="background-color: green;">',
32
- :on_lime => '<span style="background-color: lime;">',
33
- :on_olive => '<span style="background-color: olive;">',
34
- :on_yellow => '<span style="background-color: yellow;">',
35
- :on_navy => '<span style="background-color: navy;">',
36
- :on_blue => '<span style="background-color: blue;">',
37
- :on_teal => '<span style="background-color: teal;">',
38
- :on_aqua => '<span style="background-color: aqua;">',
39
- :on_orange => '<span style="background-color: orange;">',
40
- :bold => '<span style="font-weight: bold;">',
41
- :underline => '<span style="text-decoration: underline;">',
42
- :overline => '<span style="text-decoration: overline;">',
43
- :line_through => '<span style="text-decoration: line-through;">',
44
- :blink => '<span style="text-decoration: blink;">'
5
+ STYLES = {
6
+ :black => 'color: black',
7
+ :silver => 'color: silver',
8
+ :gray => 'color: gray',
9
+ :white => 'color: white',
10
+ :maroon => 'color: maroon',
11
+ :red => 'color: red',
12
+ :purple => 'color: purple',
13
+ :fuchsia => 'color: fuchsia',
14
+ :green => 'color: green',
15
+ :lime => 'color: lime',
16
+ :olive => 'color: olive',
17
+ :yellow => 'color: yellow',
18
+ :navy => 'color: navy',
19
+ :blue => 'color: blue',
20
+ :teal => 'color: teal',
21
+ :aqua => 'color: aqua',
22
+ :orange => 'color: orange',
23
+ :on_black => 'background-color: black',
24
+ :on_silver => 'background-color: silver',
25
+ :on_gray => 'background-color: gray',
26
+ :on_white => 'background-color: white',
27
+ :on_maroon => 'background-color: maroon',
28
+ :on_red => 'background-color: red',
29
+ :on_purple => 'background-color: purple',
30
+ :on_fuchsia => 'background-color: fuchsia',
31
+ :on_green => 'background-color: green',
32
+ :on_lime => 'background-color: lime',
33
+ :on_olive => 'background-color: olive',
34
+ :on_yellow => 'background-color: yellow',
35
+ :on_navy => 'background-color: navy',
36
+ :on_blue => 'background-color: blue',
37
+ :on_teal => 'background-color: teal',
38
+ :on_aqua => 'background-color: aqua',
39
+ :on_orange => 'background-color: orange',
40
+ :bold => 'font-weight: bold',
41
+ :underline => 'text-decoration: underline',
42
+ :overline => 'text-decoration: overline',
43
+ :line_through => 'text-decoration: line-through',
44
+ :blink => 'text-decoration: blink'
45
45
  }
46
46
 
47
47
  # HTML span begin tag
@@ -51,14 +51,12 @@ module Shiny
51
51
  SPAN_END = "</span>"
52
52
 
53
53
  # Generate html format instance methods
54
- # See FORMATS for a list of created methods
55
- FORMATS.each do |name, value|
56
- class_eval <<-DEF
57
- def #{name.to_s}
58
- @string = '#{value}' + @string + '#{SPAN_END}'
59
- self
60
- end
61
- DEF
54
+ # See STYLES for a list of created methods
55
+ STYLES.each do |name, value|
56
+ define_method name do
57
+ @string = "<span style=\"#{value};\">#{@string}</span>"
58
+ self
59
+ end
62
60
  end
63
61
 
64
62
  # Remove all html span format tags
@@ -57,6 +57,15 @@ describe Shiny do
57
57
  $stdout.should_receive(:print).with(output)
58
58
  wrap("Hello I'm wrapped", "*")
59
59
  end
60
+
61
+ it "should not count escape sequences as characters" do
62
+ output = "###########\n"
63
+ output += "# \e[31mI'm red\e[0m #\n"
64
+ output += "###########\n"
65
+
66
+ $stdout.should_receive(:print).with(output)
67
+ wrap("\e[31mI'm red\e[0m")
68
+ end
60
69
  end
61
70
 
62
71
  describe "#log" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 1
9
- version: 0.3.1
8
+ - 2
9
+ version: 0.3.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Samuel Tonini
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-11-16 00:00:00 +01:00
17
+ date: 2010-12-29 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency