Module: Transcriptic::UI
- Included in:
- CLI::Base, Client, DependenciesGenerator, SBT
- Defined in:
- lib/transcriptic/ui.rb
Constant Summary collapse
- @@kb =
1024
- @@mb =
1024 * @@kb
- @@gb =
1024 * @@mb
Class Method Summary collapse
- .disable_error_capture ⇒ Object
- .enable_error_capture ⇒ Object
- .extended(base) ⇒ Object
- .extended_into ⇒ Object
- .included(base) ⇒ Object
- .included_into ⇒ Object
Instance Method Summary collapse
-
#arrow(message) ⇒ Object
DISPLAY HELPERS.
- #confirm(message = "Are you sure you wish to continue? (y/n)?") ⇒ Object
- #confirm_billing ⇒ Object
- #confirm_command(resource_id) ⇒ Object
- #confirm_quote ⇒ Object
- #deprecate(version) ⇒ Object
- #display(msg = "", newline = true) ⇒ Object
- #display_row(row, lengths) ⇒ Object
- #display_table(objects, columns, headers) ⇒ Object
- #error(msg, color = :red) ⇒ Object
- #error_with_failure(message) ⇒ Object
- #fail(message) ⇒ Object
- #format_bytes(amount) ⇒ Object
- #format_date(date) ⇒ Object
- #format_with_bang(message) ⇒ Object
- #get_terminal_environment ⇒ Object
- #indent(message, indent = 3) ⇒ Object
- #info(message = '', color = nil, force_new_line = (message.to_s !~ /( |\t)\Z/)) ⇒ Object
- #json_decode(json) ⇒ Object
- #json_encode(object) ⇒ Object
- #longest(items) ⇒ Object
-
#mute! ⇒ Object
Mute the output of this instance of UI until #unmute! is called.
- #output(message = "") ⇒ Object
- #output_with_arrow(message = "") ⇒ Object
- #output_with_bang(message = "", new_line = true) ⇒ Object
- #output_with_indent(message, indent = 3) ⇒ Object
- #quantify(string, num) ⇒ Object
- #quiet? ⇒ Boolean
- #redisplay(line, line_break = false) ⇒ Object
- #say(message = '', color = nil, force_new_line = (message.to_s !~ /( |\t)\Z/)) ⇒ Object
- #say_status(status, message, log_status = true) ⇒ Object
- #set_buffer(enable) ⇒ Object
- #status(message) ⇒ Object
- #string_distance(first, last) ⇒ Object
- #suggestion(actual, possibilities) ⇒ Object
- #time_ago(elapsed) ⇒ Object
- #truncate(text, length) ⇒ Object
-
#unmute! ⇒ Object
Unmute the output of this instance of UI until #mute! is called.
- #wait_spinner(message, fps = 10) ⇒ Object
- #warn(message, color = :yellow) ⇒ Object
Class Method Details
.disable_error_capture ⇒ Object
302 303 304 305 306 307 308 309 310 311 |
# File 'lib/transcriptic/ui.rb', line 302 def self.disable_error_capture included_into.each do |base| base.send(:alias_method, :error, :error_without_failure) end extended_into.each do |base| class << base alias_method :error, :error_without_failure end end end |
.enable_error_capture ⇒ Object
289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/transcriptic/ui.rb', line 289 def self.enable_error_capture included_into.each do |base| base.send(:alias_method, :error_without_failure, :error) base.send(:alias_method, :error, :error_with_failure) end extended_into.each do |base| class << base alias_method :error_without_failure, :error alias_method :error, :error_with_failure end end end |
.extended(base) ⇒ Object
265 266 267 |
# File 'lib/transcriptic/ui.rb', line 265 def self.extended(base) extended_into << base end |
.extended_into ⇒ Object
257 258 259 |
# File 'lib/transcriptic/ui.rb', line 257 def self.extended_into @@extended_into ||= [] end |
.included(base) ⇒ Object
261 262 263 |
# File 'lib/transcriptic/ui.rb', line 261 def self.included(base) included_into << base end |
.included_into ⇒ Object
253 254 255 |
# File 'lib/transcriptic/ui.rb', line 253 def self.included_into @@included_into ||= [] end |
Instance Method Details
#arrow(message) ⇒ Object
DISPLAY HELPERS
202 203 204 |
# File 'lib/transcriptic/ui.rb', line 202 def arrow() "====> #{}" end |
#confirm(message = "Are you sure you wish to continue? (y/n)?") ⇒ Object
101 102 103 |
# File 'lib/transcriptic/ui.rb', line 101 def confirm( = "Are you sure you wish to continue? (y/n)?") ask().downcase == 'y' end |
#confirm_billing ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/transcriptic/ui.rb', line 91 def confirm_billing display display "This action will cause your account to be billed at the end of the month" display "Are you sure you want to do this? (y/n) ", false if ask.downcase == 'y' transcriptic.confirm_billing return true end end |
#confirm_command(resource_id) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/transcriptic/ui.rb', line 117 def confirm_command(resource_id) confirmed_resource = extract_option('--confirm', false) if confirmed_resource unless confirmed_resource == resource_id raise(Transcriptic::Command::CommandFailed, "Confirmed resource #{confirmed_resource} did not match the selected resource #{resource_id}.") end return true else display display " ! WARNING: Potentially Destructive Action" display " ! This command will affect the resource: #{resource_id}" display " ! To proceed, type \"#{resource_id}\" or re-run this command with --confirm #{resource_id}" display display "> ", false if ask.downcase != resource_id display " ! Input did not match #{resource_id}. Aborted." false else true end end end |
#confirm_quote ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/transcriptic/ui.rb', line 105 def confirm_quote display display " ! To accept this quote and begin execution, type \"confirm\" below." display " ", false if ask("> ").downcase != "confirm" display " ! Aborted." false else true end end |
#deprecate(version) ⇒ Object
71 72 73 74 |
# File 'lib/transcriptic/ui.rb', line 71 def deprecate(version) display "!!! DEPRECATION WARNING: This command will be removed in version #{version}" display end |
#display(msg = "", newline = true) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/transcriptic/ui.rb', line 40 def display(msg = "", newline = true) if newline puts(msg) else print(msg) STDOUT.flush end end |
#display_row(row, lengths) ⇒ Object
158 159 160 161 162 163 164 |
# File 'lib/transcriptic/ui.rb', line 158 def display_row(row, lengths) row.zip(lengths).each do |column, length| format = column.is_a?(Fixnum) ? "%#{length}s " : "%-#{length}s " display format % column, false end display end |
#display_table(objects, columns, headers) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/transcriptic/ui.rb', line 145 def display_table(objects, columns, headers) lengths = [] columns.each_with_index do |column, index| header = headers[index] lengths << longest([header].concat(objects.map { |o| o[column].to_s })) end display_row headers, lengths display_row headers.map { |header| "-" * header.length }, lengths objects.each do |row| display_row columns.map { |column| row[column] }, lengths end end |
#error(msg, color = :red) ⇒ Object
86 87 88 89 |
# File 'lib/transcriptic/ui.rb', line 86 def error(msg, color = :red) say msg, color exit 1 end |
#error_with_failure(message) ⇒ Object
247 248 249 250 251 |
# File 'lib/transcriptic/ui.rb', line 247 def error_with_failure() .gsub!(/^ +! */, '') display .split("\n").map { |line| " ! #{line}" }.join("\n") exit 1 end |
#fail(message) ⇒ Object
196 197 198 |
# File 'lib/transcriptic/ui.rb', line 196 def fail() raise Transcriptic::Command::CommandFailed, end |
#format_bytes(amount) ⇒ Object
272 273 274 275 276 277 278 279 |
# File 'lib/transcriptic/ui.rb', line 272 def format_bytes(amount) amount = amount.to_i return '(empty)' if amount == 0 return amount if amount < @@kb return "#{(amount / @@kb).round}k" if amount < @@mb return "#{(amount / @@mb).round}M" if amount < @@gb return "#{(amount / @@gb).round}G" end |
#format_date(date) ⇒ Object
140 141 142 143 |
# File 'lib/transcriptic/ui.rb', line 140 def format_date(date) date = Time.parse(date) if date.is_a?(String) date.strftime("%Y-%m-%d %H:%M %Z") end |
#format_with_bang(message) ⇒ Object
76 77 78 79 |
# File 'lib/transcriptic/ui.rb', line 76 def format_with_bang() return '' if .to_s.strip == "" " ! " + .split("\n").join("\n ! ") end |
#get_terminal_environment ⇒ Object
190 191 192 193 194 |
# File 'lib/transcriptic/ui.rb', line 190 def get_terminal_environment { "TERM" => ENV["TERM"], "COLUMNS" => `tput cols`, "LINES" => `tput lines` } rescue { "TERM" => ENV["TERM"] } end |
#indent(message, indent = 3) ⇒ Object
206 207 208 |
# File 'lib/transcriptic/ui.rb', line 206 def indent(, indent = 3) (" " * indent) + end |
#info(message = '', color = nil, force_new_line = (message.to_s !~ /( |\t)\Z/)) ⇒ Object
24 25 26 |
# File 'lib/transcriptic/ui.rb', line 24 def info( = '', color = nil, force_new_line = (.to_s !~ /( |\t)\Z/)) say(, color, force_new_line) end |
#json_decode(json) ⇒ Object
172 173 174 175 176 |
# File 'lib/transcriptic/ui.rb', line 172 def json_decode(json) Transcriptic::OkJson.decode(json) rescue Transcriptic::OkJson::ParserError nil end |
#json_encode(object) ⇒ Object
166 167 168 169 170 |
# File 'lib/transcriptic/ui.rb', line 166 def json_encode(object) Transcriptic::OkJson.encode(object) rescue Transcriptic::OkJson::ParserError nil end |
#longest(items) ⇒ Object
285 286 287 |
# File 'lib/transcriptic/ui.rb', line 285 def longest(items) items.map { |i| i.to_s.length }.sort.last end |
#mute! ⇒ Object
Mute the output of this instance of UI until #unmute! is called
4 5 6 |
# File 'lib/transcriptic/ui.rb', line 4 def mute! @mute = true end |
#output(message = "") ⇒ Object
237 238 239 240 |
# File 'lib/transcriptic/ui.rb', line 237 def output( = "") return if .to_s.strip == "" display " " + .split("\n").join("\n ") end |
#output_with_arrow(message = "") ⇒ Object
242 243 244 245 |
# File 'lib/transcriptic/ui.rb', line 242 def output_with_arrow( = "") return if .to_s.strip == "" display "====> " + .split("\n").join("\n ") end |
#output_with_bang(message = "", new_line = true) ⇒ Object
81 82 83 84 |
# File 'lib/transcriptic/ui.rb', line 81 def output_with_bang(="", new_line=true) return if .to_s.strip == "" display(format_with_bang(), new_line) end |
#output_with_indent(message, indent = 3) ⇒ Object
210 211 212 213 |
# File 'lib/transcriptic/ui.rb', line 210 def output_with_indent(, indent = 3) return if .to_s.strip == "" display (" " * indent) + .split("\n").join("\n ") end |
#quantify(string, num) ⇒ Object
281 282 283 |
# File 'lib/transcriptic/ui.rb', line 281 def quantify(string, num) "%d %s" % [ num, num.to_i == 1 ? string : "#{string}s" ] end |
#quiet? ⇒ Boolean
13 14 15 |
# File 'lib/transcriptic/ui.rb', line 13 def quiet? @mute end |
#redisplay(line, line_break = false) ⇒ Object
49 50 51 |
# File 'lib/transcriptic/ui.rb', line 49 def redisplay(line, line_break = false) display("\r\e[0K#{line}", line_break) end |
#say(message = '', color = nil, force_new_line = (message.to_s !~ /( |\t)\Z/)) ⇒ Object
17 18 19 20 21 |
# File 'lib/transcriptic/ui.rb', line 17 def say( = '', color = nil, force_new_line = (.to_s !~ /( |\t)\Z/)) return if quiet? super(, color, force_new_line) end |
#say_status(status, message, log_status = true) ⇒ Object
28 29 30 31 32 |
# File 'lib/transcriptic/ui.rb', line 28 def say_status(status, , log_status = true) return if quiet? super(status, , log_status) end |
#set_buffer(enable) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/transcriptic/ui.rb', line 178 def set_buffer(enable) return unless $stdin.tty? if enable `stty icanon echo` else `stty -icanon -echo` end rescue # fails on windows end |
#status(message) ⇒ Object
233 234 235 |
# File 'lib/transcriptic/ui.rb', line 233 def status() @status = end |
#string_distance(first, last) ⇒ Object
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/transcriptic/ui.rb', line 313 def string_distance(first, last) distances = [] # 0x0s 0.upto(first.length) do |index| distances << [index] + [0] * last.length end distances[0] = 0.upto(last.length).to_a 1.upto(last.length) do |last_index| 1.upto(first.length) do |first_index| first_char = first[first_index - 1, 1] last_char = last[last_index - 1, 1] if first_char == last_char distances[first_index][last_index] = distances[first_index - 1][last_index - 1] # noop else distances[first_index][last_index] = [ distances[first_index - 1][last_index], # deletion distances[first_index][last_index - 1], # insertion distances[first_index - 1][last_index - 1] # substitution ].min + 1 # cost if first_index > 1 && last_index > 1 first_previous_char = first[first_index - 2, 1] last_previous_char = last[last_index - 2, 1] if first_char == last_previous_char && first_previous_char == last_char distances[first_index][last_index] = [ distances[first_index][last_index], distances[first_index - 2][last_index - 2] + 1 # transposition ].min end end end end end distances[first.length][last.length] end |
#suggestion(actual, possibilities) ⇒ Object
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/transcriptic/ui.rb', line 347 def suggestion(actual, possibilities) distances = Hash.new {|hash,key| hash[key] = []} possibilities.each do |suggestion| distances[string_distance(actual, suggestion)] << suggestion end minimum_distance = distances.keys.min if minimum_distance < 4 suggestions = distances[minimum_distance].sort if suggestions.length == 1 "Perhaps you meant `#{suggestions.first}`." else "Perhaps you meant #{suggestions[0...-1].map {|suggestion| "`#{suggestion}`"}.join(', ')} or `#{suggestions.last}`." end else nil end end |
#time_ago(elapsed) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/transcriptic/ui.rb', line 53 def time_ago(elapsed) if elapsed < 60 "#{elapsed.floor}s ago" elsif elapsed < (60 * 60) "#{(elapsed / 60).floor}m ago" else "#{(elapsed / 60 / 60).floor}h ago" end end |
#truncate(text, length) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/transcriptic/ui.rb', line 63 def truncate(text, length) if text.size > length text[0, length - 2] + '..' else text end end |
#unmute! ⇒ Object
Unmute the output of this instance of UI until #mute! is called
9 10 11 |
# File 'lib/transcriptic/ui.rb', line 9 def unmute! @mute = false end |
#wait_spinner(message, fps = 10) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/transcriptic/ui.rb', line 215 def wait_spinner(, fps = 10) chars = %w[| / - \\] delay = 1.0 / fps iter = 0 display arrow(), false spinner = Thread.new do while iter do # Keep spinning until told otherwise print chars[(iter+=1) % chars.length] sleep delay print "\b" end end yield.tap { # After yielding to the block, save the return value iter = false # Tell the thread to exit, cleaning up after itself… spinner.join # …and wait for it to do so. } # Use the block's return value as the method's end |
#warn(message, color = :yellow) ⇒ Object
34 35 36 37 38 |
# File 'lib/transcriptic/ui.rb', line 34 def warn(, color = :yellow) return if quiet? say(, color) end |