mutter 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -7,9 +7,14 @@ mutter
7
7
 
8
8
  > mutter takes the concepts of **separation of style & content** to the command-line!
9
9
 
10
+ setup
11
+ -----
12
+
13
+ $ sudo gem install mutter -s http://gemcutter.org
14
+
10
15
  synopsis
11
16
  --------
12
-
17
+
13
18
  require 'mutter'
14
19
 
15
20
  mut = Mutter.new # creates a new 'Mutterer', who talks in command-line language
@@ -17,26 +22,26 @@ synopsis
17
22
  mut.say "hello world", :bold # bolds the whole string
18
23
  mut.say "hello [world]", :cyan # inverts 'world', and colors the string cyan
19
24
  mut.print "bonjour!" # alias of `say`
20
- mut["_hola_"] # return the stylized string without printing
25
+ mut["_hola_"] # return the stylized string without printing, alias of #process
21
26
 
22
27
  ### Tables
23
-
24
- # Define your table structure, arguments are optional.
25
- # Strings which don't fit the column width will be truncated
26
- # with '..' by default, you can change that with the :truncater
27
- # option.
28
- table = Mutter::Table.new(:delimiter => '|') do
29
- column :width => 15, :style => :green
30
- column :style => :yellow
28
+
29
+ Define your table structure, arguments are optional.
30
+
31
+ table = Mutter::Table.new(:delimiter => '|') do # Strings which don't fit the column width will be truncated
32
+ column :width => 15, :style => :green # with '..' by default, you can change that with the :truncater
33
+ column :style => :yellow # option.
31
34
  column :width => 15, :align => :right
32
35
  end
33
-
34
- # Add some rows
36
+
37
+ Add some rows
38
+
35
39
  table << ["gaspar", "morello", 1976]
36
40
  table << ["eddie", "vedder", 1964]
37
41
  table << ["david", "bowie", 1947]
38
42
 
39
- # Print
43
+ Print.
44
+
40
45
  print table.to_s
41
46
 
42
47
  If you want something barebones, you can also do
@@ -109,18 +114,8 @@ and then loaded like this:
109
114
  mut >> :blink # remove :blink
110
115
  mut << :bold << :underline # add :bold and :underline
111
116
  mut.say "hello mutter." # bold and underlined
112
-
113
- installation
114
- ------------
115
-
116
- $ sudo gem install cloudhead-mutter
117
117
 
118
118
  That's it!
119
119
  ----------
120
120
 
121
121
  _have fun_
122
-
123
- Footnote
124
- --------
125
-
126
- This code is _highly experimental_, don't try this at home!
data/Rakefile CHANGED
@@ -6,56 +6,18 @@ begin
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "mutter"
8
8
  gem.summary = %Q{the tiny CLI library}
9
- gem.description = %Q{the tiny CLI library}
9
+ gem.description = %Q{the tiny command-line interface library with lots of style}
10
10
  gem.email = "self@cloudhead.net"
11
11
  gem.homepage = "http://github.com/cloudhead/mutter"
12
12
  gem.rubyforge_project = 'mutter'
13
13
  gem.authors = ["cloudhead"]
14
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
14
  end
15
+ Jeweler::GemcutterTasks.new
16
16
  Jeweler::RubyforgeTasks.new
17
17
  rescue LoadError
18
18
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
19
  end
20
20
 
21
- require 'rake/testtask'
22
- Rake::TestTask.new(:test) do |test|
23
- test.libs << 'lib' << 'test'
24
- test.pattern = 'test/**/*_test.rb'
25
- test.verbose = true
26
- end
27
-
28
- begin
29
- require 'rcov/rcovtask'
30
- Rcov::RcovTask.new do |test|
31
- test.libs << 'test'
32
- test.pattern = 'test/**/*_test.rb'
33
- test.verbose = true
34
- end
35
- rescue LoadError
36
- task :rcov do
37
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
- end
39
- end
40
-
41
-
42
- task :default => :test
43
-
44
- require 'rake/rdoctask'
45
- Rake::RDocTask.new do |rdoc|
46
- if File.exist?('VERSION.yml')
47
- config = YAML.load(File.read('VERSION.yml'))
48
- version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
49
- else
50
- version = ""
51
- end
52
-
53
- rdoc.rdoc_dir = 'rdoc'
54
- rdoc.title = "mutter #{version}"
55
- rdoc.rdoc_files.include('README*')
56
- rdoc.rdoc_files.include('lib/**/*.rb')
57
- end
58
-
59
21
  require 'spec/rake/spectask'
