cloudhead-mutter 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
data/lib/mutter.rb CHANGED
@@ -12,24 +12,30 @@ require 'ext'
12
12
 
13
13
  module Mutter
14
14
  #
15
- # ANSI color & style codes
15
+ # ANSI color & transform codes
16
16
  #
17
- # if the value's an array, it
18
- # represents [0] is the start code
17
+ # If the value's an array,
18
+ # [0] is the start code
19
19
  # and [1] is the end code.
20
20
  #
21
- # if the value is an int, it represents
22
- # the start code, and the end code will be 0
21
+ # Colors all have the same
22
+ # reset code (39).
23
23
  #
24
24
  ANSI = {
25
- :bold => [1, 22],
26
- :underline => [4, 24],
27
- :inverse => [7, 27],
28
-
29
- :black => 30, :red => 31,
30
- :green => 32, :yellow => 33,
31
- :blue => 34, :purple => 35,
32
- :cyan => 36, :white => 37
25
+ :reset => 0,
26
+ :transforms => {
27
+ :bold => [1, 22],
28
+ :underline => [4, 24],
29
+ :blink => [5, 25],
30
+ :inverse => [7, 27]
31
+ },
32
+ :colors => {
33
+ :black => 30, :red => 31,
34
+ :green => 32, :yellow => 33,
35
+ :blue => 34, :purple => 35,
36
+ :cyan => 36, :white => 37,
37
+ :reset => 39
38
+ }
33
39
  }
34
40
 
35
41
  def self.indenter tab
@@ -1,8 +1,10 @@
1
1
  module Mutter
2
2
  class Mutterer
3
+ attr_accessor :styles
4
+
3
5
  @stream = STDOUT
4
6
 
5
- # Initialize the styles, and load the defaults from +defaults.yml+
7
+ # Initialize the styles, and load the defaults from +styles.yml+
6
8
  #
7
9
  # @active: currently active styles, which apply to the whole string
8
10
  # @styles: contains all the user + default styles
@@ -26,7 +28,7 @@ module Mutter
26
28
  load obj
27
29
  else raise ArgumentError
28
30
  end
29
-
31
+
30
32
  #
31
33
  # Create an instance method for each style
32
34
  #
@@ -89,12 +91,24 @@ module Mutter
89
91
  alias :oo watch
90
92
 
91
93
  #
92
- # Add a style to the active styles
94
+ # Add and remove styles from the active styles
93
95
  #
94
96
  def << style
95
97
  @active << style
96
98
  end
97
99
 
100
+ def >> style
101
+ @active.delete style
102
+ end
103
+
104
+ def + style
105
+ dup.tap {|m| m << style }
106
+ end
107
+
108
+ def - style
109
+ dup.tap {|m| m >> style }
110
+ end
111
+
98
112
  #
99
113
  # Parse a string to ANSI codes
100
114
  #
@@ -123,12 +137,15 @@ module Mutter
123
137
  # if the style is a custom style, we recurse, sending
124
138
  # the list of ANSI styles contained in the custom style.
125
139
  #
140
+ # TODO: use ';' delimited codes instead of multiple \e sequences
141
+ #
126
142
  def stylize string, styles = []
127
143
  [styles].flatten.inject(string) do |str, style|
128
144
  style = style.to_sym
129
- if ANSI.include? style
130
- open, close = ANSI[style]
131
- "#{esc(open)}#{str}#{esc(close || 0)}"
145
+ if ANSI[:transforms].include? style
146
+ esc str, *ANSI[:transforms][style]
147
+ elsif ANSI[:colors].include? style
148
+ esc str, ANSI[:colors][style], ANSI[:colors][:reset]
132
149
  else
133
150
  stylize(str, @styles[style][:style])
134
151
  end
@@ -138,8 +155,8 @@ module Mutter
138
155
  #
139
156
  # Escape a string, for later replacement
140
157
  #
141
- def esc style
142
- "\e#{style}\e"
158
+ def esc str, open, close
159
+ "\e#{open}\e" + str + "\e#{close}\e"
143
160
  end
144
161
 
145
162
  #
data/mutter.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{mutter}
5
- s.version = "0.2.0"
5
+ s.version = "0.3.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["cloudhead"]
9
- s.date = %q{2009-07-31}
9
+ s.date = %q{2009-08-01}
10
10
  s.email = %q{self@cloudhead.net}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
data/spec/mutter_spec.rb CHANGED
@@ -33,6 +33,11 @@ describe Mutter do
33
33
  out.should == "\e[7mhello mutter!\e[27m\n"
34
34
  end
35
35
 
36
+ it "should blink" do
37
+ Mutter.say "hello mutter!", :blink
38
+ out.should == "\e[5mhello mutter!\e[25m\n"
39
+ end
40
+
36
41
  it "should escape glyphs" do
