-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepl.go
149 lines (129 loc) · 2.42 KB
/
repl.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
package main
import (
"fmt"
"os/exec"
"strings"
"sync"
"9fans.net/go/acme"
"github.com/golang/glog"
)
type repl struct {
sync.Mutex
wi acme.WinInfo
lazyOutw *acme.Win
busych chan bool
sid string
}
func newRepl(wi acme.WinInfo) (*repl, error) {
r := &repl{wi: wi, busych: make(chan bool, 1)}
b, err := exec.Command("gonrepl", "--clone").Output()
if err != nil {
return nil, err
}
r.sid = strings.TrimSpace(string(b))
debugLog("allocated sid %q", r.sid)
return r, nil
}
func (r *repl) enter(expr string) {
res, err := r.eval(expr)
r.report("%s", res)
if err != nil {
r.report("%v", err)
}
}
func (r *repl) eval(expr string) (string, error) {
debugLog("evaluating: %s", expr)
defer r.Busy()()
c := exec.Command("gonrepl", "-s", r.sid)
c.Stdin = strings.NewReader(expr)
b, err := c.CombinedOutput()
return string(b), err
}
func (r *repl) report(format string, args ...interface{}) error {
win, err := r.outWin()
if err != nil {
return err
}
win.PrintTabbed(fmt.Sprintf(format, args...))
win.Ctl("clean")
return nil
}
func (r *repl) outWin() (*acme.Win, error) {
r.Lock()
defer r.Unlock()
if r.lazyOutw == nil {
w, err := r.createOutputWindow()
if err != nil {
return nil, err
}
go r.eventLoop(w)
r.lazyOutw = w
}
return r.lazyOutw, nil
}
func (r *repl) createOutputWindow() (*acme.Win, error) {
w, err := acme.New()
if err != nil {
return nil, err
}
w.Name("%s+REPL", r.wi.Name)
w.Ctl("clean")
w.Ctl("nomark")
w.Ctl("nomenu")
return w, nil
}
func (r *repl) start() {
go r.busyController()
}
func (r *repl) Busy() func() {
r.busych <- true
return func() {
r.busych <- false
}
}
func (r *repl) busyController() {
var win *acme.Win
busy := 0
for b := range r.busych {
if win == nil {
w, err := r.outWin()
if err != nil {
glog.Errorf("%v", err)
continue
}
win = w
}
if b {
busy++
} else {
busy--
}
debugLog("busyness is %d, setting flag accordingly", busy)
if busy > 0 {
win.Ctl("dirty")
} else {
win.Ctl("clean")
}
}
}
func (r *repl) eventLoop(win *acme.Win) {
for e := range win.EventChan() {
win.WriteEvent(e)
}
debugLog("repl closed")
r.Lock()
defer r.Unlock()
r.lazyOutw = nil
}
func (r *repl) close() {
r.Lock()
defer r.Unlock()
if r.lazyOutw != nil {
win := r.lazyOutw
win.Del(true)
}
_, err := exec.Command("gonrepl", "--close", "-s", r.sid).Output()
if err != nil {
glog.Errorf("%v", err)
}
}