diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-02 12:41:08 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-02 12:41:08 +0200 |
| commit | d0b9bc84aed1898a06a9d6fc3b82beee407d3cde (patch) | |
| tree | 7bb343960ee912d77dbc9f7720cd8cdd1f0172ea /internal/display/mem.go | |
| parent | bbc91e8764bd83c4497f2ddac86bb8947a91765c (diff) | |
Refactor display iteration/state and harden collector runtime
Diffstat (limited to 'internal/display/mem.go')
| -rw-r--r-- | internal/display/mem.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/internal/display/mem.go b/internal/display/mem.go new file mode 100644 index 0000000..7a814d8 --- /dev/null +++ b/internal/display/mem.go @@ -0,0 +1,58 @@ +package display + +import ( + "codeberg.org/snonux/loadbars/internal/constants" + "codeberg.org/snonux/loadbars/internal/stats" + "github.com/veandco/go-sdl2/sdl" +) + +func drawMemBarSmoothed(renderer *sdl.Renderer, h *stats.HostStats, smoothed *struct{ ramUsed, swapUsed float64 }, factor float64, barW int32, x, y, barH int32) { + renderer.SetDrawColor(constants.Black.R, constants.Black.G, constants.Black.B, 255) + renderer.FillRect(&sdl.Rect{X: x, Y: y, W: barW, H: barH}) + if h.Mem == nil { + return + } + var targetRam, targetSwap float64 + if memTotal := h.Mem["MemTotal"]; memTotal > 0 { + targetRam = 100 - 100*float64(h.Mem["MemFree"])/float64(memTotal) + if targetRam < 0 { + targetRam = 0 + } + if targetRam > 100 { + targetRam = 100 + } + } + if swapTotal := h.Mem["SwapTotal"]; swapTotal > 0 { + targetSwap = 100 - 100*float64(h.Mem["SwapFree"])/float64(swapTotal) + if targetSwap < 0 { + targetSwap = 0 + } + if targetSwap > 100 { + targetSwap = 100 + } + } + smoothed.ramUsed += (targetRam - smoothed.ramUsed) * factor + smoothed.swapUsed += (targetSwap - smoothed.swapUsed) * factor + + halfW := barW / 2 + pxPerPct := float64(barH) / 100.0 + ramUsedH := int32(smoothed.ramUsed * pxPerPct) + if ramUsedH > 0 { + renderer.SetDrawColor(constants.DarkGrey.R, constants.DarkGrey.G, constants.DarkGrey.B, 255) + renderer.FillRect(&sdl.Rect{X: x, Y: y + barH - ramUsedH, W: halfW, H: ramUsedH}) + } + if ramFreeH := barH - ramUsedH; ramFreeH > 0 { + renderer.SetDrawColor(constants.Black.R, constants.Black.G, constants.Black.B, 255) + renderer.FillRect(&sdl.Rect{X: x, Y: y, W: halfW, H: ramFreeH}) + } + + swapUsedH := int32(smoothed.swapUsed * pxPerPct) + if swapUsedH > 0 { + renderer.SetDrawColor(constants.Grey.R, constants.Grey.G, constants.Grey.B, 255) + renderer.FillRect(&sdl.Rect{X: x + halfW, Y: y + barH - swapUsedH, W: halfW, H: swapUsedH}) + } + if swapFreeH := barH - swapUsedH; swapFreeH > 0 { + renderer.SetDrawColor(constants.Black.R, constants.Black.G, constants.Black.B, 255) + renderer.FillRect(&sdl.Rect{X: x + halfW, Y: y, W: halfW, H: swapFreeH}) + } +} |
