Doing some work

This commit is contained in:
2026-01-22 12:21:57 -06:00
parent 623bc860d4
commit a216f1ca5b
15 changed files with 1048 additions and 71 deletions

View File

@@ -22,13 +22,28 @@ THE SOFTWARE.
package app
import (
"fmt"
"time"
"git.bullercodeworks.com/brian/expds/data"
w "git.bullercodeworks.com/brian/tcell-widgets"
h "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/gdamore/tcell"
"github.com/spf13/cobra"
)
type App struct {
repo *data.Repo
Name string
h, w int
running bool
tScreen tcell.Screen
screen AppScreen
repo *data.Repo
AppLogs []data.AppLog
style tcell.Style
}
@@ -44,10 +59,67 @@ func NewApp() *App {
func (a *App) init() error {
var err error
a.repo, err = data.NewRepo()
if err != nil {
return fmt.Errorf("error initializing repo: %w", err)
}
if a.tScreen, err = tcell.NewScreen(); err != nil {
return fmt.Errorf("error creating screen: %w", err)
}
if err = a.tScreen.Init(); err != nil {
return fmt.Errorf("error initializing screen: %w", err)
}
a.tScreen.SetStyle(a.style)
a.tScreen.Clear()
a.SetScreen(&ScreenHome{})
go func() {
var err error
for err == nil {
time.Sleep(time.Millisecond * 100)
err = a.PostNowEvent()
}
}()
return err
}
func (a *App) Run(args []string) error {
a.running = true
for {
if !a.running {
return nil
}
if a.screen != nil {
a.ClearScreen()
a.screen.Draw()
} else {
a.DrawText(1, 1, "No screen loaded", tcell.StyleDefault)
}
a.tScreen.Show()
// Poll Events
ev := a.tScreen.PollEvent()
switch ev := ev.(type) {
case *tcell.EventResize:
a.tScreen.Sync()
a.w, a.h = ev.Size()
a.screen.HandleResize(ev)
case *tcell.EventKey:
if ev.Key() == tcell.KeyCtrlC {
a.Stop()
} else if ev.Key() == tcell.KeyCtrlJ {
// Ctrl+J is the keypad 'Enter'
ev = tcell.NewEventKey(tcell.KeyEnter, 0, 0)
}
if a.screen != nil && !a.screen.HandleKey(ev) {
// Screen didn't handle the key
}
case *tcell.EventTime:
if a.screen != nil {
a.screen.HandleTime(ev)
}
}
}
return nil
}
@@ -84,14 +156,14 @@ func (a *App) Cleanup() { a.tScreen.Fini() }
func (a *App) GetRepo() *data.Repo { return a.repo }
// Draw a rune to the screen
func (a *DhApp) DrawRune(x, y int, style tcell.Style, r rune) {
func (a *App) DrawRune(x, y int, style tcell.Style, r rune) {
a.tScreen.SetContent(x, y, r, nil, style)
}
// Draw text to the screen
func (a *DhApp) DrawText(x, y int, text string, style tcell.Style) {
func (a *App) DrawText(x, y int, text string, style tcell.Style) {
h.DrawText(x, y, text, style, a.tScreen)
}
// Draw a widget to the screen
func (a *DhApp) DrawWidget(w w.Widget) { w.Draw(a.tScreen) }
func (a *App) DrawWidget(w w.Widget) { w.Draw(a.tScreen) }

View File

@@ -22,10 +22,16 @@ THE SOFTWARE.
package app
import (
"fmt"
"git.bullercodeworks.com/brian/expds/data"
"git.bullercodeworks.com/brian/expds/data/models"
wd "git.bullercodeworks.com/brian/expds/widgets"
w "git.bullercodeworks.com/brian/tcell-widgets"
h "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/gdamore/tcell"
"github.com/spf13/viper"
)
type ScreenHome struct {
@@ -37,14 +43,20 @@ type ScreenHome struct {
menuLayout *w.TopMenuLayout
menu *w.Menu
layout *w.LinearLayout
layout *w.LinearLayout
columns *w.LinearLayout
pdsListing *wd.SimpleListWithHelp
jsonContent *wd.JsonContent
activePds *models.Pds
pdsListingTypes []models.EntryType
pdsListing *wd.SimpleListWithHelp
pdsNSIDs []syntax.NSID
jsonContent *wd.JsonContent
alert *w.Alert
alertLayout *w.LinearLayout
cli *w.Cli
cursor int
}
@@ -55,38 +67,35 @@ func (s *ScreenHome) Init(a *App) {
s.menuLayout = w.NewTopMenuLayout("home.toplayout", s.style)
s.initMenu()
s.cli = w.NewCli("home.cli", s.style)
s.initCli()
s.alert = w.NewAlert("home.alert", s.style)
s.alert.SetVisible(false)
s.alertLayout = w.NewLinearLayout("home.alertlayout", s.style)
s.alertLayout.Add(s.alert)
s.layout = w.NewLinearLayout("home.layout", s.style)
s.layout.SetOrientation(w.LinLayH)
s.columns = w.NewLinearLayout("home.layout.columns", s.style)
s.columns.SetOrientation(w.LinLayH)
s.pdsListing = wd.NewSimpleListWithHelp("pdslisting", s.style)
s.pdsListing.SetBorder(h.BRD_SIMPLE)
s.pdsListing.SetTitle("No PDS Loaded")
s.pdsListing.SetOnSelect(s.loadSelectedPdsListing)
s.layout.Add(s.pdsListing)
s.pdsListing.SetOnSelect(s.selectPdsListingEntry)
s.pdsListing.SetOnChange(s.updateJsonView)
s.pdsListing.SetVimMode(viper.GetBool(data.KeyVimMode))
s.jsonContent = wd.NewJsonContent("jsoncontent", s.style)
s.layout.Add(s.jsonContent)
s.columns.AddAll(s.pdsListing, s.jsonContent)
s.layout.AddAll(s.columns, s.cli)
s.layout.SetWeight(s.columns, 4)
s.menuLayout.SetWidget(s.layout)
}
func (s *ScreenHome) initMenu() {
s.menuLayout.SetActive(true)
menu := s.menuLayout.Menu()
s.menuLayout.AddMenuItems(
menu.CreateMenuItem("File", nil, 'f',
menu.CreateMenuItem("Exit", func() bool {
s.a.Exit()
return true
}, 'x'),
),
)
}
func (s *ScreenHome) GetName() string { return "home" }
func (s *ScreenHome) HandleResize(ev *tcell.EventResize) {
s.w, s.h = ev.Size()
@@ -97,17 +106,147 @@ func (s *ScreenHome) HandleKey(ev *tcell.EventKey) bool {
if s.alert.Visible() {
return s.alert.HandleKey(ev)
}
if ev.Key() == tcell.KeyF12 {
s.toggleCli()
return true
}
return s.menuLayout.HandleKey(ev)
}
func (s *ScreenHome) HandleTime(ev *tcell.EventTime) { s.menuLayout.HandleTime(ev) }
func (s *ScreenHome) Draw() { s.a.DrawWidget(s.menuLayout) }
func (s *ScreenHome) Exit() error { return nil }
func (s *ScreenHome) Log(t string, a ...any) {
s.cli.Log(t, a...)
s.showCli()
}
func (s *ScreenHome) Draw() { s.a.DrawWidget(s.menuLayout) }
func (s *ScreenHome) initMenu() {
s.menuLayout.SetActive(true)
menu := s.menuLayout.Menu()
s.menuLayout.AddMenuItems(
menu.CreateMenuItem("File", nil, 'f',
menu.CreateMenuItem("Exit", func() bool {
s.a.Exit()
return true
}, 'x'),
),
menu.CreateMenuItem("Settings", nil, 's',
menu.CreateMenuItem("Toggle Vim Mode", func() bool {
viper.Set(data.KeyVimMode, !viper.GetBool(data.KeyVimMode))
viper.WriteConfig()
s.update()
return true
}, 'v'),
),
)
}
func (s *ScreenHome) Exit() error { return nil }
func (s *ScreenHome) update() {
s.pdsListing.SetVimMode(viper.GetBool(data.KeyVimMode))
}
func (s *ScreenHome) Log(t string, a ...any) {}
func (s *ScreenHome) initCli() {
s.cli.SetVisible(true)
s.cli.AddCommand(w.NewCliCommand("getpds", s.cliGetPds))
}
func (s *ScreenHome) loadSelectedPdsListing(idx int, nm string) bool {
func (s *ScreenHome) toggleCli() {
if s.layout.Contains(s.cli) {
s.layout.Delete(s.cli)
} else {
s.layout.Add(s.cli)
}
}
func (s *ScreenHome) hideCli() {
if s.layout.Contains(s.cli) {
s.layout.Delete(s.cli)
}
}
func (s *ScreenHome) showCli() {
if !s.layout.Contains(s.cli) {
s.layout.Add(s.cli)
}
}
func (s *ScreenHome) cliGetPds(args ...string) bool {
if len(args) == 0 {
s.Log("No id given.")
s.Log("Usage: 'getpds <atproto id>'")
return true
}
pds, err := s.r.GetPDS(args[1])
if err != nil {
s.cli.Log(err.Error())
return true
}
s.cli.Log("Retrieved: %s (%s)", pds.AtId, pds.Did)
/*
vals, err := pds.List()
if err != nil {
s.cli.Log(err.Error())
return true
}
*/
s.pdsListing.SetTitle(fmt.Sprintf("%s (%s)", pds.AtId.String(), pds.Did.String()))
// When we first get the pds, all entries are models.TypeNSID
s.pdsListingTypes = []models.EntryType{}
nsidList := pds.NSIDStringList()
for i := 0; i < len(nsidList); i++ {
s.pdsListingTypes = append(s.pdsListingTypes, models.TypeNSID)
}
s.pdsListing.SetList(nsidList)
s.hideCli()
s.activePds = pds
return true
}
func (s *ScreenHome) selectPdsListingEntry(idx int, nm string) bool {
if len(s.pdsListingTypes) < idx {
s.Log("error finding pds listing type (idx: %d >= list length: %d", idx, len(s.pdsListingTypes))
return false
}
// Update the jsonContent with the list of records
switch s.pdsListingTypes[idx] {
case models.TypeNSID:
nsid, err := syntax.ParseNSID(nm)
if err != nil {
s.Log("error parsing NSID from %s: %w", nm, err)
return false
}
recordIds := s.activePds.GetRecordIdsFor(nsid)
s.jsonContent.SetValue(recordIds)
return true
case models.TypeRecord:
}
return false
}
func (s *ScreenHome) updateJsonView(idx int, nm string) bool {
if len(s.pdsListingTypes) < idx {
s.Log("error finding pds listing type (idx: %d >= list length: %d", idx, len(s.pdsListingTypes))
return false
}
// Update the jsonContent with the list of records
switch s.pdsListingTypes[idx] {
case models.TypeNSID:
nsid, err := syntax.ParseNSID(nm)
if err != nil {
s.Log("error parsing NSID from %s: %w", nm, err)
return false
}
recordIds := s.activePds.GetRecordIdsFor(nsid)
s.jsonContent.SetValue(recordIds)
return true
case models.TypeRecord:
//s.jsonContent.SetValue(s.activePds.Records[
}
return false
}