fortify/system/enablement.go
Ophestra 292715b0f6
Some checks failed
Test / Create distribution (push) Successful in 26s
Test / Fortify (push) Successful in 2m35s
Test / Data race detector (push) Successful in 4m38s
Test / Fpkg (push) Failing after 17m17s
Test / Flake checks (push) Has been skipped
system: optimise string formatting
Signed-off-by: Ophestra <cat@gensokyo.uk>
2025-03-25 04:47:00 +09:00

48 lines
687 B
Go

package system
import (
"fmt"
"strings"
)
// Enablement represents optional system resources.
type Enablement byte
const (
EWayland Enablement = 1 << iota
EX11
EDBus
EPulse
EM
)
func (e Enablement) String() string {
switch e {
case 0:
return "(no enablements)"
case EWayland:
return "wayland"
case EX11:
return "x11"
case EDBus:
return "dbus"
case EPulse:
return "pulseaudio"
default:
buf := new(strings.Builder)
buf.Grow(32)
for i := Enablement(1); i < EM; i <<= 1 {
if e&i != 0 {
buf.WriteString(", " + i.String())
}
}
if buf.Len() == 0 {
return fmt.Sprintf("e%x", byte(e))
}
return strings.TrimPrefix(buf.String(), ", ")
}
}