-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneratecmd.go
69 lines (62 loc) · 1.93 KB
/
generatecmd.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
package main
import (
"encoding/json"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func init() {
rootCmd.AddCommand(generateMarksCmd)
}
// markCmd represents the mark command
var generateMarksCmd = &cobra.Command{
Use: "generate",
Aliases: []string{"purge"},
Short: "Generate marks from configuration preferences",
Long: `Generate marks from configuration preferences`,
Run: func(_ *cobra.Command, _ []string) {
generateMarks()
},
}
// utility function to pull attributes out from a map
func pluck(key string, input any) interface{} {
obj := input
val, ok := obj.(map[string]interface{})[key]
if !ok {
log.Tracef("Warning: key %v not found in %v", key, obj)
return nil
}
return val
}
func generateMarks() {
_, windowsList := getWindows()
preferences := viper.Get("preferences").(map[string]interface{})
log.Debugf("Preferences: %+v", preferences)
for i, windowObj := range windowsList {
windowName := pluck("app", windowObj).(string)
windowTitle := pluck("title", windowObj).(string)
log.Debugf("Considering window %v, %+v", i, windowName)
for prefName := range preferences {
if !strings.Contains(strings.ToLower(windowName), strings.ToLower(prefName)) {
continue
}
prefMark := pluck("mark", preferences[prefName]).(string)
if prefTitle := pluck("title", preferences[prefName]); prefTitle != nil {
t := prefTitle.(string)
if !strings.Contains(strings.ToLower(windowTitle), strings.ToLower(t)) {
continue
}
}
log.Debugf(" [>] Found preferred window %v (app %v), applying mark %v", windowTitle, windowName, prefMark)
jsonBA, err := json.Marshal(windowObj)
if err != nil {
log.Warningf("Warning: Error converting window obj %v to json string", windowObj)
}
m := NewWinMarker()
if err := m.SaveMark([]rune(prefMark)[0], jsonBA); err != nil {
log.Warningf("Warning: Error saving mark %v: %v", prefMark, err)
}
}
}
}