Module RubyTUI
In: lib/rubytui.rb
RubyTUI dot/f_0.png

Methods

Constants

AnsiAttributes = { 'clear' => 0, 'reset' => 0, 'bold' => 1, 'dark' => 2, 'underline' => 4, 'underscore' => 4, 'blink' => 5, 'reverse' => 7, 'concealed' => 8, 'black' => 30, 'on_black' => 40, 'red' => 31, 'on_red' => 41, 'green' => 32, 'on_green' => 42, 'yellow' => 33, 'on_yellow' => 43, 'blue' => 34, 'on_blue' => 44, 'magenta' => 35, 'on_magenta' => 45, 'cyan' => 36, 'on_cyan' => 46, 'white' => 37, 'on_white' => 47   Set some ANSI escape code constants (Shamelessly stolen from Perl‘s Term::ANSIColor by Russ Allbery <rra@stanford.edu> and Zenin <zenin@best.com>
ErasePreviousLine = "\033[A\033[K"
DIST = (`uname`.strip == "Darwin" ? "mac" : "unix")   DIST=(`uname`.strip == "Darwin" ? "mac" : `lsb_release -cs`.chomp)
HeaderColor = [ 'bold', 'white', 'on_blue' ]   Output msg as a ANSI-colored program/section header (white on blue).
HighlightColor = [ 'red', 'bold' ]   Output msg as a ANSI-colored highlighted text.
MessageColor = [ 'cyan' ]   Output msg to STDERR and flush it.
ErrorColor = [ 'bold', 'white', 'on_red' ]   Output the specified msg as an ANSI-colored error message (white on red).
DebugColor = [ 'bold', 'yellow', 'on_blue' ]   Output the specified msg as an ANSI-colored debugging message (yellow on blue).
PromptColor = ['bold', 'green']   Output the specified promptString as a prompt (in green) and return the user‘s input with leading and trailing spaces removed.

Public Instance methods

[Source]

     # File lib/rubytui.rb, line 331
331:     def _displayNumberedMenu( head, ques, m_items )
332:       header head
333:       m_items.each_with_index {|item, i|
334:         if !item.nil?
335:           highlight "\t%d" % i.to_s
336:           display ": %s\n" % item
337:         end
338:       }
339:       choice = prompt( ques )
340:       return choice.empty? ? nil : choice.to_i
341:     end

Output the specified msg colored in ANSI red and exit with a status of 1.

[Source]

     # File lib/rubytui.rb, line 182
182:     def abort( msg )
183:         print ansiCode( 'bold', 'red' ) + "Aborted: " + msg.chomp + ansiCode( 'reset' ) + "\n\n"
184:         Kernel.exit!( 1 )
185:     end

Create a string that contains the ANSI codes specified and return it

[Source]

    # File lib/rubytui.rb, line 74
74:     def ansiCode( *attributes )
75:         return '' unless $COLOR
76:         return '' unless /(?:vt10[03]|screen|[aex]term(?:-color)?|linux|rxvt(?:-unicode))/i =~ ENV['TERM']
77:         attr = attributes.collect {|a| AnsiAttributes[a] ? AnsiAttributes[a] : nil}.compact.join(';')
78:         if attr.empty? 
79:             return ''
80:         else
81:           if DIST != "mac"
82:             str = "\e[%sm"
83:           else
84:             str = "\001\033[%sm\002"
85:           end
86:           return str % attr
87:         end
88:     end

Clear the screen.

[Source]

     # File lib/rubytui.rb, line 162
162:     def clear
163:         $stderr.write `clear`
164:         $stderr.flush
165:     end

Return the given prompt with the specified attributes turned on and a reset at the end.

[Source]

    # File lib/rubytui.rb, line 92
92:     def colored( prompt, *attributes )
93:         return ansiCode( *(attributes.flatten) ) + prompt + ansiCode( 'reset' )
94:     end

[Source]

     # File lib/rubytui.rb, line 134
134:     def debugMsg( msg )
135:         return unless $DEBUG
136:         msg.chomp!
137:         $stderr.puts ansiCode( *DebugColor ) + ">>> #{msg}" + ansiCode( 'reset' )
138:         $stderr.flush
139:     end

Output the specified msg without any colors

[Source]

     # File lib/rubytui.rb, line 142
142:     def display( msg )
143:         $stderr.print msg
144:         $stderr.flush
145:     end

Output a divider made up of length hyphen characters.

[Source]

     # File lib/rubytui.rb, line 155
155:     def divider( length=75 )
156:         $stderr.puts( "-" * length )
157:         $stderr.flush
158:     end

Put a newline on the end of a message call.

[Source]

     # File lib/rubytui.rb, line 120
120:     def echo( string )
121:         message string.chomp + "\n"
122:     end

[Source]

     # File lib/rubytui.rb, line 127
127:     def errorMessage( msg )
128:         message ansiCode( *ErrorColor ) + msg + ansiCode( 'reset' )
129:     end

[Source]

     # File lib/rubytui.rb, line 99
 99:     def header( msg )
100:         msg.chomp!
101:         $stderr.puts ansiCode( *HeaderColor ) + msg + ansiCode( 'reset' )
102:         $stderr.flush
103:     end

[Source]

     # File lib/rubytui.rb, line 107
107:     def highlight( msg )
108:         $stderr.print ansiCode( *HighlightColor ) + msg + ansiCode( 'reset' )
109:         $stderr.flush
110:     end

Display a menu of numeric choices for the m_items passed in, with a title of head and a prompt of ques.

[Source]

     # File lib/rubytui.rb, line 262
262:     def menu( head, ques, *m_items )
263:         return m_items[0] if m_items.length == 1
264:         choice = _displayMenu( head, ques, *m_items )
265:         until choice and (1..(m_items.length)).include?( choice )
266:             errorMessage "\nPlease enter a number between 1 and #{m_items.length}\n\n"
267:             choice = _displayMenu( head, ques, *m_items )
268:         end
269:         return m_items[choice - 1]
270:     end

Display a menu of numeric choices for the m_items passed in, with a title of head, a prompt of ques and a default value of default.

[Source]

     # File lib/rubytui.rb, line 293
293:     def menuWithDefault( head, ques, default, *m_items )
294:         if (m_items - [default, nil]).length == 0
295:           return default
296:         end
297:         choice = _displayMenu( head, ques + " [#{default}]", *m_items )
298:         return default unless choice
299:         until (1..(m_items.length)).include?( choice )
300:             errorMessage "\nPlease enter a number between 1 and #{m_items.length}\n\n"
301:             choice = _displayMenu( head, ques + " [#{default}]", *m_items )
302:             return default unless choice
303:         end
304:         return m_items[choice - 1]
305:     end

[Source]

     # File lib/rubytui.rb, line 114
114:     def message( msg )
115:         $stderr.print ansiCode( *MessageColor ) + msg + ansiCode( 'reset' )
116:         $stderr.flush
117:     end

Yes/No prompt with default of Yes.

[Source]

     # File lib/rubytui.rb, line 241
241:     def noYes( promptString )
242:       yes_or_no(promptString, true)
243:     end

Display a menu of numeric choices for the m_items passed in, with a title of head and a prompt of ques. Unlike menu, this respects the index of the m_items array.

[Source]

     # File lib/rubytui.rb, line 276
276:     def numberedMenu( head, ques, m_items )
277:       valid_choices = []
278:       m_items.each_with_index{|x,i|
279:         valid_choices << i if !x.nil?
280:       }
281:       return m_items[valid_choices[0]] if valid_choices.length == 1
282:       choice = _displayNumberedMenu( head, ques, m_items )
283:       until valid_choices.include?( choice )
284:         errorMessage "\nPlease enter a valid choice\n\n"
285:         choice = _displayNumberedMenu( head, ques, m_items )
286:       end
287:       return m_items[choice]
288:     end

Display a menu of numeric choices for the m_items passed in, with a title of head, a prompt of ques and a default value of default. Unlike menuWithDefault, this respects the index of the m_items array.

[Source]

     # File lib/rubytui.rb, line 313
313:     def numberedMenuWithDefault( head, ques, default, m_items )
314:       if (m_items - [default, nil]).length == 0
315:         return default
316:       end
317:       choice = _displayNumberedMenu( head, ques + " [#{default}]", m_items )
318:       return default unless choice
319:       valid_choices = []
320:       m_items.each_with_index{|x,i|
321:         valid_choices << i if !x.nil?
322:       }
323:       until valid_choices.include?( choice )
324:         errorMessage "\nPlease enter a valid choice\n\n"
325:         choice = _displayNumberedMenu( head, ques + " [#{default}]", m_items )
326:         return default unless choice
327:       end
328:       return m_items[choice]
329:     end

Wait for input.

[Source]

     # File lib/rubytui.rb, line 176
176:     def pausePrompt
177:         prompt "press ENTER to continue..."
178:     end

Output the specified promptString as a prompt and return the user‘s input. If a test is provided, the prompt will repeat until the test returns true. An optional failure message can also be passed in.

[Source]

     # File lib/rubytui.rb, line 198
198:     def prompt( promptString, failure_msg="Try again.", &test )
199:         promptString.chomp!
200:         response = ""
201:         if $TIMEOUT
202:             begin
203:                 Timeout::timeout($TIMEOUT) {
204:                     response = promptResponse(promptString) 
205:                 }
206:             rescue Timeout::Error
207:                 errorMessage "\nTimed out!\n"
208:             end
209:         else
210:             response = promptResponse(promptString) 
211:         end
212:         until test.call(response)
213:             errorMessage(failure_msg)
214:             message("\n")
215:             response = prompt( promptString )
216:         end if test
217:         return response
218:     end

[Source]

     # File lib/rubytui.rb, line 190
190:     def promptResponse(promptString)
191:         return readline( ansiCode(*PromptColor) +
192:             "#{promptString}: " + ansiCode('reset') ).strip
193:     end

Prompt the user with the given promptString via prompt, substituting the given default if the user doesn‘t input anything. If a test is provided, the prompt will repeat until the test returns true. An optional failure message can also be passed in.

[Source]

     # File lib/rubytui.rb, line 224
224:     def promptWithDefault( promptString, default, failure_msg="Try again.", &test )
225:         response = prompt( "%s [%s]" % [ promptString, default ] )
226:         response = default if response.empty?
227:         until test.call(response)
228:             errorMessage(failure_msg)
229:             message("\n")
230:             response = promptWithDefault( promptString, default )
231:         end if test
232:         return response
233:     end

Erase the previous line (if supported by your terminal) and output the specified msg instead.

[Source]

     # File lib/rubytui.rb, line 149
149:     def replaceMessage( msg )
150:         print ErasePreviousLine
151:         message( msg )
152:     end

Provide a pause and prompt to continue.

[Source]

     # File lib/rubytui.rb, line 168
168:     def waitasec
169:         display "\n"
170:         divider(10)
171:         pausePrompt
172:         clear
173:     end
writeLine( length=75 )

Alias for divider

Yes/No prompt with default of No.

[Source]

     # File lib/rubytui.rb, line 236
236:     def yesNo( promptString )
237:       yes_or_no(promptString, false)
238:     end

[Source]

     # File lib/rubytui.rb, line 245
245:     def yes_or_no(promptString, default = nil)
246:       answer = ""
247:       if !default.nil?
248:         answer = promptWithDefault( promptString, default ? "yes" : "no",
249:             "Please enter 'yes' or 'no'" ) {|response|
250:             response.match( '^[YyNn]' )
251:         }
252:       else
253:         answer = prompt( promptString, "Please enter 'yes' or 'no'" ) {|response|
254:           response.match( '^[YyNn]' )
255:         }
256:       end
257:       return answer.match( '^[Yy]' )
258:     end

Private Instance methods

[Source]

     # File lib/rubytui.rb, line 343
343:     def _displayMenu( head, ques, *m_items )
344:         header head
345:         m_items.each_with_index {|item, i|
346:             highlight "\t%d" % (i+1).to_s
347:             display ": %s\n" % item
348:         }
349:         choice = prompt( ques )
350:         return choice.empty? ? nil : choice.to_i
351:     end

[Validate]