GetWorkspaces implemented

This commit is contained in:
milarin 2023-10-07 16:41:59 +02:00
parent 489f32e0c3
commit 5a556ec6c9
3 changed files with 47 additions and 1 deletions

23
get_workspaces.go Normal file
View File

@ -0,0 +1,23 @@
package sway
import (
"context"
"encoding/json"
"os/exec"
)
func GetWorkspaces(ctx context.Context) ([]Node, error) {
cmd := exec.CommandContext(ctx, "swaymsg", "-t", "get_workspaces")
data, err := cmd.Output()
if err != nil {
return nil, err
}
var workspaces []Node
if err := json.Unmarshal(data, &workspaces); err != nil {
return nil, err
}
return workspaces, nil
}

18
get_workspaces_test.go Normal file
View File

@ -0,0 +1,18 @@
package sway
import (
"context"
"fmt"
"testing"
)
func TestGetWorkspaces(t *testing.T) {
workspaces, err := GetWorkspaces(context.Background())
if err != nil {
t.Fatal(err)
}
for _, workspace := range workspaces {
fmt.Println(workspace.Type, workspace.Output, workspace.Name)
}
}

View File

@ -4,7 +4,7 @@ type Node struct {
ID NodeID `json:"id"` ID NodeID `json:"id"`
Type NodeType `json:"type"` Type NodeType `json:"type"`
Orientation Orientation `json:"orientation"` Orientation Orientation `json:"orientation"`
Percentage float64 `json:"percent"` Percentage *float64 `json:"percent"`
Urgent bool `json:"urgent"` Urgent bool `json:"urgent"`
Marks []string `json:"marks"` Marks []string `json:"marks"`
Focused bool `json:"focused"` Focused bool `json:"focused"`
@ -47,6 +47,11 @@ type Node struct {
LayerShellSurfaces []LayerShellSurface `json:"layer_shell_surfaces"` LayerShellSurfaces []LayerShellSurface `json:"layer_shell_surfaces"`
CurrentWorkspace string `json:"current_workspace"` CurrentWorkspace string `json:"current_workspace"`
CurrentMode OutputMode `json:"current_mode"` CurrentMode OutputMode `json:"current_mode"`
// NodeTypeWorkspace only
WorkspaceNumber int `json:"num"`
Output string `json:"output"`
Representation string `json:"representation"`
} }
type NodeID int64 type NodeID int64