Skip to content

Commit 5a98fd8

Browse files
committed
Improve rows caching
1 parent 8d15437 commit 5a98fd8

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

cmd/bisturi/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func clearScreen() error {
13-
cmd := exec.Command("clear") // On Windows use exec.Command("cmd", "/c", "cls")
13+
cmd := exec.Command("clear")
1414
cmd.Stdout = os.Stdout
1515
return cmd.Run()
1616
}

tui/models/packets_table.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type packetsTablemodel struct {
2727

2828
func buildTable(rows []table.Row, terminalWidth int) table.Model {
2929
return table.New([]table.Column{
30-
table.NewColumn(columnKeyID, "#", (2*terminalWidth)/100),
30+
table.NewColumn(columnKeyID, "#", (3*terminalWidth)/100),
3131
table.NewColumn(columnKeyDate, "Date", (8*terminalWidth)/100),
3232
table.NewColumn(columnKeySource, "Source", (20*terminalWidth)/100),
3333
table.NewColumn(columnKeyDestination, "Destination", (20*terminalWidth)/100),
@@ -42,7 +42,7 @@ func buildTable(rows []table.Row, terminalWidth int) table.Model {
4242
}
4343

4444
func newPacketsTable(max int, terminalWidth int) packetsTablemodel {
45-
rows := make([]table.Row, 0, max)
45+
rows := make([]table.Row, max)
4646

4747
return packetsTablemodel{
4848
maxRows: max,
@@ -82,15 +82,26 @@ func (m packetsTablemodel) View() string {
8282
}
8383

8484
func (m *packetsTablemodel) addRows(packets []sockets.NetworkPacket) {
85-
for _, np := range packets {
86-
if len(m.cachedRows) >= m.maxRows {
87-
m.cachedRows = m.cachedRows[1:]
85+
lp := len(packets)
86+
lc := len(m.cachedRows)
87+
88+
if (lp + lc) > m.maxRows {
89+
newCache := make([]table.Row, 0, m.maxRows)
90+
if lp > m.maxRows {
91+
packets = packets[lp-m.maxRows:]
92+
} else {
93+
oldToKeep := m.maxRows - lp
94+
newCache = append(newCache, m.cachedRows[lc-oldToKeep:]...)
8895
}
96+
m.cachedRows = newCache
97+
}
98+
99+
for _, np := range packets {
89100
m.counter += 1
90101

91102
newRow := table.NewRow(table.RowData{
92103
columnKeyID: m.counter,
93-
columnKeyDate: time.Now().Local().Format(time.Stamp),
104+
columnKeyDate: time.Now().Local().Format(time.TimeOnly),
94105
columnKeySource: np.Source(),
95106
columnKeyDestination: np.Destination(),
96107
columnKeyInfo: np.Info(),

0 commit comments

Comments
 (0)