Refactored to i3 module

This commit is contained in:
Magnus Åhall 2024-07-13 17:44:00 +02:00
parent f9e815b4e3
commit 3516c0a618
4 changed files with 38 additions and 21 deletions

View File

@ -1,4 +1,4 @@
package main package i3
import ( import (
// Standard // Standard
@ -11,6 +11,19 @@ import (
"strings" "strings"
) )
var (
outputIndices map[string]int
workspaces []I3Workspace
)
func init() {
outputIndices = make(map[string]int)
}
func SetWorkspaces(wss []I3Workspace) {
workspaces = wss
}
func (sess *I3Session) Open() (err error) { func (sess *I3Session) Open() (err error) {
// Find path to i3 socket // Find path to i3 socket
i3SocketPathCmd := exec.Command("i3", "--get-socketpath") i3SocketPathCmd := exec.Command("i3", "--get-socketpath")
@ -161,7 +174,7 @@ func (sess I3Session) SwitchOutput(output string) error {
if nextWorkspace == "" { if nextWorkspace == "" {
return fmt.Errorf( return fmt.Errorf(
"Switch to output (%s) failed, "+ "Switch to output (%s) failed, "+
"no visible workspace found", "no visible workspace found",
output, output,
) )
} }
@ -184,7 +197,7 @@ func (sess *I3Session) ManageSession() error {
} }
choice := strings.TrimSpace(string(out)) choice := strings.TrimSpace(string(out))
switch(choice) { switch choice {
case "reload", "restart", "exit": case "reload", "restart", "exit":
_, err = sess.Socket.Request(RUN_COMMAND, choice) _, err = sess.Socket.Request(RUN_COMMAND, choice)
if err != nil { if err != nil {
@ -337,12 +350,11 @@ func (sess *I3Session) RestoreWorkspaces() (err error) {
var list [][]string var list [][]string
var wsNum int var wsNum int
for wsName, output := range sess.workspaces { for wsName, output := range sess.workspaces {
//fmt.Printf("Restore workspace: %s to %s\n", wsName, output) //fmt.Printf("Restore workspace: %s to %s\n", wsName, output)
components := strings.Split(wsName, ":") components := strings.Split(wsName, ":")
wsNum, err = strconv.Atoi(components[0]) wsNum, err = strconv.Atoi(components[0])
if err != nil { if err != nil {
return err return err
} }
@ -439,11 +451,12 @@ func (sess I3Session) FixWorkspaces() error {
cmd := fmt.Sprintf( cmd := fmt.Sprintf(
"[workspace=\"%s\"] move window to workspace "+ "[workspace=\"%s\"] move window to workspace "+
"\"%d:%s\"", "\"%d:%s\"",
ws.Name, ws.Name,
outputIdx+(ws.Num % 10), outputIdx+(ws.Num%10),
workspaces[(ws.Num % 10)].Name, workspaces[(ws.Num%10)].Name,
) )
fmt.Printf("Move windows: %s\n", cmd)
_, err := sess.Socket.Request(RUN_COMMAND, cmd) _, err := sess.Socket.Request(RUN_COMMAND, cmd)
if err != nil { if err != nil {
return err return err

View File

@ -1,4 +1,4 @@
package main package i3
import ( import (
// Standard // Standard

View File

@ -1,4 +1,4 @@
package main package i3
import ( import (
// Standard // Standard

26
main.go
View File

@ -1,6 +1,9 @@
package main package main
import ( import (
// Internal
"i3-session-manager/i3"
// Standard // Standard
"fmt" "fmt"
"net" "net"
@ -10,21 +13,22 @@ import (
) )
var ( var (
session I3Session session i3.I3Session
sessionSubscription I3Session sessionSubscription i3.I3Session
workspaces []I3Workspace
outputIndices map[string]int
subscriptions []string subscriptions []string
) )
func init() { func init() {
outputIndices = make(map[string]int)
subscriptions = []string{ subscriptions = []string{
"binding", "binding",
"shutdown", "shutdown",
} }
}
workspaces = []I3Workspace{ func main() {
var err error
i3.SetWorkspaces([]i3.I3Workspace{
{ Num: 0, Name: "Development" }, { Num: 0, Name: "Development" },
{ Num: 1, Name: "Terminal" }, { Num: 1, Name: "Terminal" },
{ Num: 2, Name: "Browser" }, { Num: 2, Name: "Browser" },
@ -35,11 +39,7 @@ func init() {
{ Num: 7, Name: "Debug" }, { Num: 7, Name: "Debug" },
{ Num: 8, Name: "Email" }, { Num: 8, Name: "Email" },
{ Num: 9, Name: "Virtualization" }, { Num: 9, Name: "Virtualization" },
} })
}
func main() {
var err error
go SessionLoop() go SessionLoop()
@ -87,6 +87,10 @@ func main() {
fmt.Printf("Fix workspaces: %s\n", err) fmt.Printf("Fix workspaces: %s\n", err)
} }
conn.Close() conn.Close()
case "test":
fmt.Printf("test\n")
conn.Close()
} }
} }
} }