-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
179 lines (161 loc) · 3.86 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
package main
import (
"fmt"
"io"
"log"
"regexp"
"strings"
"os"
"sort"
sgr "github.com/foize/go.sgr"
"github.com/peterh/liner"
)
var (
assignPattern = regexp.MustCompile(`^\s*[_a-zA-Z][_a-zA-Z0-9]*(\s*,\s*[_a-zA-Z][_a-zA-Z0-9]*)*\s*=\s*.*$`)
cdPattern = regexp.MustCompile(`^\s*cd\s*.*$`)
commands = map[string]string{
"?": "Show this help",
"cd": "Change current working directory",
"clear": "Clear the workspace",
"exit": "Exit the interactive shell",
"help": "Show this help",
"ls": "Show files in current directory",
"pwd": "Show current working directory",
"quit": "Quit the interactive shell",
"whos": "Show all varaibles in workspace",
}
cmds []string
line *liner.State
workspace = map[string]*GoroutineDump{}
)
func init() {
cmds = make([]string, 0, len(commands))
for k := range commands {
cmds = append(cmds, k)
}
sort.Sort(sort.StringSlice(cmds))
}
func main() {
line = createLiner()
defer line.Close()
defer saveLiner(line)
for {
if cmd, err := line.Prompt(">> "); err == nil {
cmd = strings.TrimSpace(cmd)
if cmd == "" {
continue
}
line.AppendHistory(cmd)
switch cmd {
case "?", "help":
printHelp()
case "clear":
workspace = map[string]*GoroutineDump{}
fmt.Println("Workspace cleared.")
case "exit", "quit":
return
case "ls":
wd, err := os.Getwd()
if err != nil {
fmt.Println(err)
continue
}
printDir(wd)
case "pwd":
wd, err := os.Getwd()
if err != nil {
fmt.Println(err)
continue
}
fmt.Println(wd)
case "whos":
if len(workspace) == 0 {
fmt.Println("No variables defined.")
continue
}
for k := range workspace {
fmt.Printf("%s\t", k)
}
fmt.Println()
default:
if cdPattern.MatchString(cmd) {
// Change directory.
idx := strings.Index(cmd, "cd")
dir := strings.TrimSpace(cmd[idx+2:])
if dir == "" {
fmt.Println("Expect command \"cd <dir>\"")
continue
}
if err := os.Chdir(dir); err != nil {
fmt.Println(err)
}
continue
}
// Assignment.
if assignPattern.MatchString(cmd) {
if err := assign(cmd); err != nil {
fmt.Printf("Error, %s.\n", err.Error())
}
continue
}
if err := expr(cmd); err != nil {
fmt.Printf("Error, %s.\n", err.Error())
}
}
} else if err == liner.ErrPromptAborted || err == io.EOF {
fmt.Println()
break
} else {
log.Print("Error reading line: ", err)
}
}
}
func printDir(wd string) {
f, err := os.Open(wd)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
fis, err := f.Readdir(-1)
if err != nil {
fmt.Println(err)
return
}
sort.Slice(fis, func(i, j int) bool {
return fis[i].Name() < fis[j].Name()
})
for _, fi := range fis {
if fi.IsDir() {
sgr.Printf("[fg-blue]%s[reset]\n", fi.Name())
} else {
fmt.Println(fi.Name())
}
}
}
func printHelp() {
fmt.Println("Commands:")
for _, k := range cmds {
fmt.Printf(" %12s: %s\n", k, commands[k])
}
fmt.Println()
fmt.Println("Statements:")
fmt.Println("\t<var>")
fmt.Println("\t<var> = load(\"<file-name>\")")
fmt.Println("\t<var> = <another-var>")
fmt.Println("\t<var> = <another-var>.copy()")
fmt.Println("\t<var> = <another-var>.copy(\"<condition>\")")
fmt.Println("\t<var>.delete(\"<condition>\")")
fmt.Println("\tleft = <var>.diff(<another-var>)")
fmt.Println("\tleft, common = <var>.diff(<another-var>)")
fmt.Println("\tleft, common, right = <var>.diff(<another-var>)")
fmt.Println("\t<var>.keep(\"<condition>\")")
fmt.Println("\t<var>.save(\"<output-file-name>\")")
fmt.Println("\t<var>.search(\"<condition>\")")
fmt.Println("\t<var>.search(\"<condition>\", offset)")
fmt.Println("\t<var>.search(\"<condition>\", offset, limit)")
fmt.Println("\t<var>.show()")
fmt.Println("\t<var>.show(offset)")
fmt.Println("\t<var>.show(offset, limit)")
fmt.Println()
}