diff --git a/app/screen_home.go b/app/screen_home.go index c373fb4..eb177d2 100644 --- a/app/screen_home.go +++ b/app/screen_home.go @@ -50,8 +50,9 @@ type ScreenHome struct { layout *w.LinearLayout columns *w.LinearLayout - activePds *models.Pds - pdsListing *w.SimpleListWithHelp + activePds *models.Pds + //pdsListing *w.SimpleListWithHelp + pdsListing *wd.TreeBrowser jsonContent *wd.JsonContent status *wd.StatusBar stTmBlock *wd.StatusBlock @@ -102,7 +103,8 @@ 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 = w.NewSimpleListWithHelp("pdslisting", s.style) + s.pdsListing = wd.NewTreeBrowser("pdslisting", s.style) s.pdsListing.SetBorder( []rune{'─', '┬', '│', '┴', '─', '└', '│', '┌', '├', '─', '┤', '┬', '│', '┴', '┼'}, ) @@ -141,6 +143,7 @@ func (s *ScreenHome) Init(a *App) { 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) @@ -323,7 +326,10 @@ func (s *ScreenHome) cliGetPds(args ...string) bool { s.activePds = pds s.expandedEntries = make(map[string]interface{}) s.updatePdsListing() - s.changePdsList(s.pdsListing.SelectedIndex(), s.pdsListing.GetSelectedItem()) + n, err := s.pdsListing.GetActiveNode() + if err == nil && n != nil { + s.changePdsList(n) + } s.isLoading = false }() return true @@ -331,30 +337,61 @@ 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) + + /* + wrk = append(wrk, t) + 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.pdsListing.AddChild(wrk) 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 + } + 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 { + // TODO: UpdateStatusPathBlock + 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 } @@ -377,14 +414,12 @@ func (s *ScreenHome) selectPdsListingEntry(idx int, nm string) bool { return false } - -func (s *ScreenHome) changePdsList(idx int, nm string) bool { - upd := s.updateJsonView(idx, nm) - upd = s.updateStatusPathBlock(idx, nm) +func (s *ScreenHome) o_changePdsList(idx int, nm string) bool { + upd := s.o_updateJsonView(idx, nm) + upd = s.o_updateStatusPathBlock(idx, nm) return upd } - -func (s *ScreenHome) updateStatusPathBlock(idx int, nm string) bool { +func (s *ScreenHome) o_updateStatusPathBlock(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 @@ -398,7 +433,7 @@ func (s *ScreenHome) updateStatusPathBlock(idx int, nm string) bool { } return true } -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 diff --git a/helpers/helpers.go b/helpers/helpers.go index f454c60..796d34c 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -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 +} diff --git a/widgets/tree_browser.go b/widgets/tree_browser.go new file mode 100644 index 0000000..430d199 --- /dev/null +++ b/widgets/tree_browser.go @@ -0,0 +1,340 @@ +package widgets + +import ( + "errors" + + 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 false } +func (w *TreeBrowser) SetActive(a bool) {} +func (w *TreeBrowser) Visible() bool { return w.visible } +func (w *TreeBrowser) SetVisible(a bool) { w.visible = a } +func (w *TreeBrowser) Focusable() bool { return false } +func (w *TreeBrowser) SetFocusable(b bool) {} +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 + } +} + +/* + * 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) getList() []string { + ret := []string{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]) + } +}