279 lines
7.5 KiB
Go
279 lines
7.5 KiB
Go
/*
|
|
Copyright © Brian Buller <brian@bullercodeworks.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"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 {
|
|
a *App
|
|
r *data.Repo
|
|
w, h int
|
|
style tcell.Style
|
|
|
|
menuLayout *w.TopMenuLayout
|
|
menu *w.Menu
|
|
|
|
layout *w.LinearLayout
|
|
columns *w.LinearLayout
|
|
|
|
activePds *models.Pds
|
|
pdsListing *wd.SimpleListWithHelp
|
|
jsonContent *wd.JsonContent
|
|
|
|
pdsListingTypes []models.EntryType
|
|
pdsNSIDs []syntax.NSID
|
|
expandedEntries map[string]interface{}
|
|
recordIdsToNSIDs map[string]syntax.NSID
|
|
|
|
alert *w.Alert
|
|
alertLayout *w.LinearLayout
|
|
|
|
cli *w.Cli
|
|
|
|
cursor int
|
|
}
|
|
|
|
func (s *ScreenHome) Init(a *App) {
|
|
s.a, s.r = a, a.repo
|
|
s.style = a.style
|
|
|
|
s.expandedEntries = make(map[string]interface{})
|
|
s.recordIdsToNSIDs = make(map[string]syntax.NSID)
|
|
|
|
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.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.selectPdsListingEntry)
|
|
s.pdsListing.SetOnChange(s.updateJsonView)
|
|
s.pdsListing.SetVimMode(viper.GetBool(data.KeyVimMode))
|
|
|
|
s.jsonContent = wd.NewJsonContent("jsoncontent", s.style)
|
|
|
|
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) GetName() string { return "home" }
|
|
func (s *ScreenHome) HandleResize(ev *tcell.EventResize) {
|
|
s.w, s.h = ev.Size()
|
|
s.menuLayout.HandleResize(ev)
|
|
}
|
|
|
|
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) 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) update() {
|
|
s.pdsListing.SetVimMode(viper.GetBool(data.KeyVimMode))
|
|
}
|
|
|
|
func (s *ScreenHome) initCli() {
|
|
s.cli.SetVisible(true)
|
|
s.cli.AddCommand(w.NewCliCommand("getpds", s.cliGetPds))
|
|
}
|
|
|
|
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)
|
|
s.activePds = pds
|
|
s.expandedEntries = make(map[string]interface{})
|
|
s.updatePdsListing()
|
|
s.updateJsonView(s.pdsListing.SelectedIndex(), s.pdsListing.GetSelectedItem())
|
|
return true
|
|
}
|
|
|
|
func (s *ScreenHome) updatePdsListing() {
|
|
s.pdsListing.SetTitle(fmt.Sprintf("%s (%s)", s.activePds.AtId.String(), s.activePds.Did.String()))
|
|
// When we first get the pds, all entries are models.TypeNSID
|
|
s.pdsListingTypes = []models.EntryType{}
|
|
s.recordIdsToNSIDs = make(map[string]syntax.NSID)
|
|
var wrk []string
|
|
nsidList := s.activePds.NSIDStringList()
|
|
for i, v := range nsidList {
|
|
wrk = append(wrk, v)
|
|
s.pdsListingTypes = append(s.pdsListingTypes, models.TypeNSID)
|
|
if _, ok := s.expandedEntries[v]; ok {
|
|
nsid := s.activePds.NSIDs[i]
|
|
rIds := s.activePds.GetRecordIdsFor(nsid)
|
|
for j := range rIds {
|
|
wrk = append(wrk, fmt.Sprintf("• %s", rIds[j]))
|
|
s.pdsListingTypes = append(s.pdsListingTypes, models.TypeRecord)
|
|
s.recordIdsToNSIDs[rIds[j]] = nsid
|
|
}
|
|
}
|
|
}
|
|
s.pdsListing.SetList(wrk)
|
|
s.hideCli()
|
|
}
|
|
|
|
func (s *ScreenHome) selectPdsListingEntry(idx int, nm string) bool {
|
|
if !s.updateJsonView(idx, nm) {
|
|
return false
|
|
}
|
|
|
|
switch s.pdsListingTypes[idx] {
|
|
case models.TypeNSID:
|
|
// Expand the NSID
|
|
if _, ok := s.expandedEntries[nm]; ok {
|
|
delete(s.expandedEntries, nm)
|
|
} else {
|
|
s.expandedEntries[nm] = new(interface{})
|
|
}
|
|
s.updatePdsListing()
|
|
return true
|
|
|
|
case models.TypeRecord:
|
|
// If signed in and we can edit this, activate jsonContent in 'editable' mode
|
|
s.jsonContent.SetEditable(true)
|
|
s.columns.ActivateWidget(s.jsonContent)
|
|
return true
|
|
}
|
|
|
|
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:
|
|
var nsid syntax.NSID
|
|
var ok bool
|
|
nm, _ := strings.CutPrefix(nm, "• ")
|
|
if nsid, ok = s.recordIdsToNSIDs[nm]; !ok {
|
|
s.Log("error finding NSID for record %s", nm)
|
|
}
|
|
rId := fmt.Sprintf("%s/%s", nsid.String(), nm)
|
|
s.jsonContent.SetValue(s.activePds.Records[rId])
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|