60
22
 
61
23
  Spec::Rake::SpecTask.new("spec") do |t|
@@ -67,9 +29,4 @@ task :test do
67
29
  Rake::Task['spec'].invoke
68
30
  end
69
31
 
70
- Spec::Rake::SpecTask.new("rcov_spec") do |t|
71
- t.spec_files = FileList['spec/**/*_spec.rb']
72
- t.spec_opts = ['--color']
73
- t.rcov = true
74
- t.rcov_opts = ['--exclude', '^spec,/gems/']
75
- end
32
+ task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.5.0
data/lib/ext.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'mutter'
2
1
 
3
2
  class String
4
3
  def examine depth = 0
@@ -33,4 +32,4 @@ class Object
33
32
  yield self
34
33
  self
35
34
  end
36
- end
35
+ end
@@ -58,4 +58,17 @@ module Mutter
58
58
  def self.new *args
59
59
  Mutterer.new(*args)
60
60
  end
61
+
62
+ #
63
+ # Utility function, to make a block interruptible
64
+ #
65
+ def watch
66
+ begin
67
+ yield
68
+ rescue Interrupt
69
+ puts
70
+ exit 0
71
+ end
72
+ end
73
+ alias :oo watch
61
74
  end
@@ -1,6 +1,7 @@
1
1
  module Mutter
2
2
  class Mutterer
3
- @stream = STDOUT
3
+ @stream = STDOUT # output stream
4
+ @color = false # force color
4
5
 
5
6
  # Initialize the styles, and load the defaults from +styles.yml+
6
7
  #
@@ -37,7 +38,7 @@ module Mutter
37
38
  end if style.is_a? Symbol
38
39
  end
39
40
  end
40
-
41
+
41
42
  def styles
42
43
  @defaults.merge @styles
43
44
  end
@@ -48,7 +49,7 @@ module Mutter
48
49
  when :styles then @styles, @defaults = {}, {}
49
50
  when :active then @active = []
50
51
  when :all then @active, @styles, @defaults = [], {}, {}
51
- when :default,
52
+ when :default,
52
53
  :defaults then @defaults = {}
53
54
  else raise ArgumentError, "[:user, :default, :active, :all] only"
54
55
  end
@@ -69,16 +70,29 @@ module Mutter
69
70
  end
70
71
  end
71
72
 
73
+ def color?
74
+ (ENV['TERM'].include?('color') && self.class.stream.tty?) || self.class.color
75
+ end
76
+
72
77
  #
73
78
  # Output to @stream
74
79
  #
75
80
  def say msg, *styles
76
- self.write((ENV['TERM'].include?('color') ? process(msg, *styles) : msg) + "\n")
77
- return nil
81
+ self.write((color?? process(msg, *styles) : unstyle(msg)) + "\n") ; nil
78
82
  end
79
-
80
83
  alias :print say
81
-
84
+
85
+ #
86
+ # Remove all tags from string
87
+ #
88
+ def unstyle msg
89
+ styles.map do |_,v|
90
+ v[:match]
91
+ end.flatten.inject(msg) do |m, tag|
92
+ m.gsub(tag, '')
93
+ end
94
+ end
95
+
82
96
  #
83
97
  # Parse the message, but also apply a style on the whole thing
84
98
  #
@@ -94,22 +108,9 @@ module Mutter
94
108
  self.class.stream.tap do |stream|
95
109
  stream.write str
96
110
  stream.flush
97
- end; nil
111
+ end ; nil
98
112
  end
99
113
 
100
- #
101
- # Utility function, to make a block interruptible
102
- #
103
- def watch
104
- begin
105
- yield
106
- rescue Interrupt
107
- puts
108
- exit 0
109
- end
110
- end
111
- alias :oo watch
112
-
113
114
  #
114
115
  # Create a table
115
116
  #
@@ -197,5 +198,13 @@ module Mutter
197
198
  def self.stream= io
198
199
  @stream = io
199
200
  end
201
+
202
+ def self.color
203
+ @color
204
+ end
205
+
206
+ def self.color= bool
207
+ @color = bool
208
+ end
200
209
  end
201
210
  end
@@ -1,16 +1,16 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mutter}
8
- s.version = "0.4.0"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["cloudhead"]
12
- s.date = %q{2009-10-15}
13
- s.description = %q{the tiny CLI library}
12
+ s.date = %q{2009-11-08}
13
+ s.description = %q{the tiny command-line interface library with lots of style}
14
14
  s.email = %q{self@cloudhead.net}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
@@ -58,3 +58,4 @@ Gem::Specification.new do |s|
58
58
  else
59
59
  end
60
60
  end
61
+
@@ -4,11 +4,12 @@ describe Mutter do
4
4
  def out
5
5
  File.read("spec/out.txt")
6
6
  end
7
-
7
+
8
8
  before(:each) do
9
9
  Mutter::Mutterer.stream = File.new("spec/out.txt", 'w')
10
+ Mutter::Mutterer.color = true
10
11
  end
11
-
12
+
12
13
  after(:all) do
13
14
  File.delete("spec/out.txt")
14
15
  end
@@ -17,107 +18,107 @@ describe Mutter do
17
18
  Mutter.say "hello mutter!"
18
19
  out.should == "hello mutter!\n"
19
20
  end
20
-
21
+
21
22
  it "should underline" do
22
23
  Mutter.say "_hello mutter!_"
23
24
  out.should == "\e[4mhello mutter!\e[24m\n"
24
25
  end
25
-
26
+
26
27
  it "should bold" do
27
28
  Mutter.say "*hello mutter!*"
28
29
  out.should == "\e[1mhello mutter!\e[22m\n"
29
30
  end
30
-
31
+
31
32
  it "should inverse" do
32
33
  Mutter.say "[hello mutter!]"
33
34
  out.should == "\e[7mhello mutter!\e[27m\n"
34
35
  end
35
-
36
+
36
37
  it "should blink" do
37
38
  Mutter.say "hello mutter!", :blink
38
39
  out.should == "\e[5mhello mutter!\e[25m\n"
39
40
  end
40
-
41
+
41
42
  it "should escape glyphs" do
42
43
  Mutter.say "**hello * world**"
43
44
  out.should == "\e[1mhello * world\e[22m\n"
44
45
  end
45
-
46
+
46
47
  it "should set actives at the instance level" do
47
48
  Mutter.new([:bold, :underline]).say "hello mutter!"
48
49
  out.should == "\e[4m\e[1mhello mutter!\e[22m\e[24m\n"
49
50
  end
50
-
51
+
51
52
  it "should set actives at the method level" do
52
53
  Mutter.say "hello mutter!", :bold, :underline
53
54
  out.should == "\e[4m\e[1mhello mutter!\e[22m\e[24m\n"
54
55
  end
55
-
56
+
56
57
  it "should set actives at both levels" do
57
58
  Mutter.new(:bold).say "hello mutter!", :underline, :yellow
58
59
  out.should == "\e[33m\e[4m\e[1mhello mutter!\e[22m\e[24m\e[39m\n"
59
60
  end
60
-
61
+
61
62
  describe "with custom styles" do
62
63
  it "should work with custom styles" do
63
64
  style = {
64
65
  :alert => {
65
- :match => '!!',
66
+ :match => '!!',
66
67
  :style => ['bold', 'red']
67
68
  }
68
69
  }
69
70
  Mutter.new(style).say "alert!", :alert
70
71
  out.should == "\e[31m\e[1malert!\e[22m\e[39m\n"
71
72
  end
