Added MVP

This commit is contained in:
Ashish D'Souza 2024-09-15 20:48:45 -05:00
parent ba9601bb8d
commit b8155e4a74
6 changed files with 96 additions and 0 deletions

5
go.mod Normal file
View File

@ -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

2
go.sum Normal file
View File

@ -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=

22
main.go Normal file
View File

@ -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)
}
}

24
notify/i3.go Normal file
View File

@ -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)
}

27
scratchpad/last_window.go Normal file
View File

@ -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
}

16
scratchpad/show.go Normal file
View File

@ -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)
}
}