Skip to content

Commit

Permalink
👍 Add mode line & help in header
Browse files Browse the repository at this point in the history
  • Loading branch information
Takumasa Sakao committed Feb 12, 2018
1 parent ed9b507 commit 50b1fe2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
6 changes: 6 additions & 0 deletions panels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
45 changes: 37 additions & 8 deletions tcpterm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ import (
"github.com/sachaos/tview"
)

const (
TailMode = iota
SelectMode
)

type Tcpterm struct {
src *gopacket.PacketSource
view *tview.Application
primitives []tview.Primitive
table *tview.Table
detail *tview.TextView
dump *tview.TextView
frame *tview.Frame
packets []gopacket.Packet
mode int
}

const (
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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()
}
})

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 50b1fe2

Please sign in to comment.