-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
71 lines (61 loc) · 1.2 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
package main
import (
"flag"
"fmt"
"os"
"runtime/debug"
"time"
"github.com/golobby/repl/interpreter"
"github.com/c-bata/go-prompt"
)
var (
currentInterpreter *interpreter.Interpreter
DEBUG bool
)
const (
version = "0.1.2"
)
func completer(d prompt.Document) []prompt.Suggest {
var s []prompt.Suggest
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}
func handler(input string) {
defer func() {
if err := recover(); err != nil {
fmt.Printf("Panic: %v\n%s", err, debug.Stack())
}
}()
var start time.Time
if DEBUG {
start = time.Now()
}
out, err := currentInterpreter.Eval(input)
if err != nil {
fmt.Print(err.Error())
return
}
fmt.Print(out)
if DEBUG {
fmt.Printf(":::::: D => %v\n", time.Since(start))
}
}
func main() {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
debug := flag.Bool("timestamp", false, "turns timestamp mode on")
flag.Parse()
DEBUG = *debug
currentInterpreter, err = interpreter.NewSession(wd)
if err != nil {
panic(err)
}
_, err = currentInterpreter.Eval(":e 1")
if err != nil {
panic(err)
}
fmt.Printf("repl v%s\n", version)
p := prompt.New(handler, completer, prompt.OptionPrefix("repl> "))
p.Run()
}