Quellcode durchsuchen

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.
x-usr vor 4 Jahren
Ursprung
Commit
5ae510ad6f
2 geänderte Dateien mit 56 neuen und 14 gelöschten Zeilen
  1. 47 4
      pkg/text/print.go
  2. 9 10
      print.go

+ 47 - 4
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
+}
 
-	fmt.Fprintf(os.Stdout, Bold("%-16s%s")+" %s\n", str, ":", value)
+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
+	}
+
+	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)
 }

+ 9 - 10
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))