summaryrefslogtreecommitdiff
path: root/lib/leap_cli/util
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2016-07-09 02:48:27 -0700
committerelijah <elijah@riseup.net>2016-08-23 13:35:13 -0700
commit760aa6e1b0d7dd1764387c05f638c886745c55e7 (patch)
treef033c951641ec438309d8f8ac52465853144d69c /lib/leap_cli/util
parent8c207687e8dfa72f42f25cac7f46b99f895e4f57 (diff)
move console table into separate file
Diffstat (limited to 'lib/leap_cli/util')
-rw-r--r--lib/leap_cli/util/console_table.rb55
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/leap_cli/util/console_table.rb b/lib/leap_cli/util/console_table.rb
new file mode 100644
index 00000000..53c5e18a
--- /dev/null
+++ b/lib/leap_cli/util/console_table.rb
@@ -0,0 +1,55 @@
+module LeapCli; module Util
+
+ class ConsoleTable
+ def table
+ @rows = []
+ @row_options = []
+ @column_widths = []
+ @column_options = []
+ @current_row = 0
+ @current_column = 0
+ yield
+ end
+
+ def row(options=nil)
+ @current_column = 0
+ @row_options[@current_row] ||= options
+ yield
+ @current_row += 1
+ end
+
+ def column(str, options={})
+ str ||= ""
+ @rows[@current_row] ||= []
+ @rows[@current_row][@current_column] = str
+ @column_widths[@current_column] = [str.length, options[:min_width]||0, @column_widths[@current_column]||0].max
+ @column_options[@current_column] ||= options
+ @current_column += 1
+ end
+
+ def draw_table
+ @rows.each_with_index do |row, i|
+ color = (@row_options[i]||{})[:color]
+ row.each_with_index do |column, j|
+ align = (@column_options[j]||{})[:align] || "left"
+ width = @column_widths[j]
+ if color
+ str = LeapCli.logger.colorize(column, color)
+ extra_width = str.length - column.length
+ else
+ str = column
+ extra_width = 0
+ end
+ if align == "right"
+ printf " %#{width+extra_width}s" % str
+ else
+ printf " %-#{width+extra_width}s" % str
+ end
+ end
+ puts
+ end
+ puts
+ end
+ end
+
+end; end \ No newline at end of file