summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2016-06-22 01:23:06 -0700
committerelijah <elijah@riseup.net>2016-06-22 01:23:06 -0700
commitcc010ad95dda44d88120303e138b93cb4216f40c (patch)
tree438457c2a07abd4534d74faaee0186599a05d4ca
parent7818abd6a89a882cabb3d2a14a572308d9e959ff (diff)
leap_cli: removed dependency on gems 'paint' and 'command_line_reporter'
-rw-r--r--lib/leap_cli/commands/list.rb97
1 files changed, 68 insertions, 29 deletions
diff --git a/lib/leap_cli/commands/list.rb b/lib/leap_cli/commands/list.rb
index aa425432..bfb07808 100644
--- a/lib/leap_cli/commands/list.rb
+++ b/lib/leap_cli/commands/list.rb
@@ -1,5 +1,3 @@
-require 'command_line_reporter'
-
module LeapCli; module Commands
desc 'List nodes and their classifications'
@@ -18,7 +16,7 @@ module LeapCli; module Commands
# don't rely on default manager(), because we want to pass custom options to load()
manager = LeapCli::Config::Manager.new
if global_options[:color]
- colors = ['cyan', 'white']
+ colors = [:cyan, nil]
else
colors = [nil, nil]
end
@@ -62,8 +60,58 @@ module LeapCli; module Commands
puts
end
- class TagTable
- include CommandLineReporter
+ class Table
+ def table
+ @rows = []
+ @row_options = []
+ @column_widths = [20] # first column at least 20
+ @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=nil)
+ @rows[@current_row] ||= []
+ @rows[@current_row][@current_column] = str
+ @column_widths[@current_column] = [str.length, @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
+
+ class TagTable < Table
def initialize(heading, tag_list, colors)
@heading = heading
@tag_list = tag_list
@@ -71,29 +119,24 @@ module LeapCli; module Commands
end
def run
tags = @tag_list.keys.select{|tag| tag !~ /^_/}.sort # sorted list of tags, excluding _partials
- max_width = [20, (tags+[@heading]).inject(0) {|max,i| [i.size,max].max}].max
- table :border => false do
- row :color => @colors[0] do
- column @heading, :align => 'right', :width => max_width
- column "NODES", :width => HighLine::SystemExtensions.terminal_size.first - max_width - 2, :padding => 2
+ table do
+ row(color: @colors[0]) do
+ column @heading, align: 'right'
+ column "NODES"
end
tags.each do |tag|
next if @tag_list[tag].node_list.empty?
- row :color => @colors[1] do
+ row(color: @colors[1]) do
column tag
column @tag_list[tag].node_list.keys.sort.join(', ')
end
end
end
- vertical_spacing
+ draw_table
end
end
- #
- # might be handy: HighLine::SystemExtensions.terminal_size.first
- #
- class NodeTable
- include CommandLineReporter
+ class NodeTable < Table
def initialize(node_list, colors)
@node_list = node_list
@colors = colors
@@ -103,29 +146,25 @@ module LeapCli; module Commands
[node_name, @node_list[node_name].services.sort.join(', '), @node_list[node_name].tags.sort.join(', ')]
end
unless rows.any?
- puts Paint["no results", :red]
+ puts " = " + LeapCli.logger.colorize("no results", :red)
puts
return
end
- padding = 2
- max_node_width = [20, (rows.map{|i|i[0]} + ["NODES"] ).inject(0) {|max,i| [i.size,max].max}].max
- max_service_width = (rows.map{|i|i[1]} + ["SERVICES"]).inject(0) {|max,i| [i.size+padding+padding,max].max}
- max_tag_width = (rows.map{|i|i[2]} + ["TAGS"] ).inject(0) {|max,i| [i.size,max].max}
- table :border => false do
- row :color => @colors[0] do
- column "NODES", :align => 'right', :width => max_node_width
- column "SERVICES", :width => max_service_width, :padding => 2
- column "TAGS", :width => max_tag_width
+ table do
+ row(color: @colors[0]) do
+ column "NODES", align: 'right'
+ column "SERVICES"
+ column "TAGS"
end
rows.each do |r|
- row :color => @colors[1] do
+ row(color: @colors[1]) do
column r[0]
column r[1]
column r[2]
end
end
end
- vertical_spacing
+ draw_table
end
end