-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistcmd.go
95 lines (81 loc) · 2.18 KB
/
listcmd.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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
// "strings"
"github.com/MakeNowJust/heredoc"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(listMarksCmd)
}
// markCmd represents the mark command
var listMarksCmd = &cobra.Command{
Use: "ls",
Aliases: []string{"list"},
Short: "List marked windows",
Long: `List all marked windows, displaying their mark with the window title and app`,
Run: func(_ *cobra.Command, _ []string) {
listMarks()
},
}
func listMarks() {
log.Printf("Listing marks under %v", stateHome)
currentMarks, _ := findMarks()
for _, v := range currentMarks {
fmt.Printf("%v", v)
}
}
func findMarks() ([]string, []string) {
windowsById, _ := getWindows()
matches, _ := filepath.Glob(fmt.Sprintf("%s/*", stateHome))
lines := []string{}
stale := []string{}
for _, match := range matches {
f, _ := os.Stat(match)
if !f.IsDir() {
data, err := os.ReadFile(match)
if err != nil {
log.Printf("ERROR: could not read file", match)
}
id, err := jq(".id", string(data))
if err != nil {
log.Printf("Could not find .id under %v", data)
}
if obj, ok := windowsById[fmt.Sprint(id)]; ok {
line := fmt.Sprintf("%v ⟶ %v\n",
filepath.Base(match),
strings.Join(obj, " | "))
lines = append(lines, line)
} else {
log.Printf("id %v not in windowsById", id)
stale = append(stale, match)
}
}
}
return lines, stale
}
func getWindows() (map[string][]string, []interface{}) {
windowsById := make(map[string][]string)
var windowsList []interface{}
sc := yabaiscript(heredoc.Doc(`yabai -m query --windows`))
if _, stdout, stderr, err := sc.Exec(); err == nil {
if err := json.Unmarshal([]byte(stdout.String()), &windowsList); err != nil {
log.Fatalf("Error marshalling JSON: %v", err)
}
for _, v := range windowsList {
winMap := v.(map[string]interface{})
id := fmt.Sprint(winMap["id"])
title := winMap["title"].(string)
app := winMap["app"].(string)
windowsById[id] = []string{title, id, app}
}
} else {
log.Fatalf("Error running yabai: err: %s, stderr: %s", err, stderr.String())
}
return windowsById, windowsList
}