Skip to content

Commit

Permalink
Use CLI table utility for printing the CLI output
Browse files Browse the repository at this point in the history
```
table = CliTable.new(2)
table.right_align(2)
table.header("Name", "Price")
table.record("Coffee", 10)
table.record("Milk", 15)
table.render
```

Example output:

```
Name    Price
Coffee     10
Milk       15
```

Signed-off-by: Aravinda Vishwanathapura <[email protected]>
  • Loading branch information
aravindavk committed Dec 23, 2021
1 parent 9824339 commit 07a7cb9
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 18 deletions.
66 changes: 66 additions & 0 deletions mgr/src/cmds/cli_table.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
enum Align
Left
Right
end

class CliTableException < Exception
end

class CliTable
@rows = [] of Array(String)
@align = [] of Align
@max_widths = [] of Int32
@headers = [] of String

def initialize(@column_count : Int32)
@align = (0...@column_count).map { |_| Align::Left }
@max_widths = (0...@column_count).map { |_| 4 }
end

def left_align(column)
@align[column - 1] = Align::Left
end

def right_align(column)
@align[column - 1] = Align::Right
end

def record(*values)
raise CliTableException.new("Invalid number of Columns") unless values.size == @column_count

vals = [] of String
values.each_with_index do |value, idx|
@max_widths[idx] = "#{value}".size if "#{value}".size > @max_widths[idx]
vals << "#{value}"
end

@rows << vals
end

def header(*values)
raise CliTableException.new("Invalid number of Columns") unless values.size == @column_count
values.each_with_index do |value, idx|
@max_widths[idx] = "#{value}".size if "#{value}".size > @max_widths[idx]
@headers << "#{value}"
end
end

def print_row(values)
fmt = @align.map_with_index do |align, idx|
align_char = align == Align::Left ? "-" : ""
"%#{align_char}#{@max_widths[idx]}s"
end

printf("#{fmt.join(" ")}\n", values)
end

def render
if @rows.size > 0
print_row(@headers)
end

@rows.each do |row|
print_row(row)
end
end
end
12 changes: 8 additions & 4 deletions mgr/src/cmds/nodes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,21 @@ handler "node.list" do |args|
puts "No nodes added to the Pool. Run `kadalu node add #{args.pool_name}/<node-name>` to add a node." if nodes.size == 0

if args.node_args.status
printf("%36s %6s %20s %s\n", "ID", "State", "Name", "Endpoint") if nodes.size > 0
table = CliTable.new(4)
table.header("Name", "ID", "State", "Endpoint")
else
printf("%36s %20s %s\n", "ID", "Name", "Endpoint") if nodes.size > 0
table = CliTable.new(3)
table.header("Name", "ID", "Endpoint")
end

nodes.each do |node|
if args.node_args.status
printf("%36s %6s %20s %s\n", node.id, node.state, node.name, node.endpoint)
table.record(node.name, node.id, node.state, node.endpoint)
else
printf("%36s %20s %s\n", node.id, node.name, node.endpoint)
table.record(node.name, node.id, node.endpoint)
end
end

table.render
end
end
7 changes: 4 additions & 3 deletions mgr/src/cmds/pools.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ handler "pool.list" do |args|

puts "No pools. Run `kadalu pool create <name>` to create a Pool." if pools.size == 0

printf("%36s %s\n", "ID", "Name") if pools.size > 0

table = CliTable.new(2)
table.header("Name", "ID")
pools.each do |pool|
printf("%36s %s\n", pool.id, pool.name)
table.record(pool.name, pool.id)
end
table.render
end
end
32 changes: 21 additions & 11 deletions mgr/src/cmds/volumes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -141,27 +141,37 @@ handler "volume.list" do |args|
next
end

# TODO: Include volume.state
table = CliTable.new(6)
table.right_align(5)
table.right_align(6)
if args.volume_args.status
printf("%36s %20s %10s %20s %20s %s\n", "ID", "Name", "Health", "Type", "Utilization", "Utilization(Inodes)") if volumes.size > 0
table.header("Name", "ID", "State", "Type", "Utilization", "Utilization(Inodes)")
else
printf("%36s %20s %20s %10s %10s\n", "ID", "Name", "Type", "Size", "Inodes") if volumes.size > 0
table.header("Name", "ID", "State", "Type", "Size", "Inodes")
end

volumes.each do |volume|
if args.volume_args.status
printf(
"%36s %20s %10s %20s %20s %s\n", volume.id, volume.name, volume.metrics.health, volume.type,
"#{volume.metrics.size_used_bytes.humanize_bytes}/#{(volume.metrics.size_used_bytes + volume.metrics.size_free_bytes).humanize_bytes}",
"#{volume.metrics.inodes_used_count.humanize}/#{(volume.metrics.inodes_used_count + volume.metrics.inodes_free_count).humanize}",
table.record(
volume.name,
volume.id,
volume.state == "Started" ? "#{volume.state} (#{volume.metrics.health})" : volume.state,
volume.type,
"#{volume.metrics.size_used_bytes.humanize_bytes}/#{volume.metrics.size_bytes.humanize_bytes}",
"#{volume.metrics.inodes_used_count.humanize}/#{volume.metrics.inodes_count.humanize}"
)
else
printf(
"%36s %20s %20s %10s %10s\n", volume.id, volume.name, volume.type,
(volume.metrics.size_used_bytes + volume.metrics.size_free_bytes).humanize_bytes,
(volume.metrics.inodes_used_count + volume.metrics.inodes_free_count).humanize
table.record(
volume.name,
volume.id,
volume.state,
volume.type,
volume.metrics.size_bytes.humanize_bytes,
volume.metrics.inodes_count.humanize
)
end
end

table.render
end
end

0 comments on commit 07a7cb9

Please sign in to comment.