-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utils package with pretty table formatting. Eliminate external de…
…pendency
- Loading branch information
IsThisEvenCode
committed
Jun 7, 2024
1 parent
2f117e1
commit 02abb15
Showing
3 changed files
with
166 additions
and
13 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
const maxLineLength = 120 | ||
|
||
type ASCIITableHeader struct { | ||
Name string // name in table header | ||
Field string // attribute name in data row | ||
Alignment string // flag whether column is aligned to the right | ||
Size int // calculated max Size of column | ||
|
||
} | ||
|
||
// ASCIITable creates an ascii table from columns and data rows | ||
func ASCIITable(header []ASCIITableHeader, rows interface{}, escapePipes bool) (string, error) { | ||
dataRows := reflect.ValueOf(rows) | ||
if dataRows.Kind() != reflect.Slice { | ||
return "", fmt.Errorf("rows is not a slice") | ||
} | ||
|
||
err := calculateHeaderSize(header, dataRows, escapePipes) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// output header | ||
out := "" | ||
for _, head := range header { | ||
out += fmt.Sprintf(fmt.Sprintf("| %%-%ds ", head.Size), head.Name) | ||
} | ||
out += "|\n" | ||
|
||
// output separator | ||
for _, head := range header { | ||
padding := " " | ||
out += fmt.Sprintf("|%s%s%s", padding, strings.Repeat("-", head.Size), padding) | ||
} | ||
out += "|\n" | ||
|
||
// output data | ||
for i := range dataRows.Len() { | ||
rowVal := dataRows.Index(i) | ||
for _, head := range header { | ||
value, _ := asciiTableRowValue(escapePipes, rowVal, head) | ||
if head.Alignment == "right" { | ||
out += fmt.Sprintf(fmt.Sprintf("| %%%ds ", head.Size), value) | ||
} else if head.Alignment == "left" || head.Alignment == "" { | ||
out += fmt.Sprintf(fmt.Sprintf("| %%-%ds ", head.Size), value) | ||
} else if head.Alignment == "centered" { | ||
padding := (head.Size - len(value)) / 2 | ||
out += fmt.Sprintf("| %*s%-*s ", padding, "", head.Size-padding, value) | ||
} else { | ||
err := fmt.Errorf("unsupported alignment '%s' in table", head.Alignment) | ||
return "", err | ||
} | ||
} | ||
out += "|\n" | ||
} | ||
|
||
return out, nil | ||
} | ||
|
||
func asciiTableRowValue(escape bool, rowVal reflect.Value, head ASCIITableHeader) (string, error) { | ||
value := "" | ||
field := rowVal.FieldByName(head.Field) | ||
if field.IsValid() { | ||
t := field.Type().String() | ||
switch t { | ||
case "string": | ||
value = field.String() | ||
default: | ||
return "", fmt.Errorf("unsupported struct attribute type for field %s: %s", head.Field, t) | ||
} | ||
} | ||
|
||
if escape { | ||
value = strings.ReplaceAll(value, "\n", `\n`) | ||
value = strings.ReplaceAll(value, "|", "\\|") | ||
value = strings.ReplaceAll(value, "$", "\\$") | ||
value = strings.ReplaceAll(value, "*", "\\*") | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
func calculateHeaderSize(header []ASCIITableHeader, dataRows reflect.Value, escapePipes bool) error { | ||
// set headers as minimum Size | ||
for i, head := range header { | ||
header[i].Size = len(head.Name) | ||
} | ||
|
||
// adjust column Size from max row data | ||
for i := range dataRows.Len() { | ||
rowVal := dataRows.Index(i) | ||
if rowVal.Kind() != reflect.Struct { | ||
return fmt.Errorf("row %d is not a struct", i) | ||
} | ||
for num, head := range header { | ||
value, err := asciiTableRowValue(escapePipes, rowVal, head) | ||
if err != nil { | ||
return err | ||
} | ||
length := len(value) | ||
if length > header[num].Size { | ||
header[num].Size = length | ||
} | ||
} | ||
} | ||
|
||
// calculate total line length | ||
total := 0 | ||
for i := range header { | ||
total += header[i].Size + 3 // add padding | ||
} | ||
|
||
if total < maxLineLength { | ||
return nil | ||
} | ||
|
||
avgAvail := maxLineLength / len(header) | ||
tooWide := []int{} | ||
sumTooWide := 0 | ||
for i := range header { | ||
if header[i].Size > avgAvail { | ||
tooWide = append(tooWide, i) | ||
sumTooWide += header[i].Size | ||
} | ||
} | ||
avgLargeCol := (maxLineLength - (total - sumTooWide)) / len(tooWide) | ||
for _, i := range tooWide { | ||
header[i].Size = avgLargeCol | ||
} | ||
|
||
return nil | ||
} |