/
main.go
107 lines (90 loc) · 1.92 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
package main
import (
"fmt"
"os"
"path"
"strings"
"github.com/sbinet/liner"
_ "github.com/sbinet/go-croot/pkg/croot"
)
var term *liner.State = nil
func init() {
fmt.Println(`
************************
** PAW-Go interpreter **
************************
`)
fmt.Printf(":: available commands:\n%v\n::\n\n", env)
term = liner.NewLiner()
fname := path.Join(os.Getenv("HOME"), ".pawgo.history")
f, err := os.Open(fname)
if err != nil {
f, err = os.Create(fname)
if err != nil {
fmt.Printf("**warning: could not access nor create history file [%s]\n", fname)
return
}
}
defer f.Close()
_, err = term.ReadHistory(f)
if err != nil {
fmt.Printf("**warning: could not read history file [%s]\n", fname)
return
}
term.SetCompleter(paw_completer)
}
func atexit() {
fname := path.Join(os.Getenv("HOME"), ".pawgo.history")
f, err := os.OpenFile(fname, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
fmt.Printf("**warning: could not access history file [%s]\n", fname)
return
}
defer f.Close()
_, err = term.WriteHistory(f)
if err != nil {
fmt.Printf("**warning: could not write history file [%s]\n", fname)
return
}
err = term.Close()
if err != nil {
fmt.Printf("**warning: problem closing term: %v\n", err)
return
}
}
var paw_completer liner.Completer = func(line string) []string {
completions := []string{}
for _,cmd := range env.CommandNames() {
if strings.HasPrefix(cmd, line) {
completions = append(completions, cmd)
}
}
return completions
}
func main() {
defer atexit()
prompt := "paw> "
cmd := ""
for {
line, err := term.Prompt(prompt)
if err != nil {
break //os.Exit(0)
}
cmd = line
if cmd != "" {
for _, ll := range strings.Split(cmd, "\n") {
term.AppendHistory(ll)
}
} else {
continue
}
_, err = env.Run(cmd)
if err != nil {
fmt.Println(err.Error())
fmt.Printf("(error %T)\n", err)
continue
}
}
fmt.Printf("\n:: bye.\n")
}
// EOF