-
Notifications
You must be signed in to change notification settings - Fork 385
/
filter.go
810 lines (702 loc) · 21.9 KB
/
filter.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
// Copyright 2019-2022 Graham Clark. All rights reserved. Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.
// Package filter provides a termshark-specific edit widget which changes
// color according to the validity of its input, and which activates a
// drop-down menu of possible completions for the term at point.
package filter
import (
"context"
"fmt"
"io"
"os/exec"
"sync"
"syscall"
"time"
"unicode"
"github.com/gcla/gowid"
"github.com/gcla/gowid/gwutil"
"github.com/gcla/gowid/vim"
"github.com/gcla/gowid/widgets/button"
"github.com/gcla/gowid/widgets/cellmod"
"github.com/gcla/gowid/widgets/columns"
"github.com/gcla/gowid/widgets/edit"
"github.com/gcla/gowid/widgets/framed"
"github.com/gcla/gowid/widgets/holder"
"github.com/gcla/gowid/widgets/hpadding"
"github.com/gcla/gowid/widgets/list"
"github.com/gcla/gowid/widgets/menu"
"github.com/gcla/gowid/widgets/pile"
"github.com/gcla/gowid/widgets/styled"
"github.com/gcla/gowid/widgets/text"
"github.com/gcla/termshark/v2"
"github.com/gcla/termshark/v2/pkg/fields"
"github.com/gcla/termshark/v2/widgets/appkeys"
"github.com/gdamore/tcell/v2"
)
//======================================================================
// This is a debugging aid - I use it to ensure goroutines stop as expected. If they don't
// the main program will hang at termination.
var Goroutinewg *sync.WaitGroup
var fixed gowid.RenderFixed
type filtStruct struct {
txt string
app gowid.IApp
}
type Widget struct {
wrapped gowid.IWidget
opts Options
ed *edit.Widget // what the user types into - wrapped by validity styling
dropDown *menu.Widget // the menu of possible completions
dropDownSite *menu.SiteWidget // where in this widget structure the drop down is rendered
validitySite *holder.Widget // the widget swaps out the contents of this placeholder on validity changes
valid gowid.IWidget // what to display when the filter value is valid
invalid gowid.IWidget // what to display when the filter value is invalid
intermediate gowid.IWidget // what to display when the filter value's validity is being determined
empty gowid.IWidget // what to display when the filter value's is empty (special state)
edCtx context.Context
edCancelFn context.CancelFunc
edCtxLock sync.Mutex
fields fields.IPrefixCompleter // provides completions, given a prefix
completionsList *list.Widget // the filter widget replaces the list walker when new completions are generated
completionsActivator *activatorWidget // used to disable focus going to drop down
completions []string // the current set of completions, used when rendering
runthisfilterchan chan *filtStruct
filterchangedchan chan *filtStruct
quitchan chan struct{}
readytorunchan chan struct{}
temporarilyDisabled *bool // set to true right after submitting a new filter, so the menu disappears
enterPending bool // set to true if the user has hit enter; process if the filter goes to valid before another change. For slow validity processing.
*gowid.Callbacks
gowid.IsSelectable
}
var _ gowid.IWidget = (*Widget)(nil)
var _ io.Closer = (*Widget)(nil)
type IntermediateCB struct{}
type ValidCB struct{}
type InvalidCB struct{}
type EmptyCB struct{}
type SubmitCB struct{}
type Pos int
const (
Left Pos = iota
Below Pos = iota
)
type Options struct {
Completer fields.IPrefixCompleter
MenuOpener menu.IOpener
Position Pos
Validator IValidator
MaxCompletions int
}
type stringNamer string
func (f stringNamer) Name() string {
return string(f)
}
func New(name string, opt Options) *Widget {
res := &Widget{}
ed := edit.New()
ed.OnTextSet(gowid.WidgetCallback{"cb", func(app gowid.IApp, w gowid.IWidget) {
// every time the filter changes, drop any pending enter - we don't want to
// apply a filter to a stale value
res.enterPending = false
}})
validator := opt.Validator
if validator == nil {
validator = &DisplayFilterValidator{}
}
filterList := list.New(list.NewSimpleListWalker([]gowid.IWidget{}))
filterActivator := &activatorWidget{
IWidget: filterList,
}
if opt.MaxCompletions == 0 {
opt.MaxCompletions = 20
}
menuListBox2 := styled.New(
framed.NewUnicode(cellmod.Opaque(filterActivator)),
gowid.MakePaletteRef("filter-menu"),
)
ign := make([]gowid.IKey, 0, len(vim.AllDownKeys)+len(vim.AllUpKeys))
for _, k := range vim.AllDownKeys {
if !termshark.KeyPressIsPrintable(gowid.Key(k)) {
ign = append(ign, gowid.Key(k))
}
}
for _, k := range vim.AllUpKeys {
if !termshark.KeyPressIsPrintable(gowid.Key(k)) {
ign = append(ign, gowid.Key(k))
}
}
drop := menu.New(name, menuListBox2, fixed,
menu.Options{
IgnoreKeysProvided: true,
IgnoreKeys: ign,
CloseKeysProvided: true,
CloseKeys: []gowid.IKey{},
OpenCloser: opt.MenuOpener,
},
)
yOff := 1
if opt.Position == Below {
yOff = 0
}
site := menu.NewSite(menu.SiteOptions{
Namer: stringNamer("filtersite"),
YOffset: yOff,
})
cb := gowid.NewCallbacks()
onelineEd := appkeys.New(ed, handleEnter(cb, res), appkeys.Options{
ApplyBefore: true,
})
valid := styled.New(onelineEd,
gowid.MakePaletteRef("filter-valid"),
)
invalid := styled.New(onelineEd,
gowid.MakePaletteRef("filter-invalid"),
)
intermediate := styled.New(onelineEd,
gowid.MakePaletteRef("filter-intermediate"),
)
empty := styled.New(onelineEd,
gowid.MakePaletteRef("filter-empty"),
)
var placeholder *holder.Widget
placeholder = holder.New(empty)
var wrapped gowid.IWidget
switch opt.Position {
case Below:
wrapped = pile.New([]gowid.IContainerWidget{
&gowid.ContainerWidget{IWidget: placeholder, D: gowid.RenderFlow{}},
&gowid.ContainerWidget{IWidget: site, D: fixed},
})
default:
wrapped = columns.New([]gowid.IContainerWidget{
&gowid.ContainerWidget{IWidget: site, D: fixed},
&gowid.ContainerWidget{IWidget: placeholder, D: gowid.RenderWithWeight{W: 1}},
})
}
runthisfilterchan := make(chan *filtStruct)
quitchan := make(chan struct{})
readytorunchan := make(chan struct{})
filterchangedchan := make(chan *filtStruct)
*res = Widget{
wrapped: wrapped,
opts: opt,
ed: ed,
dropDown: drop,
dropDownSite: site,
validitySite: placeholder,
valid: valid,
invalid: invalid,
intermediate: intermediate,
empty: empty,
fields: opt.Completer,
completionsList: filterList,
completionsActivator: filterActivator,
completions: []string{},
filterchangedchan: filterchangedchan,
runthisfilterchan: runthisfilterchan,
quitchan: quitchan,
readytorunchan: readytorunchan,
temporarilyDisabled: new(bool),
Callbacks: cb,
}
validcb := &ValidateCB{
Fn: func(app gowid.IApp) {
app.Run(gowid.RunFunction(func(app gowid.IApp) {
res.validitySite.SetSubWidget(res.valid, app)
gowid.RunWidgetCallbacks(res.Callbacks, ValidCB{}, app, res)
if res.enterPending {
var dummy gowid.IWidget
gowid.RunWidgetCallbacks(cb, SubmitCB{}, app, dummy)
*res.temporarilyDisabled = true
res.enterPending = false
}
}))
},
}
invalidcb := &ValidateCB{
Fn: func(app gowid.IApp) {
app.Run(gowid.RunFunction(func(app gowid.IApp) {
res.validitySite.SetSubWidget(res.invalid, app)
gowid.RunWidgetCallbacks(res.Callbacks, InvalidCB{}, app, res)
res.enterPending = false
}))
},
}
killedcb := &ValidateCB{
Fn: func(app gowid.IApp) {
app.Run(gowid.RunFunction(func(app gowid.IApp) {
res.validitySite.SetSubWidget(res.intermediate, app)
gowid.RunWidgetCallbacks(res.Callbacks, IntermediateCB{}, app, res)
res.enterPending = false
}))
},
}
emptycb := &ValidateCB{
Fn: func(app gowid.IApp) {
app.Run(gowid.RunFunction(func(app gowid.IApp) {
res.validitySite.SetSubWidget(res.empty, app)
gowid.RunWidgetCallbacks(res.Callbacks, EmptyCB{}, app, res)
res.enterPending = false
}))
},
}
validator.SetValid(validcb)
validator.SetInvalid(invalidcb)
validator.SetKilled(killedcb)
validator.SetEmpty(emptycb)
// Save up filter changes, send latest over when process is ready, discard ones in between
termshark.TrackedGo(func() {
send := false
var latest *filtStruct
CL2:
for {
if send && latest != nil {
res.runthisfilterchan <- latest
latest = nil
send = false
}
select {
// tshark process ready
case <-res.quitchan:
break CL2
case <-res.readytorunchan:
send = true
// Sent by tshark process goroutine
case fs := <-res.filterchangedchan:
latest = fs
// We're ready to run a new one, so kill any process that is in progress. Take care
// because it might not have actually started yet!
validator.Kill()
}
}
}, Goroutinewg)
// Every time it gets an event, it means run the process. Another goroutine takes care of consolidating
// events. Stops when channel is closed
termshark.TrackedGo(func() {
CL:
for {
// Tell other goroutine we are ready for more - each time round the loop. This makes sure
// we don't run more than one tshark process - it will get killed if a new filter should take
// priority.
select {
case res.readytorunchan <- struct{}{}:
case <-res.quitchan:
break CL
}
select {
case <-res.quitchan:
break CL
case fs := <-res.runthisfilterchan:
validcb.App = fs.app
invalidcb.App = fs.app
killedcb.App = fs.app
emptycb.App = fs.app
validator.Validate(fs.txt)
}
}
}, Goroutinewg)
ed.OnTextSet(gowid.MakeWidgetCallback("cb2", gowid.WidgetChangedFunction(func(app gowid.IApp, ew gowid.IWidget) {
res.UpdateCompletions(app)
})))
return res
}
//======================================================================
type iFilterEnter interface {
setDisabled()
setEnterPending()
isValid() bool
}
// if the filter is valid when enter is pressed, submit the SubmitCB callback. Those
// registered will be able to respond e.g. start handling the valid filter value.
func handleEnter(cb *gowid.Callbacks, fe iFilterEnter) appkeys.KeyInputFn {
return func(evk *tcell.EventKey, app gowid.IApp) bool {
handled := false
switch evk.Key() {
case tcell.KeyEnter:
if fe.isValid() {
var dummy gowid.IWidget
gowid.RunWidgetCallbacks(cb, SubmitCB{}, app, dummy)
fe.setDisabled()
} else {
fe.setEnterPending() // remember in case the filter goes valid shortly
}
handled = true
}
return handled
}
}
func isValidFilterRune(r rune) bool {
res := true
switch {
case unicode.IsLetter(r):
case unicode.IsNumber(r):
case r == '-':
case r == '_':
case r == '.':
default:
res = false
}
return res
}
func newMenuWidgets(ed *edit.Widget, completions []string) []gowid.IWidget {
menu2Widgets := make([]gowid.IWidget, 0)
for _, s := range completions {
scopy := s
clickme := button.New(
hpadding.New(
text.New(s),
gowid.HAlignLeft{},
gowid.RenderWithUnits{U: gwutil.Max(12, len(s))},
),
button.Options{
Decoration: button.BareDecoration,
SelectKeysProvided: true,
SelectKeys: []gowid.IKey{gowid.MakeKeyExt(tcell.KeyEnter)},
},
)
clickmeStyled := styled.NewInvertedFocus(clickme, gowid.MakePaletteRef("filter-menu"))
clickme.OnClick(gowid.MakeWidgetCallback(gowid.ClickCB{}, func(app gowid.IApp, target gowid.IWidget) {
txt := ed.Text()
end := ed.CursorPos()
start := end
Loop1:
for {
if start == 0 {
break
}
start--
if !isValidFilterRune(rune(txt[start])) {
start++
break Loop1
}
}
Loop2:
for {
if end == len(txt) {
break
}
if !isValidFilterRune(rune(txt[end])) {
break Loop2
}
end++
}
ed.SetText(fmt.Sprintf("%s%s%s", txt[0:start], scopy, txt[end:len(txt)]), app)
ed.SetCursorPos(len(txt[0:start])+len(scopy), app)
}))
cols := columns.New([]gowid.IContainerWidget{
&gowid.ContainerWidget{IWidget: clickmeStyled, D: fixed},
})
menu2Widgets = append(menu2Widgets, cols)
}
return menu2Widgets
}
type fnCallback struct {
app gowid.IApp
fn func([]string, gowid.IApp)
}
var _ fields.IPrefixCompleterCallback = fnCallback{}
func (f fnCallback) Call(res []string) {
f.fn(res, f.app)
}
func makeCompletions(comp fields.IPrefixCompleter, txt string, max int, app gowid.IApp, fn func([]string, gowid.IApp)) {
if comp != nil {
cb := fnCallback{
app: app,
fn: func(completions []string, app gowid.IApp) {
completions = completions[0:gwutil.Min(max, len(completions))]
fn(completions, app)
},
}
comp.Completions(txt, cb)
}
}
func (w *Widget) setDisabled() {
*w.temporarilyDisabled = true
}
func (w *Widget) setEnterPending() {
w.enterPending = true
}
// isCurrentlyValid returns true if the current state of the filter is valid (green)
func (w *Widget) isValid() bool {
return w.validitySite.SubWidget() == w.valid
}
// Start an asynchronous routine to update the drop-down menu with completion
// options. Runs on a small delay so it can be cancelled and restarted if the
// user is typing quickly.
func (w *Widget) UpdateCompletions(app gowid.IApp) {
app.Run(gowid.RunFunction(func(app gowid.IApp) {
if w.ed.Text() != "" {
w.validitySite.SetSubWidget(w.intermediate, app)
gowid.RunWidgetCallbacks(w.Callbacks, IntermediateCB{}, app, w)
}
}))
// UpdateCompletions can be called outside of the app goroutine, so we
// need to protect the context
w.edCtxLock.Lock()
defer w.edCtxLock.Unlock()
if w.edCancelFn != nil {
w.edCancelFn()
}
w.edCtx, w.edCancelFn = context.WithCancel(context.Background())
// don't kick things off right away in case user is typing fast
go func(ctx context.Context) {
select {
case <-ctx.Done():
return
case <-time.After(time.Millisecond * 200):
break
}
// Send the value to be run by tshark. This will kill any other one in progress.
w.filterchangedchan <- &filtStruct{w.ed.Text(), app}
app.Run(gowid.RunFunction(func(app gowid.IApp) {
_, y := app.GetScreen().Size()
txt := w.ed.Text()
end := w.ed.CursorPos()
start := end
Loop:
for {
if start == 0 {
break
}
start--
if !isValidFilterRune(rune(txt[start])) {
start++
break Loop
}
}
makeCompletions(w.fields, txt[start:end], y, app, func(completions []string, app gowid.IApp) {
app.Run(gowid.RunFunction(func(app gowid.IApp) {
w.processCompletions(completions, app)
}))
})
}))
}(w.edCtx)
}
func (w *Widget) processCompletions(completions []string, app gowid.IApp) {
max := w.opts.MaxCompletions
for _, c := range completions {
max = gwutil.Max(max, len(c))
}
menu2Widgets := newMenuWidgets(w.ed, completions)
w.completions = completions
app.Run(gowid.RunFunction(func(app gowid.IApp) {
w.completionsList.SetWalker(list.NewSimpleListWalker(menu2Widgets), app)
// whenever there's an update, take focus away from drop down. This means enter
// can be used to submit a new filter.
w.completionsActivator.active = false
w.dropDown.SetWidth(gowid.RenderWithUnits{U: max + 2}, app)
// This makes for a better experience. The menu is rendered as a box because an
// explicit height is set; this results in the overlay either rendering as the
// full-height box requested; or if there's not enough vertical room, a shorter
// box. Either way, the list will render in the space provided (and the frame),
// and scroll if necessary. This means the menu isn't cut off at the bottom of
// the screen. This assumes I'm not displaying the individual widgets in flow
// mode because then each might take more than one line
if len(w.completions) >= 0 { // account for the frame...
w.dropDown.SetHeight(gowid.RenderWithUnits{U: len(w.completions) + 2}, app)
} else {
w.dropDown.SetHeight(fixed, app)
}
}))
}
func (w *Widget) Close() error {
// Two for the aggregator goroutine and the filter runner goroutine
w.quitchan <- struct{}{}
w.quitchan <- struct{}{}
return nil
}
func (w *Widget) OnSubmit(f gowid.IWidgetChangedCallback) {
gowid.AddWidgetCallback(w, SubmitCB{}, f)
}
func (w *Widget) OnIntermediate(f gowid.IWidgetChangedCallback) {
gowid.AddWidgetCallback(w, IntermediateCB{}, f)
}
func (w *Widget) OnValid(f gowid.IWidgetChangedCallback) {
gowid.AddWidgetCallback(w, ValidCB{}, f)
}
func (w *Widget) OnInvalid(f gowid.IWidgetChangedCallback) {
gowid.AddWidgetCallback(w, InvalidCB{}, f)
}
func (w *Widget) OnEmpty(f gowid.IWidgetChangedCallback) {
gowid.AddWidgetCallback(w, EmptyCB{}, f)
}
func (w *Widget) IsValid() bool {
return w.validitySite.SubWidget() == w.valid
}
func (w *Widget) Value() string {
return w.ed.Text()
}
func (w *Widget) SetValue(v string, app gowid.IApp) {
w.ed.SetText(v, app)
w.ed.SetCursorPos(len(v), app)
}
func (w *Widget) Menus() []gowid.IMenuCompatible {
return []gowid.IMenuCompatible{w.dropDown}
}
func (w *Widget) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
return gowid.RenderSize(w.wrapped, size, focus, app)
}
func (w *Widget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
// It can be disabled if e.g. the user's last input caused the filter value to
// be submitted. Then the best UX is to not display the drop down until further input
// or cursor movement.
if w.opts.MenuOpener != nil {
if focus.Focus && len(w.completions) > 0 && !*w.temporarilyDisabled {
w.opts.MenuOpener.OpenMenu(w.dropDown, w.dropDownSite, app)
} else {
w.opts.MenuOpener.CloseMenu(w.dropDown, app)
}
}
return w.wrapped.Render(size, focus, app)
}
// Reject tab because I want it to switch views. Not intended to be transferable. Reject down because
// accepting it triggers the OnCursorSet callback, which re-evaluates the filter value - the user sees
// it go orange briefly, which is unpleasant.
func (w *Widget) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
if evk, ok := ev.(*tcell.EventKey); ok {
if evk.Key() == tcell.KeyTAB || (vim.KeyIn(evk, vim.AllDownKeys) && !termshark.KeyPressIsPrintable(evk)) {
return false
}
}
*w.temporarilyDisabled = false // any input should start the appearance of the drop down again
return w.wrapped.UserInput(ev, size, focus, app)
}
//======================================================================
// activatorWidget is intended to wrap a ListBox, and will suppress focus to the listbox by
// default, which has the effect of not highlighting any listbox items. The intended effect
// is for the cursor to be "above" the first item. When the user hits down, then focus
// is passed through, so the top item is highlighted. If the key pressed is up, and the
// listbox doesn't handle it, that must mean it's at the top of its range, so the effect is
// start suppressing focus again.
type activatorWidget struct {
gowid.IWidget
active bool
}
func (w *activatorWidget) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
if _, ok := ev.(*tcell.EventPaste); ok && !w.active {
return false
}
if ev, ok := ev.(*tcell.EventKey); ok && !w.active {
if vim.KeyIn(ev, vim.AllDownKeys) && !termshark.KeyPressIsPrintable(ev) {
w.active = true
return true
} else {
return false
}
}
res := w.IWidget.UserInput(ev, size, focus, app)
if !res {
if ev, ok := ev.(*tcell.EventKey); ok && w.active {
if vim.KeyIn(ev, vim.AllUpKeys) && !termshark.KeyPressIsPrintable(ev) {
w.active = false
return true
} else {
return false
}
}
}
return res
}
func (w *activatorWidget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
newf := focus
if !w.active {
newf = gowid.NotSelected
}
return w.IWidget.Render(size, newf, app)
}
//======================================================================
// IValidator is passed to the filter constructor
type IValidator interface {
SetValid(cb IValidateCB)
SetInvalid(cb IValidateCB)
SetKilled(cb IValidateCB)
SetEmpty(cv IValidateCB)
Kill() (bool, error)
Validate(filter string)
}
//======================================================================
type IValidateCB interface {
Call(filter string)
}
type AppFilterCB func(gowid.IApp)
type ValidateCB struct {
App gowid.IApp
Fn AppFilterCB
}
var _ IValidateCB = (*ValidateCB)(nil)
func (v *ValidateCB) Call(filter string) {
v.Fn(v.App)
}
type DisplayFilterValidator struct {
Valid IValidateCB
Invalid IValidateCB
KilledCB IValidateCB
EmptyCB IValidateCB
Cmd *exec.Cmd
}
var _ IValidator = (*DisplayFilterValidator)(nil)
func (f *DisplayFilterValidator) SetValid(cb IValidateCB) {
f.Valid = cb
}
func (f *DisplayFilterValidator) SetInvalid(cb IValidateCB) {
f.Invalid = cb
}
func (f *DisplayFilterValidator) SetKilled(cb IValidateCB) {
f.KilledCB = cb
}
func (f *DisplayFilterValidator) SetEmpty(cb IValidateCB) {
f.EmptyCB = cb
}
func (f *DisplayFilterValidator) Kill() (bool, error) {
var err error
var res bool
if f.Cmd != nil {
proc := f.Cmd.Process
if proc != nil {
res = true
err = proc.Kill()
}
}
return res, err
}
func (f *DisplayFilterValidator) Validate(filter string) {
var err error
if filter == "" {
if f.EmptyCB != nil {
f.EmptyCB.Call(filter)
}
return
}
f.Cmd = exec.Command(termshark.TSharkBin(), []string{"-Y", filter, "-r", termshark.CacheFile("empty.pcap")}...)
err = f.Cmd.Run()
if err == nil {
if f.Valid != nil {
f.Valid.Call(filter)
}
} else {
killed := true
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
if status.ExitStatus() == 2 {
killed = false
}
}
}
if killed {
if f.KilledCB != nil {
f.KilledCB.Call(filter)
}
} else {
if f.Invalid != nil {
f.Invalid.Call(filter)
}
}
}
}
//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: