Table Writer
Introduction
This is a simple utility program to print data in a pretty table format to console. This package provides features like
- Simplistic API with full flexibility to mix and match different styles of printing a row
- Can print all data at once as table OR print individual row when needed
- Can position text inside the column to center, right or left. This can be changed on per row basis
Installation
go get github.com/prvn/table_writer
Examples
Initialization
columnHeaders := []string{"Id", "Count", "Timestamp"}
columnMaxWidths := []int{6, 9, 12}
tableWriter, err := table_writer.NewTableWriter(headers, widths)
Printing Table (All at once)
row1 := []string{"1", "11", "1461910429"}
row2 := []string{"2", "12", "1461910429"}
row3 := []string{"1000", "99999", "1461910429"}
tableWriter.PrintTable([][]string{row1, row2, row3}, table_writer.AlignCenter)
will print table like
+------+---------+------------+
| Id | Count | Timestamp |
+------+---------+------------+
| 1 | 11 | 1461910429 |
| 2 | 12 | 1461910429 |
| 1000 | 999999 | 1461910429 |
+------+---------+------------+
Printing Table (Stream data)
Initialize table writer as mentioned in Initialization
tableWriter.PrintHeader()
row1 := []string{"1", "11", "1461910429"}
row2 := []string{"2", "12", "1461910429"}
row3 := []string{"1000", "99999", "1461910429"}
rows := [][]string{row1, row2, row3}
for _, r := range rows {
tableWriter.PrintRow(r, table_writer.AlignCenter)
}
tableWriter.PrintRows(rows, table_writer.AlignCenter)
tableWriter.PrintFooter()
Will print table like
+------+---------+------------+
| Id | Count | Timestamp |
+------+---------+------------+
| 1 | 11 | 1461910429 |
| 2 | 12 | 1461910429 |
| 1000 | 999999 | 1461910429 |
+------+---------+------------+
Print Table & Summarize
Follow steps from above and add following code
summaryRow1 := "Id's: 3"
summaryRow2 := "Count: 1000022"
tableWriter.PrintRowAsOneColumn(summaryRow1, table_writer.AlignRight)
tableWriter.PrintRowAsOneColumn(summaryRow2, table_writer.AlignRight)
tableWriter.PrintFooter()
Will print table like
+------+---------+------------+
| Id | Count | Timestamp |
+------+---------+------------+
| 1 | 11 | 1461910429 |
| 2 | 12 | 1461910429 |
| 1000 | 999999 | 1461910429 |
+------+---------+------------+
|Id's: 3 |
|Count: 1000022 |
+------+---------+------------+