72
-
73
+
73
74
  describe "in shorthand form" do
74
75
  it "should work with simple shorthand styles" do
75
76
  Mutter.new({:bold => '~'}).say "~hello mutter!~"
76
77
  out.should == "\e[1mhello mutter!\e[22m\n"
77
78
  end
78
-
79
+
79
80
  it "should work with complex shorthand styles" do
80
81
  Mutter.new({:cyan => ['<', '>']}).say "<hello mutter!>"
81
82
  out.should == "\e[36mhello mutter!\e[39m\n"
82
83
  end
83
-
84
+
84
85
  it "should work with multiple shorthand styles" do
85
86
  Mutter.new({[:cyan, :underline] => '~'}).say "~hello mutter!~"
86
87
  out.should == "\e[4m\e[36mhello mutter!\e[39m\e[24m\n"
87
88
  end
88
89
  end
89
-
90
+
90
91
  it "should mix styles" do
91
92
  Mutter.new({:cyan => '~'}).say "_*hello* ~world~_"
92
93
  out.should == "\e[4m\e[1mhello\e[22m \e[36mworld\e[39m\e[24m\n"
93
94
  end
94
-
95
+
95
96
  it "should color backgrounds" do
96
97
  Mutter.new({:cyan => '~'}).say "~[hello mutter!]~"
97
98
  out.should == "\e[36m\e[7mhello mutter!\e[27m\e[39m\n"
98
99
  end
99
-
100
+
100
101
  it "should load styles from a yaml" do
101
102
  Mutter.new("spec/style").say "{important message!}"
102
103
  out.should == "\e[33m\e[4m\e[1mimportant message!\e[22m\e[24m\e[39m\n"
103
104
  end
104
-
105
+
105
106
  it "should be able to call styles as methods" do
106
107
  Mutter.new("spec/style").important "important message!"
107
108
  out.should == "\e[33m\e[4m\e[1mimportant message!\e[22m\e[24m\e[39m\n"
108
109
  end
109
-
110
+
110
111
  it "should clear all styles" do
111
112
  Mutter.new.clear(:all).say "[hello] *world*"
112
113
  out.should == "[hello] *world*\n"
113
114
  end
114
-
115
+
115
116
  it "should clear active styles" do
116
117
  mut = Mutter.new(:bold)
117
118
  mut.clear(:active).say "hello"
118
119
  out.should == "hello\n"
119
120
  end
120
-
121
+
121
122
  it "should add and remove active styles" do
122
123
  mut = Mutter.new
123
124
  mut << :bold << :underline << :inverse
@@ -125,7 +126,7 @@ describe Mutter do
125
126
  mut.say "hello mutter!"
126
127
  out.should == "\e[4m\e[1mhello mutter!\e[22m\e[24m\n"
127
128
  end
128
-
129
+
129
130
  it "should return instances of itself, +- active styles" do
130
131
  mut = Mutter.new
131
132
  mut << :bold << :underline
@@ -133,10 +134,22 @@ describe Mutter do
133
134
  out.should == "\e[4mhello mutter!\e[24m\n"
134
135
  end
135
136
  end
136
-
137
+
137
138
  it "should parse a complex string" do
138
139
  m = Mutter.new({:cyan => ['<','>']})
139
140
  m.say "hello *mutter*, would you _<please be quiet>_ for this <[test]>?"
140
141
  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"
141
142
  end
142
- end
143
+
144
+ describe "when color isn't supported" do
145
+ before(:each) do
146
+ Mutter::Mutterer.color = false
147
+ end
148
+
149
+ it "should remove style tags" do
150
+ m = Mutter.new
151
+ m.say "hello *mutter*, how are _you_?"
152
+ out.should == "hello mutter, how are you?\n"
153
+ end
154
+ end
155
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mutter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - cloudhead
@@ -9,11 +9,11 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-15 00:00:00 -04:00
12
+ date: 2009-11-08 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description: the tiny CLI library
16
+ description: the tiny command-line interface library with lots of style
17
17
  email: self@cloudhead.net
18
18
  executables: []
19
19