brightness/tui.go

56 lines
1.1 KiB
Go

package main
import (
"log"
"github.com/gdamore/tcell/v2"
"gitlab.com/tslocum/cview"
)
func center(width, height int, p cview.Primitive) cview.Primitive {
subFlex := cview.NewFlex()
subFlex.SetDirection(cview.FlexRow)
subFlex.AddItem(cview.NewBox(), 0, 1, false)
subFlex.AddItem(p, height, 1, true)
subFlex.AddItem(cview.NewBox(), 0, 1, false)
flex := cview.NewFlex()
flex.AddItem(cview.NewBox(), 0, 1, false)
flex.AddItem(subFlex, width, 1, true)
flex.AddItem(cview.NewBox(), 0, 1, false)
return flex
}
func runTUI(backlight *backlight) error {
done := make(chan error)
app := cview.NewApplication()
app.EnableMouse(true)
sliderChanged := func(value int) {
err := backlight.setBrightness(value)
if err != nil {
log.Fatal(err)
}
}
form := cview.NewForm()
form.AddSlider("Brightness", backlight.current, backlight.max, 10, sliderChanged)
form.SetCancelFunc(func() {
done <- nil
})
form.GetFormItem(0).SetFinishedFunc(func(key tcell.Key) {
done <- nil
})
app.SetRoot(center(40, 20, form), true)
go func() {
done <- app.Run()
}()
err := <-done
app.Stop()
return err
}