From 5ae510ad6f763b0bca752ab1d824ce11176372ea Mon Sep 17 00:00:00 2001 From: x-usr <70479203+x-usr@users.noreply.github.com> Date: Sun, 30 Aug 2020 23:13:04 +0200 Subject: [PATCH] fix(display): adapt padding to match pacman's. This change fixes part of #1332 related to splitting information into multiple lines. Change is implemented based on current pacman's code. --- pkg/text/print.go | 51 +++++++++++++++++++++++++++++++++++++++++++---- print.go | 19 +++++++++--------- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/pkg/text/print.go b/pkg/text/print.go index 1897b3c3..9e1038dd 100644 --- a/pkg/text/print.go +++ b/pkg/text/print.go @@ -3,8 +3,12 @@ package text import ( "fmt" "os" + "strconv" + "strings" + "syscall" "github.com/leonelquinteros/gotext" + "golang.org/x/sys/unix" ) const ( @@ -13,6 +17,8 @@ const ( opSymbol = "::" ) +var cachedColumnCount = -1 + func OperationInfoln(a ...interface{}) { fmt.Fprint(os.Stdout, append([]interface{}{Bold(Cyan(opSymbol + " ")), boldCode}, a...)...) fmt.Fprintln(os.Stdout, ResetCode) @@ -59,10 +65,47 @@ func Errorln(a ...interface{}) { fmt.Fprintln(os.Stderr, append([]interface{}{Bold(Red(smallArrow))}, a...)...) } -func PrintInfoValue(str, value string) { - if value == "" { - value = gotext.Get("None") +func getColumnCount() int { + if cachedColumnCount > 0 { + return cachedColumnCount + } + if count, err := strconv.Atoi(os.Getenv("COLUMNS")); err == nil { + cachedColumnCount = count + return cachedColumnCount + } + if ws, err := unix.IoctlGetWinsize(syscall.Stdout, unix.TIOCGWINSZ); err == nil { + cachedColumnCount = int(ws.Col) + return cachedColumnCount + } + return 80 +} + +func PrintInfoValue(key string, values ...string) { + // 16 (text) + 1 (:) + 1 ( ) + const ( + keyLength = 18 + delimCount = 2 + ) + + str := fmt.Sprintf(Bold("%-16s: "), key) + if len(values) == 0 || (len(values) == 1 && values[0] == "") { + fmt.Fprintf(os.Stdout, "%s%s\n", str, gotext.Get("None")) + return } - fmt.Fprintf(os.Stdout, Bold("%-16s%s")+" %s\n", str, ":", value) + maxCols := getColumnCount() + cols := keyLength + len(values[0]) + str += values[0] + for _, value := range values[1:] { + if maxCols > keyLength && cols+len(value)+delimCount >= maxCols { + cols = keyLength + str += "\n" + strings.Repeat(" ", keyLength) + } else if cols != keyLength { + str += strings.Repeat(" ", delimCount) + cols += delimCount + } + str += value + cols += len(value) + } + fmt.Println(str) } diff --git a/print.go b/print.go index 0fa47929..48867401 100644 --- a/print.go +++ b/print.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "strconv" - "strings" "github.com/leonelquinteros/gotext" rpc "github.com/mikkeloscar/aur" @@ -106,19 +105,19 @@ func (s repoQuery) printSearch(dbExecutor db.Executor) { func PrintInfo(a *rpc.Pkg, extendedInfo bool) { text.PrintInfoValue(gotext.Get("Repository"), "aur") text.PrintInfoValue(gotext.Get("Name"), a.Name) - text.PrintInfoValue(gotext.Get("Keywords"), strings.Join(a.Keywords, " ")) + text.PrintInfoValue(gotext.Get("Keywords"), a.Keywords...) text.PrintInfoValue(gotext.Get("Version"), a.Version) text.PrintInfoValue(gotext.Get("Description"), a.Description) text.PrintInfoValue(gotext.Get("URL"), a.URL) text.PrintInfoValue(gotext.Get("AUR URL"), config.AURURL+"/packages/"+a.Name) - text.PrintInfoValue(gotext.Get("Groups"), strings.Join(a.Groups, " ")) - text.PrintInfoValue(gotext.Get("Licenses"), strings.Join(a.License, " ")) - text.PrintInfoValue(gotext.Get("Provides"), strings.Join(a.Provides, " ")) - text.PrintInfoValue(gotext.Get("Depends On"), strings.Join(a.Depends, " ")) - text.PrintInfoValue(gotext.Get("Make Deps"), strings.Join(a.MakeDepends, " ")) - text.PrintInfoValue(gotext.Get("Check Deps"), strings.Join(a.CheckDepends, " ")) - text.PrintInfoValue(gotext.Get("Optional Deps"), strings.Join(a.OptDepends, " ")) - text.PrintInfoValue(gotext.Get("Conflicts With"), strings.Join(a.Conflicts, " ")) + text.PrintInfoValue(gotext.Get("Groups"), a.Groups...) + text.PrintInfoValue(gotext.Get("Licenses"), a.License...) + text.PrintInfoValue(gotext.Get("Provides"), a.Provides...) + text.PrintInfoValue(gotext.Get("Depends On"), a.Depends...) + text.PrintInfoValue(gotext.Get("Make Deps"), a.MakeDepends...) + text.PrintInfoValue(gotext.Get("Check Deps"), a.CheckDepends...) + text.PrintInfoValue(gotext.Get("Optional Deps"), a.OptDepends...) + text.PrintInfoValue(gotext.Get("Conflicts With"), a.Conflicts...) text.PrintInfoValue(gotext.Get("Maintainer"), a.Maintainer) text.PrintInfoValue(gotext.Get("Votes"), fmt.Sprintf("%d", a.NumVotes)) text.PrintInfoValue(gotext.Get("Popularity"), fmt.Sprintf("%f", a.Popularity))