@@ -82,21 +82,29 @@ func newTui() *tui {
82
82
func (t * tui ) start () int {
83
83
t .setAction ()
84
84
85
- t .stdinPane .reset ( )
86
- t .stdoutPane .reset ( )
85
+ stdinCtx , stdinCancel := context . WithCancel ( t .stdinPane .ctx )
86
+ stdoutCtx , stdoutCancel := context . WithCancel ( t .stdoutPane .ctx )
87
87
88
88
p := t .cliPane .prompt
89
+ t .cliPane .syncUpdate (func () {
90
+ t .cliPane .wg .Add (1 )
91
+ })
89
92
go func () {
93
+ defer t .cliPane .wg .Done ()
94
+ defer stdinCancel ()
95
+ defer stdoutCancel ()
90
96
if p == "" {
91
97
t .stdinPane .setData (stdinBytes )
92
98
} else {
93
- t .stdinPane .execCommand (p , stdinBytes )
99
+ t .stdinPane .execCommand (stdinCtx , p , stdinBytes )
94
100
}
95
101
var text string
96
102
t .QueueUpdate (func () {
97
103
text = t .cliPane .GetText ()
98
104
})
99
- t .stdoutPane .execCommand (text , t .stdinPane .data )
105
+ t .stdinPane .syncUpdate (func () {
106
+ t .stdoutPane .execCommand (stdoutCtx , text , t .stdinPane .data )
107
+ })
100
108
}()
101
109
102
110
if err := t .Run (); err != nil {
@@ -113,9 +121,16 @@ func (t *tui) setAction() {
113
121
return
114
122
}
115
123
t .stdoutPane .reset ()
124
+ stdoutCtx , stdoutCancel := context .WithCancel (t .stdoutPane .ctx )
116
125
117
126
go func () {
118
- t .stdoutPane .execCommand (text , t .stdinPane .data )
127
+ defer stdoutCancel ()
128
+ t .cliPane .syncUpdate (func () {
129
+ t .cliPane .wg .Wait ()
130
+ })
131
+ t .stdinPane .syncUpdate (func () {
132
+ t .stdoutPane .execCommand (stdoutCtx , text , t .stdinPane .data )
133
+ })
119
134
}()
120
135
})
121
136
@@ -140,6 +155,7 @@ func (t *tui) setAction() {
140
155
t .stdinPane .cancel ()
141
156
t .stdoutPane .cancel ()
142
157
t .Stop ()
158
+
143
159
if commandFlag {
144
160
fmt .Println (adjustPipe (t .cliPane .prompt ) + t .cliPane .GetText ())
145
161
return nil
@@ -150,7 +166,6 @@ func (t *tui) setAction() {
150
166
cmd .Stdout = os .Stdout
151
167
cmd .Stderr = os .Stderr
152
168
cmd .Run ()
153
-
154
169
return nil
155
170
156
171
case tcell .KeyBackspace , tcell .KeyBackspace2 :
@@ -161,15 +176,30 @@ func (t *tui) setAction() {
161
176
t .cliPane .setPrompt (t .cliPane .prompt )
162
177
163
178
t .stdinPane .reset ()
179
+ stdinCtx , stdinCancel := context .WithCancel (t .stdinPane .ctx )
164
180
t .stdoutPane .reset ()
181
+ stdoutCtx , stdoutCancel := context .WithCancel (t .stdoutPane .ctx )
165
182
166
183
p := t .cliPane .prompt
184
+ t .cliPane .syncUpdate (func () {
185
+ t .cliPane .wg .Add (1 )
186
+ })
167
187
go func () {
188
+ defer t .cliPane .wg .Done ()
189
+ defer stdinCancel ()
190
+ defer stdoutCancel ()
168
191
if p == "" {
169
192
t .stdinPane .setData (stdinBytes )
170
193
} else {
171
- t .stdinPane .execCommand (p , stdinBytes )
194
+ t .stdinPane .execCommand (stdinCtx , p , stdinBytes )
172
195
}
196
+ var text string
197
+ t .QueueUpdate (func () {
198
+ text = t .cliPane .GetText ()
199
+ })
200
+ t .stdinPane .syncUpdate (func () {
201
+ t .stdoutPane .execCommand (stdoutCtx , text , t .stdinPane .data )
202
+ })
173
203
}()
174
204
return nil
175
205
}
@@ -181,20 +211,21 @@ func (t *tui) setAction() {
181
211
t .cliPane .addPrompt ()
182
212
183
213
t .stdinPane .reset ()
214
+ stdinCtx , stdinCancel := context .WithCancel (t .stdinPane .ctx )
184
215
t .stdoutPane .reset ()
185
216
186
217
p := t .cliPane .prompt
218
+ t .cliPane .syncUpdate (func () {
219
+ t .cliPane .wg .Add (1 )
220
+ })
187
221
go func () {
222
+ defer t .cliPane .wg .Done ()
223
+ defer stdinCancel ()
188
224
if p == "" {
189
225
t .stdinPane .setData (stdinBytes )
190
226
} else {
191
- t .stdinPane .execCommand (p , stdinBytes )
227
+ t .stdinPane .execCommand (stdinCtx , p , stdinBytes )
192
228
}
193
- var text string
194
- t .QueueUpdate (func () {
195
- text = t .cliPane .GetText ()
196
- })
197
- t .stdoutPane .execCommand (text , t .stdinPane .data )
198
229
}()
199
230
return nil
200
231
case ' ' :
@@ -210,6 +241,8 @@ type cliPane struct {
210
241
symbol string
211
242
prompt string
212
243
skip bool
244
+ wg sync.WaitGroup
245
+ mu sync.Mutex
213
246
}
214
247
215
248
func newCliPane () * cliPane {
@@ -231,6 +264,12 @@ func newCliPane() *cliPane {
231
264
return c
232
265
}
233
266
267
+ func (c * cliPane ) syncUpdate (fn func ()) {
268
+ c .mu .Lock ()
269
+ defer c .mu .Unlock ()
270
+ fn ()
271
+ }
272
+
234
273
func (c * cliPane ) skipHandler () {
235
274
c .skip = true
236
275
}
@@ -318,26 +357,23 @@ func (si *stdinViewPane) setData(inputBytes []byte) {
318
357
si .syncUpdate (func () {
319
358
si .data = make ([]byte , len (inputBytes ))
320
359
copy (si .data , inputBytes )
321
- io . Copy ( w , bytes . NewReader ( inputBytes ))
322
- } )
360
+ }) //
361
+ io . Copy ( w , bytes . NewReader ( inputBytes ) )
323
362
}
324
363
325
- func (si * stdinViewPane ) execCommand (text string , inputBytes []byte ) {
364
+ func (si * stdinViewPane ) execCommand (ctx context. Context , text string , inputBytes []byte ) {
326
365
_data := new (bytes.Buffer )
327
366
tt := newTextLineTransformer ()
328
367
w := transform .NewWriter (si , tt )
329
368
mw := io .MultiWriter (w , _data )
330
369
331
- ctx , cancel := context .WithCancel (si .ctx )
332
- defer cancel ()
333
-
334
370
cmd := exec .CommandContext (ctx , shell , "-c" , text )
335
371
336
- si .syncUpdate (func () {
337
- cmd .Stdin = bytes .NewReader (inputBytes )
338
- cmd .Stdout = mw
372
+ cmd .Stdin = bytes .NewReader (inputBytes )
373
+ cmd .Stdout = mw
339
374
340
- cmd .Run ()
375
+ cmd .Run ()
376
+ si .syncUpdate (func () {
341
377
si .data = _data .Bytes ()
342
378
})
343
379
}
@@ -354,22 +390,17 @@ func newStdoutViewPane() *stdoutViewPane {
354
390
return so
355
391
}
356
392
357
- func (so * stdoutViewPane ) execCommand (text string , inputBytes []byte ) {
393
+ func (so * stdoutViewPane ) execCommand (ctx context. Context , text string , inputBytes []byte ) {
358
394
tt := newTextLineTransformer ()
359
395
w := transform .NewWriter (so , tt )
360
396
361
- ctx , cancel := context .WithCancel (so .ctx )
362
- defer cancel ()
363
-
364
397
cmd := exec .CommandContext (ctx , shell , "-c" , text )
365
398
366
- so .syncUpdate (func () {
367
- cmd .Stdin = bytes .NewReader (inputBytes )
368
- cmd .Stdout = w
369
- cmd .Stderr = w
399
+ cmd .Stdin = bytes .NewReader (inputBytes )
400
+ cmd .Stdout = w
401
+ cmd .Stderr = w
370
402
371
- cmd .Run ()
372
- })
403
+ cmd .Run ()
373
404
}
374
405
375
406
type textLineTransformer struct {
0 commit comments