Alter 5 files

Update main.go
Update i3bar.go
Add backlight.go
Update cpu.go
Update providers.go
This commit is contained in:
akp 2025-03-05 01:26:53 +00:00
parent a132c1de0c
commit e5d4c690e7
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
5 changed files with 111 additions and 17 deletions

View file

@ -53,9 +53,10 @@ func run() error {
// providers.NewDisk("/", 30, 10),
providers.NewBattery("BAT0", 80, 30, 20),
providers.NewWiFi("wlp0s20f3", 75),
providers.NewIPType("wlp0s20f3"),
// providers.NewIPType("wlp0s20f3"),
// providers.NewIPAddress("wlp0s20f3"),
providers.NewAudioPlayer(32),
providers.NewBacklight(),
)
if err := b.Emit([]*i3bar.Block{

View file

@ -17,13 +17,13 @@ import (
type generatorInfo struct {
Provider BlockGenerator
HasClickConsumer bool
Last *Block
Last *Block
}
type I3bar struct {
writer io.Writer
reader io.Reader
updateSignal syscall.Signal
writer io.Writer
reader io.Reader
updateSignal syscall.Signal
generators []*generatorInfo
tickNumber uint8
@ -31,9 +31,9 @@ type I3bar struct {
func New(writer io.Writer, reader io.Reader, updateSignal syscall.Signal) *I3bar {
return &I3bar{
writer: writer,
reader: reader,
updateSignal: updateSignal,
writer: writer,
reader: reader,
updateSignal: updateSignal,
}
}
@ -132,12 +132,6 @@ func (b *I3bar) tick(override bool) error {
TextColor: defaultColorSet.Bad,
}
}
if block == nil {
block = &Block{
FullText: "MISSING",
TextColor: defaultColorSet.Warning,
}
}
if block != gen.Last {
gen.Last = block

View file

@ -0,0 +1,92 @@
package providers
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/codemicro/bar/internal/i3bar"
)
type Backlight struct {
name string
showUntil time.Time
lastValue int
maxValue int
}
func NewBacklight() i3bar.BlockGenerator {
return &Backlight{
name: "backlight",
}
}
func (g *Backlight) Frequency() uint8 {
return 1
}
func (g *Backlight) parseIntFromFile(fname string) (int, error) {
fcont, err := os.ReadFile(fname)
if err != nil {
return 0, err
}
i, err := strconv.Atoi(strings.TrimSpace(string(fcont)))
if err != nil {
return 0, err
}
return i, nil
}
func (g *Backlight) getMaxVal() (int, error) {
if g.maxValue != 0 {
return g.maxValue, nil
}
i, err := g.parseIntFromFile("/sys/class/backlight/intel_backlight/max_brightness")
if err != nil {
return 0, nil
}
g.maxValue = i
return g.maxValue, nil
}
func (g *Backlight) getCurrentVal() (int, error) {
return g.parseIntFromFile("/sys/class/backlight/intel_backlight/brightness")
}
func (g *Backlight) Block(cs *i3bar.ColorSet) (*i3bar.Block, error) {
// this is a sun emoji: ☀
if g.maxValue == 0 {
if _, err := g.getMaxVal(); err != nil {
return nil, err
}
}
if cv, err := g.getCurrentVal(); err != nil {
return nil, err
} else if cv != g.lastValue {
g.lastValue = cv
g.showUntil = time.Now().Add(time.Second * 5)
}
if g.showUntil.After(time.Now()) {
return &i3bar.Block{
Name: g.name,
FullText: fmt.Sprintf("☀ %s%%", leftPad(fmt.Sprint((g.lastValue*100)/g.maxValue), ' ', 3)),
}, nil
}
return nil, nil
}
func (g *Backlight) GetNameAndInstance() (string, string) {
return g.name, ""
}

View file

@ -24,7 +24,7 @@ func NewCPU(okThreshold, warningThreshold float32) i3bar.BlockGenerator {
m := &CPU{
OkThreshold: okThreshold,
WarningThreshold: warningThreshold,
name: "cpu",
name: "cpu",
}
_ = m.doSample()
return m
@ -82,7 +82,7 @@ func (g *CPU) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) {
block := &i3bar.Block{
Name: g.name,
FullText: fmt.Sprintf("CPU: %.1f%%", p),
FullText: fmt.Sprintf("CPU: %s%%", leftPad(fmt.Sprintf("%.1f", p), ' ', 4)),
ShortText: fmt.Sprintf("C: %.1f%%", p),
}
@ -97,4 +97,4 @@ func (g *CPU) Block(colors *i3bar.ColorSet) (*i3bar.Block, error) {
func (g *CPU) GetNameAndInstance() (string, string) {
return g.name, ""
}
}

View file

@ -19,3 +19,10 @@ func runCommand(program string, args ...string) ([]byte, error) {
}
return bytes.TrimSpace(out), err
}
func leftPad(x string, c rune, n int) string {
for len(x) < n {
x = string(c) + x
}
return x
}