/
iptable.go
183 lines (155 loc) · 4.46 KB
/
iptable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package tui
import (
"fmt"
"os"
"strconv"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"golang.org/x/term"
vc "github.com/ewilliams0305/VC4-CLI/pkg/vc"
)
type IpTableModel struct {
roomId string
table table.Model
entries []vc.IpTableEntry
selected vc.IpTableEntry
err error
help HelpModel
width, height int
banner *BannerModel
}
func InitialIpTableModel(width, height int, roomid string) *IpTableModel {
entries := make([]vc.IpTableEntry, 0)
iptable = &IpTableModel{
table: newIpTableDeviceTable(entries, 0, width),
roomId: roomid,
entries: entries,
selected: vc.IpTableEntry{},
help: NewHelpModel(),
width: width,
height: height,
banner: NewBanner(fmt.Sprintf("VIEWING %s PROGRAM IP TABLES", roomid), BannerNormalState, width),
}
return iptable
}
func (m IpTableModel) Init() tea.Cmd {
return IpTableQuery(m.roomId)
}
func (m IpTableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tickMsg:
w, h, _ := term.GetSize(int(os.Stdout.Fd()))
if w != m.width || h != m.height {
iptable.width = w
iptable.height = h
}
return iptable, tea.Batch(tick, IpTableQuery(iptable.roomId))
case tea.WindowSizeMsg:
iptable.width = msg.Width
iptable.height = msg.Height
iptable.table, cmd = iptable.table.Update(msg)
return iptable, cmd
case int:
iptable.selected = iptable.entries[msg]
return iptable, nil
case []vc.IpTableEntry:
iptable.err = nil
iptable.entries = msg
iptable.banner = NewBanner(fmt.Sprintf("VIEWING %s PROGRAM IP TABLES", iptable.roomId), BannerNormalState, iptable.width)
iptable.table.SetRows(getIpTableRows(iptable.width, iptable.table.Cursor(), msg))
if len(msg) != 0 && iptable.table.Cursor() >= 0 {
iptable.selected = msg[iptable.table.Cursor()]
}
return iptable, nil
case error:
iptable.err = msg
iptable.banner = NewBanner(msg.Error(), BannerErrorState, iptable.width)
return iptable, nil
case tea.KeyMsg:
switch msg.String() {
case "ctrl+q", "q", "ctrl+c", "esc":
return ReturnRoomsModel(), tea.Batch(tick, RoomsQuery)
}
}
iptable.table, cmd = m.table.Update(msg)
return iptable, cmd
}
func (m IpTableModel) View() string {
s := m.banner.View() + "\n"
s += BaseStyle.Render(m.table.View()) + "\n\n"
s += RenderMessageBox(m.width).Render(fmt.Sprintf("IPID: %d, %s %s", m.selected.ProgramIPID, m.selected.Model, m.selected.Description))
s += m.help.renderHelpInfo()
return s
}
func newIpTableDeviceTable(entries []vc.IpTableEntry, cursor int, width int) table.Model {
columns := getIpTableColumns(width)
rows := getIpTableRows(width, cursor, entries)
t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(false),
table.WithHeight(16),
table.WithWidth(width),
)
t.Focus()
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Foreground(lipgloss.Color(AccentColor)).
Bold(true)
s.Selected = s.Selected.
Foreground(lipgloss.Color(PrimaryLight)).
Background(lipgloss.Color(PrimaryDark)).
Bold(false)
t.SetStyles(s)
t.SetCursor(cursor)
return t
}
func getIpTableColumns(width int) []table.Column {
if width < 120 {
return []table.Column{
{Title: "", Width: 1},
{Title: "IPID", Width: 8},
{Title: "MODEL", Width: 28},
{Title: "DESCRIPTION", Width: width - 57},
{Title: "STATUS", Width: 8},
}
}
return []table.Column{
{Title: "", Width: 1},
{Title: "ID", Width: 20},
{Title: "MODEL", Width: 35},
{Title: "DESCRIPTION", Width: width - 113},
{Title: "IP ADDRESS", Width: 35},
{Title: "STATUS", Width: 8},
}
}
func getIpTableRows(width int, cursor int, entries []vc.IpTableEntry) []table.Row {
rows := []table.Row{}
small := width < 120
for i, ipt := range entries {
marker := ""
if cursor == i {
marker = "\u2192"
}
if small {
rows = append(rows, table.Row{marker, strconv.FormatInt(int64(ipt.ProgramIPID), 16), ipt.Model, ipt.Description, GetOnlineIcon(ipt.Status)})
} else {
rows = append(rows, table.Row{marker, strconv.FormatInt(int64(ipt.ProgramIPID), 16), ipt.Model, ipt.Description, ipt.RemoteIP, GetOnlineIcon(ipt.Status)})
}
}
return rows
}
func IpTableQuery(id string) tea.Cmd {
return func() tea.Msg {
ipTable, err := server.GetIpTable(id)
if err != nil {
return err
}
return ipTable
}
}