-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorn.go
192 lines (166 loc) · 4.01 KB
/
gorn.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
type Cache struct {
Paths map[string]Path
History History
}
func (cache *Cache) where() string {
// Where's the cache?
cacheDir := os.Getenv("XDG_CACHE_HOME")
if cacheDir == "" {
cacheDir = filepath.Join(os.Getenv("HOME"), ".cache")
}
// Per the freedesktop spec, non-existent directories should be created 0700
os.MkdirAll(cacheDir, 0700)
cacheName := filepath.Join(cacheDir, "gorn.json")
return cacheName
}
func (cache *Cache) Write() {
cacheName := cache.where()
// serialize previous input list and write
// serialize paths and write
out, _ := os.Create(cacheName)
enc := json.NewEncoder(out)
enc.Encode(&cache)
}
func (cache *Cache) Read() {
cacheName := cache.where()
// Read the cache
in, _ := os.Open(cacheName)
dec := json.NewDecoder(in)
dec.Decode(&cache)
// Make sure the history map is up-to-date
cache.History.MakeMap()
}
type Path struct {
Dir string
Execs []string
Mtime int64
}
type History struct {
// the canonical list
S []string
// a map to make lookups quicker
m map[string]int
}
func (h *History) Clean() {
// remove dead entries before serialization
var cleanHistory []string
for _, command := range h.S {
executable := strings.Split(command, " ")[0]
_, err := exec.LookPath(executable)
if err != nil {
log.Printf("Pruning lost command: %s\n", command)
continue
}
cleanHistory = append(cleanHistory, command)
}
h.S = cleanHistory
}
func (h *History) Add(s string) {
// since we only add once per run, we don't need to recalculate the map
// add to beginning of list
newHistory := []string{s}
// if dmenu output in previous input
if i, ok := h.m[s]; ok {
// remove it
before := h.S[:i]
after := h.S[i+1:]
h.S = append(before, after...)
}
newHistory = append(newHistory, h.S...)
h.S = newHistory
log.Println(h.S)
}
func (h *History) MakeMap() {
// Only needs to be called once per run
// Populate history map
m := make(map[string]int)
h.m = m
for i, exec := range h.S {
h.m[exec] = i
}
}
func main() {
var cache Cache
cache.Read()
candidates := make(map[string]string)
// Check timestamps of everything on $PATH. If the timestamp is newer,
// regenerate that path
pathEnv := os.Getenv("PATH")
paths := strings.Split(pathEnv, ":")
for _, path := range paths {
if path == "." {
continue
}
fi, e := os.Stat(path)
if e != nil {
continue
}
mtime := fi.ModTime().Unix()
if cache.Paths[path].Mtime != mtime {
// Regenerate path
if len(cache.Paths) == 0 {
cache.Paths = make(map[string]Path, 64)
}
cache.Paths[path] = regenerate(path)
}
// now that the cache is up-to-date, read it and add to candidates
for _, exec := range cache.Paths[path].Execs {
// if it's not in previous input
if _, ok := cache.History.m[exec]; !ok {
// add it to candidates
candidates[exec] = exec
}
}
}
var input []string
// print previous input in order ...
for _, exec := range cache.History.S {
input = append(input, exec)
}
// print candidates in any order
for exec := range candidates {
input = append(input, exec)
}
inputJoined := strings.Join(input, "\n")
reader := strings.NewReader(inputJoined)
// get dmenu output
dmenu := exec.Command("dmenu", os.Args[1:]...)
dmenu.Stdin = reader
dmenuBytes, _ := dmenu.Output()
dmenuOut := strings.TrimSpace(string(dmenuBytes))
// run it, without a shell
progParts := strings.Split(dmenuOut, " ")
path, err := exec.LookPath(progParts[0])
if err != nil {
log.Fatal("executable not found in path")
}
prog := exec.Command(path, progParts[1:]...)
prog.Start()
cache.History.Add(dmenuOut)
cache.History.Clean()
cache.Write()
}
func regenerate(pathname string) Path {
var p Path
p.Dir = pathname
fi, _ := os.Stat(pathname)
p.Mtime = fi.ModTime().Unix()
fileinfos, _ := ioutil.ReadDir(pathname)
for _, fi := range fileinfos {
// Is it an executable?
if fi.IsDir() == false && fi.Mode()&0111 != 0 {
p.Execs = append(p.Execs, fi.Name())
}
}
return p
}