rssi color, added columns cleaned up header bar started adding padding for elements

This commit is contained in:
sq 2021-05-26 15:39:27 -04:00
parent 878def56ec
commit c84a110f6e
No known key found for this signature in database
GPG Key ID: 7AF3499CBA8E6251
43 changed files with 46 additions and 24 deletions

Binary file not shown.

BIN
.eros-data/devices/index Normal file

Binary file not shown.

70
tui.go

@ -30,18 +30,20 @@ type model struct {
func getSpinner() spinner.Model {
s := spinner.NewModel()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
s.Style = lipgloss.NewStyle(
).Foreground(lipgloss.Color("205"))
return s
}
var initialModel = model {
choices: []string{},
choices: []string{ strings.Repeat(" ", 6 ) + "Address" + strings.Repeat(" ", 17) + "RSSI" },
// 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(),
cursor: 1,
}
func initializeRadios() {
@ -125,15 +127,35 @@ func (m model) Init() tea.Cmd {
)
}
func getFormattedRow(dev projVars.DiscoveredDevice) string {
return fmt.Sprintf("%s%s%s",
dev.ScanResult.Address.String(),
strings.Repeat(" ", 7),
func(rssi int16) string {
if rssi < -80 {
return lipgloss.NewStyle(
).Foreground(lipgloss.Color("#FF0000"),
).Render(fmt.Sprintf("%d", rssi))
}
if rssi < -50 {
return lipgloss.NewStyle(
).Foreground(lipgloss.Color("#FFFF00"),
).Render(fmt.Sprintf("%d", rssi))
} else {
return lipgloss.NewStyle(
).Foreground(lipgloss.Color("#008000"),
).Render(fmt.Sprintf("%d", rssi))
}
}(dev.ScanResult.RSSI))
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case projVars.DiscoveredDevice:
for k, v := range m.choices {
if strings.HasPrefix(v, fmt.Sprintf("address: %s", msg.ScanResult.Address.String())) {
m.choices[k] = fmt.Sprintf("address: %s rssi: %d",
msg.ScanResult.Address.String(),
msg.ScanResult.RSSI)
if strings.HasPrefix(v, msg.ScanResult.Address.String()) {
m.choices[k] = getFormattedRow(msg)
return m, listenForScanResults
}
@ -148,10 +170,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}(len(m.choices)):],
fmt.Sprintf("address: %s rssi: %d", msg.ScanResult.Address.String(), msg.ScanResult.RSSI)) */
m.choices = append(m.choices,
fmt.Sprintf("address: %s rssi: %d",
msg.ScanResult.Address.String(),
msg.ScanResult.RSSI))
m.choices = append(m.choices, getFormattedRow(msg))
return m, listenForScanResults
@ -172,7 +191,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// The "up" and "k" keys move the cursor up
case "up", "k":
if m.cursor > 0 {
if m.cursor > 1 {
m.cursor--
}
@ -201,16 +220,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m model) View() string {
// The header
s := m.spinner.View(
) + lipgloss.NewStyle(
s := lipgloss.NewStyle(
).Bold(true,
).Foreground(lipgloss.Color("#FAFAFA"),
).Background(lipgloss.Color("#7D56F4"),
).PaddingTop(0,
).PaddingLeft(0,
).Width(80,
).Render("Scanning for devices",
) + "\n\n"
).Foreground(lipgloss.Color("#FAFAFA"),
).Background(lipgloss.Color("#7D56F4"),
).PaddingTop(0,
).PaddingLeft(0,
).Width(78,
).Render("protomolecule" + strings.Repeat(" ", 78 - 24) + "[q] - exit",
) + "\n\n"
/* Iterate over our choices
@ -223,6 +241,10 @@ func (m model) View() string {
https://stackoverflow.com/questions/24562942/golang-how-do-i-determine-the-number-of-lines-in-a-file-efficiently/24563853
*/
for i, choice := range m.choices {
if strings.HasPrefix(choice, " ") {
s += fmt.Sprintf("%s\n", choice)
continue
}
// Is the cursor pointing at this choice?
cursor := " " // no cursor
@ -230,7 +252,7 @@ func (m model) View() string {
//cursor = ">" // cursor!
cursor = lipgloss.NewStyle(
).Blink(true,
).Render(">")
).Render(">")
}
// Is this choice selected?
@ -239,7 +261,7 @@ func (m model) View() string {
//checked = "x" // selected!
checked = lipgloss.NewStyle(
).Bold(true,
).Render("x")
).Render("x")
}
// Render the row
@ -247,7 +269,7 @@ func (m model) View() string {
}
// The footer
s += "\nPress q to quit.\n"
s += m.spinner.View()
// Send the UI for rendering
return s
@ -261,7 +283,7 @@ func listenForScanResults() tea.Msg {
//time.Sleep( 1 * time.Second)
return dev
} else {
time.Sleep( 1 * time.Second)
time.Sleep(1 * time.Second)
}
}
}