1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
package utils
import (
"io"
"strings"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/go-gh/pkg/tableprinter"
)
type TablePrinter interface {
IsTTY() bool
AddField(string, func(int, string) string, func(string) string)
EndRow()
Render() error
}
type TablePrinterOptions struct {
IsTTY bool
MaxWidth int
Out io.Writer
}
// Deprecated: use internal/tableprinter
func NewTablePrinter(io *iostreams.IOStreams) TablePrinter {
return NewTablePrinterWithOptions(io, TablePrinterOptions{
IsTTY: io.IsStdoutTTY(),
})
}
// Deprecated: use internal/tableprinter
func NewTablePrinterWithOptions(ios *iostreams.IOStreams, opts TablePrinterOptions) TablePrinter {
var out io.Writer
if opts.Out != nil {
out = opts.Out
} else {
out = ios.Out
}
var maxWidth int
if opts.IsTTY {
if opts.MaxWidth > 0 {
maxWidth = opts.MaxWidth
} else {
maxWidth = ios.TerminalWidth()
}
}
tp := tableprinter.New(out, opts.IsTTY, maxWidth)
return &printer{
tp: tp,
isTTY: opts.IsTTY,
}
}
type printer struct {
tp tableprinter.TablePrinter
colIndex int
isTTY bool
}
func (p printer) IsTTY() bool {
return p.isTTY
}
func (p *printer) AddField(s string, truncateFunc func(int, string) string, colorFunc func(string) string) {
if truncateFunc == nil {
// Disallow ever truncating the 1st colum or any URL value
if p.colIndex == 0 || isURL(s) {
p.tp.AddField(s, tableprinter.WithTruncate(nil), tableprinter.WithColor(colorFunc))
} else {
p.tp.AddField(s, tableprinter.WithColor(colorFunc))
}
} else {
p.tp.AddField(s, tableprinter.WithTruncate(truncateFunc), tableprinter.WithColor(colorFunc))
}
p.colIndex++
}
func (p *printer) EndRow() {
p.tp.EndRow()
p.colIndex = 0
}
func (p *printer) Render() error {
return p.tp.Render()
}
func isURL(s string) bool {
return strings.HasPrefix(s, "https://") || strings.HasPrefix(s, "http://")
}
|