Add 's' shortcut to switch next view mode on the fly

This commit is contained in:
chenjiandongx 2021-11-18 00:27:40 +08:00
parent 61117145b1
commit 0f3c3e93a6
6 changed files with 27 additions and 13 deletions

View File

@ -76,6 +76,15 @@ Flags:
-v, --version version for sniffer
```
**Shortcuts**
| Keys | Description |
| ---- | ----------- |
| <kbd>Space</kbd> | pause refreshing |
| <kbd>Tab</kbd> | rearrange tables |
| <kbd>s</kbd> | switch next view mode |
| <kbd>q</kbd> / <kbd>Ctrl</kbd>+<kbd>C</kbd> | quit |
## View Mode
***Bytes Mode:*** display traffic stats in bytes by the Table widget.

2
cli.go
View File

@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)
const version = "v0.1.1"
const version = "v0.2.0"
func NewApp() *cobra.Command {
defaultOpts := DefaultOptions()

View File

@ -91,16 +91,6 @@ func (lc *lsofConn) getOpenSockets(pids ...int32) (OpenSockets, error) {
if err != nil {
continue
}
if len(pids) > 0 {
pid, err := strconv.Atoi(fields[1])
if err != nil {
continue
}
if !set[int32(pid)] {
continue
}
}
sockets[LocalSocket{IP: ipport[0], Port: uint16(port), Protocol: ProtoTCP}] = procName
case "UDP":

View File

@ -1,6 +1,7 @@
package main
import (
"errors"
"strconv"
"strings"
"sync"
@ -139,6 +140,10 @@ func (c *PcapClient) getAvailableDevices() error {
}
}
if len(c.handlers) == 0 {
return errors.New("no available devices found")
}
return nil
}

View File

@ -85,6 +85,14 @@ func NewSniffer(opts Options) (*Sniffer, error) {
}, nil
}
func (s *Sniffer) SwitchViewMode() {
s.opts.ViewMode = (s.opts.ViewMode + 1) % 3
s.statsManager = NewStatsManager(s.opts)
s.ui.Close()
s.ui = NewUIComponent(s.opts)
}
func (s *Sniffer) Start() {
events := termui.PollEvents()
s.Refresh()
@ -102,6 +110,8 @@ func (s *Sniffer) Start() {
case "<Resize>":
payload := e.Payload.(termui.Resize)
s.ui.viewer.Resize(payload.Width, payload.Height)
case "s", "S":
s.SwitchViewMode()
case "q", "Q", "<C-c>":
return
}
@ -115,9 +125,9 @@ func (s *Sniffer) Start() {
}
func (s *Sniffer) Close() {
s.ui.Close()
s.pcapClient.Close()
s.dnsResolver.Close()
s.ui.Close()
}
func (s *Sniffer) Refresh() {

2
ui.go
View File

@ -76,7 +76,7 @@ func (u Unit) Ratio() float64 {
}
func newFooter() *widgets.Paragraph {
return newParagraph("Press <Space> to pause, <q>/<Ctrl+C> to exit. Use <Tab> to rearrange tables")
return newParagraph("Press <Space> to pause, <q>/<Ctrl+C> to exit, <s> to switch mode. Use <Tab> to rearrange tables")
}
func newParagraph(text string) *widgets.Paragraph {