cldwalker-hirb 0.1.2 → 0.2.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/CHANGELOG.rdoc +7 -0
- data/README.rdoc +27 -105
- data/Rakefile +3 -2
- data/VERSION.yml +2 -2
- data/lib/hirb/console.rb +31 -5
- data/lib/hirb/formatter.rb +199 -0
- data/lib/hirb/helpers/active_record_table.rb +2 -3
- data/lib/hirb/helpers/auto_table.rb +2 -2
- data/lib/hirb/helpers/object_table.rb +4 -4
- data/lib/hirb/helpers/table.rb +72 -19
- data/lib/hirb/menu.rb +47 -0
- data/lib/hirb/pager.rb +94 -0
- data/lib/hirb/util.rb +53 -2
- data/lib/hirb/view.rb +116 -140
- data/lib/hirb.rb +28 -3
- data/test/console_test.rb +12 -0
- data/test/formatter_test.rb +172 -0
- data/test/menu_test.rb +94 -0
- data/test/pager_test.rb +164 -0
- data/test/table_test.rb +100 -21
- data/test/test_helper.rb +30 -0
- data/test/util_test.rb +53 -18
- data/test/view_test.rb +98 -151
- metadata +28 -15
data/lib/hirb/helpers/table.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Base Table class from which other table classes inherit.
|
2
2
|
# By default, a table is constrained to a default width but this can be adjusted
|
3
|
-
# via
|
3
|
+
# via the max_width option or Hirb::View.width.
|
4
4
|
# Rows can be an array of arrays or an array of hashes.
|
5
5
|
#
|
6
6
|
# An array of arrays ie [[1,2], [2,3]], would render:
|
@@ -25,10 +25,10 @@
|
|
25
25
|
#--
|
26
26
|
# derived from http://gist.github.com/72234
|
27
27
|
class Hirb::Helpers::Table
|
28
|
-
|
28
|
+
BORDER_LENGTH = 3 # " | " and "-+-" are the borders
|
29
29
|
class TooManyFieldsForWidthError < StandardError; end
|
30
|
+
|
30
31
|
class << self
|
31
|
-
attr_accessor :max_width
|
32
32
|
|
33
33
|
# Main method which returns a formatted table.
|
34
34
|
# ==== Options:
|
@@ -40,11 +40,15 @@ class Hirb::Helpers::Table
|
|
40
40
|
# length.
|
41
41
|
# [:max_width] The maximum allowed width of all fields put together. This option is enforced except when the field_lengths option is set.
|
42
42
|
# This doesn't count field borders as part of the total.
|
43
|
+
# [:number] When set to true, numbers rows by adding a :hirb_number column as the first column. Default is false.
|
44
|
+
# [:filters] A hash of fields and the filters that each row in the field must run through. The filter converts the cell's value by applying
|
45
|
+
# a given proc or an array containing a method and optional arguments to it.
|
43
46
|
# Examples:
|
44
47
|
# Hirb::Helpers::Table.render [[1,2], [2,3]]
|
45
48
|
# Hirb::Helpers::Table.render [[1,2], [2,3]], :field_lengths=>{0=>10}
|
46
49
|
# Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}]
|
47
50
|
# Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :headers=>{:weight=>"Weight(lbs)"}
|
51
|
+
# Hirb::Helpers::Table.render [{:age=>10, :weight=>100}, {:age=>80, :weight=>500}], :filters=>{:age=>[:to_f]}
|
48
52
|
def render(rows, options={})
|
49
53
|
new(rows,options).render
|
50
54
|
end
|
@@ -53,13 +57,18 @@ class Hirb::Helpers::Table
|
|
53
57
|
#:stopdoc:
|
54
58
|
def initialize(rows, options={})
|
55
59
|
@options = options
|
56
|
-
@
|
60
|
+
@options[:filters] ||= {}
|
61
|
+
@fields = @options[:fields] ? @options[:fields].dup : ((rows[0].is_a?(Hash)) ? rows[0].keys.sort {|a,b| a.to_s <=> b.to_s} :
|
57
62
|
rows[0].is_a?(Array) ? (0..rows[0].length - 1).to_a : [])
|
58
63
|
@rows = setup_rows(rows)
|
59
64
|
@headers = @fields.inject({}) {|h,e| h[e] = e.to_s; h}
|
60
|
-
if options.has_key?(:headers)
|
61
|
-
@headers = options[:headers].is_a?(Hash) ? @headers.merge(options[:headers]) :
|
62
|
-
(options[:headers].is_a?(Array) ? array_to_indices_hash(options[:headers]) : options[:headers])
|
65
|
+
if @options.has_key?(:headers)
|
66
|
+
@headers = @options[:headers].is_a?(Hash) ? @headers.merge(@options[:headers]) :
|
67
|
+
(@options[:headers].is_a?(Array) ? array_to_indices_hash(@options[:headers]) : @options[:headers])
|
68
|
+
end
|
69
|
+
if @options[:number]
|
70
|
+
@headers[:hirb_number] = "number"
|
71
|
+
@fields.unshift :hirb_number
|
63
72
|
end
|
64
73
|
end
|
65
74
|
|
@@ -71,6 +80,8 @@ class Hirb::Helpers::Table
|
|
71
80
|
new_rows << array_to_indices_hash(row)
|
72
81
|
}
|
73
82
|
end
|
83
|
+
rows = filter_values(rows)
|
84
|
+
rows.each_with_index {|e,i| e[:hirb_number] = (i + 1).to_s} if @options[:number]
|
74
85
|
validate_values(rows)
|
75
86
|
rows
|
76
87
|
end
|
@@ -124,26 +135,53 @@ class Hirb::Helpers::Table
|
|
124
135
|
if @options[:field_lengths]
|
125
136
|
@field_lengths.merge!(@options[:field_lengths])
|
126
137
|
else
|
127
|
-
table_max_width =
|
128
|
-
table_max_width
|
129
|
-
restrict_field_lengths(@field_lengths, table_max_width)
|
138
|
+
table_max_width = @options.has_key?(:max_width) ? @options[:max_width] : Hirb::View.width
|
139
|
+
restrict_field_lengths(@field_lengths, table_max_width) if table_max_width
|
130
140
|
end
|
131
141
|
end
|
132
142
|
|
143
|
+
def restrict_field_lengths(field_lengths, max_width)
|
144
|
+
max_width -= @fields.size * BORDER_LENGTH + 1
|
145
|
+
original_field_lengths = field_lengths.dup
|
146
|
+
@min_field_length = BORDER_LENGTH
|
147
|
+
adjust_long_fields(field_lengths, max_width)
|
148
|
+
rescue TooManyFieldsForWidthError
|
149
|
+
raise
|
150
|
+
rescue
|
151
|
+
default_restrict_field_lengths(field_lengths, original_field_lengths, max_width)
|
152
|
+
end
|
153
|
+
|
133
154
|
# Simple algorithm which given a max width, allows smaller fields to be displayed while
|
134
155
|
# restricting longer fields at an average_long_field_length.
|
135
|
-
def
|
156
|
+
def adjust_long_fields(field_lengths, max_width)
|
136
157
|
total_length = field_lengths.values.inject {|t,n| t += n}
|
137
|
-
|
138
|
-
|
139
|
-
raise TooManyFieldsForWidthError if @fields.size > max_width.to_f / min_field_length
|
158
|
+
while total_length > max_width
|
159
|
+
raise TooManyFieldsForWidthError if @fields.size > max_width.to_f / @min_field_length
|
140
160
|
average_field_length = total_length / @fields.size.to_f
|
141
161
|
long_lengths = field_lengths.values.select {|e| e > average_field_length}
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
162
|
+
if long_lengths.empty?
|
163
|
+
raise "Algorithm didn't work, resort to default"
|
164
|
+
else
|
165
|
+
total_long_field_length = (long_lengths.inject {|t,n| t += n}) * max_width/total_length
|
166
|
+
average_long_field_length = total_long_field_length / long_lengths.size
|
167
|
+
field_lengths.each {|f,length|
|
168
|
+
field_lengths[f] = average_long_field_length if length > average_long_field_length
|
169
|
+
}
|
170
|
+
end
|
171
|
+
total_length = field_lengths.values.inject {|t,n| t += n}
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
# Produces a field_lengths which meets the max_width requirement
|
176
|
+
def default_restrict_field_lengths(field_lengths, original_field_lengths, max_width)
|
177
|
+
original_total_length = original_field_lengths.values.inject {|t,n| t += n}
|
178
|
+
relative_lengths = original_field_lengths.values.map {|v| (v / original_total_length.to_f * max_width).to_i }
|
179
|
+
# set fields by their relative weight to original length
|
180
|
+
if relative_lengths.all? {|e| e > @min_field_length} && (relative_lengths.inject {|a,e| a += e} <= max_width)
|
181
|
+
original_field_lengths.each {|k,v| field_lengths[k] = (v / original_total_length.to_f * max_width).to_i }
|
182
|
+
else
|
183
|
+
# set all fields the same if nothing else works
|
184
|
+
field_lengths.each {|k,v| field_lengths[k] = max_width / @fields.size}
|
147
185
|
end
|
148
186
|
end
|
149
187
|
|
@@ -159,6 +197,21 @@ class Hirb::Helpers::Table
|
|
159
197
|
field_lengths
|
160
198
|
end
|
161
199
|
|
200
|
+
def filter_values(rows)
|
201
|
+
rows.map {|row|
|
202
|
+
new_row = {}
|
203
|
+
@fields.each {|f|
|
204
|
+
if @options[:filters][f]
|
205
|
+
new_row[f] = @options[:filters][f].is_a?(Proc) ? @options[:filters][f].call(row[f]) :
|
206
|
+
row[f].send(*@options[:filters][f])
|
207
|
+
else
|
208
|
+
new_row[f] = row[f]
|
209
|
+
end
|
210
|
+
}
|
211
|
+
new_row
|
212
|
+
}
|
213
|
+
end
|
214
|
+
|
162
215
|
def validate_values(rows)
|
163
216
|
rows.each {|row|
|
164
217
|
@fields.each {|f|
|
data/lib/hirb/menu.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module Hirb
|
2
|
+
# This class provides a selection menu using Hirb's table helpers by default to display choices.
|
3
|
+
class Menu
|
4
|
+
# Menu which asks to select from the given array and returns the selected menu items as an array. See Hirb::Util.choose_from_array
|
5
|
+
# for the syntax for specifying selections. All options except for the ones below are passed to render the menu.
|
6
|
+
#
|
7
|
+
# ==== Options:
|
8
|
+
# [:helper_class] Helper class to render menu. Helper class is expected to implement numbering given a :number option.
|
9
|
+
# To use a very basic menu, set this to false. Defaults to Hirb::Helpers::AutoTable.
|
10
|
+
# [:prompt] String for menu prompt. Defaults to "Choose: ".
|
11
|
+
# [:validate_one] Validates that only one item in array is chosen and returns just that item. Default is false.
|
12
|
+
# [:ask] Always ask for input, even if there is only one choice. Default is true.
|
13
|
+
# Examples:
|
14
|
+
# extend Hirb::Console
|
15
|
+
# menu([1,2,3], :fields=>[:field1, :field2], :validate_one=>true)
|
16
|
+
# menu([1,2,3], :helper_class=>Hirb::Helpers::Table)
|
17
|
+
def self.render(output, options={})
|
18
|
+
default_options = {:helper_class=>Hirb::Helpers::AutoTable, :prompt=>"Choose #{options[:validate_one] ? 'one' : ''}: ", :ask=>true}
|
19
|
+
options = default_options.merge(options)
|
20
|
+
output = [output] unless output.is_a?(Array)
|
21
|
+
chosen = choose_from_menu(output, options)
|
22
|
+
yield(chosen) if block_given? && chosen.is_a?(Array) && chosen.size > 0
|
23
|
+
chosen
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.choose_from_menu(output, options) #:nodoc:
|
27
|
+
return output if output.size == 1 && !options[:ask]
|
28
|
+
if (helper_class = Util.any_const_get(options[:helper_class]))
|
29
|
+
View.render_output(output, :class=>options[:helper_class], :options=>options.merge(:number=>true))
|
30
|
+
else
|
31
|
+
output.each_with_index {|e,i| puts "#{i+1}: #{e}" }
|
32
|
+
end
|
33
|
+
print options[:prompt]
|
34
|
+
input = $stdin.gets.chomp.strip
|
35
|
+
chosen = Util.choose_from_array(output, input)
|
36
|
+
if options[:validate_one]
|
37
|
+
if chosen.size != 1
|
38
|
+
$stderr.puts "Choose one. You chose #{chosen.size} items."
|
39
|
+
return nil
|
40
|
+
else
|
41
|
+
return chosen[0]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
chosen
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/hirb/pager.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
module Hirb
|
2
|
+
# This class provides class methods for paging and an object which can conditionally page given a terminal size that is exceeded.
|
3
|
+
class Pager
|
4
|
+
class<<self
|
5
|
+
# Pages using a configured or detected shell command.
|
6
|
+
def command_pager(output, options={})
|
7
|
+
basic_pager(output) if valid_pager_command?(options[:pager_command])
|
8
|
+
end
|
9
|
+
|
10
|
+
def pager_command(*commands) #:nodoc:
|
11
|
+
@pager_command = (!@pager_command.nil? && commands.empty?) ? @pager_command :
|
12
|
+
begin
|
13
|
+
commands = [ENV['PAGER'], 'less', 'more', 'pager'] if commands.empty?
|
14
|
+
commands.compact.uniq.find {|e| Util.command_exists?(e[/\w+/]) }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Pages with a ruby-only pager which either pages or quits.
|
19
|
+
def default_pager(output, options={})
|
20
|
+
pager = new(options[:width], options[:height])
|
21
|
+
while pager.activated_by?(output, options[:inspect])
|
22
|
+
puts pager.slice!(output, options[:inspect])
|
23
|
+
return unless continue_paging?
|
24
|
+
end
|
25
|
+
puts output
|
26
|
+
puts "=== Pager finished. ==="
|
27
|
+
end
|
28
|
+
|
29
|
+
#:stopdoc:
|
30
|
+
def valid_pager_command?(cmd)
|
31
|
+
cmd ? pager_command(cmd) : pager_command
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
def basic_pager(output)
|
36
|
+
pager = IO.popen(pager_command, "w")
|
37
|
+
begin
|
38
|
+
save_stdout = STDOUT.clone
|
39
|
+
STDOUT.reopen(pager)
|
40
|
+
STDOUT.puts output
|
41
|
+
ensure
|
42
|
+
STDOUT.reopen(save_stdout)
|
43
|
+
save_stdout.close
|
44
|
+
pager.close
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def continue_paging?
|
49
|
+
puts "=== Press enter/return to continue or q to quit: ==="
|
50
|
+
!$stdin.gets.chomp[/q/i]
|
51
|
+
end
|
52
|
+
#:startdoc:
|
53
|
+
end
|
54
|
+
|
55
|
+
attr_reader :width, :height
|
56
|
+
|
57
|
+
def initialize(width, height, options={})
|
58
|
+
resize(width, height)
|
59
|
+
@pager_command = options[:pager_command] if options[:pager_command]
|
60
|
+
end
|
61
|
+
|
62
|
+
# Pages given string using configured pager.
|
63
|
+
def page(string, inspect_mode)
|
64
|
+
if self.class.valid_pager_command?(@pager_command)
|
65
|
+
self.class.command_pager(string, :pager_command=>@pager_command)
|
66
|
+
else
|
67
|
+
self.class.default_pager(string, :width=>@width, :height=>@height, :inspect=>inspect_mode)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def slice!(output, inspect_mode=false) #:nodoc:
|
72
|
+
effective_height = @height - 2 # takes into account pager prompt
|
73
|
+
if inspect_mode
|
74
|
+
sliced_output = output.slice(0, @width * effective_height)
|
75
|
+
output.replace output.slice(@width * effective_height..-1)
|
76
|
+
sliced_output
|
77
|
+
else
|
78
|
+
# could use output.scan(/[^\n]*\n?/) instead of split
|
79
|
+
sliced_output = output.split("\n").slice(0, effective_height).join("\n")
|
80
|
+
output.replace output.split("\n").slice(effective_height..-1).join("\n")
|
81
|
+
sliced_output
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Determines if string should be paged based on configured width and height.
|
86
|
+
def activated_by?(string_to_page, inspect_mode=false)
|
87
|
+
inspect_mode ? (string_to_page.size > @height * @width) : (string_to_page.count("\n") > @height)
|
88
|
+
end
|
89
|
+
|
90
|
+
def resize(width, height) #:nodoc:
|
91
|
+
@width, @height = Hirb::View.determine_terminal_size(width, height)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
data/lib/hirb/util.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
module Hirb
|
2
|
+
# Group of handy utility functions used throughout Hirb.
|
2
3
|
module Util
|
3
4
|
extend self
|
4
|
-
# Returns a constant like const_get
|
5
|
+
# Returns a constant like Module#const_get no matter what namespace it's nested in.
|
5
6
|
# Returns nil if the constant is not found.
|
6
7
|
def any_const_get(name)
|
7
8
|
return name if name.is_a?(Module)
|
@@ -21,9 +22,59 @@ module Hirb
|
|
21
22
|
hash1.merge(hash2) {|k,o,n| (o.is_a?(Hash)) ? recursive_hash_merge(o,n) : n}
|
22
23
|
end
|
23
24
|
|
24
|
-
#
|
25
|
+
# From Rails ActiveSupport, converting undescored lowercase to camel uppercase.
|
25
26
|
def camelize(string)
|
26
27
|
string.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
27
28
|
end
|
29
|
+
|
30
|
+
# Used by Hirb::Menu to select items from an array. Array counting starts at 1. Ranges of numbers are specified with a '-' or '..'.
|
31
|
+
# Multiple ranges can be comma delimited. Anything that isn't a valid number is ignored. All elements can be returned with a '*'.
|
32
|
+
# Examples:
|
33
|
+
# 1-3,5-6 -> [1,2,3,5,6]
|
34
|
+
# * -> all elements in array
|
35
|
+
# '' -> []
|
36
|
+
def choose_from_array(array, input, options={})
|
37
|
+
options = {:splitter=>","}.merge(options)
|
38
|
+
return array if input.strip == '*'
|
39
|
+
result = []
|
40
|
+
input.split(options[:splitter]).each do |e|
|
41
|
+
if e =~ /-|\.\./
|
42
|
+
min,max = e.split(/-|\.\./)
|
43
|
+
slice_min = min.to_i - 1
|
44
|
+
result.push(*array.slice(slice_min, max.to_i - min.to_i + 1))
|
45
|
+
elsif e =~ /\s*(\d+)\s*/
|
46
|
+
index = $1.to_i - 1
|
47
|
+
next if index < 0
|
48
|
+
result.push(array[index]) if array[index]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
return result
|
52
|
+
end
|
53
|
+
|
54
|
+
# Determines if a shell command exists by searching for it in ENV['PATH'].
|
55
|
+
def command_exists?(command)
|
56
|
+
ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exists? File.join(d, command) }
|
57
|
+
end
|
58
|
+
|
59
|
+
# Returns [width, height] of terminal when detected, nil if not detected.
|
60
|
+
# Think of this as a simpler version of Highline's Highline::SystemExtensions.terminal_size()
|
61
|
+
def detect_terminal_size
|
62
|
+
(ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/) ? [ENV['COLUMNS'].to_i, ENV['LINES'].to_i] :
|
63
|
+
( command_exists?('stty') ? `stty size`.scan(/\d+/).map { |s| s.to_i }.reverse : nil )
|
64
|
+
rescue
|
65
|
+
nil
|
66
|
+
end
|
67
|
+
|
68
|
+
# Captures STDOUT of anything run in its block and returns it as string.
|
69
|
+
def capture_stdout(&block)
|
70
|
+
original_stdout = $stdout
|
71
|
+
$stdout = fake = StringIO.new
|
72
|
+
begin
|
73
|
+
yield
|
74
|
+
ensure
|
75
|
+
$stdout = original_stdout
|
76
|
+
end
|
77
|
+
fake.string
|
78
|
+
end
|
28
79
|
end
|
29
80
|
end
|