git.sophuwu.com > statlog
added bar generator for ram
sophuwu sophie@sophuwu.com
Mon, 07 Jul 2025 13:08:14 +0200
commit

6c42af6aaa44ec33efaf89ed924e28c81b8f0b56

parent

4548b5df0ca07c47d68ee1a4aad9cc58c1bcd69d

7 files changed, 170 insertions(+), 79 deletions(-)

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

@@ -7,6 +7,14 @@ )

func main() { hw := &statlog.HWInfo{} - hw.Update() - fmt.Println(hw) + for i := 0; i < 5; i++ { + hw.Update() + s, err := hw.MEM.Bar() + if err != nil { + fmt.Println("\nError generating memory bar:", err) + break + } + fmt.Printf("\r%s", s) + } + fmt.Println() }
M device/mem.godevice/mem.go

@@ -5,6 +5,7 @@ "encoding/json"

"fmt" "git.sophuwu.com/statlog/types" "os" + "strings" ) type MEM struct {

@@ -13,8 +14,9 @@ Free types.Bytes `json:"Free"`

Buffer types.Bytes `json:"Buffer"` Used types.Bytes `json:"Used"` Percent struct { - Used types.Percent `json:"Used"` - WithBuff types.Percent `json:"WithBuff"` + Used types.Percent `json:"Used"` + Buff types.Percent `json:"Buff"` + Free types.Percent `json:"Free"` } `json:"Percent"` }

@@ -34,11 +36,58 @@ m.Used.FromKiB(seeker.GetNum())

