From 36bc242e9db96e14811736379bbcde5ea7b00bd7 Mon Sep 17 00:00:00 2001 From: eltrac Date: Tue, 26 May 2026 10:00:29 +0800 Subject: fix(tui): showing multiple results --- actions.go | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) (limited to 'actions.go') diff --git a/actions.go b/actions.go index 81d0e63..d672332 100644 --- a/actions.go +++ b/actions.go @@ -1,16 +1,19 @@ package main import ( + "bytes" "fmt" + "io" + "os" "slices" "sort" + "strings" "sync" "github.com/fatih/color" ) func fetchAndDisplay(pkgName string) *statistics { - fmt.Print("\033[s") showLoadingPrompt(fmt.Sprintf("What the heck is \"%s\"", pkgName)) stat := initStat(pkgName) @@ -27,22 +30,26 @@ func fetchAndDisplay(pkgName string) *statistics { var fml *FormulaInfo var cask *CaskInfo var rvs []string - remaining := 3 - for remaining > 0 { + prevLines := 0 + + for i := 0; i < 5; i++ { select { case v := <-fmlChan: fml = v - remaining-- case v := <-caskChan: cask = v - remaining-- case v := <-rvsChan: rvs = v - remaining-- } - fmt.Print("\033[u\033[J") + if i == 0 { + hideLoadingPrompt() + } else { + for j := 0; j < prevLines; j++ { + fmt.Print("\033[F\033[K") + } + } stat = NewStatistics(fml, cask, pkgName, rvs) - stat.Print() + prevLines = capturePrint(stat) } }() wg.Wait() @@ -50,6 +57,32 @@ func fetchAndDisplay(pkgName string) *statistics { return stat } +func capturePrint(s *statistics) int { + r, w, err := os.Pipe() + if err != nil { + s.Print() + return 0 + } + + oldStdout := os.Stdout + oldColor := color.Output + os.Stdout = w + color.Output = w + + s.Print() + + w.Close() + os.Stdout = oldStdout + color.Output = oldColor + + var buf bytes.Buffer + io.Copy(&buf, r) + output := buf.String() + os.Stdout.WriteString(output) + + return strings.Count(output, "\n") +} + func initStat(pkgName string) *statistics { if NoCache { return &statistics{PkgName: pkgName} -- cgit v1.2.3