6 Commits

Author SHA1 Message Date
dependabot[bot]
0755d20673 Bump actions/setup-go from 5 to 6
Bumps [actions/setup-go](https://github.com/actions/setup-go) from 5 to 6.
- [Release notes](https://github.com/actions/setup-go/releases)
- [Commits](https://github.com/actions/setup-go/compare/v5...v6)

---
updated-dependencies:
- dependency-name: actions/setup-go
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-10-01 17:27:59 +00:00
dependabot[bot]
85a1e6530c Bump actions/checkout from 4 to 5 (#5) 2025-09-19 07:56:48 +02:00
feba0f3580 bump version 2025-08-25 09:53:53 +02:00
2fbb3ebc59 add doc about prev flag 2025-08-25 09:53:40 +02:00
T.v.Dein
8e48b42bad enhance window switch debugging (#4) 2025-08-25 09:02:14 +02:00
kkvark
7a5657b778 add --prev option and sort floating_nodes (#3)
* add --prev option
* sort floating_nodes to avoid skipping floating windows
2025-08-25 08:59:21 +02:00
3 changed files with 64 additions and 8 deletions

View File

@@ -10,10 +10,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
uses: actions/checkout@v5
- name: Set up Go
uses: actions/setup-go@v5
uses: actions/setup-go@v6
with:
go-version: 1.23.5

View File

@@ -24,6 +24,13 @@ Add such a line to your sway config file (e.g. in `$HOME/.config/sway/config`):
bindsym $mod+Tab exec ~/bin/swaycycle
```
You may also add a second key binding to do the reverse, which is
sometimes very useful:
```default
bindsym $mod+Shift+Tab exec ~/bin/swaycycle --prev
```
## Debugging
You may call `swaycycle` in a terminal window on a workspace with at

61
main.go
View File

@@ -24,6 +24,7 @@ import (
"log/slog"
"os"
"runtime/debug"
"sort"
"github.com/lmittmann/tint"
"github.com/mattn/go-isatty"
@@ -42,7 +43,7 @@ const (
LevelNotice = slog.Level(2)
VERSION = "v0.3.0"
VERSION = "v0.3.1"
IPC_HEADER_SIZE = 14
IPC_MAGIC = "i3-ipc"
@@ -55,8 +56,10 @@ const (
var (
Visibles = []*i3ipc.Node{}
CurrentWorkspace = ""
Previous = false
Debug = false
Dumptree = false
Dumpvisibles = false
Version = false
Verbose = false
Notswitch = false
@@ -69,9 +72,11 @@ const Usage string = `This is swaycycle - cycle focus through all visible window
Usage: swaycycle [-vdDn] [-l <log>]
Options:
-p, --prev cycle backward
-n, --no-switch do not switch windows
-d, --debug enable debugging
-D, --dump dump the sway tree (needs -d as well)
--dump-visibles dump a list of visible windows on current workspace (needs -d)
-l, --logfile string write output to logfile
-v, --version show program version
@@ -80,8 +85,10 @@ Licensed under the terms of the GNU GPL version 3.
`
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(&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")
@@ -134,8 +141,14 @@ func main() {
os.Exit(0)
}
id := findNextWindow()
slog.Debug("findNextWindow", "nextid", id)
id := 0
if Previous {
id = findPrevWindow()
slog.Debug("findPrevWindow", "nextid", id)
} else {
id = findNextWindow()
slog.Debug("findNextWindow", "nextid", id)
}
if id > 0 && !Notswitch {
switchFocus(id, ipc)
@@ -162,7 +175,9 @@ func processJSON(sway *i3ipc.Node) error {
}
}
slog.Debug("processed visible windows", "visibles", Visibles)
if Dumpvisibles {
dumpVisibles()
}
return nil
}
@@ -194,6 +209,24 @@ func findNextWindow() int {
return 0
}
func findPrevWindow() int {
vislen := len(Visibles)
if vislen == 0 {
return 0
}
prevnode := Visibles[vislen-1].Id
for _, node := range Visibles {
if node.Focused {
return prevnode
}
prevnode = node.Id
}
return 0
}
// actually switch focus using a swaymsg command
func switchFocus(id int, ipc *i3ipc.I3ipc) error {
responses, err := ipc.RunContainerCommand(id, "focus")
@@ -210,11 +243,17 @@ func switchFocus(id int, ipc *i3ipc.I3ipc) error {
// iterate recursively over given node list extracting visible windows
func recurseNodes(nodes []*i3ipc.Node) {
for _, node := range nodes {
// we handle nodes and floating_nodes identical
node.Nodes = append(node.Nodes, node.FloatingNodes...)
if istype(node, workspace) {
if node.Name == CurrentWorkspace {
//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
})
//now we can handle nodes and floating_nodes identical
node.Nodes = append(node.Nodes, FloatVis...)
recurseNodes(node.Nodes)
return
}
@@ -234,6 +273,16 @@ func recurseNodes(nodes []*i3ipc.Node) {
}
}
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)
}
slog.Debug("visible windows on current workspace", "visibles", windows)
}
// we use line wise logging, unless debugging is enabled
func setupLogging(output io.Writer) {
logLevel := &slog.LevelVar{}