Module | RubyTUI |
In: |
lib/rubytui.rb
|
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. |
# File lib/rubytui.rb, line 332 332: def _displayNumberedMenu( head, ques, m_items ) 333: header head 334: m_items.each_with_index {|item, i| 335: if !item.nil? 336: highlight "\t%d" % i.to_s 337: display ": %s\n" % item 338: end 339: } 340: choice = prompt( ques ) 341: return choice.empty? ? nil : choice.to_i 342: end
Create a string that contains the ANSI codes specified and return it
# File lib/rubytui.rb, line 75 75: def ansiCode( *attributes ) 76: return '' unless $COLOR 77: return '' unless /(?:vt10[03]|screen|[aex]term(?:-color)?|linux|rxvt(?:-unicode))/i =~ ENV['TERM'] 78: attr = attributes.collect {|a| AnsiAttributes[a] ? AnsiAttributes[a] : nil}.compact.join(';') 79: if attr.empty? 80: return '' 81: else 82: if DIST != "mac" 83: str = "\e[%sm" 84: else 85: str = "\001\033[%sm\002" 86: end 87: return str % attr 88: end 89: end
Clear the screen.
# File lib/rubytui.rb, line 163 163: def clear 164: $stderr.write `clear` 165: $stderr.flush 166: end
# File lib/rubytui.rb, line 135 135: def debugMsg( msg ) 136: return unless $DEBUG 137: msg.chomp! 138: $stderr.puts ansiCode( *DebugColor ) + ">>> #{msg}" + ansiCode( 'reset' ) 139: $stderr.flush 140: end
Output the specified msg without any colors
# File lib/rubytui.rb, line 143 143: def display( msg ) 144: $stderr.print msg 145: $stderr.flush 146: end
# File lib/rubytui.rb, line 128 128: def errorMessage( msg ) 129: message ansiCode( *ErrorColor ) + msg + ansiCode( 'reset' ) 130: end
# File lib/rubytui.rb, line 100 100: def header( msg ) 101: msg.chomp! 102: $stderr.puts ansiCode( *HeaderColor ) + msg + ansiCode( 'reset' ) 103: $stderr.flush 104: end
# File lib/rubytui.rb, line 108 108: def highlight( msg ) 109: $stderr.print ansiCode( *HighlightColor ) + msg + ansiCode( 'reset' ) 110: $stderr.flush 111: end
Display a menu of numeric choices for the m_items passed in, with a title of head and a prompt of ques.
# File lib/rubytui.rb, line 263 263: def menu( head, ques, *m_items ) 264: return m_items[0] if m_items.length == 1 265: choice = _displayMenu( head, ques, *m_items ) 266: until choice and (1..(m_items.length)).include?( choice ) 267: errorMessage "\nPlease enter a number between 1 and #{m_items.length}\n\n" 268: choice = _displayMenu( head, ques, *m_items ) 269: end 270: return m_items[choice - 1] 271: 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.
# File lib/rubytui.rb, line 294 294: def menuWithDefault( head, ques, default, *m_items ) 295: if (m_items - [default, nil]).length == 0 296: return default 297: end 298: choice = _displayMenu( head, ques + " [#{default}]", *m_items ) 299: return default unless choice 300: until (1..(m_items.length)).include?( choice ) 301: errorMessage "\nPlease enter a number between 1 and #{m_items.length}\n\n" 302: choice = _displayMenu( head, ques + " [#{default}]", *m_items ) 303: return default unless choice 304: end 305: return m_items[choice - 1] 306: end
# File lib/rubytui.rb, line 115 115: def message( msg ) 116: $stderr.print ansiCode( *MessageColor ) + msg + ansiCode( 'reset' ) 117: $stderr.flush 118: 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.
# File lib/rubytui.rb, line 277 277: def numberedMenu( head, ques, m_items ) 278: valid_choices = [] 279: m_items.each_with_index{|x,i| 280: valid_choices << i if !x.nil? 281: } 282: return m_items[valid_choices[0]] if valid_choices.length == 1 283: choice = _displayNumberedMenu( head, ques, m_items ) 284: until valid_choices.include?( choice ) 285: errorMessage "\nPlease enter a valid choice\n\n" 286: choice = _displayNumberedMenu( head, ques, m_items ) 287: end 288: return m_items[choice] 289: 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.
# File lib/rubytui.rb, line 314 314: def numberedMenuWithDefault( head, ques, default, m_items ) 315: if (m_items - [default, nil]).length == 0 316: return default 317: end 318: choice = _displayNumberedMenu( head, ques + " [#{default}]", m_items ) 319: return default unless choice 320: valid_choices = [] 321: m_items.each_with_index{|x,i| 322: valid_choices << i if !x.nil? 323: } 324: until valid_choices.include?( choice ) 325: errorMessage "\nPlease enter a valid choice\n\n" 326: choice = _displayNumberedMenu( head, ques + " [#{default}]", m_items ) 327: return default unless choice 328: end 329: return m_items[choice] 330: end
Wait for input.
# File lib/rubytui.rb, line 177 177: def pausePrompt 178: prompt "press ENTER to continue..." 179: 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.
# File lib/rubytui.rb, line 199 199: def prompt( promptString, failure_msg="Try again.", &test ) 200: promptString.chomp! 201: response = "" 202: if $TIMEOUT 203: begin 204: Timeout::timeout($TIMEOUT) { 205: response = promptResponse(promptString) 206: } 207: rescue Timeout::Error 208: errorMessage "\nTimed out!\n" 209: end 210: else 211: response = promptResponse(promptString) 212: end 213: until test.call(response) 214: errorMessage(failure_msg) 215: message("\n") 216: response = prompt( promptString ) 217: end if test 218: return response 219: end
# File lib/rubytui.rb, line 191 191: def promptResponse(promptString) 192: return readline( ansiCode(*PromptColor) + 193: "#{promptString}: " + ansiCode('reset') ).strip 194: 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.
# File lib/rubytui.rb, line 225 225: def promptWithDefault( promptString, default, failure_msg="Try again.", &test ) 226: response = prompt( "%s [%s]" % [ promptString, default ] ) 227: response = default if response.empty? 228: until test.call(response) 229: errorMessage(failure_msg) 230: message("\n") 231: response = promptWithDefault( promptString, default ) 232: end if test 233: return response 234: end
Erase the previous line (if supported by your terminal) and output the specified msg instead.
# File lib/rubytui.rb, line 150 150: def replaceMessage( msg ) 151: print ErasePreviousLine 152: message( msg ) 153: end
# File lib/rubytui.rb, line 246 246: def yes_or_no(promptString, default = nil) 247: answer = "" 248: if !default.nil? 249: answer = promptWithDefault( promptString, default ? "yes" : "no", 250: "Please enter 'yes' or 'no'" ) {|response| 251: response.match( '^[YyNn]' ) 252: } 253: else 254: answer = prompt( promptString, "Please enter 'yes' or 'no'" ) {|response| 255: response.match( '^[YyNn]' ) 256: } 257: end 258: return answer.match( '^[Yy]' ) 259: end