added spinner

This commit is contained in:
sq 2021-05-26 12:18:02 -04:00
parent 1dfaf1fe20
commit c89316d781
No known key found for this signature in database
GPG Key ID: 7AF3499CBA8E6251
6 changed files with 37 additions and 22 deletions

59
tui.go

@ -3,7 +3,9 @@ package main
import (
"flag"
"fmt"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"os"
@ -22,16 +24,24 @@ type model struct {
choices []string // items on the to-do list
cursor int // which to-do list item our cursor is pointing at
selected map[int]struct{} // which to-do items are selected
spinner spinner.Model
}
func getSpinner() spinner.Model {
s := spinner.NewModel()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
return s
}
var initialModel = model {
// Our to-do list is just a grocery list
choices: []string{},
// A map which indicates which choices are selected. We're using
// the map like a mathematical set. The keys refer to the indexes
// of the `choices` slice, above.
selected: make(map[int]struct{}),
spinner: getSpinner(),
}
func initializeRadios() {
@ -76,7 +86,9 @@ func initializeRadios() {
}
// define pretty printer
consoleWriter := zerolog.ConsoleWriter{Out: os.Stderr}
consoleWriter := zerolog.ConsoleWriter {
Out: os.Stderr,
}
// initialize simultaneous pretty printing and json logging
multi := zerolog.MultiLevelWriter(consoleWriter, lf)
@ -97,11 +109,19 @@ func (m model) Init() tea.Cmd {
return tea.Batch(
func() tea.Msg {
startBTScan()
var scanID int
var scan *scanStuff.Scan
scanID = _ScanMgr.NewScan()
scan = _ScanMgr.Scans[scanID]
//time.Sleep(30 * time.Millisecond)
dust.Must("Scan", scan.Start())
return nil
},
runUpdaterProcess,
listenForScanResults,
spinner.Tick,
)
}
@ -111,11 +131,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case projVars.DiscoveredDevice:
for _, v := range m.choices {
if strings.HasPrefix(v, fmt.Sprintf("address: %s", msg.ScanResult.Address.String())) {
return m, runUpdaterProcess
// TODO update RSSI
return m, listenForScanResults
}
}
/*m.choices = append(m.choices[func(lLength int) int {
/* m.choices = append(m.choices[func(lLength int) int {
if lLength >= 10 {
return 1
} else {
@ -127,7 +148,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.choices = append(m.choices,
fmt.Sprintf("address: %s rssi: %d", msg.ScanResult.Address.String(), msg.ScanResult.RSSI))
return m, runUpdaterProcess
return m, listenForScanResults
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
// Is it a key press?
case tea.KeyMsg:
@ -147,7 +173,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// The "down" and "j" keys move the cursor down
case "down", "j":
if m.cursor < len(m.choices)-1 {
if m.cursor < len(m.choices) - 1 {
m.cursor++
}
@ -170,7 +196,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m model) View() string {
// The header
s := "Scanning...\n\n"
s := m.spinner.View() + " Scanning for devices" + "\n\n"
// Iterate over our choices
for i, choice := range m.choices {
@ -198,7 +224,7 @@ func (m model) View() string {
return s
}
func runUpdaterProcess() tea.Msg {
func listenForScanResults() tea.Msg {
for {
select {
case dev, ok := <- projVars.DiscoveredDeviceChan:
@ -212,17 +238,6 @@ func runUpdaterProcess() tea.Msg {
}
}
func startBTScan() {
var scanID int
var scan *scanStuff.Scan
scanID = _ScanMgr.NewScan()
scan = _ScanMgr.Scans[scanID]
//time.Sleep(30 * time.Millisecond)
dust.Must("Scan", scan.Start())
}
func main() {
p := tea.NewProgram(initialModel)