Skip to content

Commit

Permalink
Add a menu option to toggle packet colors.
Browse files Browse the repository at this point in the history
Wireshark has this option. The menu option is not available if the
tshark binary used does not support packet colors (because it would have
no effect).
  • Loading branch information
gcla committed Nov 30, 2019
1 parent a6a005c commit 6bb574e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
14 changes: 7 additions & 7 deletions cmd/termshark/termshark.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 503,13 @@ func cmain() int {
// Determine if the current binary supports color. Tshark will fail with an error if it's too old
// and you supply the --color flag. Assume true, and check if our current binary is not in the
// validate list.
binSupportsColor := true
ui.PacketColorsSupported = true
colorTsharks := termshark.ConfStrings("main.color-tsharks")

if !termshark.StringInSlice(tsharkBin, colorTsharks) {
binSupportsColor, err = termshark.TSharkSupportsColor(tsharkBin)
ui.PacketColorsSupported, err = termshark.TSharkSupportsColor(tsharkBin)
if err != nil {
binSupportsColor = false
ui.PacketColorsSupported = false
} else {
colorTsharks = append(colorTsharks, tsharkBin)
termshark.SetConf("main.color-tsharks", colorTsharks)
Expand Down Expand Up @@ -611,6 611,7 @@ func cmain() int {
// Initialize application state for dark mode and auto-scroll
ui.DarkMode = termshark.ConfBool("main.dark-mode", false)
ui.AutoScroll = termshark.ConfBool("main.auto-scroll", true)
ui.PacketColors = termshark.ConfBool("main.packet-colors", true)

// Set them up here so they have access to any command-line flags that
// need to be passed to the tshark commands used
Expand All @@ -620,10 621,9 @@ func cmain() int {
psmlArgs = append(psmlArgs, "-t", opts.TimestampFormat)
}
tsharkArgs := termshark.ConfStringSlice("main.tshark-args", []string{})
enableColor := termshark.ConfBool("main.packet-colors", true)
if enableColor && !binSupportsColor {
if ui.PacketColors && !ui.PacketColorsSupported {
log.Warnf("Packet coloring is enabled, but %s does not support --color", tsharkBin)
enableColor = false
ui.PacketColors = false
}
cacheSize := termshark.ConfInt("main.pcap-cache-size", 64)
bundleSize := termshark.ConfInt("main.pcap-bundle-size", 1000)
Expand All @@ -633,7 633,7 @@ func cmain() int {
bundleSize = maxBundleSize
}
ui.PcapScheduler = pcap.NewScheduler(
pcap.MakeCommands(opts.DecodeAs, tsharkArgs, pdmlArgs, psmlArgs, enableColor),
pcap.MakeCommands(opts.DecodeAs, tsharkArgs, pdmlArgs, psmlArgs, ui.PacketColors),
pcap.Options{
CacheSize: cacheSize,
PacketsPerLoad: bundleSize,
Expand Down
32 changes: 27 additions & 5 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 153,12 @@ var QuitRequestedChan chan struct{}

var Loader *pcap.Loader
var PcapScheduler *pcap.Scheduler
var DarkMode bool // global state in app
var AutoScroll bool // true if the packet list should auto-scroll when listening on an interface.
var newPacketsArrived bool // true if current updates are due to new packets when listening on an interface.
var Running bool // true if gowid/tcell is controlling the terminal
var DarkMode bool // global state in app
var PacketColors bool // global state in app
var PacketColorsSupported bool // global state in app - true if it's even possible
var AutoScroll bool // true if the packet list should auto-scroll when listening on an interface.
var newPacketsArrived bool // true if current updates are due to new packets when listening on an interface.
var Running bool // true if gowid/tcell is controlling the terminal

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

Expand Down Expand Up @@ -576,7 578,7 @@ func (t *rowFocusTableWidget) At(lpos list.IWalkerPosition) gowid.IWidget {
),
}

if pos >= 0 {
if pos >= 0 && PacketColors {
res = styled.New(res,
gowid.MakePaletteEntry(t.colors[pos].FG, t.colors[pos].BG),
)
Expand Down Expand Up @@ -2311,6 2313,26 @@ func Build() (*gowid.App, error) {
},
}

if PacketColorsSupported {
generalMenuItems = append(
generalMenuItems[0:2],
append(
[]menuutil.SimpleMenuItem{
menuutil.SimpleMenuItem{
Txt: "Toggle Packet Colors",
Key: gowid.MakeKey('c'),
CB: func(app gowid.IApp, w gowid.IWidget) {
generalMenu.Close(app)
PacketColors = !PacketColors
termshark.SetConf("main.packet-colors", PacketColors)
},
},
},
generalMenuItems[2:]...,
)...,
)
}

generalMenuListBox := menuutil.MakeMenuWithHotKeys(generalMenuItems)

var generalNext menuutil.NextMenu
Expand Down

0 comments on commit 6bb574e

Please sign in to comment.