Module: CloudFlock::Interface::CLI::Console
Overview
Instance Method Summary collapse
-
#annoy ⇒ Object
Public: Set text flashing.
-
#blue ⇒ Object
Public: Set text blue.
-
#bold ⇒ Object
Public: Set text bold.
-
#build_grid(options, labels = nil) ⇒ Object
Public: Generate a reasonably formatted, printable table.
-
#escape(seq) ⇒ Object
Public: Escape sequence.
-
#green ⇒ Object
Public: Set text green.
-
#prompt(question, args = {}) ⇒ Object
Public: Prompt user for input, allowing for a default answer and a list of valid responses.
-
#prompt_yn(question, args = {}) ⇒ Object
Public: Wrap Console#prompt but require a Y/N response.
-
#red ⇒ Object
Public: Set text red.
-
#reset ⇒ Object
Public: Reset terminal control.
-
#spinner(message, &block) ⇒ Object
Public: Render a spinner on the command line and yield to a block, reporting success if nothing is raised, otherwise reporting failure.
-
#underline ⇒ Object
Public: Set text underlined.
-
#white ⇒ Object
Public: Set text white.
Instance Method Details
#annoy ⇒ Object
Public: Set text flashing
Returns a String the terminal control sequence to set text flashing.
192 |
# File 'lib/cloudflock/interface/cli/console.rb', line 192 def annoy; escape("5"); end |
#blue ⇒ Object
Public: Set text blue
Returns a String the terminal control sequence to set text blue.
207 |
# File 'lib/cloudflock/interface/cli/console.rb', line 207 def blue; escape("34"); end |
#bold ⇒ Object
Public: Set text bold
Returns a String the terminal control sequence to set text bold.
182 |
# File 'lib/cloudflock/interface/cli/console.rb', line 182 def bold; escape("1"); end |
#build_grid(options, labels = nil) ⇒ Object
Public: Generate a reasonably formatted, printable table.
options - An Array containing Hash objects which contain desired options:
[{:col1 => val1, :col2 => val2}, {:col1 => val3, :col2 => val4}]
labels - A Hash containing key-value pairs to label each key in options.
(default: nil)
Returns a String containing the grid. Raises ArgumentError if options is not an Array which contains at least one element.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/cloudflock/interface/cli/console.rb', line 122 def build_grid(, labels = nil) raise ArgumentError unless .kind_of?(Array) and ![0].nil? if labels.nil? .unshift([0].keys.reduce({}) { |c,e| {e => e.to_s} }) else .unshift(labels) end keys = [0].keys max_lengths = keys.reduce({}) { |c,e| c.merge({e => 0}) } .each do |row| row.each_key do |key| if max_lengths[key] < row[key].to_s.length max_lengths[key] = row[key].to_s.length end end end # Base width = 3n+1 grid_width = (max_lengths.length * 3) + 1 # Construct rule grid_rule = "+" [0].each_key { |k| grid_rule << "-" * (max_lengths[k] + 2) + "+" } # Construct grid grid = "" grid << grid_rule .each_with_index do |row, idx| grid << "\n|" keys.each do |key| grid << sprintf(" % #{max_lengths[key]}s |", row[key]) end # Visually separate the labels grid << "\n" + grid_rule if idx == 0 end grid << "\n" + grid_rule end |
#escape(seq) ⇒ Object
Public: Escape sequence
Returns a String containing an escaped terminal control sequence.
172 |
# File 'lib/cloudflock/interface/cli/console.rb', line 172 def escape(seq); "\033[#{seq}m"; end |
#green ⇒ Object
Public: Set text green
Returns a String the terminal control sequence to set text green.
202 |
# File 'lib/cloudflock/interface/cli/console.rb', line 202 def green; escape("32"); end |
#prompt(question, args = {}) ⇒ Object
Public: Prompt user for input, allowing for a default answer and a list of valid responses.
question - String containing the question to present to the user. args - Hash containing arguments to control acceptable responses.
(default: {}):
:default_answer - String containing the default answer. If the
default is nil, a non-empty answer MUST be given.
:valid_answers - An Array containing all valid responses.
Returns a String containing the answer provided by the user.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cloudflock/interface/cli/console.rb', line 23 def prompt(question, args = {}) default = args[:default_answer].to_s allow_empty = !args[:default_answer].nil? valid = args[:valid_answers] || [] default_display = default.empty? ? "" : "[%s]" % default.strip question.strip! acceptable = false until acceptable printf("%s %s> ", question, default_display) answer = $stdin.readline.strip if answer.empty? && allow_empty acceptable = true elsif valid.empty? && !answer.empty? acceptable = true elsif !(valid.grep(answer)).empty? && !valid.empty? acceptable = true end end answer.empty? ? default.to_s : answer end |
#prompt_yn(question, args = {}) ⇒ Object
Public: Wrap Console#prompt but require a Y/N response.
question - String containing the question to present to the user. args - Hash containing arguments to control acceptable responses.
(default: {}):
:default_answer - String containing the default answer.
Returns true or false corresponding to Y or N answer respectively.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/cloudflock/interface/cli/console.rb', line 56 def prompt_yn(question, args = {}) args[:valid] = [] answer = nil until answer =~ /^[yn]/i answer = prompt(question, args) end /^n/i.match(answer).nil? end |
#red ⇒ Object
Public: Set text red
Returns a String the terminal control sequence to set text red.
197 |
# File 'lib/cloudflock/interface/cli/console.rb', line 197 def red; escape("31"); end |
#reset ⇒ Object
Public: Reset terminal control
Returns a String the ‘reset’ terminal control sequence.
177 |
# File 'lib/cloudflock/interface/cli/console.rb', line 177 def reset; escape("0"); end |
#spinner(message, &block) ⇒ Object
Public: Render a spinner on the command line and yield to a block, reporting success if nothing is raised, otherwise reporting failure.
message - Message to be displayed describing the task being evaluated. block - Block to be yielded to determine pass or fail.
Returns the result of the yielded block if successful. Raises whatever is raised inside the yielded block.
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 |
# File 'lib/cloudflock/interface/cli/console.rb', line 75 def spinner(, &block) success = nil result = nil pre = "\r#{bold}#{white} [#{reset}" post = "#{bold}#{white}] #{reset}#{}" pre_ok = "\r#{bold}#{white} [#{green} ok " pre_fail = "\r#{bold}#{white} [#{red}fail" thread = Thread.new do step = 0 spin = [" ", ". ", ".. ", "... ", "....", " ...", " ..", " ."] while success.nil? print "#{pre}#{spin[step % 8]}#{post}" step += 1 sleep 0.5 end if success print "#{pre_ok}#{post}\n" else print "#{pre_fail}#{post}\n" end end begin result = yield success = true thread.join return result rescue success = false thread.join raise end end |
#underline ⇒ Object
Public: Set text underlined
Returns a String the terminal control sequence to set text underlined.
187 |
# File 'lib/cloudflock/interface/cli/console.rb', line 187 def underline; escape("4"); end |
#white ⇒ Object
Public: Set text white
Returns a String the terminal control sequence to set text white.
212 |
# File 'lib/cloudflock/interface/cli/console.rb', line 212 def white; escape("37"); end |