From b8155e4a740b683821d1cf989ddf56c03bcb994b Mon Sep 17 00:00:00 2001 From: Ashish D'Souza Date: Sun, 15 Sep 2024 20:48:45 -0500 Subject: [PATCH] Added MVP --- go.mod | 5 +++++ go.sum | 2 ++ main.go | 22 ++++++++++++++++++++++ notify/i3.go | 24 ++++++++++++++++++++++++ scratchpad/last_window.go | 27 +++++++++++++++++++++++++++ scratchpad/show.go | 16 ++++++++++++++++ 6 files changed, 96 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 notify/i3.go create mode 100644 scratchpad/last_window.go create mode 100644 scratchpad/show.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..211a345 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module gitea.ashishdsouza.com/ashish/i3-last-scratchpad + +go 1.21.12 + +require github.com/mdirkse/i3ipc v0.0.0-20171212230543-ac599a872375 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3660309 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mdirkse/i3ipc v0.0.0-20171212230543-ac599a872375 h1:PPiUjQrUS1oVKBPeRBa4uP8z6jkRgbwsD88QAzXM6Eo= +github.com/mdirkse/i3ipc v0.0.0-20171212230543-ac599a872375/go.mod h1:QCgFg+QImlvQdfX7W8i1ueSJJYNOeOKEApyCXQnyt+w= diff --git a/main.go b/main.go new file mode 100644 index 0000000..638a95b --- /dev/null +++ b/main.go @@ -0,0 +1,22 @@ +package main + +import "github.com/mdirkse/i3ipc" + +import "gitea.ashishdsouza.com/ashish/i3-last-scratchpad/notify" +import "gitea.ashishdsouza.com/ashish/i3-last-scratchpad/scratchpad" + + +func main() { + var ipcSocket, socketErr = i3ipc.GetIPCSocket() + if socketErr != nil { + notify.SendNotification("Failed to acquire i3 IPC socket", notify.CriticalPriority) + panic(socketErr) + } + + var lastScratchpadNode = scratchpad.GetLastScratchpadNode(ipcSocket) + if lastScratchpadNode == nil { + notify.SendNotification("Scratchpad is empty", notify.NormalPriority) + } else { + scratchpad.ShowScratchpadNode(ipcSocket, lastScratchpadNode) + } +} diff --git a/notify/i3.go b/notify/i3.go new file mode 100644 index 0000000..662ce50 --- /dev/null +++ b/notify/i3.go @@ -0,0 +1,24 @@ +package notify + +import "os/exec" + + +type i3Urgency string + +const ( + LowPriority i3Urgency = "low" + NormalPriority i3Urgency = "normal" + CriticalPriority i3Urgency = "critical" +) + +func SendNotification(msg string, priority i3Urgency) { + var cmd = exec.Command("notify-send", "-u", string(priority), msg) + if err := cmd.Start(); err != nil { + panic(err) + } +} + +func FatalNotification(msg string) { + SendNotification(msg, CriticalPriority) + panic(msg) +} diff --git a/scratchpad/last_window.go b/scratchpad/last_window.go new file mode 100644 index 0000000..5614729 --- /dev/null +++ b/scratchpad/last_window.go @@ -0,0 +1,27 @@ +package scratchpad + +import "github.com/mdirkse/i3ipc" + +import "gitea.ashishdsouza.com/ashish/i3-last-scratchpad/notify" + + +func GetLastScratchpadNode(ipcSocket *i3ipc.IPCSocket) *i3ipc.I3Node { + var root, err = ipcSocket.GetTree() + if err != nil { + notify.SendNotification("Failed to fetch layout tree", notify.CriticalPriority) + panic(err) + } + + var leaves = root.Leaves() + for i := len(leaves) - 1; i >= 0; i-- { + if leaves[i].Parent == nil { + notify.FatalNotification("Leaf node nas no parent") + } + + // https://i3wm.org/docs/ipc.html#_tree_reply + if leaves[i].Parent.Scratchpad_State != "none" { + return leaves[i] + } + } + return nil +} diff --git a/scratchpad/show.go b/scratchpad/show.go new file mode 100644 index 0000000..ae6ce8c --- /dev/null +++ b/scratchpad/show.go @@ -0,0 +1,16 @@ +package scratchpad + +import "fmt" + +import "github.com/mdirkse/i3ipc" + +import "gitea.ashishdsouza.com/ashish/i3-last-scratchpad/notify" + + +func ShowScratchpadNode(ipcSocket *i3ipc.IPCSocket, node *i3ipc.I3Node) { + var i3Command = fmt.Sprintf("[con_id=%d] scratchpad show", node.ID) + if success, err := ipcSocket.Command(i3Command); !success { + notify.SendNotification("Failed to show scratchpad window", notify.CriticalPriority) + panic(err) + } +}