1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.2 KiB

package main
import (
"barista.run/bar"
"barista.run/colors"
"barista.run/modules/cputemp"
"barista.run/modules/meminfo"
"barista.run/modules/sysinfo"
"barista.run/outputs"
"barista.run/pango"
"github.com/dustin/go-humanize"
"github.com/martinlindhe/unit"
"strings"
"time"
)
func memorySegment() bar.Module {
return meminfo.New().Output(func(m meminfo.Info) bar.Output {
h := strings.SplitN(humanize.IBytes(uint64(m.Available().Bytes())), " ", 2)
freeText := pango.Text(h[0])
if len(h) == 2 {
freeText = freeText.Append(spacer, pango.Text(h[1]).Small())
}
out := outputs.Pango(pango.Text("Mem").XSmall(), spacer, freeText)
freeGigs := m.Available().Gigabytes()
switch {
case freeGigs < 0.5:
out.Urgent(true)
case freeGigs < 1:
out.Color(colors.Scheme("bad"))
case freeGigs < 2:
out.Color(colors.Scheme("degraded"))
case freeGigs > 12:
out.Color(colors.Scheme("good"))
}
out.OnClick(startTaskManager)
return out
})
}
func loadAverageSegment() bar.Module {
return sysinfo.New().Output(func(s sysinfo.Info) bar.Output {
out := outputs.Pango(pango.Text("Load 1m").XSmall(), spacer, pango.Textf("%0.2f", s.Loads[0]), spacer, pango.Text("15m").XSmall(), spacer, pango.Textf("%0.2f", s.Loads[2]))
// Load averages are unusually high for a few minutes after boot.
if s.Uptime < 10*time.Minute {
// so don't add colours until 10 minutes after system start.
return out
}
switch {
case s.Loads[0] > 128, s.Loads[2] > 64:
out.Urgent(true)
case s.Loads[0] > 64, s.Loads[2] > 32:
out.Color(colors.Scheme("bad"))
case s.Loads[0] > 32, s.Loads[2] > 16:
out.Color(colors.Scheme("degraded"))
}
out.OnClick(startTaskManager)
return out
})
}
func cputempSegment() bar.Module {
return cputemp.New().
RefreshInterval(2 * time.Second).
Output(func(temp unit.Temperature) bar.Output {
out := outputs.Pango(
pango.Text("CPU").XSmall(), spacer,
pango.Textf("%2d", int(temp.Celsius())),
pango.Text("℃").XSmall(),
)
switch {
case temp.Celsius() > 90:
out.Urgent(true)
case temp.Celsius() > 70:
out.Color(colors.Scheme("bad"))
case temp.Celsius() > 60:
out.Color(colors.Scheme("degraded"))
}
return out
})
}