Class: ConsoleUI
Constant Summary
collapse
- CONSOLE_COLOR_STRINGS =
{
:gray => [1,30], :black => [30],
:light_red => [1,31], :red => [31],
:light_green => [1,32], :green => [32],
:light_yellow => [1,33], :yellow => [33],
:light_blue => [1,34], :blue => [34],
:light_purple => [1,35], :purple => [35],
:light_cyan => [1,36], :cyan => [36],
:white => [1,37], :light_gray => [37],
:underscore => [4],
:blink => [5],
:inverse => [7],
:concealed => [8],
:default => [0]
}
- CONSOLE_COLORS =
CONSOLE_COLOR_STRINGS.keys
Instance Method Summary
collapse
Methods inherited from BaseUI
#enable_progress_bar, #error, #name, #not_implemented, #pinc, #pmsg, #progress_bar, #progress_bar_enabled?, #question_yes_no, #question_yes_no_retry, #select_index, #warn
Instance Method Details
#bell(options = {}) ⇒ Object
20
21
22
|
# File 'lib/libisi/ui/console.rb', line 20
def bell(options = {})
print "\007"
end
|
#colorize(color) ⇒ Object
#console_commands(commands) ⇒ Object
150
|
# File 'lib/libisi/ui/console.rb', line 150
def console_commands(commands) ; commands.map {|number| "\e[#{number}m"}.join ;end
|
#diff(file1, file2, options = {}) ⇒ Object
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/libisi/ui/console.rb', line 210
def diff(file1, file2, options = {})
text = result_of_system("diff -r -u '#{file1}' '#{file2}'",true)
color1 = :green
color2 = :red
text = text.gsub(/^\+.*$/) {|l| colorize(color1) { l }}
text = text.gsub(/^\-.*$/) {|l| colorize(color2) { l }}
print text
$?.success?
end
|
#execute_in_console(command, options = {}) ⇒ Object
199
200
201
|
# File 'lib/libisi/ui/console.rb', line 199
def execute_in_console(command, options = {})
system(command)
end
|
#info(text, options = {}) ⇒ Object
23
24
25
26
27
|
# File 'lib/libisi/ui/console.rb', line 23
def info(text, options = {})
bell
print "#{text} [hit ENTER]"
STDIN.readline
end
|
#info_non_blocking(text, options = {}) ⇒ Object
28
29
30
|
# File 'lib/libisi/ui/console.rb', line 28
def info_non_blocking(text, options = {})
print("#{text}\n")
end
|
#password(text) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/libisi/ui/console.rb', line 57
def password(text)
begin
print "#{text}: "
system "stty -echo"
pw = STDIN.gets.chomp
print "\n"
ensure
system "stty echo"
end
pw
end
|
#progress(count) ⇒ Object
177
178
179
|
# File 'lib/libisi/ui/console.rb', line 177
def progress(count)
@pbar.set(count) if @pbar
end
|
#progress_bar_implementation(text, total) ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/libisi/ui/console.rb', line 159
def progress_bar_implementation(text,total)
ret = nil
begin
require "progressbar"
@pbar = ProgressBar.new(text, total)
ret = yield
pmsg
@pbar.finish
@pbar = nil
ensure
if @pbar
@pbar.halt
@pbar = nil
end
end
ret
end
|
#progress_inc ⇒ Object
194
195
196
197
|
# File 'lib/libisi/ui/console.rb', line 194
def progress_inc
return unless @pbar
@pbar.inc
end
|
#progress_message(message) ⇒ Object
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/libisi/ui/console.rb', line 181
def progress_message(message)
return unless @pbar
width = (@pbar.instance_eval("get_width")) - 6
if message.length > width then
message = message[0..(width/2)] + ">..<" + message[-(width/2)..-1]
end
STDERR.print("\n")
STDERR.print("\e[K")
STDERR.print(message.to_s)
STDERR.print("\e[#{message.to_s.length}D")
STDERR.print("\e[1A")
end
|
#question(text, options = {}) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/libisi/ui/console.rb', line 31
def question(text, options = {})
default = nil
unless options[:default].nil?
default = (options[:default] ? "y" : "n")
end
while true
case default
when "y"
print(text + "(Y/n)")
when "n"
print(text + "(y/N)")
else
print(text + "(y/n)")
end
answer = STDIN.readline.to_s.strip.downcase
answer = default if answer == ""
case answer
when "y"
return true
when "n"
return false
end
end
end
|
#select(list, multi_select = false, options = {}) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/libisi/ui/console.rb', line 70
def select(list, multi_select = false, options = {})
texts = []
list.each_with_index {|val, index|
if block_given?
item_text = yield(val)
else
item_text = val.to_s
end
if item_text.include?("\n")
item_text = item_text.split("\n").map {|l| " #{l}"}.join("\n")
end
print "#{index + 1}: #{item_text}\n"
texts << item_text
}
ret = nil
while ret.nil?
begin
if multi_select
print "Please make your choice. (Comma seperated list or 'all' for all.)\n"
else
print "Please make your choice:"
end
select = STDIN.readline
if select.strip == "all" or select.strip == "a"
indexes = (0..(list.length-1)).to_a
else
values = select.split(",").map {|s| s.strip}
indexes = values.map {|n| n.strip}.map {|sel| sel.to_i - 1}.sort.uniq
indexes = indexes.select {|i| i>=0 }
if indexes.length < values.length
indexes = values.map {|s| texts.index(s) }.compact.sort.uniq
end
raise "Not all values could be found!" if indexes.length < values.length
end
ret = indexes.map {|sel|
raise "Unexpected input, please enter a number!" if sel < 0
raise "Item with index '#{sel + 1}' not found." unless list[sel]
list[sel]
}
if ret.length > 1 and !multi_select
raise "Only one item allowe"
end
rescue
print "#{$!}\n"
ret = nil
end
end
return indexes if options[:return_indexes]
ret
end
|
#shell ⇒ Object
203
204
205
206
207
208
|
# File 'lib/libisi/ui/console.rb', line 203
def shell
shell_name = "bash"
print("\nFallen into #{shell_name} shell\n")
print("(CTRL-D) for exit\n\n")
system("/bin/#{shell_name} < /dev/tty > /dev/tty 2> /dev/tty")
end
|
#test_colors ⇒ Object
144
145
146
147
148
|
# File 'lib/libisi/ui/console.rb', line 144
def test_colors
CONSOLE_COLORS.sort_by{|n| n.to_s}.each {|color|
print "#{color}: " + colorize(color) { "****" } + "\n"
}
end
|