|
| 1 | +// Package tableprinter facilitates rendering column-formatted data to a terminal and TSV-formatted data to |
| 2 | +// a script or a file. It is suitable for presenting tabular data in a human-readable format that is |
| 3 | +// guaranteed to fit within the given viewport, while at the same time offering the same data in a |
| 4 | +// machine-readable format for scripts. |
| 5 | +package tableprinter |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/mattn/go-runewidth" |
| 13 | +) |
| 14 | + |
| 15 | +type fieldOption func(*tableField) |
| 16 | + |
| 17 | +type TablePrinter interface { |
| 18 | + AddField(string, ...fieldOption) |
| 19 | + EndRow() |
| 20 | + Render() error |
| 21 | +} |
| 22 | + |
| 23 | +// WithTruncate overrides the truncation function for the field. The function should transform a string |
| 24 | +// argument into a string that fits within the given display width. The default behavior is to truncate the |
| 25 | +// value by adding "..." in the end. Pass nil to disable truncation for this value. |
| 26 | +func WithTruncate(fn func(int, string) string) fieldOption { |
| 27 | + return func(f *tableField) { |
| 28 | + f.truncateFunc = fn |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// WithColor sets the color function for the field. The function should transform a string value by wrapping |
| 33 | +// it in ANSI escape codes. The color function will not be used if the table was initialized in non-terminal mode. |
| 34 | +func WithColor(fn func(string) string) fieldOption { |
| 35 | + return func(f *tableField) { |
| 36 | + f.colorFunc = fn |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// New initializes a table printer with terminal mode and terminal width. When terminal mode is enabled, the |
| 41 | +// output will be human-readable, column-formatted to fit available width, and rendered with color support. |
| 42 | +// In non-terminal mode, the output is tab-separated and all truncation of values is disabled. |
| 43 | +func New(w io.Writer, isTTY bool, maxWidth int) TablePrinter { |
| 44 | + if isTTY { |
| 45 | + return &ttyTablePrinter{ |
| 46 | + out: w, |
| 47 | + maxWidth: maxWidth, |
| 48 | + } |
| 49 | + } |
| 50 | + return &tsvTablePrinter{ |
| 51 | + out: w, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +type tableField struct { |
| 56 | + text string |
| 57 | + truncateFunc func(int, string) string |
| 58 | + colorFunc func(string) string |
| 59 | +} |
| 60 | + |
| 61 | +type ttyTablePrinter struct { |
| 62 | + out io.Writer |
| 63 | + maxWidth int |
| 64 | + rows [][]tableField |
| 65 | +} |
| 66 | + |
| 67 | +func (t *ttyTablePrinter) AddField(s string, opts ...fieldOption) { |
| 68 | + if t.rows == nil { |
| 69 | + t.rows = make([][]tableField, 1) |
| 70 | + } |
| 71 | + rowI := len(t.rows) - 1 |
| 72 | + field := tableField{ |
| 73 | + text: s, |
| 74 | + truncateFunc: truncateText, |
| 75 | + } |
| 76 | + for _, opt := range opts { |
| 77 | + opt(&field) |
| 78 | + } |
| 79 | + t.rows[rowI] = append(t.rows[rowI], field) |
| 80 | +} |
| 81 | + |
| 82 | +func (t *ttyTablePrinter) EndRow() { |
| 83 | + t.rows = append(t.rows, []tableField{}) |
| 84 | +} |
| 85 | + |
| 86 | +func (t *ttyTablePrinter) Render() error { |
| 87 | + if len(t.rows) == 0 { |
| 88 | + return nil |
| 89 | + } |
| 90 | + |
| 91 | + delim := " " |
| 92 | + numCols := len(t.rows[0]) |
| 93 | + colWidths := t.calculateColumnWidths(len(delim)) |
| 94 | + |
| 95 | + for _, row := range t.rows { |
| 96 | + for col, field := range row { |
| 97 | + if col > 0 { |
| 98 | + _, err := fmt.Fprint(t.out, delim) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + } |
| 103 | + truncVal := field.text |
| 104 | + if field.truncateFunc != nil { |
| 105 | + truncVal = field.truncateFunc(colWidths[col], field.text) |
| 106 | + } |
| 107 | + if col < numCols-1 { |
| 108 | + // pad value with spaces on the right |
| 109 | + if padWidth := colWidths[col] - displayWidth(field.text); padWidth > 0 { |
| 110 | + truncVal += strings.Repeat(" ", padWidth) |
| 111 | + } |
| 112 | + } |
| 113 | + if field.colorFunc != nil { |
| 114 | + truncVal = field.colorFunc(truncVal) |
| 115 | + } |
| 116 | + _, err := fmt.Fprint(t.out, truncVal) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + } |
| 121 | + if len(row) > 0 { |
| 122 | + _, err := fmt.Fprint(t.out, "\n") |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +func (t *ttyTablePrinter) calculateColumnWidths(delimSize int) []int { |
| 132 | + numCols := len(t.rows[0]) |
| 133 | + maxColWidths := make([]int, numCols) |
| 134 | + colWidths := make([]int, numCols) |
| 135 | + |
| 136 | + for _, row := range t.rows { |
| 137 | + for col, field := range row { |
| 138 | + w := displayWidth(field.text) |
| 139 | + if w > maxColWidths[col] { |
| 140 | + maxColWidths[col] = w |
| 141 | + } |
| 142 | + // if this field has disabled truncating, ensure that the column is wide enough |
| 143 | + if field.truncateFunc == nil && w > colWidths[col] { |
| 144 | + colWidths[col] = w |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + availWidth := func() int { |
| 150 | + setWidths := 0 |
| 151 | + for col := 0; col < numCols; col++ { |
| 152 | + setWidths += colWidths[col] |
| 153 | + } |
| 154 | + return t.maxWidth - delimSize*(numCols-1) - setWidths |
| 155 | + } |
| 156 | + numFixedCols := func() int { |
| 157 | + fixedCols := 0 |
| 158 | + for col := 0; col < numCols; col++ { |
| 159 | + if colWidths[col] > 0 { |
| 160 | + fixedCols++ |
| 161 | + } |
| 162 | + } |
| 163 | + return fixedCols |
| 164 | + } |
| 165 | + |
| 166 | + // set the widths of short columns |
| 167 | + if w := availWidth(); w > 0 { |
| 168 | + if numFlexColumns := numCols - numFixedCols(); numFlexColumns > 0 { |
| 169 | + perColumn := w / numFlexColumns |
| 170 | + for col := 0; col < numCols; col++ { |
| 171 | + if max := maxColWidths[col]; max < perColumn { |
| 172 | + colWidths[col] = max |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + // truncate long columns to the remaining available width |
| 179 | + if numFlexColumns := numCols - numFixedCols(); numFlexColumns > 0 { |
| 180 | + perColumn := availWidth() / numFlexColumns |
| 181 | + for col := 0; col < numCols; col++ { |
| 182 | + if colWidths[col] == 0 { |
| 183 | + if max := maxColWidths[col]; max < perColumn { |
| 184 | + colWidths[col] = max |
| 185 | + } else if perColumn > 0 { |
| 186 | + colWidths[col] = perColumn |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + // add the remainder to truncated columns |
| 193 | + if w := availWidth(); w > 0 { |
| 194 | + for col := 0; col < numCols; col++ { |
| 195 | + d := maxColWidths[col] - colWidths[col] |
| 196 | + toAdd := w |
| 197 | + if d < toAdd { |
| 198 | + toAdd = d |
| 199 | + } |
| 200 | + colWidths[col] += toAdd |
| 201 | + w -= toAdd |
| 202 | + if w <= 0 { |
| 203 | + break |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + return colWidths |
| 209 | +} |
| 210 | + |
| 211 | +type tsvTablePrinter struct { |
| 212 | + out io.Writer |
| 213 | + currentCol int |
| 214 | +} |
| 215 | + |
| 216 | +func (t *tsvTablePrinter) AddField(text string, opts ...fieldOption) { |
| 217 | + if t.currentCol > 0 { |
| 218 | + fmt.Fprint(t.out, "\t") |
| 219 | + } |
| 220 | + fmt.Fprint(t.out, text) |
| 221 | + t.currentCol++ |
| 222 | +} |
| 223 | + |
| 224 | +func (t *tsvTablePrinter) EndRow() { |
| 225 | + fmt.Fprint(t.out, "\n") |
| 226 | + t.currentCol = 0 |
| 227 | +} |
| 228 | + |
| 229 | +func (t *tsvTablePrinter) Render() error { |
| 230 | + return nil |
| 231 | +} |
| 232 | + |
| 233 | +func truncateText(maxWidth int, s string) string { |
| 234 | + if maxWidth < 5 { |
| 235 | + return runewidth.Truncate(s, maxWidth, "") |
| 236 | + } |
| 237 | + return runewidth.Truncate(s, maxWidth, "...") |
| 238 | +} |
| 239 | + |
| 240 | +func displayWidth(s string) int { |
| 241 | + return runewidth.StringWidth(s) |
| 242 | +} |
0 commit comments