Something went wrong. Try again.
cli wrapper around bluetoothctl commands
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485package ui
import ( "fmt" "os" "sort" "time"
"tangled.org/tannerr.dev/bt-cli/bluetooth"
"github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss")
type model struct { width int height int devices []bluetooth.Device controller *bluetooth.Controller selected int scanning bool loading bool scanInFlight bool statusMsg string err error}
func InitialModel() model { return model{ selected: 0, loading: true, }}
func (m model) Init() tea.Cmd { return tea.Batch( refreshDevices, getController, tea.Tick(time.Second*5, func(t time.Time) tea.Msg { return refreshControllerMsg{} }), )}
type refreshControllerMsg struct{}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.WindowSizeMsg: m.width = msg.Width m.height = msg.Height return m, nil case tea.KeyMsg: switch msg.String() { case "ctrl+c", "q": return m, tea.Quit case "up", "k": if m.selected > 0 { m.selected-- } case "down", "j": if m.selected < len(m.devices)-1 { m.selected++ } case "enter": if len(m.devices) > 0 && m.selected < len(m.devices) { return m, toggleDevice(m.devices[m.selected]) } case "c": if len(m.devices) > 0 && m.selected < len(m.devices) { return m, connectDevice(m.devices[m.selected].MAC) } case "d": if len(m.devices) > 0 && m.selected < len(m.devices) { return m, disconnectDevice(m.devices[m.selected].MAC) } case "t": if len(m.devices) > 0 && m.selected < len(m.devices) { return m, toggleTrust(m.devices[m.selected]) } case "r": m.loading = true return m, tea.Batch(refreshDevices, getController) case "s": if m.scanInFlight { return m, nil } m.scanInFlight = true m.scanning = !m.scanning return m, toggleScan(m.scanning) case "p": if len(m.devices) > 0 && m.selected < len(m.devices) { return m, pairDevice(m.devices[m.selected].MAC) } case "P": return m, togglePower() // case "x": // if len(m.devices) > 0 && m.selected < len(m.devices) { // return m, removeDevice(m.devices[m.selected].MAC) // } } case []bluetooth.Device: m.devices = msg m.loading = false m.err = nil case *bluetooth.Controller: m.controller = msg case devicesAndControllerMsg: devices, ctrl, err := getDevicesAndController() if err != nil { m.err = err m.loading = false } else { m.devices = devices m.controller = ctrl m.loading = false m.err = nil } case refreshControllerMsg: return m, tea.Batch( getController, tea.Tick(time.Second*5, func(t time.Time) tea.Msg { return refreshControllerMsg{} }), ) case scanDoneMsg: m.scanInFlight = false case string: m.statusMsg = msg case error: m.err = msg m.loading = false } return m, nil}
func (m model) View() string { var borderStyle lipgloss.Style var innerStyle lipgloss.Style var minWidth int
if m.width > 0 && m.width < 70 { borderStyle = BorderStyle innerStyle = InnerStyle minWidth = m.width } else { borderStyle = BorderStyleWide innerStyle = InnerStyleWide minWidth = m.width if minWidth > 80 { minWidth = 80 } } if minWidth < 50 { minWidth = 50 }
s := borderStyle.Render
header := TitleStyle.Render(" Bluetooth ") if m.controller != nil { power := "●" powerColor := lipgloss.Color("46") if !m.controller.Powered { power = "○" powerColor = lipgloss.Color("241") } header = fmt.Sprintf("%s %s %s", header, lipgloss.Style{}. Foreground(powerColor).Render(power), lipgloss.Style{}. Foreground(lipgloss.Color("250")).Render(m.controller.Name)) } if m.scanning { header = fmt.Sprintf("%s %s", header, lipgloss.Style{}. Foreground(lipgloss.Color("46")).Render("◐ Scanning")) }
output := lipgloss.JoinVertical( lipgloss.Left, header, "", )
statusMode := 2 trustMode := 2
if m.loading { output = lipgloss.JoinVertical(lipgloss.Left, output, " Loading...") } else if len(m.devices) == 0 { output = lipgloss.JoinVertical(lipgloss.Left, output, " No paired devices") output = lipgloss.JoinVertical(lipgloss.Left, output, " Press 's' to scan") } else { deviceHeader := HeaderStyle.Render(" Paired Devices ") output = lipgloss.JoinVertical(lipgloss.Left, output, deviceHeader)
nameWidth := 25
if minWidth < 60 { nameWidth = 15 statusMode = 1 trustMode = 1 } if minWidth < 45 { nameWidth = 10 statusMode = 1 trustMode = 1 } if minWidth < 35 { nameWidth = 8 statusMode = 0 trustMode = 0 }
sort.Slice(m.devices, func(i, j int) bool { return m.devices[i].DisplayName() < m.devices[j].DisplayName() })
for i, device := range m.devices { trustMark := " " if trustMode > 0 && device.Trusted { trustMark = "★" }
displayName := device.DisplayName() if lipgloss.Width(displayName) > nameWidth { displayName = truncate(displayName, nameWidth) }
var statusStr string var statusStyle lipgloss.Style connected := device.Connected
switch statusMode { case 0: statusStyle = StatusDisconnected if connected { statusStyle = StatusConnected statusStr = "●" } else { statusStr = "○" } case 1: statusStyle = StatusDisconnected if connected { statusStyle = StatusConnected statusStr = "Conn" } else { statusStr = "Disc" } default: statusStyle = StatusDisconnected if connected { statusStyle = StatusConnected statusStr = "Connected " } else { statusStr = "Disconnected" } }
deviceLine := fmt.Sprintf(" %s %-*s %s %s", device.TypeIcon(), nameWidth, displayName, statusStyle.Render(statusStr), trustMark, )
if i == m.selected { deviceLine = SelectedStyle.Render(deviceLine) } else { deviceLine = NormalStyle.Render(deviceLine) }
output = lipgloss.JoinVertical(lipgloss.Left, output, deviceLine) } }
output = lipgloss.JoinVertical(lipgloss.Left, output, "") output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render(" Navigation")) output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render(" ↑↓ Navigate")) output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render("")) output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render(" Device Actions")) output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render(" Enter Connect/Disconnect")) if minWidth >= 45 { output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render(" c Connect")) output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render(" d Disconnect")) } output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render(" t Toggle Trust")) // output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render(" x Remove Device")) output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render("")) output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render(" System")) output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render(" s Toggle Scan")) output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render(" P Toggle Power")) output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render(" r Refresh")) output = lipgloss.JoinVertical(lipgloss.Left, output, HelpStyle.Width(minWidth-4).Render(" q Quit"))
if trustMode > 0 { output = lipgloss.JoinVertical(lipgloss.Left, output, "") switch trustMode { case 1: output = lipgloss.JoinVertical(lipgloss.Left, output, lipgloss.Style{}.Foreground(lipgloss.Color("245")).Render(" ★ = Trusted")) default: output = lipgloss.JoinVertical(lipgloss.Left, output, lipgloss.Style{}.Foreground(lipgloss.Color("245")).Render(" ★ = Trusted (auto-reconnect)")) } }
if m.statusMsg != "" { if m.err != nil { output = lipgloss.JoinVertical(lipgloss.Left, output, ErrorStyle.Render(" ✗ "+m.statusMsg)) } else { output = lipgloss.JoinVertical(lipgloss.Left, output, SuccessStyle.Render(" ✓ "+m.statusMsg)) } }
bordered := s(innerStyle.Render(output))
if m.width > 0 && m.height > 0 { return lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center, bordered) }
return bordered}
func truncate(s string, maxWidth int) string { width := 0 for i, r := range s { w := 2 if r < 128 { w = 1 } if width+w > maxWidth { return s[:i] + "…" } width += w } return s}
func toggleDevice(device bluetooth.Device) tea.Cmd { return func() tea.Msg { var err error if device.Connected { err = bluetooth.DisconnectDevice(device.MAC) } else { err = bluetooth.ConnectDevice(device.MAC) } if err != nil { return err } return devicesAndControllerMsg{} }}
func connectDevice(mac string) tea.Cmd { return func() tea.Msg { err := bluetooth.ConnectDevice(mac) if err != nil { return err } return devicesAndControllerMsg{} }}
func disconnectDevice(mac string) tea.Cmd { return func() tea.Msg { err := bluetooth.DisconnectDevice(mac) if err != nil { return err } return devicesAndControllerMsg{} }}
type devicesAndControllerMsg struct{}
func getDevicesAndController() ([]bluetooth.Device, *bluetooth.Controller, error) { devices, err := bluetooth.GetDevices() if err != nil { return nil, nil, err } ctrl, err := bluetooth.GetController() if err != nil { return devices, nil, err } return devices, ctrl, nil}
func toggleTrust(device bluetooth.Device) tea.Cmd { return func() tea.Msg { var err error if device.Trusted { err = bluetooth.UntrustDevice(device.MAC) } else { err = bluetooth.TrustDevice(device.MAC) } if err != nil { return err } return "Trust updated" }}
func toggleScan(on bool) tea.Cmd { return func() tea.Msg { err := bluetooth.SetScan(on) if err != nil { return err } return scanDoneMsg{on: on} }}
type scanDoneMsg struct { on bool}
func refreshDevices() tea.Msg { devices, err := bluetooth.GetDevices() if err != nil { return err } return devices}
func getController() tea.Msg { ctrl, err := bluetooth.GetController() if err != nil { return err } return ctrl}
func pairDevice(mac string) tea.Cmd { return func() tea.Msg { err := bluetooth.PairDevice(mac) if err != nil { return err } devices, err := bluetooth.GetDevices() if err != nil { return err } return devices }}
func togglePower() tea.Cmd { return func() tea.Msg { ctrl, err := bluetooth.GetController() if err != nil { return err } err = bluetooth.SetPower(!ctrl.Powered) if err != nil { return err } newCtrl, err := bluetooth.GetController() if err != nil { return err } return newCtrl }}
func removeDevice(mac string) tea.Cmd { return func() tea.Msg { err := bluetooth.RemoveDevice(mac) if err != nil { return err } devices, err := bluetooth.GetDevices() if err != nil { return err } return devices }}
func Run() { p := tea.NewProgram(InitialModel(), tea.WithAltScreen()) if _, err := p.Run(); err != nil { fmt.Printf("Error running program: %v", err) os.Exit(1) }}