bbcloud 0.6.2 → 0.7
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/bbcloud.gemspec +1 -3
- data/lib/bbcloud/commands/servers-show.rb +1 -1
- data/lib/bbcloud/version.rb +1 -1
- metadata +26 -61
- data/lib/bbcloud/commands/servers-restart.rb +0 -30
- data/lib/bbcloud/vendor/hirb/.gemspec +0 -22
- data/lib/bbcloud/vendor/hirb/CHANGELOG.rdoc +0 -106
- data/lib/bbcloud/vendor/hirb/LICENSE.txt +0 -22
- data/lib/bbcloud/vendor/hirb/README.rdoc +0 -182
- data/lib/bbcloud/vendor/hirb/Rakefile +0 -35
- data/lib/bbcloud/vendor/hirb/lib/bond/completions/hirb.rb +0 -15
- data/lib/bbcloud/vendor/hirb/lib/hirb/console.rb +0 -43
- data/lib/bbcloud/vendor/hirb/lib/hirb/dynamic_view.rb +0 -113
- data/lib/bbcloud/vendor/hirb/lib/hirb/formatter.rb +0 -116
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/auto_table.rb +0 -24
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/object_table.rb +0 -14
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/parent_child_tree.rb +0 -24
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/table/filters.rb +0 -10
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/table/resizer.rb +0 -82
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/table.rb +0 -323
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/tree.rb +0 -181
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers/vertical_table.rb +0 -37
- data/lib/bbcloud/vendor/hirb/lib/hirb/helpers.rb +0 -17
- data/lib/bbcloud/vendor/hirb/lib/hirb/import_object.rb +0 -10
- data/lib/bbcloud/vendor/hirb/lib/hirb/menu.rb +0 -221
- data/lib/bbcloud/vendor/hirb/lib/hirb/pager.rb +0 -95
- data/lib/bbcloud/vendor/hirb/lib/hirb/string.rb +0 -44
- data/lib/bbcloud/vendor/hirb/lib/hirb/util.rb +0 -96
- data/lib/bbcloud/vendor/hirb/lib/hirb/version.rb +0 -3
- data/lib/bbcloud/vendor/hirb/lib/hirb/view.rb +0 -284
- data/lib/bbcloud/vendor/hirb/lib/hirb/views/couch_db.rb +0 -11
- data/lib/bbcloud/vendor/hirb/lib/hirb/views/misc_db.rb +0 -15
- data/lib/bbcloud/vendor/hirb/lib/hirb/views/mongo_db.rb +0 -14
- data/lib/bbcloud/vendor/hirb/lib/hirb/views/orm.rb +0 -11
- data/lib/bbcloud/vendor/hirb/lib/hirb/views/rails.rb +0 -19
- data/lib/bbcloud/vendor/hirb/lib/hirb/views.rb +0 -8
- data/lib/bbcloud/vendor/hirb/lib/hirb.rb +0 -81
- data/lib/bbcloud/vendor/hirb/test/auto_table_test.rb +0 -30
- data/lib/bbcloud/vendor/hirb/test/console_test.rb +0 -27
- data/lib/bbcloud/vendor/hirb/test/deps.rip +0 -4
- data/lib/bbcloud/vendor/hirb/test/dynamic_view_test.rb +0 -94
- data/lib/bbcloud/vendor/hirb/test/formatter_test.rb +0 -171
- data/lib/bbcloud/vendor/hirb/test/hirb_test.rb +0 -39
- data/lib/bbcloud/vendor/hirb/test/import_test.rb +0 -9
- data/lib/bbcloud/vendor/hirb/test/menu_test.rb +0 -239
- data/lib/bbcloud/vendor/hirb/test/object_table_test.rb +0 -79
- data/lib/bbcloud/vendor/hirb/test/pager_test.rb +0 -162
- data/lib/bbcloud/vendor/hirb/test/resizer_test.rb +0 -62
- data/lib/bbcloud/vendor/hirb/test/table_test.rb +0 -550
- data/lib/bbcloud/vendor/hirb/test/test_helper.rb +0 -61
- data/lib/bbcloud/vendor/hirb/test/tree_test.rb +0 -184
- data/lib/bbcloud/vendor/hirb/test/util_test.rb +0 -59
- data/lib/bbcloud/vendor/hirb/test/view_test.rb +0 -172
- data/lib/bbcloud/vendor/hirb/test/views_test.rb +0 -13
@@ -1,221 +0,0 @@
|
|
1
|
-
module Hirb
|
2
|
-
# This class provides a menu using Hirb's table helpers by default to display choices.
|
3
|
-
# Menu choices (syntax at Hirb::Util.choose_from_array) refer to rows. However, when in
|
4
|
-
# two_d mode, choices refer to specific cells by appending a ':field' to a choice.
|
5
|
-
# A field name can be an abbreviated. Menus can also have an action mode, which turns the
|
6
|
-
# menu prompt into a commandline that executes the choices as arguments and uses methods as
|
7
|
-
# actions/commands.
|
8
|
-
class Menu
|
9
|
-
class Error < StandardError; end
|
10
|
-
|
11
|
-
# Detects valid choices and optional field/column
|
12
|
-
CHOSEN_REGEXP = /^(\d([^:]+)?)(?::)?(\S+)?/
|
13
|
-
CHOSEN_ARG = '%s'
|
14
|
-
DIRECTIONS = "Specify individual choices (4,7), range of choices (1-3) or all choices (*)."
|
15
|
-
|
16
|
-
|
17
|
-
# This method will return an array unless it's exited by simply pressing return, which returns nil.
|
18
|
-
# If given a block, the block will yield if and with any menu items are chosen.
|
19
|
-
# All options except for the ones below are passed to render the menu.
|
20
|
-
#
|
21
|
-
# ==== Options:
|
22
|
-
# [*:helper_class*] Helper class to render menu. Helper class is expected to implement numbering given a :number option.
|
23
|
-
# To use a very basic menu, set this to false. Defaults to Hirb::Helpers::AutoTable.
|
24
|
-
# [*:prompt*] String for menu prompt. Defaults to "Choose: ".
|
25
|
-
# [*:ask*] Always ask for input, even if there is only one choice. Default is true.
|
26
|
-
# [*:directions*] Display directions before prompt. Default is true.
|
27
|
-
# [*:readline*] Use readline to get user input if available. Input strings are added to readline history. Default is false.
|
28
|
-
# [*:two_d*] Turn menu into a 2 dimensional (2D) menu by allowing user to pick values from table cells. Default is false.
|
29
|
-
# [*:default_field*] Default field for a 2D menu. Defaults to first field in a table.
|
30
|
-
# [*:action*] Turn menu into an action menu by letting user pass menu choices as an argument to a method/command.
|
31
|
-
# A menu choice's place amongst other arguments is preserved. Default is false.
|
32
|
-
# [*:multi_action*] Execute action menu multiple times iterating over the menu choices. Default is false.
|
33
|
-
# [*:action_object*] Object that takes method/command calls. Default is main.
|
34
|
-
# [*:command*] Default method/command to call when no command given.
|
35
|
-
# [*:reopen*] Reopens $stdin with given file or with /dev/tty when set to true. Use when
|
36
|
-
# $stdin is already reading in piped data.
|
37
|
-
# Examples:
|
38
|
-
# >> extend Hirb::Console
|
39
|
-
# => self
|
40
|
-
# >> menu [1,2,3], :prompt=> "So many choices, so little time: "
|
41
|
-
# >> menu [{:a=>1, :b=>2}, {:a=>3, :b=>4}], :fields=>[:a,b], :two_d=>true)
|
42
|
-
def self.render(output, options={}, &block)
|
43
|
-
new(options).render(output, &block)
|
44
|
-
rescue Error=>e
|
45
|
-
$stderr.puts "Error: #{e.message}"
|
46
|
-
end
|
47
|
-
|
48
|
-
#:stopdoc:
|
49
|
-
def initialize(options={})
|
50
|
-
@options = {:helper_class=>Hirb::Helpers::AutoTable, :prompt=>"Choose: ", :ask=>true,
|
51
|
-
:directions=>true}.merge options
|
52
|
-
@options[:reopen] = '/dev/tty' if @options[:reopen] == true
|
53
|
-
end
|
54
|
-
|
55
|
-
def render(output, &block)
|
56
|
-
@output = Array(output)
|
57
|
-
return [] if @output.size.zero?
|
58
|
-
chosen = choose_from_menu
|
59
|
-
block.call(chosen) if block && chosen.size > 0
|
60
|
-
@options[:action] ? execute_action(chosen) : chosen
|
61
|
-
end
|
62
|
-
|
63
|
-
def get_input
|
64
|
-
prompt = pre_prompt + @options[:prompt]
|
65
|
-
prompt = DIRECTIONS+"\n"+prompt if @options[:directions]
|
66
|
-
$stdin.reopen @options[:reopen] if @options[:reopen]
|
67
|
-
|
68
|
-
if @options[:readline] && readline_loads?
|
69
|
-
get_readline_input(prompt)
|
70
|
-
else
|
71
|
-
print prompt
|
72
|
-
$stdin.gets.chomp.strip
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
def get_readline_input(prompt)
|
77
|
-
input = Readline.readline prompt
|
78
|
-
Readline::HISTORY << input
|
79
|
-
input
|
80
|
-
end
|
81
|
-
|
82
|
-
def pre_prompt
|
83
|
-
prompt = ''
|
84
|
-
prompt << "Default field: #{default_field}\n" if @options[:two_d] && default_field
|
85
|
-
prompt << "Default command: #{@options[:command]}\n" if @options[:action] && @options[:command]
|
86
|
-
prompt
|
87
|
-
end
|
88
|
-
|
89
|
-
def choose_from_menu
|
90
|
-
return unasked_choice if @output.size == 1 && !@options[:ask]
|
91
|
-
|
92
|
-
if (helper_class = Util.any_const_get(@options[:helper_class]))
|
93
|
-
View.render_output(@output, :class=>@options[:helper_class], :options=>@options.merge(:number=>true))
|
94
|
-
else
|
95
|
-
@output.each_with_index {|e,i| puts "#{i+1}: #{e}" }
|
96
|
-
end
|
97
|
-
|
98
|
-
parse_input get_input
|
99
|
-
end
|
100
|
-
|
101
|
-
def unasked_choice
|
102
|
-
return @output unless @options[:action]
|
103
|
-
raise(Error, "Default command and field required for unasked action menu") unless default_field && @options[:command]
|
104
|
-
@new_args = [@options[:command], CHOSEN_ARG]
|
105
|
-
map_tokens([[@output, default_field]])
|
106
|
-
end
|
107
|
-
|
108
|
-
def execute_action(chosen)
|
109
|
-
return nil if chosen.size.zero?
|
110
|
-
if @options[:multi_action]
|
111
|
-
chosen.each {|e| invoke command, add_chosen_to_args(e) }
|
112
|
-
else
|
113
|
-
invoke command, add_chosen_to_args(chosen)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
def invoke(cmd, args)
|
118
|
-
action_object.send(cmd, *args)
|
119
|
-
end
|
120
|
-
|
121
|
-
def parse_input(input)
|
122
|
-
if (@options[:two_d] || @options[:action])
|
123
|
-
tokens = input_to_tokens(input)
|
124
|
-
map_tokens(tokens)
|
125
|
-
else
|
126
|
-
Util.choose_from_array(@output, input)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
def map_tokens(tokens)
|
131
|
-
if return_cell_values?
|
132
|
-
@output[0].is_a?(Hash) ? tokens.map {|arr,f| arr.map {|e| e[f]} }.flatten :
|
133
|
-
tokens.map {|arr,f|
|
134
|
-
arr.map {|e| e.is_a?(Array) && f.is_a?(Integer) ? e[f] : e.send(f) }
|
135
|
-
}.flatten
|
136
|
-
else
|
137
|
-
tokens.map {|e| e[0] }.flatten
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
def return_cell_values?
|
142
|
-
@options[:two_d]
|
143
|
-
end
|
144
|
-
|
145
|
-
def input_to_tokens(input)
|
146
|
-
@new_args = []
|
147
|
-
tokens = (@args = split_input_args(input)).map {|word| parse_word(word) }.compact
|
148
|
-
cleanup_new_args
|
149
|
-
tokens
|
150
|
-
end
|
151
|
-
|
152
|
-
def parse_word(word)
|
153
|
-
if word[CHOSEN_REGEXP]
|
154
|
-
@new_args << CHOSEN_ARG
|
155
|
-
field = $3 ? unalias_field($3) : default_field ||
|
156
|
-
raise(Error, "No default field/column found. Fields must be explicitly picked.")
|
157
|
-
[Util.choose_from_array(@output, word), field ]
|
158
|
-
else
|
159
|
-
@new_args << word
|
160
|
-
nil
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
def cleanup_new_args
|
165
|
-
if @new_args.all? {|e| e == CHOSEN_ARG }
|
166
|
-
@new_args = [CHOSEN_ARG]
|
167
|
-
else
|
168
|
-
i = @new_args.index(CHOSEN_ARG) || raise(Error, "No rows chosen")
|
169
|
-
@new_args.delete(CHOSEN_ARG)
|
170
|
-
@new_args.insert(i, CHOSEN_ARG)
|
171
|
-
end
|
172
|
-
end
|
173
|
-
|
174
|
-
def add_chosen_to_args(items)
|
175
|
-
args = @new_args.dup
|
176
|
-
args[args.index(CHOSEN_ARG)] = items
|
177
|
-
args
|
178
|
-
end
|
179
|
-
|
180
|
-
def command
|
181
|
-
@command ||= begin
|
182
|
-
cmd = (@new_args == [CHOSEN_ARG]) ? nil : @new_args.shift
|
183
|
-
cmd ||= @options[:command] || raise(Error, "No command given for action menu")
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
def action_object
|
188
|
-
@options[:action_object] || eval("self", TOPLEVEL_BINDING)
|
189
|
-
end
|
190
|
-
|
191
|
-
def split_input_args(input)
|
192
|
-
input.split(/\s+/)
|
193
|
-
end
|
194
|
-
|
195
|
-
def default_field
|
196
|
-
@default_field ||= @options[:default_field] || fields[0]
|
197
|
-
end
|
198
|
-
|
199
|
-
# Has to be called after displaying menu
|
200
|
-
def fields
|
201
|
-
@fields ||= @options[:fields] || (@options[:ask] && table_helper_class? && Helpers::Table.last_table ?
|
202
|
-
Helpers::Table.last_table.fields[1..-1] : [])
|
203
|
-
end
|
204
|
-
|
205
|
-
def table_helper_class?
|
206
|
-
@options[:helper_class].is_a?(Class) && @options[:helper_class] < Helpers::Table
|
207
|
-
end
|
208
|
-
|
209
|
-
def unalias_field(field)
|
210
|
-
fields.sort_by {|e| e.to_s }.find {|e| e.to_s[/^#{field}/] } || raise(Error, "Invalid field '#{field}'")
|
211
|
-
end
|
212
|
-
|
213
|
-
def readline_loads?
|
214
|
-
require 'readline'
|
215
|
-
true
|
216
|
-
rescue LoadError
|
217
|
-
false
|
218
|
-
end
|
219
|
-
#:startdoc:
|
220
|
-
end
|
221
|
-
end
|
@@ -1,95 +0,0 @@
|
|
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
|
-
rescue Errno::EPIPE
|
42
|
-
ensure
|
43
|
-
STDOUT.reopen(save_stdout)
|
44
|
-
save_stdout.close
|
45
|
-
pager.close
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
def continue_paging?
|
50
|
-
puts "=== Press enter/return to continue or q to quit: ==="
|
51
|
-
!$stdin.gets.chomp[/q/i]
|
52
|
-
end
|
53
|
-
#:startdoc:
|
54
|
-
end
|
55
|
-
|
56
|
-
attr_reader :width, :height
|
57
|
-
|
58
|
-
def initialize(width, height, options={})
|
59
|
-
resize(width, height)
|
60
|
-
@pager_command = options[:pager_command] if options[:pager_command]
|
61
|
-
end
|
62
|
-
|
63
|
-
# Pages given string using configured pager.
|
64
|
-
def page(string, inspect_mode)
|
65
|
-
if self.class.valid_pager_command?(@pager_command)
|
66
|
-
self.class.command_pager(string, :pager_command=>@pager_command)
|
67
|
-
else
|
68
|
-
self.class.default_pager(string, :width=>@width, :height=>@height, :inspect=>inspect_mode)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def slice!(output, inspect_mode=false) #:nodoc:
|
73
|
-
effective_height = @height - 2 # takes into account pager prompt
|
74
|
-
if inspect_mode
|
75
|
-
sliced_output = String.slice(output, 0, @width * effective_height)
|
76
|
-
output.replace String.slice(output, @width * effective_height, String.size(output))
|
77
|
-
sliced_output
|
78
|
-
else
|
79
|
-
# could use output.scan(/[^\n]*\n?/) instead of split
|
80
|
-
sliced_output = output.split("\n").slice(0, effective_height).join("\n")
|
81
|
-
output.replace output.split("\n").slice(effective_height..-1).join("\n")
|
82
|
-
sliced_output
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
# Determines if string should be paged based on configured width and height.
|
87
|
-
def activated_by?(string_to_page, inspect_mode=false)
|
88
|
-
inspect_mode ? (String.size(string_to_page) > @height * @width) : (string_to_page.count("\n") > @height)
|
89
|
-
end
|
90
|
-
|
91
|
-
def resize(width, height) #:nodoc:
|
92
|
-
@width, @height = View.determine_terminal_size(width, height)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
@@ -1,44 +0,0 @@
|
|
1
|
-
module Hirb
|
2
|
-
# Provides string helpers to deal with UTF-8 and ruby 1.8.x
|
3
|
-
module String
|
4
|
-
extend self
|
5
|
-
# :stopdoc:
|
6
|
-
if RUBY_VERSION < '1.9'
|
7
|
-
def size(string)
|
8
|
-
string.scan(/./).length
|
9
|
-
end
|
10
|
-
|
11
|
-
def ljust(string, desired_length)
|
12
|
-
leftover = desired_length - size(string)
|
13
|
-
leftover > 0 ? string + " " * leftover : string
|
14
|
-
end
|
15
|
-
|
16
|
-
def rjust(string, desired_length)
|
17
|
-
leftover = desired_length - size(string)
|
18
|
-
leftover > 0 ? " " * leftover + string : string
|
19
|
-
end
|
20
|
-
|
21
|
-
def slice(string, start, finish)
|
22
|
-
string.scan(/./).slice(start, finish).join('')
|
23
|
-
end
|
24
|
-
else
|
25
|
-
def size(string)
|
26
|
-
string.length
|
27
|
-
end
|
28
|
-
|
29
|
-
def ljust(string, desired_length)
|
30
|
-
string.ljust(desired_length)
|
31
|
-
end
|
32
|
-
|
33
|
-
def rjust(string, desired_length)
|
34
|
-
string.rjust(desired_length)
|
35
|
-
end
|
36
|
-
|
37
|
-
def slice(*args)
|
38
|
-
string = args.shift
|
39
|
-
string.slice(*args)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
#:startdoc:
|
43
|
-
end
|
44
|
-
end
|
@@ -1,96 +0,0 @@
|
|
1
|
-
module Hirb
|
2
|
-
# Group of handy utility functions used throughout Hirb.
|
3
|
-
module Util
|
4
|
-
extend self
|
5
|
-
# Returns a constant like Module#const_get no matter what namespace it's nested in.
|
6
|
-
# Returns nil if the constant is not found.
|
7
|
-
def any_const_get(name)
|
8
|
-
return name if name.is_a?(Module)
|
9
|
-
begin
|
10
|
-
klass = Object
|
11
|
-
name.split('::').each {|e|
|
12
|
-
klass = klass.const_get(e)
|
13
|
-
}
|
14
|
-
klass
|
15
|
-
rescue
|
16
|
-
nil
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
# Recursively merge hash1 with hash2.
|
21
|
-
def recursive_hash_merge(hash1, hash2)
|
22
|
-
hash1.merge(hash2) {|k,o,n| (o.is_a?(Hash)) ? recursive_hash_merge(o,n) : n}
|
23
|
-
end
|
24
|
-
|
25
|
-
# From Rails ActiveSupport, converting undescored lowercase to camel uppercase.
|
26
|
-
def camelize(string)
|
27
|
-
string.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
|
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
|
-
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
|
-
if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/)
|
63
|
-
[ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
|
64
|
-
elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput')
|
65
|
-
[`tput cols`.to_i, `tput lines`.to_i]
|
66
|
-
elsif STDIN.tty? && command_exists?('stty')
|
67
|
-
`stty size`.scan(/\d+/).map { |s| s.to_i }.reverse
|
68
|
-
else
|
69
|
-
nil
|
70
|
-
end
|
71
|
-
rescue
|
72
|
-
nil
|
73
|
-
end
|
74
|
-
|
75
|
-
# Captures STDOUT of anything run in its block and returns it as string.
|
76
|
-
def capture_stdout(&block)
|
77
|
-
original_stdout = $stdout
|
78
|
-
$stdout = fake = StringIO.new
|
79
|
-
begin
|
80
|
-
yield
|
81
|
-
ensure
|
82
|
-
$stdout = original_stdout
|
83
|
-
end
|
84
|
-
fake.string
|
85
|
-
end
|
86
|
-
|
87
|
-
# From Rubygems, determine a user's home.
|
88
|
-
def find_home
|
89
|
-
['HOME', 'USERPROFILE'].each {|e| return ENV[e] if ENV[e] }
|
90
|
-
return "#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}" if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
|
91
|
-
File.expand_path("~")
|
92
|
-
rescue
|
93
|
-
File::ALT_SEPARATOR ? "C:/" : "/"
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|