hlctl/groupctl/group.go

106 lines
2.6 KiB
Go

package groupctl
import (
"fmt"
"strconv"
"sectorinf.com/emilis/hlctl/config"
"sectorinf.com/emilis/hlctl/ctllog"
"sectorinf.com/emilis/hlctl/hlcl"
"sectorinf.com/emilis/hlctl/hlcl/cmd"
)
var log = ctllog.Logger{}.New("group", ctllog.ColorRGB{}.FromHex("27ee7e"))
func ResetActive() error {
err := config.New(config.ActiveGroup, "1", cmd.Uint)
if err != nil {
return fmt.Errorf("new active group: %w", err)
}
return Update()
}
// Update switches to the correct tag based on the active group
func Update() error {
activeCfg, err := config.Get(config.ActiveGroup)
if err != nil {
return fmt.Errorf("%s: %w", config.ActiveGroup, err)
}
active, err := activeCfg.Uint()
if err != nil {
return fmt.Errorf("%s: %w", config.ActiveGroup, err)
}
focus, err := config.GetFocusedTag()
if err != nil {
return fmt.Errorf("focus: %w", err)
}
numCfg, err := config.Get(config.TagsPerGroup)
if err != nil {
return fmt.Errorf("get %s: %w", config.TagsPerGroup, err)
}
num, err := numCfg.Uint()
if err != nil {
return fmt.Errorf("%s: %w", config.TagsPerGroup, err)
}
relativeTag := focus % uint8(num)
absTag := ((active - 1) * num) + relativeTag
if focus != absTag {
log.Printf("Tag %d -> %d", focus, absTag)
}
if err := hlcl.UseIndex(absTag); err != nil {
return fmt.Errorf("UseIndex[%d]: %w", absTag, err)
}
return SetKeybinds(active, num)
}
func MoveTo(group uint8) error {
numCfg, err := config.Get(config.TagsPerGroup)
if err != nil {
return fmt.Errorf("get %s: %w", config.TagsPerGroup, err)
}
num, err := numCfg.Uint()
if err != nil {
return fmt.Errorf("%s: %w", config.TagsPerGroup, err)
}
focus, err := config.GetFocusedTag()
if err != nil {
return fmt.Errorf("focus: %w", err)
}
relative := focus % num
newTag := ((group - 1) * num) + relative
log.Printf("Moving window to tag -> %d", newTag)
return hlcl.MoveIndex(newTag)
}
func SetKeybinds(group uint8, num uint8) error {
offset := int((group - 1) * num)
mod, err := config.Get(config.ModKey)
if err != nil {
return fmt.Errorf("%s: %w", config.ModKey, err)
}
log.Printf("Setting %d keybinds for group %d", num, group)
for i := 0; i < int(num); i++ {
actualTag := strconv.Itoa(offset + i)
tag := strconv.Itoa(i + 1)
err = hlcl.Keybind(
[]string{mod.String(), tag},
cmd.UseIndex,
actualTag,
)
if err != nil {
return fmt.Errorf("Setting use_index for [%d]: %w", i+1, err)
}
err = hlcl.Keybind(
[]string{mod.String(), "Shift", tag},
cmd.MoveIndex,
actualTag,
)
if err != nil {
return fmt.Errorf("Setting move_index for [%d]: %w", i+1, err)
}
}
return nil
}