Skip to content

Commit 71e3dab

Browse files
committed
Display selected packet/row info in a dedicated box
1 parent a7a15f2 commit 71e3dab

File tree

2 files changed

+57
-23
lines changed

2 files changed

+57
-23
lines changed

tui/models/bisturi_model.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type bisturiModel struct {
3636
spinner spinner.Model
3737
startMenu startMenuModel
3838
rowsInput textinput.Model
39-
packetsTable packetsTablemodel
39+
packetsTable packetsTableModel
4040
selectedInterface net.Interface
4141
selectedProtocol string
4242
selectedEthType uint16
@@ -217,7 +217,7 @@ func (m *bisturiModel) updateRowsInput(msg tea.Msg) (tea.Model, tea.Cmd) {
217217
case "enter":
218218
maxRows, err := strconv.Atoi(m.rowsInput.Value())
219219
if err == nil && maxRows > 0 {
220-
m.packetsTable = newPacketsTable(maxRows, m.terminalWidth)
220+
m.packetsTable = newPacketsTable(maxRows, m.terminalHeight, m.terminalWidth)
221221
m.step = receivePackets
222222

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

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

252252
func (m bisturiModel) readPackets() {
253253
readPackets := []sockets.NetworkPacket{}
254-
timer := time.NewTicker(3 * time.Second)
254+
timer := time.NewTicker(5 * time.Second)
255255
defer timer.Stop()
256256

257257
for {

tui/models/packets_table.go

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/NamelessOne91/bisturi/sockets"
8+
"github.com/NamelessOne91/bisturi/tui/styles"
89
tea "github.com/charmbracelet/bubbletea"
910
"github.com/charmbracelet/lipgloss"
1011
"github.com/evertras/bubble-table/table"
@@ -18,48 +19,56 @@ const (
1819
columnKeyInfo = "info"
1920
)
2021

21-
type packetsTablemodel struct {
22+
type packetsTableModel struct {
2223
table table.Model
24+
height int
25+
width int
2326
maxRows int
2427
cachedRows []table.Row
2528
counter uint64
2629
}
2730

28-
func buildTable(rows []table.Row, terminalWidth int) table.Model {
29-
return table.New([]table.Column{
30-
table.NewColumn(columnKeyID, "#", (3*terminalWidth)/100),
31-
table.NewColumn(columnKeyDate, "Date", (8*terminalWidth)/100),
32-
table.NewColumn(columnKeySource, "Source", (20*terminalWidth)/100),
33-
table.NewColumn(columnKeyDestination, "Destination", (20*terminalWidth)/100),
34-
table.NewColumn(columnKeyInfo, "Info", (46*terminalWidth)/100),
31+
func (m *packetsTableModel) buildTable() {
32+
m.table = table.New([]table.Column{
33+
table.NewColumn(columnKeyID, "#", (3*m.width)/100),
34+
table.NewColumn(columnKeyDate, "Date", (7*m.width)/100),
35+
table.NewColumn(columnKeySource, "Source", (20*m.width)/100),
36+
table.NewColumn(columnKeyDestination, "Destination", (20*m.width)/100),
3537
}).
36-
WithRows(rows).
38+
WithRows(m.cachedRows).
39+
Focused(true).
3740
WithBaseStyle(lipgloss.NewStyle().
3841
BorderForeground(lipgloss.Color("#00cc99")).
3942
Foreground(lipgloss.Color("#00cc99")).
4043
Align(lipgloss.Center),
4144
)
4245
}
4346

44-
func newPacketsTable(max int, terminalWidth int) packetsTablemodel {
47+
func newPacketsTable(max int, height, width int) packetsTableModel {
4548
rows := make([]table.Row, 0, max)
4649

47-
return packetsTablemodel{
50+
ptm := packetsTableModel{
51+
height: height,
52+
width: width,
4853
maxRows: max,
4954
cachedRows: rows,
50-
table: buildTable(rows, terminalWidth),
5155
}
56+
ptm.buildTable()
57+
58+
return ptm
5259
}
5360

54-
func (m *packetsTablemodel) resizeTable(terminalWidth int) {
55-
m.table = buildTable(m.cachedRows, terminalWidth)
61+
func (m *packetsTableModel) resize(height, width int) {
62+
m.height = height
63+
m.width = width
64+
m.buildTable()
5665
}
5766

58-
func (m packetsTablemodel) Init() tea.Cmd {
67+
func (m packetsTableModel) Init() tea.Cmd {
5968
return nil
6069
}
6170

62-
func (m packetsTablemodel) Update(msg tea.Msg) (packetsTablemodel, tea.Cmd) {
71+
func (m packetsTableModel) Update(msg tea.Msg) (packetsTableModel, tea.Cmd) {
6372
switch msg := msg.(type) {
6473
case readPacketsMsg:
6574
m.addRows(msg)
@@ -77,11 +86,36 @@ func (m packetsTablemodel) Update(msg tea.Msg) (packetsTablemodel, tea.Cmd) {
7786
return m, cmd
7887
}
7988

80-
func (m packetsTablemodel) View() string {
81-
return fmt.Sprintf("Displaying up to the last %d rows\n\n%s", m.maxRows, m.table.View())
89+
func (m packetsTableModel) View() string {
90+
var detailTxt string
91+
if len(m.table.GetVisibleRows()) > 0 {
92+
detailTxt = m.table.HighlightedRow().Data[columnKeyInfo].(string)
93+
}
94+
95+
detailsBox := lipgloss.NewStyle().
96+
BorderStyle(lipgloss.NormalBorder()).
97+
BorderForeground(lipgloss.Color("#00cc99")).
98+
Padding(1, 2).
99+
Width((40 * m.width) / 100).
100+
Height((80 * m.height) / 100).
101+
Render(detailTxt)
102+
103+
mainView := lipgloss.JoinHorizontal(
104+
lipgloss.Top,
105+
m.table.View(),
106+
detailsBox,
107+
)
108+
109+
view := lipgloss.JoinVertical(
110+
lipgloss.Left,
111+
styles.Subtle.Render(fmt.Sprintf("Displaying up to the last %d rows", m.maxRows)),
112+
mainView,
113+
) + "\n"
114+
115+
return view
82116
}
83117

84-
func (m *packetsTablemodel) addRows(packets []sockets.NetworkPacket) {
118+
func (m *packetsTableModel) addRows(packets []sockets.NetworkPacket) {
85119
lp := len(packets)
86120
lc := len(m.cachedRows)
87121

0 commit comments

Comments
 (0)