-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use CLI table utility for printing the CLI output
``` 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
1 parent
9824339
commit 07a7cb9
Showing
4 changed files
with
99 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters