From 50b1fe2c7d9fd4065714bf28e20d6c561455f09b Mon Sep 17 00:00:00 2001 From: Takumasa Sakao Date: Mon, 12 Feb 2018 22:11:47 +0900 Subject: [PATCH] :+1: Add mode line & help in header --- panels.go | 6 ++++++ tcpterm.go | 45 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/panels.go b/panels.go index 237e372..18a5b99 100644 --- a/panels.go +++ b/panels.go @@ -37,3 +37,9 @@ func preparePacketDump() *tview.TextView { text.SetBorder(true).SetTitle("Dump").SetBackgroundColor(tcell.ColorDefault) return text } + +func prepareFrame(primitive tview.Primitive) *tview.Frame { + frame := tview.NewFrame(primitive).SetBorders(0, 0, 0, 0, 0, 0) + frame.SetBackgroundColor(tcell.ColorDefault) + return frame +} diff --git a/tcpterm.go b/tcpterm.go index d6d3617..92356c6 100644 --- a/tcpterm.go +++ b/tcpterm.go @@ -10,6 +10,11 @@ import ( "github.com/sachaos/tview" ) +const ( + TailMode = iota + SelectMode +) + type Tcpterm struct { src *gopacket.PacketSource view *tview.Application @@ -17,7 +22,9 @@ type Tcpterm struct { table *tview.Table detail *tview.TextView dump *tview.TextView + frame *tview.Frame packets []gopacket.Packet + mode int } const ( @@ -36,8 +43,9 @@ func NewTcpterm(src *gopacket.PacketSource) *Tcpterm { AddItem(packetList, 0, 1, true). AddItem(packetDetail, 0, 1, false). AddItem(packetDump, 0, 1, false) + frame := prepareFrame(layout) - view.SetRoot(layout, true).SetFocus(packetList) + view.SetRoot(frame, true).SetFocus(packetList) app := &Tcpterm{ src: src, @@ -46,7 +54,9 @@ func NewTcpterm(src *gopacket.PacketSource) *Tcpterm { table: packetList, detail: packetDetail, dump: packetDump, + frame: frame, } + app.SwitchToTailMode() view.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { if event.Key() == tcell.KeyCtrlC { @@ -60,15 +70,12 @@ func NewTcpterm(src *gopacket.PacketSource) *Tcpterm { }) packetList.SetDoneFunc(func(key tcell.Key) { - if key == tcell.KeyEnter { - packetList.SetSelectable(true, false) - row, column := packetList.GetOffset() - packetList.Select(row+1, column) - app.displayDetailOf(row + 1) + if key == tcell.KeyEsc { + app.SwitchToTailMode() } - if key == tcell.KeyEsc { - packetList.SetSelectable(false, false) + if key == tcell.KeyEnter { + app.SwitchToSelectMode() } }) @@ -109,6 +116,28 @@ func (app *Tcpterm) Stop() { app.view.Stop() } +func (app *Tcpterm) SwitchToTailMode() { + app.mode = TailMode + + app.table.SetSelectable(false, false) + app.table.ScrollToEnd() + + app.frame.Clear().AddText("**Tail**", true, tview.AlignLeft, tcell.ColorGreen) + app.frame.AddText("g: page top, G: page end, TAB: rotate panel, Enter: Detail mode", true, tview.AlignRight, tcell.ColorDefault) +} + +func (app *Tcpterm) SwitchToSelectMode() { + app.mode = SelectMode + + app.table.SetSelectable(true, false) + row, _ := app.table.GetOffset() + app.table.Select(row+1, 0) + app.displayDetailOf(row + 1) + + app.frame.Clear().AddText("*Detail*", true, tview.AlignLeft, tcell.ColorBlue) + app.frame.AddText("g: page top, G: page end, TAB: rotate panel, ECS: Tail mode", true, tview.AlignRight, tcell.ColorDefault) +} + func (app *Tcpterm) displayDetailOf(row int) { if row < 1 || row > len(app.packets) { return