Merge remote-tracking branch 'cli/master' into pr-count

This commit is contained in:
UmairShahzad 2020-02-27 00:11:38 +05:00
commit b8a7c87142
28 changed files with 907 additions and 215 deletions

View file

@ -14,13 +14,17 @@ var checkedTerminal = false
func isStdoutTerminal() bool {
if !checkedTerminal {
fd := os.Stdout.Fd()
_isStdoutTerminal = isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)
_isStdoutTerminal = IsTerminal(os.Stdout)
checkedTerminal = true
}
return _isStdoutTerminal
}
// IsTerminal reports whether the file descriptor is connected to a terminal
func IsTerminal(f *os.File) bool {
return isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd())
}
// NewColorable returns an output stream that handles ANSI color sequences on Windows
func NewColorable(f *os.File) io.Writer {
return colorable.NewColorable(f)

View file

@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"github.com/cli/cli/pkg/text"
"github.com/mattn/go-isatty"
"golang.org/x/crypto/ssh/terminal"
)
@ -21,6 +22,7 @@ type TablePrinter interface {
func NewTablePrinter(w io.Writer) TablePrinter {
if outFile, isFile := w.(*os.File); isFile {
// TODO: use utils.IsTerminal()
isCygwin := isatty.IsCygwinTerminal(outFile.Fd())
if isatty.IsTerminal(outFile.Fd()) || isCygwin {
ttyWidth := 80
@ -62,16 +64,16 @@ func (t ttyTablePrinter) IsTTY() bool {
return true
}
func (t *ttyTablePrinter) AddField(text string, truncateFunc func(int, string) string, colorFunc func(string) string) {
func (t *ttyTablePrinter) AddField(s string, truncateFunc func(int, string) string, colorFunc func(string) string) {
if truncateFunc == nil {
truncateFunc = truncate
truncateFunc = text.Truncate
}
if t.rows == nil {
t.rows = [][]tableField{[]tableField{}}
}
rowI := len(t.rows) - 1
field := tableField{
Text: text,
Text: s,
TruncateFunc: truncateFunc,
ColorFunc: colorFunc,
}
@ -92,7 +94,7 @@ func (t *ttyTablePrinter) Render() error {
// measure maximum content width per column
for _, row := range t.rows {
for col, field := range row {
textLen := len(field.Text)
textLen := text.DisplayWidth(field.Text)
if textLen > colWidths[col] {
colWidths[col] = textLen
}
@ -128,7 +130,9 @@ func (t *ttyTablePrinter) Render() error {
truncVal := field.TruncateFunc(colWidths[col], field.Text)
if col < numCols-1 {
// pad value with spaces on the right
truncVal = fmt.Sprintf("%-*s", colWidths[col], truncVal)
if padWidth := colWidths[col] - text.DisplayWidth(field.Text); padWidth > 0 {
truncVal += strings.Repeat(" ", padWidth)
}
}
if field.ColorFunc != nil {
truncVal = field.ColorFunc(truncVal)
@ -173,13 +177,3 @@ func (t *tsvTablePrinter) EndRow() {
func (t *tsvTablePrinter) Render() error {
return nil
}
func truncate(maxLength int, title string) string {
if len(title) > maxLength {
if maxLength > 3 {
return title[0:maxLength-3] + "..."
}
return title[0:maxLength]
}
return title
}

View file

@ -63,7 +63,7 @@ func FuzzyAgo(ago time.Duration) string {
return fmtDuration(int(ago.Hours()/24/365), "year")
}
func GetTitle(cmd *cobra.Command, cmdType string, limit int, matchCount int, baseRepo *ghrepo.Interface) string {
func GetTitle(cmd *cobra.Command, cmdType string, limit int, matchCount int, baseRepo ghrepo.Interface) string {
userSetFlagCounter := 0
limitSet := false
@ -81,14 +81,14 @@ func GetTitle(cmd *cobra.Command, cmdType string, limit int, matchCount int, bas
if userSetFlagCounter > 0 {
msg = fmt.Sprintf("No %ss match your search", cmdType)
}
return fmt.Sprintf(title, msg, ghrepo.FullName(*baseRepo))
return fmt.Sprintf(title, msg, ghrepo.FullName(baseRepo))
}
if (!limitSet && userSetFlagCounter > 0) || (userSetFlagCounter > 1) {
title = "\n%s match your search in %s\n\n"
}
out := fmt.Sprintf(title, Pluralize(matchCount, cmdType), ghrepo.FullName(*baseRepo))
out := fmt.Sprintf(title, Pluralize(matchCount, cmdType), ghrepo.FullName(baseRepo))
if limit < matchCount {
out = out + fmt.Sprintln(Gray(fmt.Sprintf("Showing %d/%d results\n", limit, matchCount)))