-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscover.go
88 lines (68 loc) · 1.77 KB
/
discover.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
package main
import (
"fmt"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var docStyle = lipgloss.NewStyle().Margin(1, 2)
type item struct {
isService bool
uuid string
description string
}
func (i item) Title() string {
typ := "Characteristic"
if i.isService {
typ = "Service"
}
return fmt.Sprintf("%s %s", typ, i.uuid)
}
func (i item) Description() string {
return i.description
}
func (i item) FilterValue() string { return i.uuid }
func (m *model) discover(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case errMsg:
m.err = msg.err
return m, nil
case connectDeviceMsg:
return m, m.connectToDevice(m.devices.SelectedRow()[0])
case discoverServicesMsg:
return m, m.discoverServices()
case servicesDiscoveredMsg:
name := m.devices.SelectedRow()[0]
if m.devices.SelectedRow()[2] != "" {
name = m.devices.SelectedRow()[2]
}
m.servicesList = initServicesList(name, msg.items)
case tea.WindowSizeMsg:
w, h := docStyle.GetFrameSize()
m.w, m.h = msg.Width, msg.Height
m.servicesList.SetSize(msg.Width-w, msg.Height-h)
}
var cmd tea.Cmd
m.servicesList, cmd = m.servicesList.Update(msg)
return m, cmd
}
func (m model) discoveringView() string {
if m.err != nil {
return fmt.Sprintf("\nERROR: %v\n\n", m.err)
}
str := fmt.Sprintf("\n\nDiscovering services...\n\n")
return str
}
func (m model) discoverView() string {
if m.err != nil {
return fmt.Sprintf("\nERROR: %v\n\n", m.err)
}
return docStyle.Render(m.servicesList.View())
}
func initServicesList(title string, items []list.Item) list.Model {
l := list.New(items, list.NewDefaultDelegate(), 80, 40)
l.Title = title
l.SetShowStatusBar(true)
l.SetFilteringEnabled(false)
return l
}