From e0596caab1b39b7052050217edf90d285993f117 Mon Sep 17 00:00:00 2001 From: zhaoke Date: Wed, 10 May 2023 19:36:52 +0800 Subject: [PATCH] Support win and mac . --- internal/pkg/helper/exec/report-ztf.go | 6 +-- .../{terminal.go => terminal-darkwin.go} | 12 +++-- pkg/lib/shell/terminal-unix.go | 30 +++++++++++++ pkg/lib/shell/terminal-win.go | 45 +++++++++++++++++++ 4 files changed, 87 insertions(+), 6 deletions(-) rename pkg/lib/shell/{terminal.go => terminal-darkwin.go} (70%) create mode 100644 pkg/lib/shell/terminal-unix.go create mode 100644 pkg/lib/shell/terminal-win.go diff --git a/internal/pkg/helper/exec/report-ztf.go b/internal/pkg/helper/exec/report-ztf.go index 68cae132..291ab845 100644 --- a/internal/pkg/helper/exec/report-ztf.go +++ b/internal/pkg/helper/exec/report-ztf.go @@ -66,7 +66,7 @@ func GenZTFTestReport(report commDomain.ZtfReport, pathMaxWidth int, divider := "--------------------------------" window := shellUtils.WindowSize() if window.Col != 0 { - divider = strings.Repeat("-", int(window.Col)) + divider = strings.Repeat("-", window.Col) } msgFail += divider @@ -93,7 +93,7 @@ func GenZTFTestReport(report commDomain.ZtfReport, pathMaxWidth int, failStr := fmt.Sprintf(fmtStr, i118Utils.Sprintf("fail_num"), report.Fail, float32(failRate)) skipStr := fmt.Sprintf(fmtStr, i118Utils.Sprintf("skip_num"), report.Skip, float32(skipRate)) - if commConsts.ExecFrom == commConsts.FromCmd{ + if commConsts.ExecFrom == commConsts.FromCmd { passStr = fmt.Sprintf(fmtStr, color.New(color.FgGreen).Sprint(i118Utils.Sprintf("pass_num")), report.Pass, float32(passRate)) failStr = fmt.Sprintf(fmtStr, color.New(color.FgRed).Sprint(i118Utils.Sprintf("fail_num")), report.Fail, float32(failRate)) skipStr = fmt.Sprintf(fmtStr, color.New(color.FgYellow).Sprint(i118Utils.Sprintf("skip_num")), report.Skip, float32(skipRate)) @@ -144,7 +144,7 @@ func appendFailedStepResult(cs commDomain.FuncResult, failedSteps *[]string) (pa step.Id = strings.TrimRight(step.Id, ".") status := i118Utils.Sprintf(string(step.Status)) - if commConsts.ExecFrom == commConsts.FromCmd{ + if commConsts.ExecFrom == commConsts.FromCmd { if step.Status == commConsts.FAIL { status = color.New(color.FgRed).Sprint(status) } else if step.Status == commConsts.PASS { diff --git a/pkg/lib/shell/terminal.go b/pkg/lib/shell/terminal-darkwin.go similarity index 70% rename from pkg/lib/shell/terminal.go rename to pkg/lib/shell/terminal-darkwin.go index 0d0f79d0..fd63e086 100644 --- a/pkg/lib/shell/terminal.go +++ b/pkg/lib/shell/terminal-darkwin.go @@ -1,20 +1,26 @@ +//go:build darwin +// +build darwin + package shellUtils import ( + "runtime" "syscall" "unsafe" ) type window struct { - Row uint16 - Col uint16 + Row int + Col int } func WindowSize() window { win := window{0, 0} + tio := syscall.TIOCGWINSZ_OSX + res, _, _ := syscall.Syscall(syscall.SYS_IOCTL, uintptr(syscall.Stdin), - uintptr(syscall.TIOCGWINSZ), //此参数,不同的操作系统可能不一样,例如:TIOCGWINSZ_OSX + uintptr(tio), uintptr(unsafe.Pointer(&win)), ) if int(res) == -1 { diff --git a/pkg/lib/shell/terminal-unix.go b/pkg/lib/shell/terminal-unix.go new file mode 100644 index 00000000..62c1d8d7 --- /dev/null +++ b/pkg/lib/shell/terminal-unix.go @@ -0,0 +1,30 @@ +//go:build linux +// +build linux + +package shellUtils + +import ( + "syscall" + "unsafe" +) + +type window struct { + Row int + Col int +} + +func WindowSize() window { + win := window{0, 0} + tio := syscall.TIOCGWINSZ + + res, _, _ := syscall.Syscall(syscall.SYS_IOCTL, + uintptr(syscall.Stdin), + uintptr(tio), + uintptr(unsafe.Pointer(&win)), + ) + if int(res) == -1 { + return window{0, 0} + } + + return win +} diff --git a/pkg/lib/shell/terminal-win.go b/pkg/lib/shell/terminal-win.go new file mode 100644 index 00000000..43eb8319 --- /dev/null +++ b/pkg/lib/shell/terminal-win.go @@ -0,0 +1,45 @@ +//go:build windows +// +build windows + +package execHelper + +import ( + "fmt" + "os/exec" + "strings" + "syscall" +) + +type window struct { + Row int + Col int +} + +func WindowSize(uuid string) window { + win := window{0, 0} + + cmd1 := exec.Command("cmd") + cmd1.SysProcAttr = &syscall.SysProcAttr{CmdLine: "/c mode con", HideWindow: true} + + out, _ := cmd1.Output() + lines := strings.Split(string(out), "\n") + + for index, line := range lines { + if win.Row > 0 && win.Col > 0 { + return win + } + line = strings.TrimSpace(line) + if strings.Contain("行") || strings.Contain("Row") { + re := regexp.MustCompile(`\d+`) + rs := re.FindAllString(out.String(), -1) + win.Row, _ = strconv.Atoi(rs[1]) + } + if strings.Contain("列") || strings.Contain("Col") { + re := regexp.MustCompile(`\d+`) + rs := re.FindAllString(out.String(), -1) + win.Col, _ = strconv.Atoi(rs[1]) + } + } + + return win +} -- GitLab