/
main.go
348 lines (290 loc) · 8.23 KB
/
main.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"strings"
"syscall"
"github.com/Coffelius/rofi-code/workspace"
"github.com/Wing924/shellwords"
"github.com/akamensky/argparse"
)
// Storage struct represents the structure of a storage.json file
type Storage struct {
OpenedPathsList struct {
Workspaces3 []string `json:"workspaces3"`
} `json:"openedPathsList"`
LastKnownMenuBarData struct {
Menus struct {
File struct {
Items []struct {
ID string `json:"id"`
Label string `json:"label"`
Submenu struct {
Items []struct {
ID string `json:"id"`
URI struct {
External string `json:"external"`
Path string `json:"path"`
} `json:"uri"`
} `json:"items"`
} `json:"submenu"`
} `json:"items"`
} `json:"File"`
} `json:"menus"`
} `json:"lastKnownMenuBarData"`
}
func newWorkspaceFromPath(j string) (*workspace.Workspace, error) {
var workspace workspace.Workspace
var err error
workspace.Folder = j
if len(workspace.Folder) <= 8 || !strings.HasPrefix(workspace.Folder, "file:///") {
return nil, errors.New("Wrong folder " + j)
}
workspace.Folder = workspace.Folder[7:]
workspace.Name = workspace.Folder
workspace.Basename = path.Base(workspace.Name)
_, err = os.Stat(workspace.Folder)
if err != nil {
return nil, err
}
return &workspace, nil
}
// Get the list of workspaces from the storage.json file
func getWorkspacesFromStorage(s string) workspace.WorkspaceCollection {
var workspaces workspace.WorkspaceCollection
var storage Storage
var err error
err = loadJSON(path.Join(s, "storage.json"), &storage)
if err != nil {
return workspaces
}
for _, j := range storage.OpenedPathsList.Workspaces3 {
var workspace *workspace.Workspace
var err error
workspace, err = newWorkspaceFromPath(j)
if err != nil {
continue
}
if !*fullpath {
workspace.Name = contractTilde(workspace.Name)
}
workspaces = append(workspaces, *workspace)
}
for _, item := range storage.LastKnownMenuBarData.Menus.File.Items {
if item.ID != "openRecentFolder" || len(item.Submenu.Items) == 0 {
continue
}
for _, submenu := range item.Submenu.Items {
var workspace *workspace.Workspace
workspace, err = newWorkspaceFromPath(submenu.URI.External)
if err != nil {
continue
}
if !*fullpath {
workspace.Name = contractTilde(workspace.Name)
}
workspaces = append(workspaces, *workspace)
}
}
return workspaces
}
func loadJSON(filename string, v interface{}) error {
var err error
jsonFile, err := os.Open(filename)
// if we os.Open returns an error then handle it
if err != nil {
return err
}
defer jsonFile.Close()
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
return err
}
err = json.Unmarshal(byteValue, v)
return err
}
func getWorkspace(s string) (*workspace.Workspace, error) {
var modifiedtime int64
var err error
var file os.FileInfo
if *sortOption == "time" {
file, err = os.Stat(path.Join(s, "state.vscdb"))
if err != nil {
return nil, err
}
modifiedtime = file.ModTime().Unix()
}
var workspace workspace.Workspace
err = loadJSON(path.Join(s, "workspace.json"), &workspace)
if err != nil {
return nil, err
}
workspace.Time = modifiedtime
if len(workspace.Folder) <= 8 || !strings.HasPrefix(workspace.Folder, "file:///") {
return nil, errors.New("Protocol unknown in url " + workspace.Folder)
}
workspace.Folder = workspace.Folder[7:]
workspace.Name = workspace.Folder
workspace.Basename = path.Base(workspace.Name)
file, err = os.Stat(workspace.Folder)
if err != nil {
return nil, err
}
return &workspace, nil
}
var homeDir string
var rofiCmd *string
var codeCmd *string
var sortOption *string
var fullpath *bool
var basePath string
func init() {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
homeDir = usr.HomeDir
}
func expandTilde(s string) string {
// replace tilde by homedir
if strings.HasPrefix(s, "~/") {
return filepath.Join(homeDir, (s)[2:])
}
return s
}
func contractTilde(s string) string {
if strings.Index(s, homeDir) == 0 {
return "~" + s[len(homeDir):]
}
return s
}
func runRofi(workspaces workspace.WorkspaceCollection) {
args, err := shellwords.Split(*rofiCmd)
if err != nil {
log.Fatal(err)
}
cmd := exec.Command(args[0], args[1:]...)
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
go func() {
defer stdin.Close()
for _, s := range workspaces {
io.WriteString(stdin, s.Name+"\n")
}
}()
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
args, err = shellwords.Split(*codeCmd)
if err != nil {
log.Fatal(err)
}
selectedItem := strings.TrimSpace(string(out))
selectedItem = expandTilde(selectedItem)
args = append(args, selectedItem)
executable, err := exec.LookPath(args[0])
if err != nil {
log.Fatal(err)
}
err = syscall.Exec(executable, args, os.Environ())
if err != nil {
log.Fatal(err)
}
}
func getWorkspacesFromUserWorkspace(basePath string) workspace.WorkspaceCollection {
var workspaces workspace.WorkspaceCollection
paths, err := filepath.Glob(path.Join(basePath, "User/workspaceStorage/*"))
if err != nil {
return workspaces
}
for _, s := range paths {
workspace, err := getWorkspace(s)
if err == nil {
// replace home path by tilde
if !*fullpath {
workspace.Name = contractTilde(workspace.Name)
}
workspaces = append(workspaces, *workspace)
}
}
return workspaces
}
func detectCodeExecutablePath() (*string, error) {
var binaries = []string{"codium", "code"}
for _, binary := range binaries {
path, err := exec.LookPath(binary)
if err == nil {
return &path, nil
}
}
return nil, errors.New("No Code or Codium automatically found. You will have to specify an alternative editor executable using the --code option")
}
func main() {
var err error
// Create new parser object
parser := argparse.NewParser("rofi-code", "Use rofi to quickly open a VSCode or Codium workspace")
// Create string flag
s := parser.String("d", "dir", &argparse.Options{Required: false, Help: "Comma separated paths to the config directories", Default: "~/.config/VSCodium,~/.config/Code,~/.config/Code\\ -\\ OSS"})
sortOption = parser.Selector("s", "sort", []string{"time", "path", "name"}, &argparse.Options{Required: false, Help: "How the workspaces should be sorted", Default: "time"})
fullpath = parser.Flag("f", "full", &argparse.Options{Required: false, Help: "Show the full path instead of collapsing the home directory to a tilde", Default: false})
var output *bool = parser.Flag("o", "output", &argparse.Options{Required: false, Help: "Just prints the workspaces to stdout and exit", Default: false})
insensitive := parser.Flag("i", "insensitive", &argparse.Options{Required: false, Help: "Case insensitive search", Default: false})
rofiCmd = parser.String("r", "rofi", &argparse.Options{Required: false, Help: "Command line to launch rofi", Default: "rofi -dmenu -p \"Open workspace\" -no-custom"})
codeCmd = parser.String("c", "code", &argparse.Options{Required: false, Help: "Command line to launch the editor. It will try to detect codium or code", Default: nil})
err = parser.Parse(os.Args)
if err != nil {
fmt.Print(parser.Usage(err))
}
if *codeCmd == "" {
codeCmd, err = detectCodeExecutablePath()
if err != nil {
log.Fatal(err)
}
}
if *insensitive {
*rofiCmd += " -i"
}
var basePaths []string
basePaths = strings.Split(*s, ",")
for i := range basePaths {
basePaths[i] = expandTilde(strings.TrimSpace(basePaths[i]))
}
var workspaces workspace.WorkspaceCollection
for _, basePath := range basePaths {
workspaces = append(workspaces, getWorkspacesFromUserWorkspace(basePath)...)
}
if *sortOption == "time" {
workspaces.Sort(*sortOption)
}
var workspacesFromJSON workspace.WorkspaceCollection
for _, basePath := range basePaths {
workspacesFromJSON = append(workspacesFromJSON, getWorkspacesFromStorage(basePath)...)
}
workspaces = append(workspacesFromJSON, workspaces...)
workspaces = workspaces.Unique()
if len(workspaces) == 0 {
log.Fatal("Not found any workspace")
}
if *sortOption != "time" {
workspaces.Sort(*sortOption)
}
if *output {
for _, s := range workspaces {
fmt.Println(s.Name)
}
return
}
runRofi(workspaces)
}