mirror of
https://codeberg.org/scip/swaycycle.git
synced 2026-08-24 14:54:19 +02:00
update swayipc, go, add linter cfg, satisfy linter (#2)
This commit is contained in:
97
main.go
97
main.go
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright © 2025 Thomas von Dein
|
||||
Copyright © 2025-2026 Thomas von Dein
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -27,9 +27,9 @@ import (
|
||||
"runtime/debug"
|
||||
"sort"
|
||||
|
||||
"codeberg.org/scip/swayipc/v2"
|
||||
"github.com/lmittmann/tint"
|
||||
"github.com/mattn/go-isatty"
|
||||
"github.com/tlinden/i3ipc"
|
||||
"github.com/tlinden/yadu"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
@@ -44,18 +44,11 @@ const (
|
||||
|
||||
LevelNotice = slog.Level(2)
|
||||
|
||||
VERSION = "v0.3.1"
|
||||
|
||||
IPC_HEADER_SIZE = 14
|
||||
IPC_MAGIC = "i3-ipc"
|
||||
|
||||
// message types
|
||||
IPC_GET_TREE = 4
|
||||
IPC_RUN_COMMAND = 0
|
||||
VERSION = "v0.3.2"
|
||||
)
|
||||
|
||||
var (
|
||||
Visibles = []*i3ipc.Node{}
|
||||
Visibles = []*swayipc.Node{}
|
||||
CurrentWorkspace = ""
|
||||
Previous = false
|
||||
Debug = false
|
||||
@@ -88,7 +81,7 @@ func main() {
|
||||
flag.BoolVarP(&Previous, "prev", "p", false, "cycle backward")
|
||||
flag.BoolVarP(&Debug, "debug", "d", false, "enable debugging")
|
||||
flag.BoolVarP(&Dumptree, "dump", "D", false, "dump the sway tree (needs -d as well)")
|
||||
flag.BoolVarP(&Dumpvisibles, "dump-visibles", "", false, "dump a list of visible windows on current workspace (needs -d)")
|
||||
flag.BoolVarP(&Dumpvisibles, "dump-visibles", "", false, "dump the sway tree (needs -d as well)")
|
||||
flag.BoolVarP(&Notswitch, "no-switch", "n", false, "do not switch windows")
|
||||
flag.BoolVarP(&Version, "version", "v", false, "show program version")
|
||||
flag.BoolVarP(&Showhelp, "help", "h", Showhelp, "show help")
|
||||
@@ -117,35 +110,46 @@ func main() {
|
||||
log.Fatalf("failed to close log file: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
setupLogging(file)
|
||||
} else {
|
||||
setupLogging(os.Stdout)
|
||||
}
|
||||
|
||||
if err := cycle(); err != nil {
|
||||
log.Fatal(err) // nolint:gocritic
|
||||
}
|
||||
}
|
||||
|
||||
func cycle() error {
|
||||
// connect to sway unix socket
|
||||
ipc := i3ipc.NewI3ipc()
|
||||
ipc := swayipc.NewSwayIPC()
|
||||
|
||||
err := ipc.Connect()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
defer ipc.Close()
|
||||
defer func() {
|
||||
if err := ipc.Close(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
sway, err := ipc.GetTree()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
// traverse the tree and find visible windows
|
||||
if err := processJSON(sway); err != nil {
|
||||
log.Fatalf("%s", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if len(Visibles) == 0 {
|
||||
os.Exit(0)
|
||||
return nil
|
||||
}
|
||||
|
||||
id := 0
|
||||
var id int
|
||||
if Previous {
|
||||
id = findPrevWindow()
|
||||
slog.Debug("findPrevWindow", "nextid", id)
|
||||
@@ -156,14 +160,16 @@ func main() {
|
||||
|
||||
if id > 0 && !Notswitch {
|
||||
if err := switchFocus(id, ipc); err != nil {
|
||||
log.Fatalf("%s", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// get into the sway tree, determine current workspace and extract all
|
||||
// its visible windows, store them in the global var Visibles
|
||||
func processJSON(sway *i3ipc.Node) error {
|
||||
func processJSON(sway *swayipc.Node) error {
|
||||
if !istype(sway, root) && len(sway.Nodes) == 0 {
|
||||
return errors.New("invalid or empty JSON structure")
|
||||
}
|
||||
@@ -173,10 +179,11 @@ func processJSON(sway *i3ipc.Node) error {
|
||||
}
|
||||
|
||||
for _, node := range sway.Nodes {
|
||||
if node.Current_workspace != "" {
|
||||
if node.CurrentWorkspace != "" {
|
||||
// this is an output node containing the current workspace
|
||||
CurrentWorkspace = node.Current_workspace
|
||||
CurrentWorkspace = node.CurrentWorkspace
|
||||
recurseNodes(node.Nodes)
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -200,16 +207,17 @@ func findNextWindow() int {
|
||||
for _, node := range Visibles {
|
||||
if node.Focused {
|
||||
seenfocused = true
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
if seenfocused {
|
||||
return node.Id
|
||||
return node.ID
|
||||
}
|
||||
}
|
||||
|
||||
if seenfocused {
|
||||
return Visibles[0].Id
|
||||
return Visibles[0].ID
|
||||
}
|
||||
|
||||
return 0
|
||||
@@ -221,23 +229,24 @@ func findPrevWindow() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
prevnode := Visibles[vislen-1].Id
|
||||
prevnode := Visibles[vislen-1].ID
|
||||
|
||||
for _, node := range Visibles {
|
||||
if node.Focused {
|
||||
return prevnode
|
||||
}
|
||||
prevnode = node.Id
|
||||
|
||||
prevnode = node.ID
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
// actually switch focus using a swaymsg command
|
||||
func switchFocus(id int, ipc *i3ipc.I3ipc) error {
|
||||
func switchFocus(id int, ipc *swayipc.SwayIPC) error {
|
||||
responses, err := ipc.RunContainerCommand(id, "focus")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to send focus command to container %d: %s (%s)",
|
||||
return fmt.Errorf("failed to send focus command to container %d: %s (%s)",
|
||||
id, responses[0].Error, err)
|
||||
}
|
||||
|
||||
@@ -247,20 +256,20 @@ func switchFocus(id int, ipc *i3ipc.I3ipc) error {
|
||||
}
|
||||
|
||||
// iterate recursively over given node list extracting visible windows
|
||||
func recurseNodes(nodes []*i3ipc.Node) {
|
||||
func recurseNodes(nodes []*swayipc.Node) {
|
||||
for _, node := range nodes {
|
||||
|
||||
if istype(node, workspace) {
|
||||
if node.Name == CurrentWorkspace {
|
||||
//floating_nodes need to be sorted because
|
||||
//order changes each time they are focused.
|
||||
// floating_nodes need to be sorted because
|
||||
// order changes each time they are focused.
|
||||
FloatVis := node.FloatingNodes
|
||||
sort.Slice(FloatVis, func(i, j int) bool {
|
||||
return FloatVis[i].Id < FloatVis[j].Id
|
||||
return FloatVis[i].ID < FloatVis[j].ID
|
||||
})
|
||||
//now we can handle nodes and floating_nodes identical
|
||||
// now we can handle nodes and floating_nodes identical
|
||||
node.Nodes = append(node.Nodes, FloatVis...)
|
||||
recurseNodes(node.Nodes)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -283,7 +292,7 @@ func dumpVisibles() {
|
||||
windows := make([]string, len(Visibles))
|
||||
|
||||
for idx, node := range Visibles {
|
||||
windows[idx] = fmt.Sprintf("id: %02d, focus: %5t, name: %s", node.Id, node.Focused, node.Name)
|
||||
windows[idx] = fmt.Sprintf("id: %02d, focus: %5t, name: %s", node.ID, node.Focused, node.Name)
|
||||
}
|
||||
|
||||
slog.Debug("visible windows on current workspace", "visibles", windows)
|
||||
@@ -329,25 +338,25 @@ func setupLogging(output io.Writer) {
|
||||
}
|
||||
}
|
||||
|
||||
// little helper to distinguish sway tree node types
|
||||
func istype(nd *i3ipc.Node, which int) bool {
|
||||
// istype is a little helper to distinguish sway tree node types
|
||||
func istype(nd *swayipc.Node, which int) bool {
|
||||
switch nd.Type {
|
||||
case "root":
|
||||
return which == root
|
||||
return which == swayipc.NodeTypeRoot
|
||||
case "output":
|
||||
return which == output
|
||||
return which == swayipc.NodeTypeOutput
|
||||
case "workspace":
|
||||
return which == workspace
|
||||
return which == swayipc.NodeTypeWorkspace
|
||||
case "con":
|
||||
return which == con
|
||||
return which == swayipc.NodeTypeCon
|
||||
case "floating_con":
|
||||
return which == floating
|
||||
return which == swayipc.NodeTypeFloating
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// returns TRUE if stdout is NOT a tty or windows
|
||||
// IsNoTty returns TRUE if stdout is NOT a tty or windows
|
||||
func IsNoTty() bool {
|
||||
if !isatty.IsTerminal(os.Stdout.Fd()) {
|
||||
return true
|
||||
|
||||
Reference in New Issue
Block a user