m.Used = m.Total - m.Used m.Buffer.FromKiB(seeker.GetNum() + seeker.GetNum()) m.Percent.Used.CalcBytes(m.Used, m.Total) - m.Percent.WithBuff.CalcBytes(m.Used+m.Buffer, m.Total) + m.Percent.Buff.CalcBytes(m.Buffer, m.Total) + m.Percent.Free.CalcBytes(m.Free, m.Total) } func (m *MEM) String() string { - return fmt.Sprintf("%s (%s) USED %s BUFF %s FREE", types.Bytes(m.Used), m.Percent.Used.String(), m.Buffer, m.Free) + return fmt.Sprintf("%s (%s) USED %s BUFF %s FREE", m.Used, m.Percent.Used.Compact(), m.Buffer, m.Free) +} + +func (m *MEM) Bar() (string, error) { + w, _ := types.TermSize() + if w < 40 { + return "", fmt.Errorf("terminal too narrow") + } + + s := "MEM: " + // s += "[\033[1;35m" + m.Used.Human() + " USED\033[0m " + // s += "\033[1;36m" + m.Buffer.Human() + " BUFF\033[0m " + // s += "\033[1;32m" + m.Free.Human() + " FREE\033[0m]" + s += "[\033[1;35mUSED\033[0m/" + s += "\033[1;36mBUFF\033[0m/" + s += "\033[1;32mFREE\033[0m] " + + 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") + } + w -= 2 + + s += "[\033[1;35m" + i, ul := 0, int(m.Percent.Used.Value()*float64(w)/100) + c := m.Percent.Used.Compact() + s += c + for i = len(c); i < w && i < ul; i++ { + s += "|" + } + s += "\033[0m\033[1;36m" + c = m.Percent.Buff.Compact() + s += c + i += len(c) + for ul += int(m.Percent.Buff.Value() * float64(w) / 100); i < w && i < ul; i++ { + s += "|" + } + s += "\033[0m\033[1;32m" + c = m.Percent.Free.Compact() + s += c + i += len(c) + for ; i < w; i++ { + s += "|" + } + s += "\033[0m]" + return s, nil } func (m *MEM) JSON() (string, error) {
M go.modgo.mod

@@ -1,3 +1,7 @@

module git.sophuwu.com/statlog go 1.24.2 + +require golang.org/x/term v0.32.0 + +require golang.org/x/sys v0.33.0 // indirect
A go.sum

@@ -0,0 +1,4 @@

+golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= +golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg= +golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
A types/bytes.go

@@ -0,0 +1,39 @@

+package types + +import ( + "fmt" + "git.sophuwu.com/statlog/types/units" +) + +type Bytes uint64 + +func (b Bytes) Human() string { + n := float64(b) + var i int + for i = units.B; n >= units.KiB && i < units.GiB; i *= units.KiB { + n /= units.KiB + } + + return fmt.Sprintf("%.2f %s", n, units.Labels[i]) +} + +func (b Bytes) HumanSI() string { + n := float64(b) + var i int + for i = units.B; n >= units.KB && i < units.GB; i *= units.KB { + n /= units.KB + } + return fmt.Sprintf("%.2f %s", n, units.Labels[i]) +} + +func (b *Bytes) FromKiB(v uint64) { + *b = Bytes(v * units.KiB) +} + +func (b Bytes) Value() uint64 { + return uint64(b) +} + +func (b Bytes) String() string { + return b.Human() +}
A types/percent.go

@@ -0,0 +1,49 @@

+package types + +import "fmt" + +type Percent float64 + +func (p Percent) String() string { + if p < 0 { + return "" + } + return fmt.Sprintf("%3.0f %%", p) +} + +func (p Percent) Compact() string { + if p < 0 { + return "" + } + return fmt.Sprintf("%.0f%%", p) +} + +func (p *Percent) Value() float64 { + return float64(*p) +} +func (P *Percent) Clamp() { + if *P < 0 { + *P = 0 + } else if *P > 100 { + *P = 100 + } +} +func (P *Percent) CalcBytes(Vn, Vmax Bytes) { + P.Calc(float64(Vn), float64(Vmax)) +} +func (P *Percent) Calc(Vn, Vmax float64) { + if Vmax == 0 { + *P = 0 + return + } + *P = Percent(100 * Vn / Vmax) +} +func (P *Percent) FromFloat(v float64) { + *P = Percent(v * 100) +} +func (P *Percent) SetValue(v float64) { + *P = Percent(v) +} +func (P *Percent) SetValueInt(v int) { + *P = Percent(v) +}
M types/types.gotypes/types.go

@@ -2,81 +2,9 @@ package types

import ( "fmt" - "git.sophuwu.com/statlog/types/units" + "golang.org/x/term" ) -type Bytes uint64 - -func (b Bytes) Human() string { - n := float64(b) - var i int - for i = units.B; n >= units.KiB && i < units.GiB; i *= units.KiB { - n /= units.KiB - } - - return fmt.Sprintf("%.2f %s", n, units.Labels[i]) -} - -func (b Bytes) HumanSI() string { - n := float64(b) - var i int - for i = units.B; n >= units.KB && i < units.GB; i *= units.KB { - n /= units.KB - } - return fmt.Sprintf("%.2f %s", n, units.Labels[i]) -} - -func (b *Bytes) FromKiB(v uint64) { - *b = Bytes(v * units.KiB) -} - -func (b Bytes) Value() uint64 { - return uint64(b) -} - -func (b Bytes) String() string { - return b.Human() -} - -type Percent float64 - -func (p Percent) String() string { - if p < 0 { - return "" - } - return fmt.Sprintf("%3.0f %%", p) -} - -func (p *Percent) Value() float64 { - return float64(*p) -} -func (P *Percent) Clamp() { - if *P < 0 { - *P = 0 - } else if *P > 100 { - *P = 100 - } -} -func (P *Percent) CalcBytes(Vn, Vmax Bytes) { - P.Calc(float64(Vn), float64(Vmax)) -} -func (P *Percent) Calc(Vn, Vmax float64) { - if Vmax == 0 { - *P = 0 - return - } - *P = Percent(100 * Vn / Vmax) -} -func (P *Percent) FromFloat(v float64) { - *P = Percent(v * 100) -} -func (P *Percent) SetValue(v float64) { - *P = Percent(v) -} -func (P *Percent) SetValueInt(v int) { - *P = Percent(v) -} - type MHz int func (m MHz) String() string {

@@ -94,3 +22,13 @@ return ""

} return fmt.Sprintf("%3d C", c) } + +func TermSize() (w int, h int) { + var e error + w, h, e = term.GetSize(0) + if e != nil { + w = 80 + h = 24 + } + return +}