Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f3c63fe95b | |||
| 5fd2640bf8 | |||
| b935edd96f | |||
| ce67592dbb | |||
| 3474f20ce1 | |||
| 330a2e3ddf | |||
| abf21d8e1b | |||
| c84fe6b807 | |||
| 3f347d8342 |
@@ -49,7 +49,7 @@ type App struct {
|
||||
|
||||
func NewApp() *App {
|
||||
a := &App{
|
||||
style: tcell.StyleDefault.Foreground(tcell.ColorLime),
|
||||
style: tcell.StyleDefault.Foreground(tcell.ColorOrange),
|
||||
}
|
||||
err := a.init()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
@@ -31,7 +31,9 @@ import (
|
||||
w "git.bullercodeworks.com/brian/tcell-widgets"
|
||||
"github.com/bluesky-social/indigo/atproto/syntax"
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/skratchdot/open-golang/open"
|
||||
"github.com/spf13/viper"
|
||||
"golang.design/x/clipboard"
|
||||
)
|
||||
|
||||
type ScreenHome struct {
|
||||
@@ -49,8 +51,10 @@ type ScreenHome struct {
|
||||
columns *w.LinearLayout
|
||||
|
||||
activePds *models.Pds
|
||||
pdsListing *w.SimpleListWithHelp
|
||||
pdsListing *wd.TreeBrowser
|
||||
jsonContent *wd.JsonContent
|
||||
status *wd.StatusBar
|
||||
stPathBlock *wd.StatusBlock
|
||||
|
||||
pdsListingTypes []models.EntryType
|
||||
pdsNSIDs []syntax.NSID
|
||||
@@ -97,32 +101,66 @@ func (s *ScreenHome) Init(a *App) {
|
||||
s.columns = w.NewLinearLayout("home.layout.columns", s.style)
|
||||
s.columns.SetOrientation(w.LinLayH)
|
||||
|
||||
s.pdsListing = w.NewSimpleListWithHelp("pdslisting", s.style)
|
||||
s.pdsListing.SetBorder(
|
||||
[]rune{'─', '┬', '│', '┴', '─', '└', '│', '┌', '├', '─', '┤', '┬', '│', '┴', '┼'},
|
||||
)
|
||||
//s.pdsListing = w.NewSimpleListWithHelp("pdslisting", s.style)
|
||||
s.pdsListing = wd.NewTreeBrowser("pdslisting", s.style)
|
||||
s.pdsListing.SetBorder([]rune{'─', '┬', '│', '┴', '─', '└', '│', '┌', '├', '─', '┤', '┬', '│', '┴', '┼'})
|
||||
|
||||
s.pdsListing.SetTitle(strings.Repeat(" ", 30))
|
||||
s.pdsListing.SetOnSelect(s.selectPdsListingEntry)
|
||||
s.pdsListing.SetOnChange(s.updateJsonView)
|
||||
s.pdsListing.SetOnChange(s.changePdsList)
|
||||
s.pdsListing.SetVimMode(viper.GetBool(data.KeyVimMode))
|
||||
s.pdsListing.SetLogger(s.Log)
|
||||
|
||||
s.jsonContent = wd.NewJsonContent("jsoncontent", s.style)
|
||||
brd := w.NewBorderedWidget("jsoncontentbrd", s.style, s.jsonContent)
|
||||
brd.SetBorder(
|
||||
km := s.jsonContent.GetKeyMap()
|
||||
km.Add(
|
||||
w.NewKey(w.BuildEK(tcell.KeyEnter), func(ev *tcell.EventKey) bool {
|
||||
// Init returns an error if the package is not ready for use.
|
||||
err := clipboard.Init()
|
||||
if err != nil {
|
||||
s.Log("Error initializing clipboard: %s", err.Error())
|
||||
return true
|
||||
}
|
||||
clipboard.Write(clipboard.FmtText, []byte(s.jsonContent.GetSelectedValue()))
|
||||
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.SetBorder(
|
||||
[]rune{'─', '┐', '│', '┘', '─', '─', ' ', '─', '├', '─', '┤', '┬', '│', '┴', '┼'},
|
||||
)
|
||||
|
||||
s.columns.AddAll(s.pdsListing, brd)
|
||||
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.layout.AddAll(s.columns)
|
||||
s.layout.SetLogger(s.Log)
|
||||
s.layout.SetWeight(s.columns, 4)
|
||||
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) HandleResize(ev *tcell.EventResize) {
|
||||
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 {
|
||||
@@ -131,6 +169,10 @@ func (s *ScreenHome) HandleKey(ev *tcell.EventKey) bool {
|
||||
return true
|
||||
}
|
||||
if s.doOpen {
|
||||
if ev.Key() == tcell.KeyEscape {
|
||||
s.doOpen = false
|
||||
return true
|
||||
}
|
||||
if ev.Key() == tcell.KeyCtrlU {
|
||||
s.openPdsEntry.SetValue("")
|
||||
return true
|
||||
@@ -141,12 +183,12 @@ func (s *ScreenHome) HandleKey(ev *tcell.EventKey) bool {
|
||||
}
|
||||
return s.openPdsEntry.HandleKey(ev)
|
||||
}
|
||||
|
||||
return s.menuLayout.HandleKey(ev)
|
||||
}
|
||||
func (s *ScreenHome) HandleTime(ev *tcell.EventTime) {
|
||||
s.menuLayout.HandleTime(ev)
|
||||
s.loading.HandleTime(ev)
|
||||
s.status.HandleTime(ev)
|
||||
}
|
||||
func (s *ScreenHome) Draw() {
|
||||
if s.doOpen {
|
||||
@@ -162,6 +204,7 @@ func (s *ScreenHome) Draw() {
|
||||
if s.isLoading {
|
||||
s.a.DrawWidget(s.loading)
|
||||
}
|
||||
s.a.DrawWidget(s.status)
|
||||
}
|
||||
func (s *ScreenHome) Exit() error { return nil }
|
||||
func (s *ScreenHome) Log(t string, a ...any) {
|
||||
@@ -214,6 +257,7 @@ func (s *ScreenHome) update() {
|
||||
s.menuLayout.SetWidget(s.layout)
|
||||
}
|
||||
s.pdsListing.SetVimMode(viper.GetBool(data.KeyVimMode))
|
||||
s.jsonContent.SetVimMode(viper.GetBool(data.KeyVimMode))
|
||||
vimMI := s.menuLayout.FindItem("settings.vimmode")
|
||||
var vimText = "Enable Vim Mode"
|
||||
if viper.GetBool(data.KeyVimMode) {
|
||||
@@ -281,7 +325,13 @@ func (s *ScreenHome) cliGetPds(args ...string) bool {
|
||||
s.activePds = pds
|
||||
s.expandedEntries = make(map[string]interface{})
|
||||
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
|
||||
}()
|
||||
return true
|
||||
@@ -289,30 +339,57 @@ func (s *ScreenHome) cliGetPds(args ...string) bool {
|
||||
|
||||
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
|
||||
s.pdsListing.Clear()
|
||||
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
|
||||
}
|
||||
t := wd.NewTreeNode(v, v)
|
||||
nsid := s.activePds.NSIDs[i]
|
||||
rIds := s.activePds.GetRecordIdsFor(nsid)
|
||||
for j := range rIds {
|
||||
c := wd.NewTreeNode(fmt.Sprintf("%s (%s)", "record", rIds[j]), rIds[j])
|
||||
t.AddChild(c)
|
||||
}
|
||||
s.pdsListing.Add(t)
|
||||
}
|
||||
s.pdsListing.SetList(wrk)
|
||||
s.layout.ActivateWidget(s.columns)
|
||||
s.columns.ActivateWidget(s.pdsListing)
|
||||
s.pdsListing.SetActive(true)
|
||||
s.pdsListing.SetFocusable(true)
|
||||
s.hideCli()
|
||||
}
|
||||
|
||||
func (s *ScreenHome) selectPdsListingEntry(idx int, nm string) bool {
|
||||
if !s.updateJsonView(idx, nm) {
|
||||
func (s *ScreenHome) selectPdsListingEntry(tn *wd.TreeNode) bool {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -328,16 +405,14 @@ func (s *ScreenHome) selectPdsListingEntry(idx int, nm string) bool {
|
||||
return true
|
||||
|
||||
case models.TypeRecord:
|
||||
// If signed in and we can edit this, activate jsonContent in 'editable' mode
|
||||
s.jsonContent.SetEditable(true)
|
||||
// If signed in and we can edit this, activate jsonContent
|
||||
s.columns.ActivateWidget(s.jsonContent)
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *ScreenHome) updateJsonView(idx int, nm string) bool {
|
||||
func (s *ScreenHome) o_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
|
||||
@@ -368,3 +443,4 @@ func (s *ScreenHome) updateJsonView(idx int, nm string) bool {
|
||||
|
||||
return false
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -56,6 +56,7 @@ type Pds struct {
|
||||
NSIDs []syntax.NSID
|
||||
RecordIds []string
|
||||
nsidToRecordIds map[syntax.NSID][]string
|
||||
recordIdsToNSID map[string]syntax.NSID
|
||||
Records map[string]map[string]any
|
||||
|
||||
RefreshTime time.Time
|
||||
@@ -96,6 +97,7 @@ func NewPdsFromDid(id string) (*Pds, error) {
|
||||
Did: ident.DID,
|
||||
localPath: carPath,
|
||||
nsidToRecordIds: make(map[syntax.NSID][]string),
|
||||
recordIdsToNSID: make(map[string]syntax.NSID),
|
||||
Records: make(map[string]map[string]any),
|
||||
RefreshTime: time.Now(),
|
||||
}
|
||||
@@ -142,6 +144,7 @@ func (p *Pds) unpack() error {
|
||||
p.NSIDs = append(p.NSIDs, col)
|
||||
}
|
||||
p.nsidToRecordIds[col] = append(p.nsidToRecordIds[col], sRKey)
|
||||
p.recordIdsToNSID[sRKey] = col
|
||||
return nil
|
||||
})
|
||||
//slices.Sort(p.NSIDs)
|
||||
@@ -179,4 +182,5 @@ func (p *Pds) List() (map[string]string, error) {
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
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] }
|
||||
|
||||
5
go.mod
5
go.mod
@@ -8,8 +8,10 @@ require (
|
||||
github.com/gdamore/tcell v1.4.1
|
||||
github.com/ipfs/go-cid v0.4.1
|
||||
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/viper v1.21.0
|
||||
golang.design/x/clipboard v0.7.1
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -90,6 +92,9 @@ require (
|
||||
go.uber.org/zap v1.26.0 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.4 // indirect
|
||||
golang.org/x/crypto v0.21.0 // indirect
|
||||
golang.org/x/exp/shiny v0.0.0-20250606033433-dcc06ee1d476 // indirect
|
||||
golang.org/x/image v0.28.0 // indirect
|
||||
golang.org/x/mobile v0.0.0-20250606033058-a2a15c67f36f // indirect
|
||||
golang.org/x/sys v0.40.0 // indirect
|
||||
golang.org/x/text v0.33.0 // indirect
|
||||
golang.org/x/time v0.3.0 // indirect
|
||||
|
||||
11
go.sum
11
go.sum
@@ -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/go.mod h1:sZh36u/YSZ918v0Io+U9ogLYQJ9tLLBmM4eneO6WwsI=
|
||||
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/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=
|
||||
@@ -226,6 +228,8 @@ go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
|
||||
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
|
||||
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
|
||||
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||
golang.design/x/clipboard v0.7.1 h1:OEG3CmcYRBNnRwpDp7+uWLiZi3hrMRJpE9JkkkYtz2c=
|
||||
golang.design/x/clipboard v0.7.1/go.mod h1:i5SiIqj0wLFw9P/1D7vfILFK0KHMk7ydE72HRrUIgkg=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
@@ -233,7 +237,14 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
|
||||
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
|
||||
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ=
|
||||
golang.org/x/exp/shiny v0.0.0-20250606033433-dcc06ee1d476 h1:Wdx0vgH5Wgsw+lF//LJKmWOJBLWX6nprsMqnf99rYDE=
|
||||
golang.org/x/exp/shiny v0.0.0-20250606033433-dcc06ee1d476/go.mod h1:ygj7T6vSGhhm/9yTpOQQNvuAUFziTH7RUiH74EoE2C8=
|
||||
golang.org/x/image v0.28.0 h1:gdem5JW1OLS4FbkWgLO+7ZeFzYtL3xClb97GaUzYMFE=
|
||||
golang.org/x/image v0.28.0/go.mod h1:GUJYXtnGKEUgggyzh+Vxt+AviiCcyiwpsl8iQ8MvwGY=
|
||||
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/mobile v0.0.0-20250606033058-a2a15c67f36f h1:/n+PL2HlfqeSiDCuhdBbRNlGS/g2fM4OHufalHaTVG8=
|
||||
golang.org/x/mobile v0.0.0-20250606033058-a2a15c67f36f/go.mod h1:ESkJ836Z6LpG6mTVAhA48LpfW/8fNR0ifStlH2axyfg=
|
||||
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
|
||||
@@ -31,3 +31,10 @@ var Sep = string(os.PathSeparator)
|
||||
func Path(parts ...string) string {
|
||||
return strings.Join(parts, Sep)
|
||||
}
|
||||
|
||||
func MaxI(a, b int) int {
|
||||
if a < b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
@@ -31,7 +31,6 @@ import (
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
// TODO: Format contents as json. For now this is a duplice of the Text widget
|
||||
type JsonContent struct {
|
||||
id string
|
||||
|
||||
@@ -40,15 +39,20 @@ type JsonContent struct {
|
||||
style tcell.Style
|
||||
x, y int
|
||||
w, h int
|
||||
visible bool
|
||||
active bool
|
||||
focusable bool
|
||||
keyMap *wd.KeyMap
|
||||
|
||||
border []rune
|
||||
visible bool
|
||||
active bool
|
||||
focusable bool
|
||||
keyMap *wd.KeyMap
|
||||
|
||||
editable bool
|
||||
value any
|
||||
|
||||
activeNode []string
|
||||
|
||||
vimMode bool
|
||||
cursor int
|
||||
}
|
||||
|
||||
var _ wd.Widget = (*JsonContent)(nil)
|
||||
@@ -64,7 +68,43 @@ func (w *JsonContent) Init(id string, style tcell.Style) {
|
||||
w.style = style
|
||||
w.visible = true
|
||||
w.focusable = false
|
||||
w.keyMap = wd.BlankKeyMap()
|
||||
w.keyMap = wd.NewKeyMap(
|
||||
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.KeyEnter), func(ev *tcell.EventKey) bool { return false }),
|
||||
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.BuildEKr('j'), func(ev *tcell.EventKey) bool {
|
||||
if !w.vimMode {
|
||||
return false
|
||||
}
|
||||
return w.MoveDown()
|
||||
}),
|
||||
wd.NewKey(wd.BuildEKr('k'), func(ev *tcell.EventKey) bool {
|
||||
if !w.vimMode {
|
||||
return false
|
||||
}
|
||||
return w.MoveUp()
|
||||
}),
|
||||
wd.NewKey(wd.BuildEKr('b'), func(ev *tcell.EventKey) bool {
|
||||
if !w.vimMode {
|
||||
return false
|
||||
}
|
||||
if ev.Modifiers()&tcell.ModCtrl != 0 {
|
||||
return w.PageUp()
|
||||
}
|
||||
return false
|
||||
}),
|
||||
wd.NewKey(wd.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 *JsonContent) Id() string { return w.id }
|
||||
@@ -73,15 +113,61 @@ func (w *JsonContent) HandleResize(ev *tcell.EventResize) { w.w, w.h = ev.Size()
|
||||
func (w *JsonContent) GetKeyMap() *wd.KeyMap { return w.keyMap }
|
||||
func (w *JsonContent) SetKeyMap(km *wd.KeyMap) { w.keyMap = km }
|
||||
|
||||
func (w *JsonContent) HandleKey(ev *tcell.EventKey) bool { return w.keyMap.Handle(ev) }
|
||||
func (w *JsonContent) HandleTime(ev *tcell.EventTime) {}
|
||||
func (w *JsonContent) HandleKey(ev *tcell.EventKey) bool {
|
||||
return w.keyMap.Handle(ev)
|
||||
}
|
||||
func (w *JsonContent) HandleTime(ev *tcell.EventTime) {}
|
||||
func (w *JsonContent) Draw(screen tcell.Screen) {
|
||||
if !w.visible {
|
||||
return
|
||||
}
|
||||
y := w.y
|
||||
for i := range w.contents {
|
||||
wh.DrawText(w.x, y, w.contents[i], w.style, screen)
|
||||
x, y := w.x, w.y
|
||||
brdSz := 0
|
||||
if len(w.border) > 0 {
|
||||
brdSz = 2
|
||||
wh.BorderFilled(x, y, x+w.w, y+w.h, w.border, w.style, screen)
|
||||
}
|
||||
x, y = x+1, y+1
|
||||
h := w.h - brdSz
|
||||
ln := len(w.contents)
|
||||
st, ed := 0, ln-1
|
||||
if ln == 0 {
|
||||
return
|
||||
}
|
||||
if ln > w.h-2 {
|
||||
mid := h / 2
|
||||
if w.cursor < mid {
|
||||
// contents need to start at 0
|
||||
ed = h + 1
|
||||
} else if w.cursor > ln-mid {
|
||||
// contents need to begin at ln-h
|
||||
st = ln - h + 1
|
||||
} else {
|
||||
st = w.cursor - mid
|
||||
ed = st + h + 1
|
||||
}
|
||||
}
|
||||
// ed cannot be higher than ln-1
|
||||
if st < 0 {
|
||||
st = 0
|
||||
}
|
||||
if ed > ln-1 {
|
||||
ed = ln - 1
|
||||
}
|
||||
for i := st; i <= ed; i++ {
|
||||
st := w.style
|
||||
dim := true
|
||||
if i == w.cursor {
|
||||
dim = false
|
||||
} else {
|
||||
st = st.Foreground(tcell.ColorGreen)
|
||||
}
|
||||
txt := w.contents[i]
|
||||
if len(txt) > w.w-brdSz && w.w-brdSz >= 0 {
|
||||
txt = txt[:(w.w - brdSz)]
|
||||
}
|
||||
|
||||
wh.DrawText(w.x, y, txt, st.Dim(dim).Bold(!dim), screen)
|
||||
y++
|
||||
}
|
||||
}
|
||||
@@ -120,6 +206,13 @@ func (w *JsonContent) SetValue(v any) error {
|
||||
w.contents = strings.Split(w.valueString, "\n")
|
||||
return nil
|
||||
}
|
||||
func (w *JsonContent) SetBorder(brd []rune) {
|
||||
if len(brd) == 0 {
|
||||
w.border = wh.BRD_SIMPLE
|
||||
} else {
|
||||
w.border = wh.ValidateBorder(brd)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *JsonContent) SetEditable(v bool) { w.editable = v }
|
||||
|
||||
@@ -136,3 +229,44 @@ func (w *JsonContent) SetEditable(v bool) { w.editable = v }
|
||||
func (w *JsonContent) GetJsonContent() string { return w.text }
|
||||
func (w *JsonContent) GetContents() []string { return w.contents }
|
||||
*/
|
||||
|
||||
func (w *JsonContent) SetVimMode(b bool) { w.vimMode = b }
|
||||
func (w *JsonContent) MoveUp() bool {
|
||||
w.cursor--
|
||||
if w.cursor < 0 {
|
||||
w.cursor = 0
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (w *JsonContent) MoveDown() bool {
|
||||
w.cursor++
|
||||
if w.cursor >= len(w.contents) {
|
||||
w.cursor = len(w.contents) - 1
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (w *JsonContent) PageUp() bool {
|
||||
w.cursor -= w.h
|
||||
if w.cursor < 0 {
|
||||
w.cursor = 0
|
||||
}
|
||||
return true
|
||||
}
|
||||
func (w *JsonContent) PageDn() bool {
|
||||
w.cursor += w.h
|
||||
if w.cursor > len(w.contents)-1 {
|
||||
w.cursor = len(w.contents) - 1
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (w *JsonContent) GetSelectedValue() string {
|
||||
if w.cursor >= len(w.contents) {
|
||||
w.cursor = len(w.contents) - 1
|
||||
}
|
||||
var ret string
|
||||
ret = w.contents[w.cursor]
|
||||
ret = ret[strings.Index(ret, "\": \"")+3:]
|
||||
ret = ret[1 : len(ret)-1]
|
||||
return ret
|
||||
}
|
||||
|
||||
@@ -22,7 +22,10 @@ THE SOFTWARE.
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
t "git.bullercodeworks.com/brian/tcell-widgets"
|
||||
th "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
@@ -34,6 +37,9 @@ type StatusBar struct {
|
||||
visible bool
|
||||
keyMap *t.KeyMap
|
||||
|
||||
blocks []*StatusBlock
|
||||
blockFlags map[*StatusBlock]t.LayoutFlag
|
||||
|
||||
logger func(string, ...any)
|
||||
}
|
||||
|
||||
@@ -47,26 +53,33 @@ func (w *StatusBar) Init(id string, s tcell.Style) {
|
||||
w.id = id
|
||||
w.style = s
|
||||
w.visible = true
|
||||
w.blockFlags = make(map[*StatusBlock]t.LayoutFlag)
|
||||
}
|
||||
|
||||
func (w *StatusBar) Id() string { return w.id }
|
||||
func (w *StatusBar) HandleResize(ev *tcell.EventResize) {
|
||||
w.w, w.h = ev.Size()
|
||||
w.updatePosAndSize()
|
||||
}
|
||||
|
||||
func (w *StatusBar) GetKeyMap() *t.KeyMap { return w.keyMap }
|
||||
func (w *StatusBar) SetKeyMap(km *t.KeyMap) { w.keyMap = km }
|
||||
|
||||
func (w *StatusBar) HandleKey(ev *tcell.EventKey) bool {
|
||||
return false
|
||||
}
|
||||
func (w *StatusBar) HandleKey(ev *tcell.EventKey) bool { 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) {
|
||||
if !w.visible {
|
||||
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 }
|
||||
@@ -76,21 +89,85 @@ func (w *StatusBar) Visible() bool { return w.visible }
|
||||
func (w *StatusBar) SetVisible(a bool) { w.visible = a }
|
||||
func (w *StatusBar) Focusable() bool { return false }
|
||||
func (w *StatusBar) SetFocusable(b bool) {}
|
||||
func (w *StatusBar) SetX(x int) { w.x = x }
|
||||
func (w *StatusBar) SetY(y int) { w.y = y }
|
||||
func (w *StatusBar) SetX(x int) { w.SetPos(t.Coord{X: x, Y: w.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) GetY() int { return 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) GetW() int { return w.w }
|
||||
func (w *StatusBar) GetH() int { return w.h }
|
||||
func (w *StatusBar) SetW(wd int) { w.w = wd }
|
||||
func (w *StatusBar) SetH(h int) { w.h = h }
|
||||
func (w *StatusBar) SetSize(c t.Coord) { w.w, w.h = c.X, c.Y }
|
||||
func (w *StatusBar) WantW() int { return w.w }
|
||||
func (w *StatusBar) WantH() int { return w.h }
|
||||
func (w *StatusBar) MinW() int { return w.w }
|
||||
func (w *StatusBar) MinH() int { return 1 }
|
||||
|
||||
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) GetH() int { return w.h }
|
||||
func (w *StatusBar) SetW(wd int) { w.SetSize(t.Coord{X: wd, Y: w.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
|
||||
w.updatePosAndSize()
|
||||
}
|
||||
func (w *StatusBar) WantW() int { return w.w }
|
||||
func (w *StatusBar) WantH() int { return w.h }
|
||||
func (w *StatusBar) MinW() int { return w.w }
|
||||
func (w *StatusBar) MinH() int { return 1 }
|
||||
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) 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
128
widgets/status_bar_block.go
Normal 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
371
widgets/tree_browser.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user