mutter 0.3.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -11,7 +11,7 @@ synopsis
11
11
  --------
12
12
 
13
13
  require 'mutter'
14
-
14
+
15
15
  mut = Mutter.new # creates a new 'Mutterer', who talks in command-line language
16
16
  mut.say "hello _world_" # underlines 'world'
17
17
  mut.say "hello world", :bold # bolds the whole string
@@ -19,6 +19,34 @@ synopsis
19
19
  mut.print "bonjour!" # alias of `say`
20
20
  mut["_hola_"] # return the stylized string without printing
21
21
 
22
+ ### 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
31
+ column :width => 15, :align => :right
32
+ end
33
+
34
+ # Add some rows
35
+ table << ["gaspar", "morello", 1976]
36
+ table << ["eddie", "vedder", 1964]
37
+ table << ["david", "bowie", 1947]
38
+
39
+ # Print
40
+ print table.to_s
41
+
42
+ If you want something barebones, you can also do
43
+
44
+ t = Mutter::Table.new
45
+ t.rows = (1..10).map {|n| [n, n **2, n **3] }
46
+ t.print
47
+
48
+ And it'll make sure everything is aligned nicely
49
+
22
50
  styles
23
51
  ------
24
52
  mutter supports these styles:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.7
1
+ 0.4.0
@@ -8,6 +8,7 @@ $:.unshift File.dirname(__FILE__) + '/mutter'
8
8
 
9
9
  require 'mutterer'
10
10
  require 'indenter'
11
+ require 'table'
11
12
  require 'ext'
12
13
 
13
14
  module Mutter
@@ -50,6 +51,10 @@ module Mutter
50
51
  new.stylize *args
51
52
  end
52
53
 
54
+ def self.process *args
55
+ new.process *args
56
+ end
57
+
53
58
  def self.new *args
54
59
  Mutterer.new(*args)
55
60
  end
@@ -9,7 +9,7 @@ module Mutter
9
9
  #
10
10
  def initialize obj = {}
11
11
  self.reset
12
- @defaults = load File.dirname(__FILE__) + "/styles"
12
+ @defaults = load 'default'
13
13
 
14
14
  case obj
15
15
  when Hash # A style definition: expand quick-styles and merge with @styles
@@ -44,12 +44,13 @@ module Mutter
44
44
 
45
45
  def clear opt = :all
46
46
  case opt
47
- when :user then @styles = {}
48
- when :default then @defaults = {}
49
- when :styles then @styles, @defaults = {}, {}
50
- when :active then @active = []
51
- when :all then @active, @styles, @defaults = [], {}, {}
52
- else raise ArgumentError, "[:user, :default, :active, :all] only"
47
+ when :user then @styles = {}
48
+ when :styles then @styles, @defaults = {}, {}
49
+ when :active then @active = []
50
+ when :all then @active, @styles, @defaults = [], {}, {}
51
+ when :default,
52
+ :defaults then @defaults = {}
53
+ else raise ArgumentError, "[:user, :default, :active, :all] only"
53
54
  end
54
55
  self
55
56
  end
@@ -60,7 +61,8 @@ module Mutter
60
61
  # and converts the keys to symbols
61
62
  #
62
63
  def load styles
63
- styles += '.yml' unless styles =~ /\.ya?ml/
64
+ styles += '.yml' unless styles =~ /\.ya?ml$/
65
+ styles = File.join(File.dirname(__FILE__), "styles", styles) unless File.exist? styles
64
66
  YAML.load_file(styles).inject({}) do |h, (key, value)|
65
67
  value = { :match => value['match'], :style => value['style'] }
66
68
  h.merge key.to_sym => value
@@ -107,6 +109,13 @@ module Mutter
107
109
  end
108
110
  end
109
111
  alias :oo watch
112
+
113
+ #
114
+ # Create a table
115
+ #
116
+ def table *args, &blk
117
+ Table.new(*args, &blk)
118
+ end
110
119
 
111
120
  #
112
121
  # Add and remove styles from the active styles
