-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_windows.go
96 lines (80 loc) · 2.04 KB
/
run_windows.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
// +build windows
package main
import (
"io/fs"
"io/ioutil"
"os"
"strings"
"golang.org/x/sys/windows"
)
const pathVarSep = ";"
var shortcutDirs = []string{
"C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs",
}
var dirMap = make(map[string]int)
func runSelected(selection string) {
if strings.HasSuffix(selection, ".lnk") {
idx := dirMap[selection]
runLnk(selection, idx)
} else {
runProg(selection)
}
}
func runLnk(lnk string, idx int) {
base := shortcutDirs[idx]
lnk = base + "\\" + lnk
err := shellExecute(lnk)
if err != nil {
panic(err)
}
}
func shellExecute(lnk string) error {
var handle windows.Handle = 0;
args := windows.StringToUTF16Ptr(lnk)
var show int32 = 9
return windows.ShellExecute(handle, nil, args, nil, nil, show)
}
func findRunnableThings() []string {
cacheDir, err := os.UserConfigDir()
if err == nil {
startmenuFolder2 := cacheDir + "\\Microsoft\\Windows\\Start Menu\\Programs"
shortcutDirs = append(shortcutDirs, startmenuFolder2)
}
execs := findExecutablesInPath()
shortcuts := []string{}
shortcuts, dirMap = findShortcuts()
return append(execs, shortcuts...)
}
func findShortcuts() ([]string, map[string]int) {
dirMap := make(map[string]int, 128);
shortcuts := make([]string, 0, 128)
for dirIdx, dir := range shortcutDirs {
newShortcuts := findShortcutsInDir(dir)
for i, s := range newShortcuts {
r := s[len(dir) + 1:]
newShortcuts[i] = r
dirMap[r] = dirIdx
}
shortcuts = append(shortcuts, newShortcuts...)
}
return shortcuts, dirMap
}
func findShortcutsInDir(dir string) []string {
entries, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
shortcuts := make([]string, 0, 32)
for _, entry := range entries {
if entry.IsDir() {
dirShortcuts := findShortcutsInDir(dir + "\\" + entry.Name())
shortcuts = append(shortcuts, dirShortcuts...)
} else {
shortcuts = append(shortcuts, dir + "\\" + entry.Name())
}
}
return shortcuts
}
func isExecutable(file fs.FileInfo) bool {
return file.Mode() & 0111 != 0 || strings.HasSuffix(file.Name(), ".exe")
}