git.sophuwu.com > statlog
fixed too narrow error malformed
sophuwu sophie@sophuwu.com
Sat, 06 Dec 2025 03:19:38 +0100
commit

7eb41a0b8808de31f26c04c084a672af9e1e8831

parent

a9e189866cf29d772e2c694b759f1160dd3a0c4b

2 files changed, 25 insertions(+), 17 deletions(-)

jump to
M cmd/main.gocmd/main.go

@@ -5,6 +5,7 @@ "errors"

"fmt" "os" "os/signal" + "time" "git.sophuwu.com/statlog" "git.sophuwu.com/statlog/types"

@@ -43,6 +44,16 @@ var w int

prnt := func() { fmt.Printf("\033[2J\033[1;1H\r%s\n", s) } + ERR := func() bool { + if errors.Is(e, types.ErrTooNarrow) { + s = "terminal too narrow" + prnt() + time.Sleep(100 * time.Millisecond) + return true + } + fatal(e) + return false + } for bl { w, _ = types.TermSize() s = ""

@@ -51,15 +62,15 @@ w, _ = types.TermSize()

// mem ss, e = hw.MEM.Bar() - if errors.Is(e, types.ErrTooNarrow) { - s = "Terminal too narrow" - prnt() + + if ERR() { continue } - fatal(e) s += "MEM: " + hw.MEM.String() + "\n" + ss + "\n" ss, e = grMem(w, 5, int(hw.MEM.Percent.Used+hw.MEM.Percent.Buff)) - fatal(e) + if ERR() { + continue + } s += ss + "\n" // cpu

@@ -68,29 +79,25 @@ // print cpu info string

s += "CPU: " + hw.CPU.String() + "\n" // make load graph ss, e = grCpu(w, 5, int(hw.CPU.LoadAvg)) - fatal(e) + if ERR() { + continue + } // print load graph s += ss + "\n" // cpu bar graphs // make bar graph ss, e = hw.CPU.LoadAvg.Bar("CPU Avg", w) - if errors.Is(e, types.ErrTooNarrow) { - s = "Terminal too narrow" - prnt() + if ERR() { continue } - fatal(e) // print bar graph s += ss + "\n" // make core bar graphs ss, e = hw.CPU.LoadBar(w) - if errors.Is(e, types.ErrTooNarrow) { - s = "Terminal too narrow" - prnt() + if ERR() { continue } - fatal(e) // print core bar graphs s += ss + "\n"
M device/mem.godevice/mem.go

@@ -3,9 +3,10 @@

import ( "encoding/json" "fmt" - "git.sophuwu.com/statlog/types" "os" "strings" + + "git.sophuwu.com/statlog/types" ) type MEM struct {

@@ -47,7 +48,7 @@

func (m *MEM) Bar() (string, error) { w, _ := types.TermSize() if w < 40 { - return "", fmt.Errorf("terminal too narrow") + return "", types.ErrTooNarrow } s := "MEM: "

@@ -61,7 +62,7 @@

l := len(strings.NewReplacer("\033[1;32m", "", "\033[1;36m", "", "\033[1;35m", "", "\033[0m", "").Replace(s)) w -= l if w < 40 { - return "", fmt.Errorf("terminal too narrow") + return "", types.ErrTooNarrow } w -= 2