Skip to content

Commit

Permalink
A minibuffer command to load a new pcap file
Browse files Browse the repository at this point in the history
For example

load /tmp/test.pcap

For vim-compatibility, you can also issue

r /tmp/test.pcap
  • Loading branch information
gcla committed Jul 19, 2020
1 parent 6a54009 commit 1722d07
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
60 changes: 60 additions & 0 deletions ui/lastline.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package ui

import (
"fmt"
"path/filepath"
"strconv"

"github.com/gcla/gowid"
Expand All @@ -21,6 +22,7 @@ import (

var notEnoughArgumentsErr = fmt.Errorf("Not enough arguments provided")
var invalidSetCommandErr = fmt.Errorf("Invalid set command")
var invalidReadCommandErr = fmt.Errorf("Invalid read command")

type minibufferFn func(gowid.IApp, ...string) error

Expand Down Expand Up @@ -128,6 +130,26 @@ func (s setArg) Completions() []string {

//======================================================================

type fileArg struct {
prefix string
}

var _ minibuffer.IArg = fileArg{}

func (s fileArg) OfferCompletion() bool {
return true
}

func (s fileArg) Completions() []string {
matches, _ := filepath.Glob(s.prefix + "*")
if matches == nil {
return []string{}
}
return matches[0:gwutil.Min(16, len(matches))]
}

//======================================================================

func stringIn(s string, a []string) bool {
for _, s2 := range a {
if s == s2 {
Expand Down Expand Up @@ -266,6 +288,44 @@ func (d setCommand) Arguments(toks []string) []minibuffer.IArg {
return res
}

//======================================================================

type readCommand struct {
complete bool
}

var _ minibuffer.IAction = readCommand{}

func (d readCommand) Run(app gowid.IApp, args ...string) error {
var err error

if len(args) != 2 {
err = invalidReadCommandErr
} else {
RequestLoadPcapWithCheck(args[1], FilterWidget.Value(), app)
}

if err != nil {
OpenMessage(fmt.Sprintf("Error: %s", err), appView, app)
}

return err
}

func (d readCommand) OfferCompletion() bool {
return d.complete
}

func (d readCommand) Arguments(toks []string) []minibuffer.IArg {
res := make([]minibuffer.IArg, 0)
pref := ""
if len(toks) > 0 {
pref = toks[0]
}
res = append(res, fileArg{prefix: pref})
return res
}

//======================================================================
// Local Variables:
// mode: Go
Expand Down
4 changes: 4 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,10 @@ func lastLineMode(app gowid.IApp) {

MiniBuffer.Register("set", setCommand{})

// read new pcap
MiniBuffer.Register("r", readCommand{complete: false})
MiniBuffer.Register("load", readCommand{complete: true})

minibuffer.Open(MiniBuffer, appView, ratio(1.0), flow, app)
}

Expand Down

0 comments on commit 1722d07

Please sign in to comment.