diff --git a/cmd/termshark/termshark.go b/cmd/termshark/termshark.go index def1d9d..4f8f831 100644 --- a/cmd/termshark/termshark.go +++ b/cmd/termshark/termshark.go @@ -25,6 +25,7 @@ import ( "github.com/gcla/termshark/v2/pkg/capinfo" "github.com/gcla/termshark/v2/pkg/confwatcher" "github.com/gcla/termshark/v2/pkg/convs" + "github.com/gcla/termshark/v2/pkg/fields" "github.com/gcla/termshark/v2/pkg/pcap" "github.com/gcla/termshark/v2/pkg/shark" "github.com/gcla/termshark/v2/pkg/streams" @@ -611,7 +612,7 @@ func cmain() int { // If the last tshark we used isn't the same as the current one, then remove the cached fields // data structure so it can be regenerated. if tsharkBin != profiles.ConfString("main.last-used-tshark", "") { - termshark.DeleteCachedFields() + fields.DeleteCachedFields() } // Write out the last-used tshark path. We do this to make the above fields cache be consistent diff --git a/fields.go b/pkg/fields/fields.go similarity index 95% rename from fields.go rename to pkg/fields/fields.go index 41306f1..474eaa3 100644 --- a/fields.go +++ b/pkg/fields/fields.go @@ -2,7 +2,7 @@ // code is governed by the MIT license that can be found in the LICENSE // file. -package termshark +package fields import ( "bufio" @@ -13,6 +13,7 @@ import ( "strings" "sync" + "github.com/gcla/termshark/v2" log "github.com/sirupsen/logrus" ) @@ -160,25 +161,25 @@ type IPrefixCompleter interface { Completions(prefix string, cb IPrefixCompleterCallback) } -func NewFields() *TSharkFields { +func New() *TSharkFields { return &TSharkFields{} } func DeleteCachedFields() error { - return os.Remove(CacheFile("tsharkfieldsv3.gob.gz")) + return os.Remove(termshark.CacheFile("tsharkfieldsv3.gob.gz")) } // Can be run asynchronously. // This ought to use interfaces to make it testable. func (w *TSharkFields) Init() error { - newer, err := FileNewerThan(CacheFile("tsharkfieldsv3.gob.gz"), DirOfPathCommandUnsafe(TSharkBin())) + newer, err := termshark.FileNewerThan(termshark.CacheFile("tsharkfieldsv3.gob.gz"), termshark.DirOfPathCommandUnsafe(termshark.TSharkBin())) if err == nil && newer { f := &FieldsAndProtos{ Fields: make(map[string]interface{}), Protocols: make(map[string]struct{}), } - err = ReadGob(CacheFile("tsharkfieldsv3.gob.gz"), f) + err = termshark.ReadGob(termshark.CacheFile("tsharkfieldsv3.gob.gz"), f) if err == nil { w.ser = f log.Infof("Read cached tshark fields.") @@ -193,7 +194,7 @@ func (w *TSharkFields) Init() error { return err } - err = WriteGob(CacheFile("tsharkfieldsv3.gob.gz"), w.ser) + err = termshark.WriteGob(termshark.CacheFile("tsharkfieldsv3.gob.gz"), w.ser) if err != nil { return err } @@ -202,7 +203,7 @@ func (w *TSharkFields) Init() error { } func (w *TSharkFields) InitNoCache() error { - cmd := exec.Command(TSharkBin(), []string{"-G", "fields"}...) + cmd := exec.Command(termshark.TSharkBin(), []string{"-G", "fields"}...) out, err := cmd.StdoutPipe() if err != nil { diff --git a/fields_test.go b/pkg/fields/fields_test.go similarity index 94% rename from fields_test.go rename to pkg/fields/fields_test.go index 70792a5..6d8085e 100644 --- a/fields_test.go +++ b/pkg/fields/fields_test.go @@ -1,7 +1,7 @@ // 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 termshark +package fields import ( "testing" @@ -13,7 +13,7 @@ import ( func TestFields1(t *testing.T) { - fields := NewFields() + fields := New() err := fields.InitNoCache() assert.NoError(t, err) diff --git a/ui/ui.go b/ui/ui.go index 3b8d793..c8b8f3b 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -47,11 +47,12 @@ import ( "github.com/gcla/gowid/widgets/vpadding" "github.com/gcla/termshark/v2" "github.com/gcla/termshark/v2/configs/profiles" + "github.com/gcla/termshark/v2/pkg/fields" "github.com/gcla/termshark/v2/pkg/pcap" "github.com/gcla/termshark/v2/pkg/pdmltree" "github.com/gcla/termshark/v2/pkg/psmlmodel" - "github.com/gcla/termshark/v2/pkg/system" "github.com/gcla/termshark/v2/pkg/shark" + "github.com/gcla/termshark/v2/pkg/system" "github.com/gcla/termshark/v2/pkg/theme" "github.com/gcla/termshark/v2/ui/menuutil" "github.com/gcla/termshark/v2/ui/tableutil" @@ -239,7 +240,7 @@ var lastJumpPos int var NoGlobalJump termshark.GlobalJumpPos // leave as default, like a placeholder var Loader *pcap.PacketLoader -var FieldCompleter *termshark.TSharkFields // share this - safe once constructed +var FieldCompleter *fields.TSharkFields // share this - safe once constructed var WriteToSelected bool // true if the user provided the -w flag var WriteToDeleted bool // true if the user deleted the temporary pcap before quitting @@ -366,11 +367,11 @@ func makePdmlFilterMenu(filter string, val string) *menu.Widget { // are needed. if ok { switch field.Type { - case termshark.FT_STRING: + case fields.FT_STRING: needQuotes = true - case termshark.FT_STRINGZ: + case fields.FT_STRINGZ: needQuotes = true - case termshark.FT_STRINGZPAD: + case fields.FT_STRINGZPAD: needQuotes = true } } @@ -3135,10 +3136,10 @@ func UpdateRecentMenu(app gowid.IApp) { type savedCompleterCallback struct { prefix string - comp termshark.IPrefixCompleterCallback + comp fields.IPrefixCompleterCallback } -var _ termshark.IPrefixCompleterCallback = (*savedCompleterCallback)(nil) +var _ fields.IPrefixCompleterCallback = (*savedCompleterCallback)(nil) func (s *savedCompleterCallback) Call(orig []string) { if s.prefix == "" { @@ -3153,12 +3154,12 @@ func (s *savedCompleterCallback) Call(orig []string) { } type savedCompleter struct { - def termshark.IPrefixCompleter + def fields.IPrefixCompleter } -var _ termshark.IPrefixCompleter = (*savedCompleter)(nil) +var _ fields.IPrefixCompleter = (*savedCompleter)(nil) -func (s savedCompleter) Completions(prefix string, cb termshark.IPrefixCompleterCallback) { +func (s savedCompleter) Completions(prefix string, cb fields.IPrefixCompleterCallback) { ncomp := &savedCompleterCallback{ prefix: prefix, comp: cb, @@ -3783,7 +3784,7 @@ func Build(tty string) (*gowid.App, error) { ) // For completing filter expressions - FieldCompleter = termshark.NewFields() + FieldCompleter = fields.New() FieldCompleter.Init() FilterWidget = filter.New("filter", filter.Options{ diff --git a/widgets/filter/filter.go b/widgets/filter/filter.go index f758ce6..8989eff 100644 --- a/widgets/filter/filter.go +++ b/widgets/filter/filter.go @@ -33,6 +33,7 @@ import ( "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" ) @@ -64,10 +65,10 @@ type Widget struct { edCtx context.Context edCancelFn context.CancelFunc edCtxLock sync.Mutex - fields termshark.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 + 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{} @@ -95,7 +96,7 @@ const ( ) type Options struct { - Completer termshark.IPrefixCompleter + Completer fields.IPrefixCompleter MenuOpener menu.IOpener Position Pos Validator IValidator @@ -451,13 +452,13 @@ type fnCallback struct { fn func([]string, gowid.IApp) } -var _ termshark.IPrefixCompleterCallback = fnCallback{} +var _ fields.IPrefixCompleterCallback = fnCallback{} func (f fnCallback) Call(res []string) { f.fn(res, f.app) } -func makeCompletions(comp termshark.IPrefixCompleter, txt string, max int, app gowid.IApp, fn func([]string, gowid.IApp)) { +func makeCompletions(comp fields.IPrefixCompleter, txt string, max int, app gowid.IApp, fn func([]string, gowid.IApp)) { if comp != nil { cb := fnCallback{ app: app, diff --git a/widgets/search/search.go b/widgets/search/search.go index b909613..f09ae70 100644 --- a/widgets/search/search.go +++ b/widgets/search/search.go @@ -26,8 +26,8 @@ import ( "github.com/gcla/gowid/widgets/null" "github.com/gcla/gowid/widgets/styled" "github.com/gcla/gowid/widgets/text" - "github.com/gcla/termshark/v2" "github.com/gcla/termshark/v2/configs/profiles" + "github.com/gcla/termshark/v2/pkg/fields" "github.com/gcla/termshark/v2/ui/menuutil" "github.com/gcla/termshark/v2/widgets/filter" "github.com/gcla/termshark/v2/widgets/ifwidget" @@ -247,7 +247,7 @@ type Widget struct { filterHolder *holder.Widget filt *filter.Widget menuOpener menu.IOpener - completer termshark.IPrefixCompleter + completer fields.IPrefixCompleter cols *columns.Widget findBtn *disable.Widget searchTargetBtn *button.Widget @@ -338,7 +338,7 @@ func New(alg IAlgorithm, searchPktDetails func() ICallbacks, searchByFilter func() ICallbacks, men menu.IOpener, - comp termshark.IPrefixCompleter, + comp fields.IPrefixCompleter, errHandler IErrorHandler) *Widget { res := &Widget{ @@ -535,8 +535,8 @@ func (w *Widget) invokeSearch(app gowid.IApp) { w.alg.SearchPackets(searchTerm, w.currentAlg, app) } -func (w *Widget) getCompleter() termshark.IPrefixCompleter { - var completer termshark.IPrefixCompleter +func (w *Widget) getCompleter() fields.IPrefixCompleter { + var completer fields.IPrefixCompleter s2 := getSearchType() switch s2 { case "filter": @@ -550,7 +550,7 @@ func (w *Widget) focusOnFilter(app gowid.IApp) { w.cols.SetFocus(app, 6) } -func (w *Widget) setFilter(validator filter.IValidator, completer termshark.IPrefixCompleter, app gowid.IApp) { +func (w *Widget) setFilter(validator filter.IValidator, completer fields.IPrefixCompleter, app gowid.IApp) { filt := filter.New("searchfilter", filter.Options{ MenuOpener: w.menuOpener, Completer: completer,