-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcmd.go
230 lines (217 loc) · 4.82 KB
/
cmd.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"context"
"errors"
"fmt"
"io"
"path/filepath"
"strings"
"sync/atomic"
"github.com/as/edit"
"github.com/as/event"
"github.com/as/frame"
"github.com/as/path"
"github.com/as/shiny/event/lifecycle"
"github.com/as/text"
"github.com/as/ui/tag"
"github.com/as/ui/win"
)
var (
ErrBadFD = errors.New("bad file descriptor")
ErrNoFD = errors.New("no fd")
)
type Cmd interface {
Arg() []string
Fd(int) (io.ReadWriter, error)
Env() []string
Start() error
Wait() error
Redir(fd int, src io.ReadWriter) error
}
func editcmd(ed interface{}, origin, cmd string) {
prog, err := edit.Compile(cmd, &edit.Options{Sender: nil, Origin: origin})
if err != nil {
logf("editcmd: %s", err)
return
}
runeditcmd(prog, ed)
{
ed, _ := ed.(text.Editor)
if ed != nil {
ajump2(ed, false)
}
}
}
func runeditcmd(prog *edit.Command, ed interface{}) {
switch ed := ed.(type) {
case *win.Win:
if ed == actTag.Label {
ed = actTag.Window.(*win.Win)
}
prog.Run(ed)
case *tag.Tag:
prog.Run(ed.Window)
case *Grid:
for _, ed := range ed.List {
runeditcmd(prog, ed)
}
case *Col:
for _, ed := range ed.List {
runeditcmd(prog, ed)
}
case text.Editor:
prog.Run(ed)
case interface{}:
logf("dont know what %T is", ed)
}
}
func getcmd(t *tag.Tag) {
// Add rendering here if image?
if *images && tryImage(t.FileName()) {
render(t)
} else {
t.Get(t.FileName())
}
}
var (
// ndel is a delete counter. writers monitor its status to avoid
// writing to deleted windows
ndel uint32
)
func acmd(e event.Cmd) {
s := string(e.P)
switch s {
case "Img":
renderimage(actTag)
repaint()
case "Load":
Load(g, "a.dump")
case "Dump":
Dump(g, g.cwd(), "gomono", "goregular")
case "Elastic":
t := actTag
w, _ := t.Window.(*win.Win)
if w != nil && w.Frame != nil {
cf := &t.Config.Body.Frame
if cf.Flag&frame.FrElastic == 0 {
cf.Flag |= frame.FrElastic
} else {
cf.Flag &^= frame.FrElastic
}
cf.Flag |= frame.FrElastic
w.Frame.SetFlags(cf.Flag)
w.Resize(w.Size())
}
repaint()
case "Font":
if actTag == g.Tag {
nextFace(g)
} else if actTag == actCol.Tag {
nextFace(actCol)
} else {
nextFace(actTag)
}
case "Put":
actTag.Put()
repaint()
case "Get":
getcmd(actTag)
//repaint()
case "New":
newtag := New(actCol, "", "")
moveMouse(newtag.Bounds().Min)
case "Newcol":
moveMouse(NewColParams(g, "").Bounds().Min)
case "Undo", "Redo":
do(s == "Undo")
case "Del":
w := Del(actCol, actCol.ID(actTag))
atomic.AddUint32(&ndel, +1)
w.Close()
case "Sort":
logf("Sort: TODO")
case "Delcol":
ws := Delcol(g, g.ID(actCol))
atomic.AddUint32(&ndel, 1)
for _, w := range ws {
if w, _ := w.(io.Closer); w != nil {
w.Close()
}
}
case "Exit":
D.Lifecycle <- lifecycle.Event{To: lifecycle.StageDead}
return
case "Diff":
Diff(actTag)
default:
if len(e.To) == 0 {
logf("cmd has no destination: %q", s)
}
abs := AbsOf(e.Basedir, e.Name)
p := prefixer{string: strings.TrimSpace(s)}
switch {
case p.Prefix("|"):
editcmd(e.To[0], abs, s)
case p.Prefix("<"):
cmdexec(context.Background(), actTag, actTag, path.DirOf(abs), strings.Fields(p.Chop())...)
case p.Prefix(">"):
cmdexec(context.Background(), nil, actTag, path.DirOf(abs), strings.Fields(p.Chop())...)
case p.Prefix("Edit"):
editcmd(e.To[0], abs, p.Chop())
case p.Prefix("Look"):
// Determine where this command is executed. If it's at the grid or column
// labels, we run Look on all the text.Editors below that point. A column
// runs Look on all planes under the column. A Grid does the same for
// all columns.
//
// Use the track data structure to determine what Editor was last selected
// and use that as the search parameter. Seperate the two cases so the
// new feature is less likely to blow up the process if it has bugs--it's
// less likely to be used anyway
if k, from := KindOf(e.From); k.List() {
data := track.win.Rdsel()
VisitAll(from, func(p Named) {
switch ed := p.(type) {
case nil:
return
case text.Editor:
q0, q1 := ed.Dot()
lookliteraltag(ed, q0, q1, data)
}
})
} else {
switch ed := actTag.Window.(type) {
case *win.Win:
q0, q1 := ed.Dot()
lookliteraltag(ed, q0, q1, ed.Rdsel())
}
}
case p.Prefix("Install"):
g.Install(actTag, p.Chop())
default:
cmdexec(context.Background(), nil, nil, path.DirOf(abs), strings.Fields(s)...)
}
reload(e.To[0])
}
}
type prefixer struct {
string
n int
}
func (p *prefixer) Prefix(pre string) bool {
p.n = 0
if !strings.HasPrefix(p.string, pre) {
return false
}
p.n = len(pre)
return true
}
func (p *prefixer) Chop() string {
if p.n != 0 {
return p.string[p.n:]
}
return p.string
}
func cmdlabel(name, dir string) (label string) {
return fmt.Sprintf("%s%c-%s", dir, filepath.Separator, name)
}