This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workspace.go
97 lines (76 loc) · 2.26 KB
/
workspace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"strings"
i3 "go.i3wm.org/i3/v4"
)
type workspaces struct {
i3wss []i3.Workspace
extensionPrefix string
}
func (wss workspaces) GetFocused() (workspace, error) {
for _, ws := range wss.i3wss {
if ws.Focused {
return workspace{ws, wss.extensionPrefix}, nil
}
}
return workspace{}, fmt.Errorf("No focused workspace among %d available workspaces", len(wss.i3wss))
}
func listWorkspaces(extensionPrefix string) (workspaces, error) {
i3wss, err := i3.GetWorkspaces()
if err != nil {
return workspaces{}, fmt.Errorf("failed to list workspaces: %w", err)
}
return workspaces{i3wss: i3wss, extensionPrefix: extensionPrefix}, nil
}
type workspace struct {
i3.Workspace
extensionPrefix string
}
func (ws workspace) id() string {
return fmt.Sprintf("num=%d:name=%s", ws.Num, ws.Name)
}
func (ws workspace) String() string {
return ws.id()
}
var (
i3msg func(string) ([]i3.CommandResult, error) = i3.RunCommand
)
func (ws workspace) Focus() error {
if cr, err := i3msg(fmt.Sprintf("workspace %s", ws.Name)); err != nil {
return fmt.Errorf("failed to focus workspace '%s': %+v", ws.id(), cr)
}
return nil
}
func (ws workspace) FlipFocusedContainer() error {
if cr, err := i3msg(fmt.Sprintf("move container to workspace %s", ws.Name)); err != nil {
return fmt.Errorf("failed to move workspace '%s': %+v", ws.id(), cr)
}
return nil
}
func (ws workspace) IsExtension() bool {
return ws.hasExtensionPrefix()
}
func (ws workspace) IsMain() bool {
return !ws.hasExtensionPrefix()
}
func (ws workspace) hasExtensionPrefix() bool {
return strings.HasPrefix(ws.Name, ws.extensionPrefix)
}
func (ws workspace) Main() (workspace, error) {
if !ws.hasExtensionPrefix() {
return workspace{}, fmt.Errorf("workspace %s is already main", ws.id())
}
name := strings.TrimPrefix(ws.Name, ws.extensionPrefix)
return ws.mutate(name), nil
}
func (ws workspace) Extension() (workspace, error) {
if ws.hasExtensionPrefix() {
return workspace{}, fmt.Errorf("workspace %s is already an extension", ws.id())
}
name := fmt.Sprintf("%s%s", ws.extensionPrefix, ws.Name)
return ws.mutate(name), nil
}
func (ws workspace) mutate(newName string) workspace {
return workspace{Workspace: i3.Workspace{Name: newName}, extensionPrefix: ws.extensionPrefix}
}