-
Notifications
You must be signed in to change notification settings - Fork 1
/
sys.go
52 lines (47 loc) · 1.31 KB
/
sys.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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
// run sys command in the interactive mode
func runCommand(line string) (string, error) {
cmd := exec.Command("sh", "-c", line)
output, err := cmd.CombinedOutput()
if code := cmd.ProcessState.ExitCode(); code != 0 {
return "", fmt.Errorf("%s", string(output))
}
return string(output), err
}
func sysCommandComplete(line []rune, pos int) ([][]rune, int) {
prefix := string(line)
prefix = strings.TrimLeft(prefix[1:], " ")
var suggests [][]rune
fields := strings.Fields(prefix)
// lookup the file in current path
if strings.HasSuffix(prefix, " ") || len(fields) > 1 {
var p string
if len(fields) > 1 && !strings.HasSuffix(prefix, " ") {
p = fields[len(fields)-1]
}
entries, _ := os.ReadDir("./")
for _, entry := range entries {
if strings.HasPrefix(entry.Name(), p) {
suggests = append(suggests, []rune(strings.TrimPrefix(entry.Name(), p)))
}
}
return suggests, len(p)
}
// lookup the commands in $PATH
paths := strings.Split(os.Getenv("PATH"), ":")
for _, path := range paths {
entries, _ := os.ReadDir(path)
for _, entry := range entries {
if strings.HasPrefix(entry.Name(), prefix) {
suggests = append(suggests, []rune(strings.TrimPrefix(entry.Name(), prefix)))
}
}
}
return suggests, pos - 1 // remove the $
}