37
42
  Mutter.say "**hello * world**"
38
43
  out.should == "\e[1mhello * world\e[22m\n"
@@ -50,7 +55,7 @@ describe Mutter do
50
55
 
51
56
  it "should set defaults at both levels" do
52
57
  Mutter.new(:bold).say "hello mutter!", :underline, :yellow
53
- out.should == "\e[33m\e[4m\e[1mhello mutter!\e[22m\e[24m\e[0m\n"
58
+ out.should == "\e[33m\e[4m\e[1mhello mutter!\e[22m\e[24m\e[39m\n"
54
59
  end
55
60
 
56
61
  describe "with custom styles" do
@@ -62,7 +67,7 @@ describe Mutter do
62
67
  }
63
68
  }
64
69
  Mutter.new(style).say "alert!", :alert
65
- out.should == "\e[31m\e[1malert!\e[22m\e[0m\n"
70
+ out.should == "\e[31m\e[1malert!\e[22m\e[39m\n"
66
71
  end
67
72
 
68
73
  it "should work with shorthand custom styles" do
@@ -72,38 +77,53 @@ describe Mutter do
72
77
 
73
78
  it "should color" do
74
79
  Mutter.new({:cyan => ['<', '>']}).say "<hello mutter!>"
75
- out.should == "\e[36mhello mutter!\e[0m\n"
80
+ out.should == "\e[36mhello mutter!\e[39m\n"
76
81
  end
77
82
 
78
83
  it "should mix styles" do
79
84
  Mutter.new({:cyan => '~'}).say "_*hello* ~world~_"
80
- out.should == "\e[4m\e[1mhello\e[22m \e[36mworld\e[0m\e[24m\n"
85
+ out.should == "\e[4m\e[1mhello\e[22m \e[36mworld\e[39m\e[24m\n"
81
86
  end
82
87
 
83
88
  it "should color backgrounds" do
84
89
  Mutter.new({:cyan => '~'}).say "~[hello mutter!]~"
85
- out.should == "\e[36m\e[7mhello mutter!\e[27m\e[0m\n"
90
+ out.should == "\e[36m\e[7mhello mutter!\e[27m\e[39m\n"
86
91
  end
87
92
 
88
93
  it "should work with multiple shorthand styles" do
89
94
  Mutter.new({[:cyan, :underline] => '~'}).say "~hello mutter!~"
90
- out.should == "\e[4m\e[36mhello mutter!\e[0m\e[24m\n"
95
+ out.should == "\e[4m\e[36mhello mutter!\e[39m\e[24m\n"
91
96
  end
92
97
 
93
98
  it "should load styles from a yaml" do
94
99
  Mutter.new("spec/style").say "{important message!}"
95
- out.should == "\e[33m\e[4m\e[1mimportant message!\e[22m\e[24m\e[0m\n"
100
+ out.should == "\e[33m\e[4m\e[1mimportant message!\e[22m\e[24m\e[39m\n"
96
101
  end
97
102
 
98
103
  it "should be able to call styles as methods" do
99
104
  Mutter.new("spec/style").important "important message!"
100
- out.should == "\e[33m\e[4m\e[1mimportant message!\e[22m\e[24m\e[0m\n"
105
+ out.should == "\e[33m\e[4m\e[1mimportant message!\e[22m\e[24m\e[39m\n"
106
+ end
107
+
108
+ it "should add and remove active styles" do
109
+ mut = Mutter.new
110
+ mut << :bold << :underline << :inverse
111
+ mut >> :inverse
112
+ mut.say "hello mutter!"
113
+ out.should == "\e[4m\e[1mhello mutter!\e[22m\e[24m\n"
114
+ end
115
+
116
+ it "should return instances of itself, +- active styles" do
117
+ mut = Mutter.new
118
+ mut << :bold << :underline
119
+ (mut - :bold).say "hello mutter!"
120
+ out.should == "\e[4mhello mutter!\e[24m\n"
101
121
  end
102
122
  end
103
123
 
104
124
  it "should parse a complex string" do
105
125
  m = Mutter.new({:cyan => ['<','>']})
106
126
  m.say "hello *mutter*, would you _<please be quiet>_ for this <[test]>?"
107
- out.should == "hello \e[1mmutter\e[22m, would you \e[4m\e[36mplease be quiet\e[0m\e[24m for this \e[36m\e[7mtest\e[27m\e[0m?\n"
127
+ out.should == "hello \e[1mmutter\e[22m, would you \e[4m\e[36mplease be quiet\e[39m\e[24m for this \e[36m\e[7mtest\e[27m\e[39m?\n"
108
128
  end
109
129
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudhead-mutter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - cloudhead
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-31 00:00:00 -07:00
12
+ date: 2009-08-01 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15