Module: HmxClient::Helpers
Instance Method Summary collapse
- #display(msg = "", newline = true) ⇒ Object
- #display_row(row, lengths) ⇒ Object
- #display_tab(data) ⇒ Object
- #display_table(objects, columns, headers) ⇒ Object
- #error(msg) ⇒ Object
- #getFromUser(prompt) ⇒ Object
- #longest(items) ⇒ Object
Instance Method Details
#display(msg = "", newline = true) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/hmx/helpers.rb', line 31 def display(msg="", newline=true) if newline puts(msg) else print(msg) STDOUT.flush end end |
#display_row(row, lengths) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/hmx/helpers.rb', line 53 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_tab(data) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/hmx/helpers.rb', line 61 def display_tab(data) resp = '' data.each { | line | line.each { | cell | if (cell.is_a? Array) cell.each { | inner | resp = resp + "%20.20s\t" % inner } else resp = resp + "%20.20s\t" % cell end } resp = resp + "\n" } dout resp end |
#display_table(objects, columns, headers) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/hmx/helpers.rb', line 40 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) ⇒ Object
23 24 25 26 |
# File 'lib/hmx/helpers.rb', line 23 def error(msg) puts(colorize(msg, :red)) exit 1 end |
#getFromUser(prompt) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/hmx/helpers.rb', line 7 def getFromUser(prompt) content = '' done = false puts prompt while(!done) line = gets.strip if (line[line.length-1] == '\\') done = false content = content + line[0 .. line.length-2] else content = content + line done = true end end content end |
#longest(items) ⇒ Object
27 28 29 |
# File 'lib/hmx/helpers.rb', line 27 def longest(items) items.map { |i| i.to_s.length }.sort.last end |