5
5
"time"
6
6
7
7
"github.com/NamelessOne91/bisturi/sockets"
8
+ "github.com/NamelessOne91/bisturi/tui/styles"
8
9
tea "github.com/charmbracelet/bubbletea"
9
10
"github.com/charmbracelet/lipgloss"
10
11
"github.com/evertras/bubble-table/table"
@@ -18,48 +19,56 @@ const (
18
19
columnKeyInfo = "info"
19
20
)
20
21
21
- type packetsTablemodel struct {
22
+ type packetsTableModel struct {
22
23
table table.Model
24
+ height int
25
+ width int
23
26
maxRows int
24
27
cachedRows []table.Row
25
28
counter uint64
26
29
}
27
30
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 ),
35
37
}).
36
- WithRows (rows ).
38
+ WithRows (m .cachedRows ).
39
+ Focused (true ).
37
40
WithBaseStyle (lipgloss .NewStyle ().
38
41
BorderForeground (lipgloss .Color ("#00cc99" )).
39
42
Foreground (lipgloss .Color ("#00cc99" )).
40
43
Align (lipgloss .Center ),
41
44
)
42
45
}
43
46
44
- func newPacketsTable (max int , terminalWidth int ) packetsTablemodel {
47
+ func newPacketsTable (max int , height , width int ) packetsTableModel {
45
48
rows := make ([]table.Row , 0 , max )
46
49
47
- return packetsTablemodel {
50
+ ptm := packetsTableModel {
51
+ height : height ,
52
+ width : width ,
48
53
maxRows : max ,
49
54
cachedRows : rows ,
50
- table : buildTable (rows , terminalWidth ),
51
55
}
56
+ ptm .buildTable ()
57
+
58
+ return ptm
52
59
}
53
60
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 ()
56
65
}
57
66
58
- func (m packetsTablemodel ) Init () tea.Cmd {
67
+ func (m packetsTableModel ) Init () tea.Cmd {
59
68
return nil
60
69
}
61
70
62
- func (m packetsTablemodel ) Update (msg tea.Msg ) (packetsTablemodel , tea.Cmd ) {
71
+ func (m packetsTableModel ) Update (msg tea.Msg ) (packetsTableModel , tea.Cmd ) {
63
72
switch msg := msg .(type ) {
64
73
case readPacketsMsg :
65
74
m .addRows (msg )
@@ -77,11 +86,36 @@ func (m packetsTablemodel) Update(msg tea.Msg) (packetsTablemodel, tea.Cmd) {
77
86
return m , cmd
78
87
}
79
88
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
82
116
}
83
117
84
- func (m * packetsTablemodel ) addRows (packets []sockets.NetworkPacket ) {
118
+ func (m * packetsTableModel ) addRows (packets []sockets.NetworkPacket ) {
85
119
lp := len (packets )
86
120
lc := len (m .cachedRows )
87
121
0 commit comments