@@ -5,23 +5,19 @@ import (
55 "strings"
66)
77
8- // Table - parsed table's header, subheader and format specifier
9- type Table struct {
8+ // Output - parsed table's header, subheader and format specifier
9+ type Output struct {
1010 Header string
1111 SubHeader string
1212 Format string
1313}
1414
15- // Columns - maps short names of columns to their structure defining:
16- // full name, length and whether it's right justified
17- //
18- // For Example:
19- // "env": Column{Name: "Environment", Length: 14},
20- // "cls": Column{Name: "Cluster", Length: 40},
21- // "srv": Column{Name: "Service", Length: 35},
22- // "hst": Column{Name: "Host", Length: 45},
23- // "pct": Column{Name: "%CPU", Length: 7, RightJustified: true},
24- type Columns map [string ]* Column
15+ // Table - maps and orders short names of columns to their structure defining:
16+ // full name, length and whether it's right justified
17+ type Table struct {
18+ Columns map [string ]* Column
19+ order * []string
20+ }
2521
2622// Column - defines column's name, length and if it's right justified
2723type Column struct {
@@ -30,64 +26,88 @@ type Column struct {
3026 RightJustified bool
3127}
3228
33- // New - Creates a map of tabular Columns
34- func New () Columns { return Columns {} }
29+ // All - pass this to Print() or Parse() to print or parse all columns of a table
30+ const All = "*"
31+
32+ // New - Creates a new table
33+ func New () Table {
34+ return Table {
35+ Columns : map [string ]* Column {},
36+ order : & []string {},
37+ }
38+ }
3539
3640// Print - does the following:
3741//
38- // 1) prints a table style heading for a given list of columns.
39- //
40- // For example if Columns are defined as:
42+ // 1) prints a table style heading for a given list of columns,
43+ // for example, if Columns are defined as:
4144//
4245// "env": Column{Name: "Environment", Length: 14},
4346// "cls": Column{Name: "Cluster", Length: 40},
4447// "srv": Column{Name: "Service", Length: 35},
4548//
46- // It'll produce:
49+ // It'll produce:
4750//
4851// Environment Cluster Service
4952// -------------- ---------------------------------------- -----------------------------------
5053//
5154// 2) Returns an fmt style format specifier string that you can use
52- // to output values under the above heading via Printf(format,...):
55+ // to output values under the above heading via Printf(format,...):
5356//
5457// %-14v %-40v %-35v
55- func (cl Columns ) Print (cols ... string ) string {
56- t := cl .parse (cols ... )
58+ func (tbl Table ) Print (cols ... string ) string {
59+ t := tbl .parse (cols ... )
5760 fmt .Println (t .Header )
5861 fmt .Println (t .SubHeader )
5962 return t .Format
6063}
6164
62- // Parse - builds a Table out of a given list of columns
65+ // Parse - constructs Table's Output structure containing it's header,
66+ // sub-header and format modifier out of a given list of columns.
6367//
64- // To simply print the table's title call Print() instead
68+ // To simply print the table's title call Print() instead.
6569//
6670// Parse() is usefull when you need to control where
67- // to output the title, for example to a log or a trace file
68- func (cl Columns ) Parse (cols ... string ) Table {
69- return cl .parse (cols ... )
71+ // to output the title, for example to a log or a trace file.
72+ func (tbl Table ) Parse (cols ... string ) Output {
73+ return tbl .parse (cols ... )
7074}
7175
72- // Col - adds a new column to existing tabular Format
73- func (cl Columns ) Col (shortName , fullName string , columnLength int ) {
74- cl [shortName ] = & Column {Name : fullName , Length : columnLength }
76+ // Col - adds a new column to an existing table
77+ func (tbl Table ) Col (shortName , fullName string , columnLength int ) {
78+ tbl .Columns [shortName ] = & Column {Name : fullName , Length : columnLength }
79+ tbl .appendColumn (shortName )
7580}
7681
77- func (cl Columns ) parse (cols ... string ) Table {
82+ // ColRJ - adds a new Right Justified column to an existing table
83+ func (tbl Table ) ColRJ (shortName , fullName string , columnLength int ) {
84+ tbl .Columns [shortName ] = & Column {Name : fullName , Length : columnLength , RightJustified : true }
85+ tbl .appendColumn (shortName )
86+ }
87+
88+ func (tbl Table ) appendColumn (shortName string ) {
89+ * tbl .order = append (* tbl .order , shortName )
90+ }
91+
92+ func (tbl Table ) parse (cols ... string ) Output {
7893 var header string
7994 var subHeader string
8095 var format string
8196 var space string
97+
98+ if len (cols ) == 1 && cols [0 ] == All {
99+ cols = * tbl .order
100+ }
101+
82102 for _ , c := range cols {
83- cf := cl [c ].f ()
84- header = header + space + fmt .Sprintf (cf , cl [c ].Name )
85- subHeader = subHeader + space + fmt .Sprintf (cf , r (cl [c ].Length ))
103+ cf := tbl . Columns [c ].f ()
104+ header = header + space + fmt .Sprintf (cf , tbl . Columns [c ].Name )
105+ subHeader = subHeader + space + fmt .Sprintf (cf , r (tbl . Columns [c ].Length ))
86106 format = format + space + cf
87107 space = " "
88108 }
89109
90- return Table {
110+ return Output {
91111 Header : header ,
92112 SubHeader : subHeader ,
93113 Format : format + "\n " ,
0 commit comments