hunting bugs while trying to understand the wizardry that is bubbletea viewports

This commit is contained in:
kayos 2021-05-27 09:58:33 -07:00
parent 98eb606b4e
commit f1569b06e7
6 changed files with 39 additions and 53 deletions

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/protomolecule.iml" filepath="$PROJECT_DIR$/.idea/protomolecule.iml" />
</modules>
</component>
</project>

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

@ -19,10 +19,8 @@ func GetBanner() []string {
var head []string
scanner := bufio.NewScanner(strings.NewReader(s))
var count int = 0
for scanner.Scan() {
count++
head[count] = scanner.Text()
head = append(head, scanner.Text())
}
return head

@ -89,17 +89,17 @@ func Awaken() {
deviceDb, err = bitcask.Open(DataDir + "devices")
if err != nil {
panic(err.Error)
panic(err.Error())
}
attackDb, err = bitcask.Open(DataDir + "exploits")
if err != nil {
panic(err.Error)
panic(err.Error())
}
serviceDb, err = bitcask.Open(DataDir + "services")
if err != nil {
panic(err.Error)
panic(err.Error())
}
}

59
tui.go

@ -1,5 +1,17 @@
package main
/*
log.Debug().Msg("Starting scan")
var scanID int
var scan *scanStuff.Scan
scanID = _ScanMgr.NewScan()
scan = _ScanMgr.Scans[scanID]
dust.Must("Scan", scan.Start())
*/
import (
"flag"
"fmt"
@ -7,7 +19,6 @@ import (
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
term "github.com/muesli/termenv"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"io"
@ -134,16 +145,6 @@ func init() {
// initialize database engine
eros.Awaken()
log.Debug().Msg("Starting scan")
var scanID int
var scan *scanStuff.Scan
scanID = _ScanMgr.NewScan()
scan = _ScanMgr.Scans[scanID]
dust.Must("Scan", scan.Start())
}
// this tomfoolery was taken from the example for viewports
@ -254,19 +255,24 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
for k, v := range m.choices {
if strings.HasPrefix(v, msg.ScanResult.Address.String()) {
m.choices[k] = getFormattedRow(msg)
for _, dev := range m.choices {
m.content += dev
}
return m, listenForScanResults
cmds = append(cmds, listenForScanResults)
return m, tea.Batch(cmds...)
}
}
/* m.choices = append(m.choices[func(lLength int) int {
m.choices = append(m.choices[func(lLength int) int {
if lLength >= 10 {
return 1
} else {
return 0
}
}(len(m.choices)):],
fmt.Sprintf("address: %s rssi: %d", msg.ScanResult.Address.String(), msg.ScanResult.RSSI)) */
fmt.Sprintf("address: %s rssi: %d", msg.ScanResult.Address.String(), msg.ScanResult.RSSI))
m.choices = append(m.choices, getFormattedRow(msg))
}
@ -281,15 +287,16 @@ func (m model) View() string {
return "\n Loading ProtoMolecule..."
}
// load our banner
head = dust.GetBanner()
s := lipgloss.NewStyle().
header := lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#5991D4")).
Align(lipgloss.Center).
PaddingTop(0).
PaddingLeft(0).
//Width(78).
Width(m.viewport.Width).
Render(fmt.Sprintf("%s\n%s\n%s", head[0], head[1], head[2]))
/* Iterate over our choices
@ -301,9 +308,10 @@ func (m model) View() string {
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)
m.content += fmt.Sprintf("%s\n", choice)
continue
}
@ -324,14 +332,14 @@ func (m model) View() string {
}
// Render the row
s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
m.content += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
}
// The footer
s += m.spinner.View()
footer := m.spinner.View()
// Send the UI for rendering
return s
return fmt.Sprintf("%s\n%s\n%s", header, m.viewport.View(), footer)
}
func listenForScanResults() tea.Msg {
@ -349,11 +357,14 @@ func listenForScanResults() tea.Msg {
}
func main() {
defer term.ClearScreen()
p := tea.NewProgram(initialModel)
content := "Starting..."
p := tea.NewProgram(model{content: string(content)})
p.EnterAltScreen()
defer p.ExitAltScreen()
if err := p.Start(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
log.Fatal().Err(err).Msg("FATAL")
}
}