8 Commits
v0.2.0 ... main

Author SHA1 Message Date
f3c63fe95b Some fixes 2026-02-01 17:43:10 -06:00
5fd2640bf8 Remove dupe code 2026-02-01 17:37:07 -06:00
b935edd96f Merge branch 'main' of ssh://git.bullercodeworks.com:2200/brian/expds 2026-02-01 17:36:29 -06:00
ce67592dbb Fix Tree Browser 2026-02-01 17:33:34 -06:00
3474f20ce1 Trying to get keys to the treebrowser 2026-01-30 16:59:26 -06:00
330a2e3ddf Rebuilding with Tree Browser 2026-01-29 20:43:32 -06:00
abf21d8e1b Working on the status bar 2026-01-29 11:19:31 -06:00
c84fe6b807 Some work 2026-01-28 16:51:50 -06:00
10 changed files with 699 additions and 51 deletions

View File

@@ -49,7 +49,7 @@ type App struct {
func NewApp() *App { func NewApp() *App {
a := &App{ a := &App{
style: tcell.StyleDefault.Foreground(tcell.ColorLime), style: tcell.StyleDefault.Foreground(tcell.ColorOrange),
} }
err := a.init() err := a.init()
cobra.CheckErr(err) cobra.CheckErr(err)

View File

@@ -31,6 +31,7 @@ import (
w "git.bullercodeworks.com/brian/tcell-widgets" w "git.bullercodeworks.com/brian/tcell-widgets"
"github.com/bluesky-social/indigo/atproto/syntax" "github.com/bluesky-social/indigo/atproto/syntax"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/skratchdot/open-golang/open"
"github.com/spf13/viper" "github.com/spf13/viper"
"golang.design/x/clipboard" "golang.design/x/clipboard"
) )
@@ -50,8 +51,10 @@ type ScreenHome struct {
columns *w.LinearLayout columns *w.LinearLayout
activePds *models.Pds activePds *models.Pds
pdsListing *w.SimpleListWithHelp pdsListing *wd.TreeBrowser
jsonContent *wd.JsonContent jsonContent *wd.JsonContent
status *wd.StatusBar
stPathBlock *wd.StatusBlock
pdsListingTypes []models.EntryType pdsListingTypes []models.EntryType
pdsNSIDs []syntax.NSID pdsNSIDs []syntax.NSID
@@ -98,14 +101,15 @@ func (s *ScreenHome) Init(a *App) {
s.columns = w.NewLinearLayout("home.layout.columns", s.style) s.columns = w.NewLinearLayout("home.layout.columns", s.style)
s.columns.SetOrientation(w.LinLayH) s.columns.SetOrientation(w.LinLayH)
s.pdsListing = w.NewSimpleListWithHelp("pdslisting", s.style) //s.pdsListing = w.NewSimpleListWithHelp("pdslisting", s.style)
s.pdsListing.SetBorder( s.pdsListing = wd.NewTreeBrowser("pdslisting", s.style)
[]rune{'─', '┬', '│', '┴', '─', '└', '│', '┌', '├', '─', '┤', '┬', '│', '┴', '┼'}, s.pdsListing.SetBorder([]rune{'─', '┬', '│', '┴', '─', '└', '│', '┌', '├', '─', '┤', '┬', '│', '┴', '┼'})
)
s.pdsListing.SetTitle(strings.Repeat(" ", 30)) s.pdsListing.SetTitle(strings.Repeat(" ", 30))
s.pdsListing.SetOnSelect(s.selectPdsListingEntry) s.pdsListing.SetOnSelect(s.selectPdsListingEntry)
s.pdsListing.SetOnChange(s.updateJsonView) s.pdsListing.SetOnChange(s.changePdsList)
s.pdsListing.SetVimMode(viper.GetBool(data.KeyVimMode)) s.pdsListing.SetVimMode(viper.GetBool(data.KeyVimMode))
s.pdsListing.SetLogger(s.Log)
s.jsonContent = wd.NewJsonContent("jsoncontent", s.style) s.jsonContent = wd.NewJsonContent("jsoncontent", s.style)
km := s.jsonContent.GetKeyMap() km := s.jsonContent.GetKeyMap()
@@ -120,22 +124,43 @@ func (s *ScreenHome) Init(a *App) {
clipboard.Write(clipboard.FmtText, []byte(s.jsonContent.GetSelectedValue())) clipboard.Write(clipboard.FmtText, []byte(s.jsonContent.GetSelectedValue()))
return true return true
}), }),
w.NewKey(w.BuildEKr('O'), func(ev *tcell.EventKey) bool {
url := fmt.Sprintf("https://%s/xrpc/com.atproto.sync.getBlob?did=did:plc:pqwuemo2ic5tqmpwrajb2phi&cid=%s", s.activePds.AtId.String(), s.jsonContent.GetSelectedValue())
open.Run(url)
return true
}),
) )
s.jsonContent.SetKeyMap(km) s.jsonContent.SetKeyMap(km)
s.jsonContent.SetBorder( s.jsonContent.SetBorder(
[]rune{'─', '┐', '│', '┘', '─', '─', ' ', '─', '├', '─', '┤', '┬', '│', '┴', '┼'}, []rune{'─', '┐', '│', '┘', '─', '─', ' ', '─', '├', '─', '┤', '┬', '│', '┴', '┼'},
) )
statusStyle := s.style.Background(tcell.ColorDarkSlateGray)
s.status = wd.NewStatusBar("home.statusbar", statusStyle)
s.status.SetPos(w.Coord{X: 0, Y: s.a.GetH() - 1})
s.status.SetLogger(s.Log)
s.stPathBlock = wd.NewStatusBlock("home.statusbar.block", statusStyle.Foreground(tcell.ColorDarkSlateGray).Background(tcell.ColorOrange))
s.stPathBlock.SetType(wd.SBTypePath)
s.stPathBlock.SetParts([]string{"No PDS Loaded"})
s.status.Add(s.stPathBlock)
s.columns.AddAll(s.pdsListing, s.jsonContent) s.columns.AddAll(s.pdsListing, s.jsonContent)
s.layout.AddAll(s.columns) s.layout.AddAll(s.columns)
s.layout.SetLogger(s.Log)
s.layout.SetWeight(s.columns, 4) s.layout.SetWeight(s.columns, 4)
s.menuLayout.SetWidget(s.layout) s.menuLayout.SetWidget(s.layout)
s.layout.SetLogger(s.Log)
s.columns.SetLogger(s.Log)
} }
func (s *ScreenHome) GetName() string { return "home" } func (s *ScreenHome) GetName() string { return "home" }
func (s *ScreenHome) HandleResize(ev *tcell.EventResize) { func (s *ScreenHome) HandleResize(ev *tcell.EventResize) {
s.w, s.h = ev.Size() s.w, s.h = ev.Size()
s.menuLayout.HandleResize(ev) s.menuLayout.HandleResize(w.Coord{X: s.w, Y: s.h - 1}.ResizeEvent())
s.status.SetPos(w.Coord{X: 0, Y: s.h - 1})
s.status.HandleResize(w.Coord{X: s.w, Y: 1}.ResizeEvent())
} }
func (s *ScreenHome) HandleKey(ev *tcell.EventKey) bool { func (s *ScreenHome) HandleKey(ev *tcell.EventKey) bool {
@@ -158,12 +183,12 @@ func (s *ScreenHome) HandleKey(ev *tcell.EventKey) bool {
} }
return s.openPdsEntry.HandleKey(ev) return s.openPdsEntry.HandleKey(ev)
} }
return s.menuLayout.HandleKey(ev) return s.menuLayout.HandleKey(ev)
} }
func (s *ScreenHome) HandleTime(ev *tcell.EventTime) { func (s *ScreenHome) HandleTime(ev *tcell.EventTime) {
s.menuLayout.HandleTime(ev) s.menuLayout.HandleTime(ev)
s.loading.HandleTime(ev) s.loading.HandleTime(ev)
s.status.HandleTime(ev)
} }
func (s *ScreenHome) Draw() { func (s *ScreenHome) Draw() {
if s.doOpen { if s.doOpen {
@@ -179,6 +204,7 @@ func (s *ScreenHome) Draw() {
if s.isLoading { if s.isLoading {
s.a.DrawWidget(s.loading) s.a.DrawWidget(s.loading)
} }
s.a.DrawWidget(s.status)
} }
func (s *ScreenHome) Exit() error { return nil } func (s *ScreenHome) Exit() error { return nil }
func (s *ScreenHome) Log(t string, a ...any) { func (s *ScreenHome) Log(t string, a ...any) {
@@ -231,6 +257,7 @@ func (s *ScreenHome) update() {
s.menuLayout.SetWidget(s.layout) s.menuLayout.SetWidget(s.layout)
} }
s.pdsListing.SetVimMode(viper.GetBool(data.KeyVimMode)) s.pdsListing.SetVimMode(viper.GetBool(data.KeyVimMode))
s.jsonContent.SetVimMode(viper.GetBool(data.KeyVimMode))
vimMI := s.menuLayout.FindItem("settings.vimmode") vimMI := s.menuLayout.FindItem("settings.vimmode")
var vimText = "Enable Vim Mode" var vimText = "Enable Vim Mode"
if viper.GetBool(data.KeyVimMode) { if viper.GetBool(data.KeyVimMode) {
@@ -298,7 +325,13 @@ func (s *ScreenHome) cliGetPds(args ...string) bool {
s.activePds = pds s.activePds = pds
s.expandedEntries = make(map[string]interface{}) s.expandedEntries = make(map[string]interface{})
s.updatePdsListing() s.updatePdsListing()
s.updateJsonView(s.pdsListing.SelectedIndex(), s.pdsListing.GetSelectedItem()) n, err := s.pdsListing.GetActiveNode()
if err == nil && n != nil {
s.changePdsList(n)
}
s.layout.ActivateWidget(s.columns)
s.columns.ActivateWidget(s.pdsListing)
s.isLoading = false s.isLoading = false
}() }()
return true return true
@@ -306,30 +339,57 @@ func (s *ScreenHome) cliGetPds(args ...string) bool {
func (s *ScreenHome) updatePdsListing() { func (s *ScreenHome) updatePdsListing() {
s.pdsListing.SetTitle(fmt.Sprintf("─ %s (%s)", s.activePds.AtId.String(), s.activePds.Did.String())) 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.pdsListing.Clear()
s.pdsListingTypes = []models.EntryType{}
s.recordIdsToNSIDs = make(map[string]syntax.NSID)
var wrk []string
nsidList := s.activePds.NSIDStringList() nsidList := s.activePds.NSIDStringList()
for i, v := range nsidList { for i, v := range nsidList {
wrk = append(wrk, v) t := wd.NewTreeNode(v, v)
s.pdsListingTypes = append(s.pdsListingTypes, models.TypeNSID)
if _, ok := s.expandedEntries[v]; ok {
nsid := s.activePds.NSIDs[i] nsid := s.activePds.NSIDs[i]
rIds := s.activePds.GetRecordIdsFor(nsid) rIds := s.activePds.GetRecordIdsFor(nsid)
for j := range rIds { for j := range rIds {
wrk = append(wrk, fmt.Sprintf("%s", rIds[j])) c := wd.NewTreeNode(fmt.Sprintf("%s (%s)", "record", rIds[j]), rIds[j])
s.pdsListingTypes = append(s.pdsListingTypes, models.TypeRecord) t.AddChild(c)
s.recordIdsToNSIDs[rIds[j]] = nsid
} }
s.pdsListing.Add(t)
} }
} s.layout.ActivateWidget(s.columns)
s.pdsListing.SetList(wrk) s.columns.ActivateWidget(s.pdsListing)
s.pdsListing.SetActive(true)
s.pdsListing.SetFocusable(true)
s.hideCli() s.hideCli()
} }
func (s *ScreenHome) selectPdsListingEntry(idx int, nm string) bool { func (s *ScreenHome) selectPdsListingEntry(tn *wd.TreeNode) bool {
if !s.updateJsonView(idx, nm) { if !s.updateJsonView(tn) {
return false
}
if tn.HasChildren() {
tn.ToggleExpand()
s.pdsListing.UpdateList()
} else {
s.columns.ActivateWidget(s.jsonContent)
}
return true
}
func (s *ScreenHome) changePdsList(tn *wd.TreeNode) bool {
upd := s.updateJsonView(tn)
upd = s.updateStatusPathBlock(tn)
return upd
}
func (s *ScreenHome) updateStatusPathBlock(tn *wd.TreeNode) bool {
s.stPathBlock.SetParts(tn.GetLabelPath())
return true
}
func (s *ScreenHome) updateJsonView(tn *wd.TreeNode) bool {
// TODO: Update JSON View
return true
}
/*
func (s *ScreenHome) o_selectPdsListingEntry(idx int, nm string) bool {
if !s.o_updateJsonView(idx, nm) {
return false return false
} }
@@ -352,8 +412,7 @@ func (s *ScreenHome) selectPdsListingEntry(idx int, nm string) bool {
return false return false
} }
func (s *ScreenHome) o_updateJsonView(idx int, nm string) bool {
func (s *ScreenHome) updateJsonView(idx int, nm string) bool {
if len(s.pdsListingTypes) < idx { if len(s.pdsListingTypes) < idx {
s.Log("error finding pds listing type (idx: %d >= list length: %d", idx, len(s.pdsListingTypes)) s.Log("error finding pds listing type (idx: %d >= list length: %d", idx, len(s.pdsListingTypes))
return false return false
@@ -384,3 +443,4 @@ func (s *ScreenHome) updateJsonView(idx int, nm string) bool {
return false return false
} }
*/

View File

@@ -56,6 +56,7 @@ type Pds struct {
NSIDs []syntax.NSID NSIDs []syntax.NSID
RecordIds []string RecordIds []string
nsidToRecordIds map[syntax.NSID][]string nsidToRecordIds map[syntax.NSID][]string
recordIdsToNSID map[string]syntax.NSID
Records map[string]map[string]any Records map[string]map[string]any
RefreshTime time.Time RefreshTime time.Time
@@ -96,6 +97,7 @@ func NewPdsFromDid(id string) (*Pds, error) {
Did: ident.DID, Did: ident.DID,
localPath: carPath, localPath: carPath,
nsidToRecordIds: make(map[syntax.NSID][]string), nsidToRecordIds: make(map[syntax.NSID][]string),
recordIdsToNSID: make(map[string]syntax.NSID),
Records: make(map[string]map[string]any), Records: make(map[string]map[string]any),
RefreshTime: time.Now(), RefreshTime: time.Now(),
} }
@@ -142,6 +144,7 @@ func (p *Pds) unpack() error {
p.NSIDs = append(p.NSIDs, col) p.NSIDs = append(p.NSIDs, col)
} }
p.nsidToRecordIds[col] = append(p.nsidToRecordIds[col], sRKey) p.nsidToRecordIds[col] = append(p.nsidToRecordIds[col], sRKey)
p.recordIdsToNSID[sRKey] = col
return nil return nil
}) })
//slices.Sort(p.NSIDs) //slices.Sort(p.NSIDs)
@@ -180,3 +183,4 @@ func (p *Pds) List() (map[string]string, error) {
} }
func (p *Pds) GetRecordIdsFor(n syntax.NSID) []string { return p.nsidToRecordIds[n] } func (p *Pds) GetRecordIdsFor(n syntax.NSID) []string { return p.nsidToRecordIds[n] }
func (p *Pds) GetNSIDForRecordId(rId string) syntax.NSID { return p.recordIdsToNSID[rId] }

1
go.mod
View File

@@ -8,6 +8,7 @@ require (
github.com/gdamore/tcell v1.4.1 github.com/gdamore/tcell v1.4.1
github.com/ipfs/go-cid v0.4.1 github.com/ipfs/go-cid v0.4.1
github.com/muesli/go-app-paths v0.2.2 github.com/muesli/go-app-paths v0.2.2
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/spf13/cobra v1.10.2 github.com/spf13/cobra v1.10.2
github.com/spf13/viper v1.21.0 github.com/spf13/viper v1.21.0
golang.design/x/clipboard v0.7.1 golang.design/x/clipboard v0.7.1

2
go.sum
View File

@@ -165,6 +165,8 @@ github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/sagikazarmark/locafero v0.12.0 h1:/NQhBAkUb4+fH1jivKHWusDYFjMOOKU88eegjfxfHb4= github.com/sagikazarmark/locafero v0.12.0 h1:/NQhBAkUb4+fH1jivKHWusDYFjMOOKU88eegjfxfHb4=
github.com/sagikazarmark/locafero v0.12.0/go.mod h1:sZh36u/YSZ918v0Io+U9ogLYQJ9tLLBmM4eneO6WwsI= github.com/sagikazarmark/locafero v0.12.0/go.mod h1:sZh36u/YSZ918v0Io+U9ogLYQJ9tLLBmM4eneO6WwsI=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw= github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=

View File

@@ -31,3 +31,10 @@ var Sep = string(os.PathSeparator)
func Path(parts ...string) string { func Path(parts ...string) string {
return strings.Join(parts, Sep) return strings.Join(parts, Sep)
} }
func MaxI(a, b int) int {
if a < b {
return b
}
return a
}

View File

@@ -71,9 +71,7 @@ func (w *JsonContent) Init(id string, style tcell.Style) {
w.keyMap = wd.NewKeyMap( w.keyMap = wd.NewKeyMap(
wd.NewKey(wd.BuildEK(tcell.KeyUp), func(_ *tcell.EventKey) bool { return w.MoveUp() }), wd.NewKey(wd.BuildEK(tcell.KeyUp), func(_ *tcell.EventKey) bool { return w.MoveUp() }),
wd.NewKey(wd.BuildEK(tcell.KeyDown), func(_ *tcell.EventKey) bool { return w.MoveDown() }), wd.NewKey(wd.BuildEK(tcell.KeyDown), func(_ *tcell.EventKey) bool { return w.MoveDown() }),
wd.NewKey(wd.BuildEK(tcell.KeyEnter), func(ev *tcell.EventKey) bool { wd.NewKey(wd.BuildEK(tcell.KeyEnter), func(ev *tcell.EventKey) bool { return false }),
return false
}),
wd.NewKey(wd.BuildEK(tcell.KeyPgDn), func(_ *tcell.EventKey) bool { return w.PageDn() }), wd.NewKey(wd.BuildEK(tcell.KeyPgDn), func(_ *tcell.EventKey) bool { return w.PageDn() }),
wd.NewKey(wd.BuildEK(tcell.KeyPgUp), func(_ *tcell.EventKey) bool { return w.PageUp() }), wd.NewKey(wd.BuildEK(tcell.KeyPgUp), func(_ *tcell.EventKey) bool { return w.PageUp() }),
wd.NewKey(wd.BuildEKr('j'), func(ev *tcell.EventKey) bool { wd.NewKey(wd.BuildEKr('j'), func(ev *tcell.EventKey) bool {

View File

@@ -22,7 +22,10 @@ THE SOFTWARE.
package widgets package widgets
import ( import (
"strings"
t "git.bullercodeworks.com/brian/tcell-widgets" t "git.bullercodeworks.com/brian/tcell-widgets"
th "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
) )
@@ -34,6 +37,9 @@ type StatusBar struct {
visible bool visible bool
keyMap *t.KeyMap keyMap *t.KeyMap
blocks []*StatusBlock
blockFlags map[*StatusBlock]t.LayoutFlag
logger func(string, ...any) logger func(string, ...any)
} }
@@ -47,26 +53,33 @@ func (w *StatusBar) Init(id string, s tcell.Style) {
w.id = id w.id = id
w.style = s w.style = s
w.visible = true w.visible = true
w.blockFlags = make(map[*StatusBlock]t.LayoutFlag)
} }
func (w *StatusBar) Id() string { return w.id } func (w *StatusBar) Id() string { return w.id }
func (w *StatusBar) HandleResize(ev *tcell.EventResize) { func (w *StatusBar) HandleResize(ev *tcell.EventResize) {
w.w, w.h = ev.Size() w.w, w.h = ev.Size()
w.updatePosAndSize()
} }
func (w *StatusBar) GetKeyMap() *t.KeyMap { return w.keyMap } func (w *StatusBar) GetKeyMap() *t.KeyMap { return w.keyMap }
func (w *StatusBar) SetKeyMap(km *t.KeyMap) { w.keyMap = km } func (w *StatusBar) SetKeyMap(km *t.KeyMap) { w.keyMap = km }
func (w *StatusBar) HandleKey(ev *tcell.EventKey) bool { func (w *StatusBar) HandleKey(ev *tcell.EventKey) bool { return false }
return false
}
func (w *StatusBar) HandleTime(ev *tcell.EventTime) {} func (w *StatusBar) HandleTime(ev *tcell.EventTime) {
for i := range w.blocks {
w.blocks[i].HandleTime(ev)
}
}
func (w *StatusBar) Draw(screen tcell.Screen) { func (w *StatusBar) Draw(screen tcell.Screen) {
if !w.visible { if !w.visible {
return return
} }
th.DrawText(w.x, w.y, strings.Repeat(" ", w.w), w.style, screen)
for i := range w.blocks {
w.blocks[i].Draw(screen)
}
} }
func (w *StatusBar) SetStyle(s tcell.Style) { w.style = s } func (w *StatusBar) SetStyle(s tcell.Style) { w.style = s }
@@ -76,21 +89,85 @@ func (w *StatusBar) Visible() bool { return w.visible }
func (w *StatusBar) SetVisible(a bool) { w.visible = a } func (w *StatusBar) SetVisible(a bool) { w.visible = a }
func (w *StatusBar) Focusable() bool { return false } func (w *StatusBar) Focusable() bool { return false }
func (w *StatusBar) SetFocusable(b bool) {} func (w *StatusBar) SetFocusable(b bool) {}
func (w *StatusBar) SetX(x int) { w.x = x } func (w *StatusBar) SetX(x int) { w.SetPos(t.Coord{X: x, Y: w.y}) }
func (w *StatusBar) SetY(y int) { w.y = y } func (w *StatusBar) SetY(y int) { w.SetPos(t.Coord{X: w.x, Y: y}) }
func (w *StatusBar) GetX() int { return w.x } func (w *StatusBar) GetX() int { return w.x }
func (w *StatusBar) GetY() int { return w.y } func (w *StatusBar) GetY() int { return w.y }
func (w *StatusBar) GetPos() t.Coord { return t.Coord{X: w.x, Y: w.y} } func (w *StatusBar) GetPos() t.Coord { return t.Coord{X: w.x, Y: w.y} }
func (w *StatusBar) SetPos(c t.Coord) { w.x, w.y = c.X, c.Y } func (w *StatusBar) SetPos(c t.Coord) {
w.x, w.y = c.X, c.Y
w.updatePosAndSize()
}
func (w *StatusBar) GetW() int { return w.w } func (w *StatusBar) GetW() int { return w.w }
func (w *StatusBar) GetH() int { return w.h } func (w *StatusBar) GetH() int { return w.h }
func (w *StatusBar) SetW(wd int) { w.w = wd } func (w *StatusBar) SetW(wd int) { w.SetSize(t.Coord{X: wd, Y: w.h}) }
func (w *StatusBar) SetH(h int) { w.h = h } func (w *StatusBar) SetH(h int) { w.SetSize(t.Coord{X: w.w, Y: h}) }
func (w *StatusBar) SetSize(c t.Coord) { w.w, w.h = c.X, c.Y } func (w *StatusBar) SetSize(c t.Coord) {
w.w, w.h = c.X, c.Y
w.updatePosAndSize()
}
func (w *StatusBar) WantW() int { return w.w } func (w *StatusBar) WantW() int { return w.w }
func (w *StatusBar) WantH() int { return w.h } func (w *StatusBar) WantH() int { return w.h }
func (w *StatusBar) MinW() int { return w.w } func (w *StatusBar) MinW() int { return w.w }
func (w *StatusBar) MinH() int { return 1 } func (w *StatusBar) MinH() int { return 1 }
func (w *StatusBar) SetLogger(l func(string, ...any)) { w.logger = l } func (w *StatusBar) SetLogger(l func(string, ...any)) { w.logger = l }
func (w *StatusBar) Log(txt string, args ...any) { w.logger(txt, args...) } func (w *StatusBar) Log(txt string, args ...any) { w.logger(txt, args...) }
func (w *StatusBar) Add(b *StatusBlock) { w.blocks = append(w.blocks, b) }
func (w *StatusBar) SetFlag(b *StatusBlock, f t.LayoutFlag) {
if _, ok := w.blockFlags[b]; ok {
w.blockFlags[b].Add(f)
} else {
w.blockFlags[b] = f
}
}
func (w *StatusBar) RemoveFlag(b *StatusBlock, f t.LayoutFlag) { w.blockFlags[b].Remove(f) }
func (w *StatusBar) ClearFlags(b *StatusBlock) { delete(w.blockFlags, b) }
func (w *StatusBar) updatePosAndSize() {
// First, all blocks that are Left Aligned (or no alignment)
x := w.x
for i := range w.blocks {
f, ok := w.blockFlags[w.blocks[i]]
if !ok || (f&t.LFAlignLeft != 0 || f&t.LFAlignH == 0) {
w.blocks[i].SetPos(t.Coord{X: x, Y: w.y})
x += w.blocks[i].Width()
}
}
// Center Aligned
// First, get the width of all center blocks
var cW int
for i := range w.blocks {
f, ok := w.blockFlags[w.blocks[i]]
if ok && (f&t.LFAlignCenter != 0) {
cW += w.blocks[i].Width()
}
}
x = w.x + (w.w / 2) - (cW / 2)
for i := range w.blocks {
f, ok := w.blockFlags[w.blocks[i]]
if ok && (f&t.LFAlignCenter != 0) {
w.blocks[i].SetPos(t.Coord{X: x, Y: w.y})
x += w.blocks[i].Width()
}
}
// Right Aligned
// First, get the width of all Right Aligned blocks
cW = 0
for i := range w.blocks {
f, ok := w.blockFlags[w.blocks[i]]
if ok && (f&t.LFAlignRight != 0) {
cW += w.blocks[i].Width()
}
}
x = w.x + w.w - cW
for i := range w.blocks {
f, ok := w.blockFlags[w.blocks[i]]
if ok && (f&t.LFAlignRight != 0) {
w.blocks[i].SetPos(t.Coord{X: x, Y: w.y})
x += w.blocks[i].Width()
}
}
}

128
widgets/status_bar_block.go Normal file
View File

@@ -0,0 +1,128 @@
package widgets
import (
"fmt"
"strings"
"time"
t "git.bullercodeworks.com/brian/tcell-widgets"
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/gdamore/tcell"
)
/*
StatusBlock is not a widget type, they're meant to be used within a StatusBar
*/
type StatusBlock struct {
id string
style tcell.Style
x, y int
tp int
parts []string
text string
sepR, sepL string
endR, endL string
dir int
sep, end string
active bool
}
const (
SBTypeText = iota
SBTypePath
SBTypeTime
SBDirL = iota
SBDirR
)
func NewStatusBlock(id string, s tcell.Style) *StatusBlock {
b := StatusBlock{
id: id, style: s,
sepR: "  ",
endR: "",
sepL: "  ",
endL: "",
}
b.SetDirection(SBDirR)
return &b
}
func (b *StatusBlock) SetDirection(d int) {
switch d {
case SBDirL, SBDirR:
b.dir = d
default:
b.dir = SBDirL
}
b.updateSeparators()
}
func (b *StatusBlock) updateSeparators() {
switch b.dir {
case SBDirL:
b.sep = b.sepL
b.end = b.endL
default: // Right
b.sep = b.sepR
b.end = b.endR
}
}
func (b *StatusBlock) SetPos(p t.Coord) { b.x, b.y = p.X, p.Y }
func (b *StatusBlock) Width() int {
switch b.tp {
case SBTypePath:
return len(fmt.Sprintf("%s%s", strings.Join(b.parts, b.sep), b.end))
default:
return len(fmt.Sprintf("%s%s%s", b.end, b.text, b.end))
}
}
func (b *StatusBlock) SetType(tp int) {
switch tp {
case SBTypePath, SBTypeTime:
b.tp = tp
default:
b.tp = SBTypeText
}
}
func (b *StatusBlock) SetParts(pts []string) { b.parts = pts }
func (b *StatusBlock) AddPart(p string) { b.parts = append(b.parts, p) }
func (b *StatusBlock) SetText(t string) { b.text = t }
func (b *StatusBlock) Draw(screen tcell.Screen) {
switch b.tp {
case SBTypeText, SBTypeTime:
b.DrawText(screen)
case SBTypePath:
b.DrawPath(screen)
}
}
func (b *StatusBlock) DrawText(screen tcell.Screen) {
wh.DrawText(b.x, b.y, b.end, b.style.Reverse(true), screen)
wh.DrawText(b.x+1, b.y, b.text, b.style, screen)
wh.DrawText(b.x+len(b.text), b.y, b.end, b.style.Reverse(true), screen)
}
func (b *StatusBlock) DrawPath(screen tcell.Screen) {
if len(b.parts) == 0 {
return
}
x := b.x
wh.DrawText(x, b.y, b.end, b.style, screen)
x++
pts := fmt.Sprintf(" %s ", strings.Join(b.parts, b.sep))
wh.DrawText(x, b.y, pts, b.style, screen)
x += len(pts) - len(b.parts)
wh.DrawText(x, b.y, b.end, b.style.Reverse(true), screen)
}
func (b *StatusBlock) HandleTime(ev *tcell.EventTime) {
switch b.tp {
case SBTypeTime:
b.text = fmt.Sprintf(" %s ", time.Now().Format(time.TimeOnly))
}
}

371
widgets/tree_browser.go Normal file
View File

@@ -0,0 +1,371 @@
package widgets
import (
"errors"
"fmt"
"strings"
h "git.bullercodeworks.com/brian/expds/helpers"
t "git.bullercodeworks.com/brian/tcell-widgets"
th "git.bullercodeworks.com/brian/tcell-widgets/helpers"
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/gdamore/tcell"
)
type TreeBrowser struct {
id string
title string
style tcell.Style
active bool
visible bool
focusable bool
x, y int
w, h int
border []rune
list []string
listNodes []*TreeNode
cursor int
cursorWrap bool
nodes []*TreeNode
onChange func(*TreeNode) bool
onSelect func(*TreeNode) bool
keyMap *t.KeyMap
vimMode bool
logger func(string, ...any)
}
var _ t.Widget = (*TreeBrowser)(nil)
func NewTreeBrowser(id string, s tcell.Style) *TreeBrowser {
ret := &TreeBrowser{id: id, style: s}
ret.Init(id, s)
return ret
}
func (w *TreeBrowser) Init(id string, style tcell.Style) {
w.visible = true
w.focusable = true
w.keyMap = t.NewKeyMap(
t.NewKey(t.BuildEK(tcell.KeyUp), func(_ *tcell.EventKey) bool { return w.MoveUp() }),
t.NewKey(t.BuildEK(tcell.KeyDown), func(_ *tcell.EventKey) bool { return w.MoveDown() }),
t.NewKey(t.BuildEK(tcell.KeyEnter), func(ev *tcell.EventKey) bool {
if w.onSelect != nil {
n, err := w.GetActiveNode()
if err != nil || n == nil {
return false
}
w.onSelect(n)
return true
}
return false
}),
t.NewKey(t.BuildEK(tcell.KeyPgDn), func(_ *tcell.EventKey) bool { return w.PageDn() }),
t.NewKey(t.BuildEK(tcell.KeyPgUp), func(_ *tcell.EventKey) bool { return w.PageUp() }),
t.NewKey(t.BuildEKr('j'), func(ev *tcell.EventKey) bool {
if !w.vimMode {
return false
}
return w.MoveDown()
}),
t.NewKey(t.BuildEKr('k'), func(ev *tcell.EventKey) bool {
if !w.vimMode {
return false
}
return w.MoveUp()
}),
t.NewKey(t.BuildEKr('b'), func(ev *tcell.EventKey) bool {
if !w.vimMode {
return false
}
if ev.Modifiers()&tcell.ModCtrl != 0 {
return w.PageUp()
}
return false
}),
t.NewKey(t.BuildEKr('f'), func(ev *tcell.EventKey) bool {
if !w.vimMode {
return false
}
if ev.Modifiers()&tcell.ModCtrl != 0 {
return w.PageDn()
}
return false
}),
)
}
func (w *TreeBrowser) Id() string { return w.id }
func (w *TreeBrowser) HandleResize(ev *tcell.EventResize) {
w.w, w.h = ev.Size()
}
func (w *TreeBrowser) GetKeyMap() *t.KeyMap { return w.keyMap }
func (w *TreeBrowser) SetKeyMap(km *t.KeyMap) { w.keyMap = km }
func (w *TreeBrowser) HandleKey(ev *tcell.EventKey) bool {
if !w.active || !w.focusable {
return false
}
return w.keyMap.Handle(ev)
}
func (w *TreeBrowser) HandleTime(ev *tcell.EventTime) {}
func (w *TreeBrowser) Draw(screen tcell.Screen) {
if !w.visible {
return
}
dS := w.style
if !w.active {
dS = dS.Dim(true)
}
x, y := w.x, w.y
brdSz := 0
if len(w.border) > 0 {
brdSz = 2
if len(w.title) > 0 {
th.TitledBorderFilled(x, y, x+w.w, y+w.h, w.title, w.border, dS, screen)
} else {
th.BorderFilled(x, y, x+w.w, y+w.h, w.border, dS, screen)
}
}
x, y = x+1, y+1
h := w.h - brdSz
for i := range w.list {
th.DrawText(x, y, w.list[i], w.style.Reverse(i == w.cursor), screen)
y++
if y > x+h {
break
}
}
}
func (w *TreeBrowser) SetStyle(s tcell.Style) { w.style = s }
func (w *TreeBrowser) Active() bool { return w.active }
func (w *TreeBrowser) SetActive(a bool) { w.active = a }
func (w *TreeBrowser) Visible() bool { return w.visible }
func (w *TreeBrowser) SetVisible(a bool) { w.visible = a }
func (w *TreeBrowser) Focusable() bool { return w.focusable }
func (w *TreeBrowser) SetFocusable(b bool) { w.focusable = b }
func (w *TreeBrowser) SetX(x int) { w.SetPos(t.Coord{X: x, Y: w.y}) }
func (w *TreeBrowser) SetY(y int) { w.SetPos(t.Coord{X: w.x, Y: y}) }
func (w *TreeBrowser) GetX() int { return w.x }
func (w *TreeBrowser) GetY() int { return w.y }
func (w *TreeBrowser) GetPos() t.Coord { return t.Coord{X: w.x, Y: w.y} }
func (w *TreeBrowser) SetPos(c t.Coord) { w.x, w.y = c.X, c.Y }
func (w *TreeBrowser) GetW() int { return w.w }
func (w *TreeBrowser) GetH() int { return w.h }
func (w *TreeBrowser) SetW(wd int) { w.SetSize(t.Coord{X: wd, Y: w.h}) }
func (w *TreeBrowser) SetH(h int) { w.SetSize(t.Coord{X: w.w, Y: h}) }
func (w *TreeBrowser) SetSize(c t.Coord) { w.w, w.h = c.X, c.Y }
func (w *TreeBrowser) WantW() int {
var want int
for i := range w.list {
want = h.MaxI(want, len(w.list[i]))
}
return w.w
}
func (w *TreeBrowser) WantH() int {
want := len(w.list)
if len(w.border) > 0 {
return want + 2
}
return want
}
func (w *TreeBrowser) MinW() int { return w.w }
func (w *TreeBrowser) MinH() int { return 5 }
func (w *TreeBrowser) SetLogger(l func(string, ...any)) { w.logger = l }
func (w *TreeBrowser) Log(txt string, args ...any) { w.logger(txt, args...) }
func (w *TreeBrowser) SetBorder(brd []rune) {
if len(brd) == 0 {
w.border = wh.BRD_SIMPLE
} else {
w.border = wh.ValidateBorder(brd)
}
}
func (w *TreeBrowser) ClearBorder() { w.border = []rune{} }
func (w *TreeBrowser) SetOnChange(c func(*TreeNode) bool) { w.onChange = c }
func (w *TreeBrowser) SetOnSelect(s func(*TreeNode) bool) { w.onSelect = s }
func (w *TreeBrowser) SetVimMode(b bool) { w.vimMode = b }
func (w *TreeBrowser) GetActiveNode() (*TreeNode, error) {
if len(w.listNodes) < 0 {
return nil, errors.New("no nodes")
}
if w.cursor < 0 {
return w.listNodes[0], nil
}
if w.cursor >= len(w.listNodes) {
return w.listNodes[len(w.listNodes)-1], nil
}
return w.listNodes[w.cursor], nil
}
func (w *TreeBrowser) SetCursorWrap(b bool) { w.cursorWrap = b }
func (w *TreeBrowser) MoveUp() bool {
if w.cursor > 0 {
w.cursor--
if w.onChange != nil {
n, err := w.GetActiveNode()
if err == nil && n != nil {
w.onChange(n)
}
}
return true
} else if w.cursorWrap {
w.cursor = len(w.list) - 1
if w.onChange != nil {
n, err := w.GetActiveNode()
if err == nil && n != nil {
w.onChange(n)
}
}
return true
}
return false
}
func (w *TreeBrowser) MoveDown() bool {
if w.cursor <= len(w.list) {
w.cursor++
if w.onChange != nil {
n, err := w.GetActiveNode()
if err == nil && n != nil {
w.onChange(n)
}
}
return true
} else if w.cursorWrap {
w.cursor = 0
if w.WantH() > w.cursor && w.onChange != nil {
n, err := w.GetActiveNode()
if err == nil && n != nil {
w.onChange(n)
}
}
return true
}
return false
}
func (w *TreeBrowser) PageUp() bool {
w.cursor -= w.h
if len(w.border) > 0 {
w.cursor += 2
}
if w.cursor < 0 {
w.cursor = 0
}
return true
}
func (w *TreeBrowser) PageDn() bool {
w.cursor += w.h
if len(w.border) > 0 {
w.cursor -= 1
}
if w.cursor > len(w.list)-1 {
w.cursor = len(w.list) - 1
}
return true
}
func (w *TreeBrowser) Title() string { return w.title }
func (w *TreeBrowser) SetTitle(ttl string) { w.title = ttl }
func (w *TreeBrowser) SetTree(l []*TreeNode) {
w.nodes = l
w.UpdateList()
}
func (w *TreeBrowser) Clear() {
w.nodes = []*TreeNode{}
w.UpdateList()
}
func (w *TreeBrowser) Add(n *TreeNode) {
w.nodes = append(w.nodes, n)
w.UpdateList()
}
func (w *TreeBrowser) UpdateList() {
w.list = []string{}
w.listNodes = []*TreeNode{}
for i := range w.nodes {
w.list = append(w.list, w.nodes[i].getList()...)
w.listNodes = append(w.listNodes, w.nodes[i].getVisibleNodeList()...)
}
if w.cursor >= len(w.list) {
w.cursor = len(w.list) - 1
}
if w.cursor <= 0 {
w.cursor = 0
}
}
/*
* Tree Node
*/
type TreeNode struct {
label string
value string
expanded bool
parent *TreeNode
children []*TreeNode
}
func NewTreeNode(l, v string) *TreeNode {
return &TreeNode{
label: l,
value: v,
}
}
func (tn *TreeNode) Depth() int {
if tn.parent == nil {
return 0
}
return tn.parent.Depth() + 1
}
func (tn *TreeNode) Label() string { return tn.label }
func (tn *TreeNode) Value() string { return tn.value }
func (tn *TreeNode) GetLabelPath() []string {
var path []string
if tn.parent != nil {
path = tn.parent.GetLabelPath()
}
return append(path, tn.Label())
}
func (tn *TreeNode) getList() []string {
pre := strings.Repeat("-", tn.Depth())
ret := []string{fmt.Sprintf("%s%s", pre, tn.label)}
if tn.expanded {
for i := range tn.children {
ret = append(ret, tn.children[i].getList()...)
}
}
return ret
}
func (tn *TreeNode) getVisibleNodeList() []*TreeNode {
ret := []*TreeNode{tn}
if tn.expanded {
for i := range tn.children {
ret = append(ret, tn.children[i].getVisibleNodeList()...)
}
}
return ret
}
func (tn *TreeNode) ToggleExpand() { tn.expanded = !tn.expanded }
func (tn *TreeNode) AddChild(t *TreeNode, rest ...*TreeNode) {
t.parent = tn
tn.children = append(tn.children, t)
for i := range rest {
rest[i].parent = tn
tn.children = append(tn.children, rest[i])
}
}
func (tn *TreeNode) HasChildren() bool { return len(tn.children) > 0 }
func (tn *TreeNode) GetPath() []string {
var path []string
if tn.parent != nil {
path = tn.parent.GetPath()
}
return append(path, tn.value)
}