Module: Nimbu::Helpers
- Included in:
- Auth, Client, Client, Command, Command::Base
- Defined in:
- lib/nimbu/helpers.rb,
lib/nimbu/command/helpers.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
-
#action(message) ⇒ Object
DISPLAY HELPERS.
- #ask ⇒ Object
- #confirm(message = "Are you sure you wish to continue? (y/n)?") ⇒ Object
- #confirm_billing ⇒ Object
- #confirm_command(app_to_confirm = app, message = nil) ⇒ Object
- #create_git_remote(remote, url) ⇒ Object
- #deprecate(version) ⇒ Object
- #display(msg = "", new_line = true) ⇒ Object
- #display_header(message = "", new_line = true) ⇒ Object
- #display_object(object) ⇒ Object
- #display_row(row, lengths) ⇒ Object
- #display_table(objects, columns, headers) ⇒ Object
- #error(msg) ⇒ 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
- #git(args) ⇒ Object
- #has_git? ⇒ Boolean
- #home_directory ⇒ Object
- #hprint(string = '') ⇒ Object
- #hputs(string = '') ⇒ Object
- #json_decode(json) ⇒ Object
- #json_encode(object) ⇒ Object
- #longest(items) ⇒ Object
- #output(message = "", new_line = true) ⇒ Object
- #output_with_arrow(message = "", new_line = true) ⇒ Object
- #output_with_bang(message = "", new_line = true) ⇒ Object
- #quantify(string, num) ⇒ Object
- #redisplay(line, line_break = false) ⇒ Object
- #retry_on_exception(*exceptions) ⇒ Object
- #run_command(command, args = []) ⇒ Object
- #running_on_a_mac? ⇒ Boolean
- #running_on_windows? ⇒ Boolean
- #set_buffer(enable) ⇒ Object
- #shell(cmd) ⇒ Object
- #status(message) ⇒ Object
- #string_distance(first, last) ⇒ Object
- #time_ago(elapsed) ⇒ Object
- #truncate(text, length) ⇒ Object
- #with_tty(&block) ⇒ Object
Class Method Details
.disable_error_capture ⇒ Object
299 300 301 302 303 304 305 306 307 308 |
# File 'lib/nimbu/helpers.rb', line 299 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
286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/nimbu/helpers.rb', line 286 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
282 283 284 |
# File 'lib/nimbu/helpers.rb', line 282 def self.extended(base) extended_into << base end |
.extended_into ⇒ Object
274 275 276 |
# File 'lib/nimbu/helpers.rb', line 274 def self.extended_into @@extended_into ||= [] end |
.included(base) ⇒ Object
278 279 280 |
# File 'lib/nimbu/helpers.rb', line 278 def self.included(base) included_into << base end |
.included_into ⇒ Object
270 271 272 |
# File 'lib/nimbu/helpers.rb', line 270 def self.included_into @@included_into ||= [] end |
Instance Method Details
#action(message) ⇒ Object
DISPLAY HELPERS
230 231 232 233 234 235 236 237 238 |
# File 'lib/nimbu/helpers.rb', line 230 def action() output_with_arrow("#{}... ", false) Nimbu::Helpers.enable_error_capture yield Nimbu::Helpers.disable_error_capture display "done", false display(", #{@status}", false) if @status display end |
#ask ⇒ Object
85 86 87 |
# File 'lib/nimbu/helpers.rb', line 85 def ask STDIN.gets.strip end |
#confirm(message = "Are you sure you wish to continue? (y/n)?") ⇒ Object
51 52 53 54 |
# File 'lib/nimbu/helpers.rb', line 51 def confirm(="Are you sure you wish to continue? (y/n)?") display("#{} ", false) ask.downcase == 'y' end |
#confirm_billing ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/nimbu/helpers.rb', line 40 def confirm_billing display display "This action will cause your account to be billed at the end of the month" display "For more information, see http://devcenter.nimbu.com/articles/billing" display "Are you sure you want to do this? (y/n) ", false if ask.downcase == 'y' nimbu.confirm_billing return true end end |
#confirm_command(app_to_confirm = app, message = nil) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/nimbu/helpers.rb', line 56 def confirm_command(app_to_confirm = app, =nil) raise(Nimbu::Command::CommandFailed, "No app specified.\nRun this command from app folder or set it adding --app <app name>") unless app_to_confirm if respond_to?(:extract_option) && confirmed_app = extract_option('--confirm', false) unless confirmed_app == app_to_confirm raise(Nimbu::Command::CommandFailed, "Confirmed app #{confirmed_app} did not match the selected app #{app_to_confirm}.") end return true else display ||= "WARNING: Potentially Destructive Action\nThis command will affect the app: #{app_to_confirm}" << "\nTo proceed, type \"#{app_to_confirm}\" or re-run this command with --confirm #{app_to_confirm}" output_with_bang() display display "> ", false if ask.downcase != app_to_confirm output_with_bang "Input did not match #{app_to_confirm}. Aborted." false else true end end end |
#create_git_remote(remote, url) ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/nimbu/helpers.rb', line 154 def create_git_remote(remote, url) return unless has_git? return if git('remote').split("\n").include?(remote) return unless File.exists?(".git") git "remote add #{remote} #{url}" display "Git remote #{remote} added" end |
#deprecate(version) ⇒ Object
30 31 32 33 |
# File 'lib/nimbu/helpers.rb', line 30 def deprecate(version) display "!!! DEPRECATION WARNING: This command will be removed in version #{version}" display end |
#display(msg = "", new_line = true) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/nimbu/helpers.rb', line 17 def display(msg="", new_line=true) if new_line puts(msg) else print(msg) STDOUT.flush end end |
#display_header(message = "", new_line = true) ⇒ Object
311 312 313 314 |
# File 'lib/nimbu/helpers.rb', line 311 def display_header(="", new_line=true) return if .to_s.strip == "" display("=== " + .to_s.split("\n").join("\n=== "), new_line) end |
#display_object(object) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/nimbu/helpers.rb', line 316 def display_object(object) case object when Array # list of objects object.each do |item| display_object(item) end when Hash # if all values are arrays, it is a list with headers # otherwise it is a single header with pairs of data if object.values.all? {|value| value.is_a?(Array)} object.keys.sort_by {|key| key.to_s}.each do |key| display_header(key) display_object(object[key]) hputs end end else hputs(object.to_s) end end |
#display_row(row, lengths) ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/nimbu/helpers.rb', line 179 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
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/nimbu/helpers.rb', line 166 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 lengths.map { |length| "-" * length }, lengths objects.each do |row| display_row columns.map { |column| row[column] }, lengths end end |
#error(msg) ⇒ Object
35 36 37 38 |
# File 'lib/nimbu/helpers.rb', line 35 def error(msg) STDERR.puts(format_with_bang(msg)) exit 1 end |
#error_with_failure(message) ⇒ Object
264 265 266 267 268 |
# File 'lib/nimbu/helpers.rb', line 264 def error_with_failure() display "failed" output_with_bang() exit 1 end |
#fail(message) ⇒ Object
224 225 226 |
# File 'lib/nimbu/helpers.rb', line 224 def fail() raise Nimbu::Command::CommandFailed, end |
#format_bytes(amount) ⇒ Object
141 142 143 144 145 146 147 148 |
# File 'lib/nimbu/helpers.rb', line 141 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
80 81 82 83 |
# File 'lib/nimbu/helpers.rb', line 80 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
254 255 256 257 |
# File 'lib/nimbu/helpers.rb', line 254 def format_with_bang() return '' if .to_s.strip == "" " ! " + .split("\n").join("\n ! ") end |
#get_terminal_environment ⇒ Object
218 219 220 221 222 |
# File 'lib/nimbu/helpers.rb', line 218 def get_terminal_environment { "TERM" => ENV["TERM"], "COLUMNS" => `tput cols`, "LINES" => `tput lines` } rescue { "TERM" => ENV["TERM"] } end |
#git(args) ⇒ Object
114 115 116 117 118 |
# File 'lib/nimbu/helpers.rb', line 114 def git(args) return "" unless has_git? flattened_args = [args].flatten.compact.join(" ") %x{ git #{flattened_args} 2>&1 }.strip end |
#has_git? ⇒ Boolean
109 110 111 112 |
# File 'lib/nimbu/helpers.rb', line 109 def has_git? %x{ git --version } $?.success? end |
#home_directory ⇒ Object
5 6 7 |
# File 'lib/nimbu/helpers.rb', line 5 def home_directory running_on_windows? ? ENV['USERPROFILE'].gsub("\\","/") : ENV['HOME'] end |
#hprint(string = '') ⇒ Object
342 343 344 345 |
# File 'lib/nimbu/helpers.rb', line 342 def hprint(string='') Kernel.print(string) STDOUT.flush end |
#hputs(string = '') ⇒ Object
338 339 340 |
# File 'lib/nimbu/helpers.rb', line 338 def hputs(string='') Kernel.puts(string) end |
#json_decode(json) ⇒ Object
193 194 195 196 197 |
# File 'lib/nimbu/helpers.rb', line 193 def json_decode(json) Nimbu::OkJson.decode(json) rescue Nimbu::OkJson::ParserError nil end |
#json_encode(object) ⇒ Object
187 188 189 190 191 |
# File 'lib/nimbu/helpers.rb', line 187 def json_encode(object) Nimbu::OkJson.encode(object) rescue Nimbu::OkJson::ParserError nil end |
#longest(items) ⇒ Object
162 163 164 |
# File 'lib/nimbu/helpers.rb', line 162 def longest(items) items.map { |i| i.to_s.length }.sort.last end |
#output(message = "", new_line = true) ⇒ Object
244 245 246 247 |
# File 'lib/nimbu/helpers.rb', line 244 def output(="", new_line=true) return if .to_s.strip == "" display(" " + .split("\n").join("\n "), new_line) end |
#output_with_arrow(message = "", new_line = true) ⇒ Object
249 250 251 252 |
# File 'lib/nimbu/helpers.rb', line 249 def output_with_arrow(="", new_line=true) return if .to_s.strip == "" display("----> " + .split("\n").join("\n "), new_line) end |
#output_with_bang(message = "", new_line = true) ⇒ Object
259 260 261 262 |
# File 'lib/nimbu/helpers.rb', line 259 def output_with_bang(="", new_line=true) return if .to_s.strip == "" display(format_with_bang(), new_line) end |
#quantify(string, num) ⇒ Object
150 151 152 |
# File 'lib/nimbu/helpers.rb', line 150 def quantify(string, num) "%d %s" % [ num, num.to_i == 1 ? string : "#{string}s" ] end |
#redisplay(line, line_break = false) ⇒ Object
26 27 28 |
# File 'lib/nimbu/helpers.rb', line 26 def redisplay(line, line_break = false) display("\r\e[0K#{line}", line_break) end |
#retry_on_exception(*exceptions) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/nimbu/helpers.rb', line 97 def retry_on_exception(*exceptions) retry_count = 0 begin yield rescue *exceptions => ex raise ex if retry_count >= 3 sleep 3 retry_count += 1 retry end end |
#run_command(command, args = []) ⇒ Object
93 94 95 |
# File 'lib/nimbu/helpers.rb', line 93 def run_command(command, args=[]) Nimbu::Command.run(command, args) end |
#running_on_a_mac? ⇒ Boolean
13 14 15 |
# File 'lib/nimbu/helpers.rb', line 13 def running_on_a_mac? RUBY_PLATFORM =~ /-darwin\d/ end |
#running_on_windows? ⇒ Boolean
9 10 11 |
# File 'lib/nimbu/helpers.rb', line 9 def running_on_windows? RUBY_PLATFORM =~ /mswin32|mingw32/ end |
#set_buffer(enable) ⇒ Object
199 200 201 202 203 204 205 206 207 |
# File 'lib/nimbu/helpers.rb', line 199 def set_buffer(enable) with_tty do if enable `stty icanon echo` else `stty -icanon -echo` end end end |
#shell(cmd) ⇒ Object
89 90 91 |
# File 'lib/nimbu/helpers.rb', line 89 def shell(cmd) FileUtils.cd(Dir.pwd) {|d| return `#{cmd}`} end |
#status(message) ⇒ Object
240 241 242 |
# File 'lib/nimbu/helpers.rb', line 240 def status() @status = end |
#string_distance(first, last) ⇒ Object
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/nimbu/helpers.rb', line 347 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 |
#time_ago(elapsed) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/nimbu/helpers.rb', line 120 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
130 131 132 133 134 135 136 |
# File 'lib/nimbu/helpers.rb', line 130 def truncate(text, length) if text.size > length text[0, length - 2] + '..' else text end end |
#with_tty(&block) ⇒ Object
209 210 211 212 213 214 215 216 |
# File 'lib/nimbu/helpers.rb', line 209 def with_tty(&block) return unless $stdin.tty? begin yield rescue # fails on windows end end |