-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.go
92 lines (77 loc) · 1.75 KB
/
scan.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
package main
import (
"strconv"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"tinygo.org/x/bluetooth"
)
func (m model) scan(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case bluetooth.ScanResult:
r := table.Row{
msg.Address.String(),
strconv.Itoa(int(msg.RSSI)),
msg.LocalName(),
}
devices := m.devices.Rows()
if len(devices) > maxRows {
devices = devices[1:]
}
devices = append(devices, r)
m.devices.SetRows(devices)
return m, m.waitForDevice()
case tea.KeyMsg:
switch msg.String() {
case "esc":
if m.devices.Focused() {
m.devices.Blur()
} else {
m.devices.Focus()
}
case " ":
adapter.StopScan()
case "enter":
adapter.StopScan()
m.state = "discovering"
// now go discover services
return m.discover(connectDeviceMsg{})
}
}
m.devices, cmd = m.devices.Update(msg)
return m, cmd
}
func (m model) scanView() string {
return baseStyle.Render(m.devices.View()) + "\n"
}
const maxRows = 36
var (
baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))
)
func initDevicesTable() table.Model {
columns := []table.Column{
{Title: "MAC", Width: 18},
{Title: "RSSI", Width: 4},
{Title: "Name", Width: 30},
}
tbl := table.New(
table.WithColumns(columns),
table.WithFocused(true),
table.WithHeight(maxRows+1),
)
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color("229")).
Background(lipgloss.Color("57")).
Bold(false)
tbl.SetStyles(s)
return tbl
}