@@ -0,0 +1,29 @@
1
+ bold:
2
+ match: ['<b>','</b>']
3
+ style:
4
+ - bold
5
+
6
+ underline:
7
+ match: ['<u>','</u>']
8
+ style:
9
+ - underline
10
+
11
+ inverse:
12
+ match: ['<i>','</i>']
13
+ style:
14
+ - inverse
15
+
16
+ error:
17
+ match: ['<err>','</err>']
18
+ style:
19
+ - red
20
+
21
+ warning:
22
+ match: ['<warn>','</warn>']
23
+ style:
24
+ - yellow
25
+
26
+ ok:
27
+ match: ['<ok>','</ok>']
28
+ style:
29
+ - green
@@ -0,0 +1,78 @@
1
+ module Mutter
2
+ class Table
3
+ attr_accessor :rows, :columns
4
+
5
+ DefaultTable = {
6
+ :delimiter => " ",
7
+ :truncater => ".."
8
+ }
9
+ DefaultColumn = {
10
+ :align => :left,
11
+ :style => []
12
+ }
13
+
14
+ def initialize options = {}, &blk
15
+ @columns, @rows = [], []
16
+ @options = DefaultTable.merge options
17
+ instance_eval(&blk) if block_given?
18
+ end
19
+
20
+ def column options = {}
21
+ @columns << DefaultColumn.merge(options)
22
+ end
23
+
24
+ def << row
25
+ if row.size > @columns.size
26
+ raise ArgumentError,
27
+ "row size is #{row.size} but I only have #{@columns.size} columns"
28
+ else
29
+ @rows << row
30
+ end
31
+ end
32
+
33
+ def render
34
+ # Create missing columns as needed
35
+ (@rows.map {|r| r.size }.max - @columns.size).times do
36
+ self.column
37
+ end
38
+
39
+ # Compute max column width
40
+ @columns.each_with_index do |col, i|
41
+ col[:_width] = @rows.map do |r|
42
+ r[i].to_s.length
43
+ end.max if @rows[i]
44
+ end
45
+
46
+ # print table
47
+ @rows.map do |row|
48
+ @columns.zip(row).map do |col, cell|
49
+ process(cell.to_s || "",
50
+ col[:width] || col[:_width],
51
+ col[:align],
52
+ col[:style])
53
+ end.join @options[:delimiter]
54
+ end
55
+ end
56
+ alias :to_a render
57
+
58
+ def to_s
59
+ render.join("\n")
60
+ end
61
+
62
+ def print
63
+ puts render
64
+ end
65
+
66
+ def process str, length = nil, align = :left, style = []
67
+ length ||= str.length
68
+
69
+ if str.length > length
70
+ str[0...(length - @options[:truncater].length)] + @options[:truncater]
71
+ else
72
+ s = [Mutter.process(str, style), ' ' * (length - str.length)]
73
+ s.reverse! if align == :right
74
+ s.join
75
+ end
76
+ end
77
+ end
78
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mutter}
8
- s.version = "0.3.7"
8
+ s.version = "0.4.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-07}
12
+ s.date = %q{2009-10-15}
13
13
  s.description = %q{the tiny CLI library}
14
14
  s.email = %q{self@cloudhead.net}
15
15
  s.extra_rdoc_files = [
@@ -27,11 +27,14 @@ Gem::Specification.new do |s|
27
27
  "lib/mutter.rb",
28
28
  "lib/mutter/indenter.rb",
29
29
  "lib/mutter/mutterer.rb",
30
- "lib/mutter/styles.yml",
30
+ "lib/mutter/styles/default.yml",
31
+ "lib/mutter/styles/html.yml",
32
+ "lib/mutter/table.rb",
31
33
  "mutter.gemspec",
32
34
  "spec/mutter_spec.rb",
33
35
  "spec/spec_helper.rb",
34
- "spec/style.yml"
36
+ "spec/style.yml",
37
+ "spec/table_spec.rb"
35
38
  ]
36
39
  s.homepage = %q{http://github.com/cloudhead/mutter}
37
40
  s.rdoc_options = ["--charset=UTF-8"]
@@ -41,7 +44,8 @@ Gem::Specification.new do |s|
41
44
  s.summary = %q{the tiny CLI library}
42
45
  s.test_files = [
43
46
  "spec/mutter_spec.rb",
44
- "spec/spec_helper.rb"
47
+ "spec/spec_helper.rb",
48
+ "spec/table_spec.rb"
45
49
  ]
46
50
 
47
51
  if s.respond_to? :specification_version then
@@ -0,0 +1,21 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe Mutter::Table do
4
+ it "should render tables defined with a block" do
5
+ table = Mutter::Table.new(:delimiter => '|') do
6
+ column :width => 8
7
+ column :align => :right
8
+ column
9
+ end
10
+
11
+ table << ["hello", "moto", "malacif"]
12
+ table << ["misha", "fafa", "fio"]
13
+ table << ["lombardic", "fister", "falalala"]
14
+
15
+ table.to_a.should == [
16
+ "hello | moto|malacif ",
17
+ "misha | fafa|fio ",
18
+ "lombar..|fister|falalala"
19
+ ]
20
+ end
21
+ 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.3.7
4
+ version: 0.4.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-10-07 00:00:00 -04:00
12
+ date: 2009-10-15 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -33,11 +33,14 @@ files:
33
33
  - lib/mutter.rb
34
34
  - lib/mutter/indenter.rb
35
35
  - lib/mutter/mutterer.rb
36
- - lib/mutter/styles.yml
36
+ - lib/mutter/styles/default.yml
37
+ - lib/mutter/styles/html.yml
38
+ - lib/mutter/table.rb
37
39
  - mutter.gemspec
38
40
  - spec/mutter_spec.rb
39
41
  - spec/spec_helper.rb
40
42
  - spec/style.yml
43
+ - spec/table_spec.rb
41
44
  has_rdoc: true
42
45
  homepage: http://github.com/cloudhead/mutter
43
46
  licenses: []
@@ -69,3 +72,4 @@ summary: the tiny CLI library
69
72
  test_files:
70
73
  - spec/mutter_spec.rb
71
74
  - spec/spec_helper.rb
75
+ - spec/table_spec.rb