|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + |
| 8 | + "golang.org/x/crypto/ssh/terminal" |
| 9 | +) |
| 10 | + |
| 11 | +type TablePrinter interface { |
| 12 | + IsTTY() bool |
| 13 | + AddField(string, func(int, string) string, func(string) string) |
| 14 | + EndRow() |
| 15 | + Render() error |
| 16 | +} |
| 17 | + |
| 18 | +func NewTablePrinter(w io.Writer) TablePrinter { |
| 19 | + if outFile, isFile := w.(*os.File); isFile { |
| 20 | + fd := int(outFile.Fd()) |
| 21 | + if terminal.IsTerminal(fd) { |
| 22 | + ttyWidth := 80 |
| 23 | + if w, _, err := terminal.GetSize(fd); err == nil { |
| 24 | + ttyWidth = w |
| 25 | + } |
| 26 | + return &ttyTablePrinter{ |
| 27 | + out: w, |
| 28 | + maxWidth: ttyWidth, |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + return &tsvTablePrinter{ |
| 33 | + out: w, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +type tableField struct { |
| 38 | + Text string |
| 39 | + TruncateFunc func(int, string) string |
| 40 | + ColorFunc func(string) string |
| 41 | +} |
| 42 | + |
| 43 | +type ttyTablePrinter struct { |
| 44 | + out io.Writer |
| 45 | + maxWidth int |
| 46 | + rows [][]tableField |
| 47 | +} |
| 48 | + |
| 49 | +func (t ttyTablePrinter) IsTTY() bool { |
| 50 | + return true |
| 51 | +} |
| 52 | + |
| 53 | +func (t *ttyTablePrinter) AddField(text string, truncateFunc func(int, string) string, colorFunc func(string) string) { |
| 54 | + if truncateFunc == nil { |
| 55 | + truncateFunc = truncate |
| 56 | + } |
| 57 | + if t.rows == nil { |
| 58 | + t.rows = [][]tableField{[]tableField{}} |
| 59 | + } |
| 60 | + rowI := len(t.rows) - 1 |
| 61 | + field := tableField{ |
| 62 | + Text: text, |
| 63 | + TruncateFunc: truncateFunc, |
| 64 | + ColorFunc: colorFunc, |
| 65 | + } |
| 66 | + t.rows[rowI] = append(t.rows[rowI], field) |
| 67 | +} |
| 68 | + |
| 69 | +func (t *ttyTablePrinter) EndRow() { |
| 70 | + t.rows = append(t.rows, []tableField{}) |
| 71 | +} |
| 72 | + |
| 73 | +func (t *ttyTablePrinter) Render() error { |
| 74 | + if len(t.rows) == 0 { |
| 75 | + return nil |
| 76 | + } |
| 77 | + |
| 78 | + numCols := len(t.rows[0]) |
| 79 | + colWidths := make([]int, numCols) |
| 80 | + // measure maximum content width per column |
| 81 | + for _, row := range t.rows { |
| 82 | + for col, field := range row { |
| 83 | + textLen := len(field.Text) |
| 84 | + if textLen > colWidths[col] { |
| 85 | + colWidths[col] = textLen |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + delim := " " |
| 91 | + availWidth := t.maxWidth - colWidths[0] - ((numCols - 1) * len(delim)) |
| 92 | + // add extra space from columns that are already narrower than threshold |
| 93 | + for col := 1; col < numCols; col++ { |
| 94 | + availColWidth := availWidth / (numCols - 1) |
| 95 | + if extra := availColWidth - colWidths[col]; extra > 0 { |
| 96 | + availWidth += extra |
| 97 | + } |
| 98 | + } |
| 99 | + // cap all but first column to fit available terminal width |
| 100 | + // TODO: support weighted instead of even redistribution |
| 101 | + for col := 1; col < numCols; col++ { |
| 102 | + availColWidth := availWidth / (numCols - 1) |
| 103 | + if colWidths[col] > availColWidth { |
| 104 | + colWidths[col] = availColWidth |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + for _, row := range t.rows { |
| 109 | + for col, field := range row { |
| 110 | + if col > 0 { |
| 111 | + _, err := fmt.Fprint(t.out, delim) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + } |
| 116 | + truncVal := field.TruncateFunc(colWidths[col], field.Text) |
| 117 | + if col < numCols-1 { |
| 118 | + // pad value with spaces on the right |
| 119 | + truncVal = fmt.Sprintf("%-*s", colWidths[col], truncVal) |
| 120 | + } |
| 121 | + if field.ColorFunc != nil { |
| 122 | + truncVal = field.ColorFunc(truncVal) |
| 123 | + } |
| 124 | + _, err := fmt.Fprint(t.out, truncVal) |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + } |
| 129 | + if len(row) > 0 { |
| 130 | + _, err := fmt.Fprint(t.out, "\n") |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +type tsvTablePrinter struct { |
| 140 | + out io.Writer |
| 141 | + currentCol int |
| 142 | +} |
| 143 | + |
| 144 | +func (t tsvTablePrinter) IsTTY() bool { |
| 145 | + return false |
| 146 | +} |
| 147 | + |
| 148 | +func (t *tsvTablePrinter) AddField(text string, _ func(int, string) string, _ func(string) string) { |
| 149 | + if t.currentCol > 0 { |
| 150 | + fmt.Fprint(t.out, "\t") |
| 151 | + } |
| 152 | + fmt.Fprint(t.out, text) |
| 153 | + t.currentCol++ |
| 154 | +} |
| 155 | + |
| 156 | +func (t *tsvTablePrinter) EndRow() { |
| 157 | + fmt.Fprint(t.out, "\n") |
| 158 | + t.currentCol = 0 |
| 159 | +} |
| 160 | + |
| 161 | +func (t *tsvTablePrinter) Render() error { |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +func truncate(maxLength int, title string) string { |
| 166 | + if len(title) > maxLength { |
| 167 | + return title[0:maxLength-3] + "..." |
| 168 | + } |
| 169 | + return title |
| 170 | +} |
0 commit comments