Skip to content

Commit

Permalink
Display selected packet/row info in a dedicated box
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessOne91 committed Aug 5, 2024
1 parent a7a15f2 commit 71e3dab
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 23 deletions.
8 changes: 4 additions & 4 deletions tui/models/bisturi_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type bisturiModel struct {
spinner spinner.Model
startMenu startMenuModel
rowsInput textinput.Model
packetsTable packetsTablemodel
packetsTable packetsTableModel
selectedInterface net.Interface
selectedProtocol string
selectedEthType uint16
Expand Down Expand Up @@ -217,7 +217,7 @@ func (m *bisturiModel) updateRowsInput(msg tea.Msg) (tea.Model, tea.Cmd) {
case "enter":
maxRows, err := strconv.Atoi(m.rowsInput.Value())
if err == nil && maxRows > 0 {
m.packetsTable = newPacketsTable(maxRows, m.terminalWidth)
m.packetsTable = newPacketsTable(maxRows, m.terminalHeight, m.terminalWidth)
m.step = receivePackets

go m.rawSocket.ReadToChan(m.packetsChan, m.errChan)
Expand All @@ -240,7 +240,7 @@ func (m *bisturiModel) updateReceivingPacket(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.WindowSizeMsg:
m.terminalHeight = msg.Height
m.terminalWidth = msg.Width
m.packetsTable.resizeTable(m.terminalWidth)
m.packetsTable.resize(m.terminalHeight, m.terminalWidth)

return m, nil
case readPacketsMsg:
Expand All @@ -251,7 +251,7 @@ func (m *bisturiModel) updateReceivingPacket(msg tea.Msg) (tea.Model, tea.Cmd) {

func (m bisturiModel) readPackets() {
readPackets := []sockets.NetworkPacket{}
timer := time.NewTicker(3 * time.Second)
timer := time.NewTicker(5 * time.Second)
defer timer.Stop()

for {
Expand Down
72 changes: 53 additions & 19 deletions tui/models/packets_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/NamelessOne91/bisturi/sockets"
"github.com/NamelessOne91/bisturi/tui/styles"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/evertras/bubble-table/table"
Expand All @@ -18,48 +19,56 @@ const (
columnKeyInfo = "info"
)

type packetsTablemodel struct {
type packetsTableModel struct {
table table.Model
height int
width int
maxRows int
cachedRows []table.Row
counter uint64
}

func buildTable(rows []table.Row, terminalWidth int) table.Model {
return table.New([]table.Column{
table.NewColumn(columnKeyID, "#", (3*terminalWidth)/100),
table.NewColumn(columnKeyDate, "Date", (8*terminalWidth)/100),
table.NewColumn(columnKeySource, "Source", (20*terminalWidth)/100),
table.NewColumn(columnKeyDestination, "Destination", (20*terminalWidth)/100),
table.NewColumn(columnKeyInfo, "Info", (46*terminalWidth)/100),
func (m *packetsTableModel) buildTable() {
m.table = table.New([]table.Column{
table.NewColumn(columnKeyID, "#", (3*m.width)/100),
table.NewColumn(columnKeyDate, "Date", (7*m.width)/100),
table.NewColumn(columnKeySource, "Source", (20*m.width)/100),
table.NewColumn(columnKeyDestination, "Destination", (20*m.width)/100),
}).
WithRows(rows).
WithRows(m.cachedRows).
Focused(true).
WithBaseStyle(lipgloss.NewStyle().
BorderForeground(lipgloss.Color("#00cc99")).
Foreground(lipgloss.Color("#00cc99")).
Align(lipgloss.Center),
)
}

func newPacketsTable(max int, terminalWidth int) packetsTablemodel {
func newPacketsTable(max int, height, width int) packetsTableModel {
rows := make([]table.Row, 0, max)

return packetsTablemodel{
ptm := packetsTableModel{
height: height,
width: width,
maxRows: max,
cachedRows: rows,
table: buildTable(rows, terminalWidth),
}
ptm.buildTable()

return ptm
}

func (m *packetsTablemodel) resizeTable(terminalWidth int) {
m.table = buildTable(m.cachedRows, terminalWidth)
func (m *packetsTableModel) resize(height, width int) {
m.height = height
m.width = width
m.buildTable()
}

func (m packetsTablemodel) Init() tea.Cmd {
func (m packetsTableModel) Init() tea.Cmd {
return nil
}

func (m packetsTablemodel) Update(msg tea.Msg) (packetsTablemodel, tea.Cmd) {
func (m packetsTableModel) Update(msg tea.Msg) (packetsTableModel, tea.Cmd) {
switch msg := msg.(type) {
case readPacketsMsg:
m.addRows(msg)
Expand All @@ -77,11 +86,36 @@ func (m packetsTablemodel) Update(msg tea.Msg) (packetsTablemodel, tea.Cmd) {
return m, cmd
}

func (m packetsTablemodel) View() string {
return fmt.Sprintf("Displaying up to the last %d rows\n\n%s", m.maxRows, m.table.View())
func (m packetsTableModel) View() string {
var detailTxt string
if len(m.table.GetVisibleRows()) > 0 {
detailTxt = m.table.HighlightedRow().Data[columnKeyInfo].(string)
}

detailsBox := lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("#00cc99")).
Padding(1, 2).
Width((40 * m.width) / 100).
Height((80 * m.height) / 100).
Render(detailTxt)

mainView := lipgloss.JoinHorizontal(
lipgloss.Top,
m.table.View(),
detailsBox,
)

view := lipgloss.JoinVertical(
lipgloss.Left,
styles.Subtle.Render(fmt.Sprintf("Displaying up to the last %d rows", m.maxRows)),
mainView,
) + "\n"

return view
}

func (m *packetsTablemodel) addRows(packets []sockets.NetworkPacket) {
func (m *packetsTableModel) addRows(packets []sockets.NetworkPacket) {
lp := len(packets)
lc := len(m.cachedRows)

Expand Down

0 comments on commit 71e3dab

Please sign in to comment.