From abf21d8e1b3ac1c8a55dc7179db6476e599b5778 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 29 Jan 2026 11:19:31 -0600 Subject: [PATCH] Working on the status bar --- app/app.go | 2 +- app/screen_home.go | 27 ++++---- widgets/status_bar.go | 128 ++++++++++++++++++++---------------- widgets/status_bar_block.go | 68 ++++++++++++++++--- 4 files changed, 144 insertions(+), 81 deletions(-) diff --git a/app/app.go b/app/app.go index 2e13472..bf94a23 100644 --- a/app/app.go +++ b/app/app.go @@ -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) diff --git a/app/screen_home.go b/app/screen_home.go index e4a378d..c373fb4 100644 --- a/app/screen_home.go +++ b/app/screen_home.go @@ -24,7 +24,6 @@ package app import ( "fmt" "strings" - "time" "git.bullercodeworks.com/brian/expds/data" "git.bullercodeworks.com/brian/expds/data/models" @@ -55,7 +54,7 @@ type ScreenHome struct { pdsListing *w.SimpleListWithHelp jsonContent *wd.JsonContent status *wd.StatusBar - stDtTmBlock *wd.StatusBlock + stTmBlock *wd.StatusBlock stPathBlock *wd.StatusBlock pdsListingTypes []models.EntryType @@ -109,7 +108,7 @@ func (s *ScreenHome) Init(a *App) { ) 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.jsonContent = wd.NewJsonContent("jsoncontent", s.style) @@ -135,17 +134,14 @@ func (s *ScreenHome) Init(a *App) { s.jsonContent.SetBorder( []rune{'─', '┐', '│', '┘', '─', '─', ' ', '─', '├', '─', '┤', '┬', '│', '┴', '┼'}, ) - s.status = wd.NewStatusBar("home.statusbar", s.style) + + 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", s.style.Background(tcell.ColorOrange).Foreground(tcell.ColorBlack)) + s.stPathBlock = wd.NewStatusBlock("home.statusbar.block", statusStyle.Foreground(tcell.ColorDarkSlateGray).Background(tcell.ColorOrange)) s.stPathBlock.SetType(wd.SBTypePath) s.status.Add(s.stPathBlock) - s.stPathBlock.SetParts([]string{"dir1", "dir2", "dir3"}) - s.stDtTmBlock = wd.NewStatusBlock("home.statusbar.dttm", s.style.Background(tcell.ColorOrange).Foreground(tcell.ColorBlack)) - s.stDtTmBlock.SetType(wd.SBTypeText) - s.status.Add(s.stDtTmBlock) - s.status.SetFlag(s.stDtTmBlock, w.LFAlignHRight) s.columns.AddAll(s.pdsListing, s.jsonContent) @@ -158,6 +154,7 @@ func (s *ScreenHome) GetName() string { return "home" } func (s *ScreenHome) HandleResize(ev *tcell.EventResize) { s.w, s.h = ev.Size() 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()) } @@ -188,7 +185,8 @@ func (s *ScreenHome) HandleKey(ev *tcell.EventKey) bool { func (s *ScreenHome) HandleTime(ev *tcell.EventTime) { s.menuLayout.HandleTime(ev) s.loading.HandleTime(ev) - s.stDtTmBlock.SetText(time.Now().Format(time.Kitchen)) + s.status.HandleTime(ev) + //s.stTmBlock.SetText(time.Now().Format(time.TimeOnly)) } func (s *ScreenHome) Draw() { if s.doOpen { @@ -325,7 +323,7 @@ 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()) + s.changePdsList(s.pdsListing.SelectedIndex(), s.pdsListing.GetSelectedItem()) s.isLoading = false }() return true @@ -385,6 +383,7 @@ func (s *ScreenHome) changePdsList(idx int, nm string) bool { upd = s.updateStatusPathBlock(idx, nm) return upd } + func (s *ScreenHome) 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)) @@ -392,10 +391,10 @@ func (s *ScreenHome) updateStatusPathBlock(idx int, nm string) bool { } switch s.pdsListingTypes[idx] { case models.TypeNSID: - s.stPathBlock.SetParts([]string{nm}) + s.stPathBlock.SetParts([]string{s.activePds.AtId.String(), nm}) case models.TypeRecord: nsidNm := s.activePds.GetNSIDForRecordId(nm).String() - s.stPathBlock.SetParts([]string{nsidNm, nm}) + s.stPathBlock.SetParts([]string{s.activePds.AtId.String(), nsidNm, nm}) } return true } diff --git a/widgets/status_bar.go b/widgets/status_bar.go index 8706eb0..6dda6a0 100644 --- a/widgets/status_bar.go +++ b/widgets/status_bar.go @@ -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" ) @@ -56,6 +59,72 @@ func (w *StatusBar) Init(id string, s tcell.Style) { 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) 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 } +func (w *StatusBar) Active() bool { return false } +func (w *StatusBar) SetActive(a bool) {} +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.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 + 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 { @@ -65,6 +134,7 @@ func (w *StatusBar) HandleResize(ev *tcell.EventResize) { x += w.blocks[i].Width() } } + // Center Aligned // First, get the width of all center blocks var cW int @@ -88,7 +158,7 @@ func (w *StatusBar) HandleResize(ev *tcell.EventResize) { cW = 0 for i := range w.blocks { f, ok := w.blockFlags[w.blocks[i]] - if ok && (f&t.LFAlignCenter != 0) { + if ok && (f&t.LFAlignRight != 0) { cW += w.blocks[i].Width() } } @@ -101,59 +171,3 @@ func (w *StatusBar) HandleResize(ev *tcell.EventResize) { } } } - -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) HandleTime(ev *tcell.EventTime) {} - -func (w *StatusBar) Draw(screen tcell.Screen) { - //th.DrawText(w.x, w.y, fmt.Sprintf("StatusBar: %d,%d", w.x, w.y), w.style, screen) - if !w.visible { - return - } - for i := range w.blocks { - w.Log("Drawing Block %d @ %d,%d", i, w.blocks[i].x, w.blocks[i].y) - w.blocks[i].Draw(screen) - } -} - -func (w *StatusBar) SetStyle(s tcell.Style) { w.style = s } -func (w *StatusBar) Active() bool { return false } -func (w *StatusBar) SetActive(a bool) {} -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) 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) 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) } diff --git a/widgets/status_bar_block.go b/widgets/status_bar_block.go index 39ff909..78ded75 100644 --- a/widgets/status_bar_block.go +++ b/widgets/status_bar_block.go @@ -3,6 +3,7 @@ package widgets import ( "fmt" "strings" + "time" t "git.bullercodeworks.com/brian/tcell-widgets" wh "git.bullercodeworks.com/brian/tcell-widgets/helpers" @@ -21,35 +22,67 @@ type StatusBlock struct { parts []string text string - separator 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, - separator: "  ", + 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(strings.Join(b.parts, b.separator)) + return len(fmt.Sprintf("%s%s", strings.Join(b.parts, b.sep), b.end)) default: - return len(b.text) + return len(fmt.Sprintf("%s%s%s", b.end, b.text, b.end)) } } func (b *StatusBlock) SetType(tp int) { switch tp { - case SBTypeText: + case SBTypePath, SBTypeTime: b.tp = tp default: b.tp = SBTypeText @@ -60,9 +93,8 @@ 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) { - //wh.DrawText(b.x, b.y, fmt.Sprintf("%d,%d: Text: %s; Path: %s", b.x, b.y, b.text, strings.Join(b.parts, b.separator)), b.style, screen) switch b.tp { - case SBTypeText: + case SBTypeText, SBTypeTime: b.DrawText(screen) case SBTypePath: b.DrawPath(screen) @@ -70,9 +102,27 @@ func (b *StatusBlock) Draw(screen tcell.Screen) { } func (b *StatusBlock) DrawText(screen tcell.Screen) { - wh.DrawText(b.x, b.y, b.text, b.style, 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) { - wh.DrawText(b.x, b.y, fmt.Sprintf("%s %s", strings.Join(b.parts, b.separator), b.separator), b.style, 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)) + } }