From f972d38ea399e681d9d45b0e881ec7fe1cf91726 Mon Sep 17 00:00:00 2001 From: fcying Date: Sun, 22 Sep 2024 15:48:02 +0800 Subject: [PATCH] fix: decode make output --- .tasks | 2 ++ go.mod | 1 + go.sum | 2 ++ internal/init.go | 23 ++++++++++++++++++++--- internal/util.go | 24 ++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/.tasks b/.tasks index c26a01a..3638405 100644 --- a/.tasks +++ b/.tasks @@ -1,8 +1,10 @@ [build] # command=go run ./cmd/compiledb/main.go -v command=go run ./cmd/compiledb/main.go -v -c --full-path < ./tests/build.log +cwd=$(VIM_ROOT) output=terminal [release] command=go install ./cmd/compiledb +cwd=$(VIM_ROOT) output=terminal diff --git a/go.mod b/go.mod index a19cb60..0b156fa 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.3 require ( github.com/sirupsen/logrus v1.9.3 github.com/urfave/cli/v2 v2.27.3 + golang.org/x/text v0.18.0 ) require ( diff --git a/go.sum b/go.sum index 95a0aae..387afd1 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,8 @@ github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGC github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/internal/init.go b/internal/init.go index 2174f61..fcdc0ac 100644 --- a/internal/init.go +++ b/internal/init.go @@ -92,15 +92,32 @@ func MakeWrap(args []string) { if ParseConfig.NoBuild == false { cmd := exec.Command("make", args...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err := cmd.Run() + // cmd.Stdout = os.Stdout + // cmd.Stderr = os.Stderr + stdout, err := cmd.StdoutPipe() if err != nil { + log.Error("Error:", err) + return + } + stderr, err := cmd.StderrPipe() + if err != nil { + log.Error("Error:", err) + return + } + + if err := cmd.Start(); err != nil { if exitError, ok := err.(*exec.ExitError); ok { StatusCode = exitError.ExitCode() log.Errorf("make failed! errorCode: %d", StatusCode) } } + + go TransferPrintScanner(stdout) + go TransferPrintScanner(stderr) + + if err := cmd.Wait(); err != nil { + log.Error("Error:", err) + } } wg.Wait() diff --git a/internal/util.go b/internal/util.go index 048fcb7..73e47af 100644 --- a/internal/util.go +++ b/internal/util.go @@ -1,9 +1,15 @@ package internal import ( + "bufio" + "fmt" + "io" "os" "os/exec" "strings" + + log "github.com/sirupsen/logrus" + "golang.org/x/text/encoding/simplifiedchinese" ) func FileExist(filename string) bool { @@ -42,3 +48,21 @@ func IsAbsPath(path string) bool { } return false } + +func TransferPrintScanner(in io.ReadCloser) { + decoder := simplifiedchinese.GB18030.NewDecoder() + scanner := bufio.NewScanner(in) + + for scanner.Scan() { + result, err := decoder.String(scanner.Text()) + if err != nil { + log.Error("decode failed!", scanner.Text()) + result = "" + } + fmt.Println(result) + } + + if err := scanner.Err(); err != nil { + log.Error("Error reading scanner:", err) + } +}