gofmt -w ./ and implementing terminal clearing
This commit is contained in:
parent
c84a110f6e
commit
52f419dfa3
Binary file not shown.
68
tui.go
68
tui.go
@ -6,6 +6,7 @@ import (
|
||||
"github.com/charmbracelet/bubbles/spinner"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
term "github.com/muesli/termenv"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"os"
|
||||
@ -19,31 +20,34 @@ import (
|
||||
|
||||
var _ScanMgr *scanStuff.Meta
|
||||
|
||||
|
||||
type model struct {
|
||||
choices []string // items on the device list
|
||||
cursor int // which device item our cursor is pointing at
|
||||
selected map[int]struct{} // which device items are selected
|
||||
spinner spinner.Model
|
||||
choices []string // items on the device list
|
||||
cursor int // which device item our cursor is pointing at
|
||||
selected map[int]struct{} // which device items are selected
|
||||
spinner spinner.Model
|
||||
}
|
||||
|
||||
func init() {
|
||||
term.ClearScreen()
|
||||
|
||||
}
|
||||
|
||||
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{ strings.Repeat(" ", 6 ) + "Address" + strings.Repeat(" ", 17) + "RSSI" },
|
||||
var initialModel = model{
|
||||
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,
|
||||
spinner: getSpinner(),
|
||||
cursor: 1,
|
||||
}
|
||||
|
||||
func initializeRadios() {
|
||||
@ -88,7 +92,7 @@ func initializeRadios() {
|
||||
}
|
||||
|
||||
// define pretty printer
|
||||
consoleWriter := zerolog.ConsoleWriter {
|
||||
consoleWriter := zerolog.ConsoleWriter{
|
||||
Out: os.Stderr,
|
||||
}
|
||||
|
||||
@ -133,18 +137,12 @@ func getFormattedRow(dev projVars.DiscoveredDevice) string {
|
||||
strings.Repeat(" ", 7),
|
||||
func(rssi int16) string {
|
||||
if rssi < -80 {
|
||||
return lipgloss.NewStyle(
|
||||
).Foreground(lipgloss.Color("#FF0000"),
|
||||
).Render(fmt.Sprintf("%d", rssi))
|
||||
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))
|
||||
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))
|
||||
return lipgloss.NewStyle().Foreground(lipgloss.Color("#008000")).Render(fmt.Sprintf("%d", rssi))
|
||||
}
|
||||
}(dev.ScanResult.RSSI))
|
||||
}
|
||||
@ -197,7 +195,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++
|
||||
}
|
||||
|
||||
@ -220,16 +218,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
|
||||
func (m model) View() string {
|
||||
// The header
|
||||
s := lipgloss.NewStyle(
|
||||
).Bold(true,
|
||||
).Foreground(lipgloss.Color("#FAFAFA"),
|
||||
).Background(lipgloss.Color("#7D56F4"),
|
||||
).PaddingTop(0,
|
||||
).PaddingLeft(0,
|
||||
).Width(78,
|
||||
).Render("protomolecule" + strings.Repeat(" ", 78 - 24) + "[q] - exit",
|
||||
) + "\n\n"
|
||||
|
||||
s := lipgloss.NewStyle().Bold(true).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
|
||||
NOTE: change to only print n rows (minus header, footer and any padding rows) to ensure
|
||||
@ -239,7 +228,7 @@ func (m model) View() string {
|
||||
https://github.com/muesli/termenv
|
||||
https://stackoverflow.com/questions/16569433/get-terminal-size-in-go
|
||||
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)
|
||||
@ -250,18 +239,14 @@ func (m model) View() string {
|
||||
cursor := " " // no cursor
|
||||
if m.cursor == i {
|
||||
//cursor = ">" // cursor!
|
||||
cursor = lipgloss.NewStyle(
|
||||
).Blink(true,
|
||||
).Render(">")
|
||||
cursor = lipgloss.NewStyle().Blink(true).Render(">")
|
||||
}
|
||||
|
||||
// Is this choice selected?
|
||||
checked := " " // not selected
|
||||
if _, ok := m.selected[i]; ok {
|
||||
//checked = "x" // selected!
|
||||
checked = lipgloss.NewStyle(
|
||||
).Bold(true,
|
||||
).Render("x")
|
||||
checked = lipgloss.NewStyle().Bold(true).Render("x")
|
||||
}
|
||||
|
||||
// Render the row
|
||||
@ -278,7 +263,7 @@ func (m model) View() string {
|
||||
func listenForScanResults() tea.Msg {
|
||||
for {
|
||||
select {
|
||||
case dev, ok := <- projVars.DiscoveredDeviceChan:
|
||||
case dev, ok := <-projVars.DiscoveredDeviceChan:
|
||||
if ok {
|
||||
//time.Sleep( 1 * time.Second)
|
||||
return dev
|
||||
@ -290,10 +275,11 @@ func listenForScanResults() tea.Msg {
|
||||
}
|
||||
|
||||
func main() {
|
||||
defer term.ClearScreen()
|
||||
p := tea.NewProgram(initialModel)
|
||||
|
||||
if err := p.Start(); err != nil {
|
||||
fmt.Printf("Alas, there's been an error: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user