NewWithWriter creates a TablePrinter from a Writer, whether the output is a terminal, the terminal width, and more.
(w io.Writer, isTTY bool, maxWidth int, cs *iostreams.ColorScheme, headers headerOption)
| 56 | |
| 57 | // NewWithWriter creates a TablePrinter from a Writer, whether the output is a terminal, the terminal width, and more. |
| 58 | func NewWithWriter(w io.Writer, isTTY bool, maxWidth int, cs *iostreams.ColorScheme, headers headerOption) *TablePrinter { |
| 59 | tp := &TablePrinter{ |
| 60 | TablePrinter: tableprinter.New(w, isTTY, maxWidth), |
| 61 | isTTY: isTTY, |
| 62 | cs: cs, |
| 63 | } |
| 64 | |
| 65 | if isTTY && len(headers.columns) > 0 { |
| 66 | // Make sure all headers are uppercase, taking a copy of the headers to avoid modifying the original slice. |
| 67 | upperCasedHeaders := make([]string, len(headers.columns)) |
| 68 | for i := range headers.columns { |
| 69 | upperCasedHeaders[i] = strings.ToUpper(headers.columns[i]) |
| 70 | } |
| 71 | |
| 72 | // Make sure all header columns are padded - even the last one. Previously, the last header column |
| 73 | // was not padded. In tests cs.Enabled() is false which allows us to avoid having to fix up |
| 74 | // numerous tests that verify header padding. |
| 75 | var paddingFunc func(int, string) string |
| 76 | if cs.Enabled { |
| 77 | paddingFunc = text.PadRight |
| 78 | } |
| 79 | |
| 80 | tp.AddHeader( |
| 81 | upperCasedHeaders, |
| 82 | WithPadding(paddingFunc), |
| 83 | WithColor(cs.TableHeader), |
| 84 | ) |
| 85 | } |
| 86 | |
| 87 | return tp |
| 88 | } |
| 89 | |
| 90 | // WithHeader defines the column names for a table. |
| 91 | // Panics if columns is nil or empty. |