Working on the status bar

This commit is contained in:
2026-01-29 11:19:31 -06:00
parent c84fe6b807
commit abf21d8e1b
4 changed files with 144 additions and 81 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

@@ -24,7 +24,6 @@ package app
import ( import (
"fmt" "fmt"
"strings" "strings"
"time"
"git.bullercodeworks.com/brian/expds/data" "git.bullercodeworks.com/brian/expds/data"
"git.bullercodeworks.com/brian/expds/data/models" "git.bullercodeworks.com/brian/expds/data/models"
@@ -55,7 +54,7 @@ type ScreenHome struct {
pdsListing *w.SimpleListWithHelp pdsListing *w.SimpleListWithHelp
jsonContent *wd.JsonContent jsonContent *wd.JsonContent
status *wd.StatusBar status *wd.StatusBar
stDtTmBlock *wd.StatusBlock stTmBlock *wd.StatusBlock
stPathBlock *wd.StatusBlock stPathBlock *wd.StatusBlock
pdsListingTypes []models.EntryType pdsListingTypes []models.EntryType
@@ -109,7 +108,7 @@ func (s *ScreenHome) Init(a *App) {
) )
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.jsonContent = wd.NewJsonContent("jsoncontent", s.style) s.jsonContent = wd.NewJsonContent("jsoncontent", s.style)
@@ -135,17 +134,14 @@ func (s *ScreenHome) Init(a *App) {
s.jsonContent.SetBorder( s.jsonContent.SetBorder(
[]rune{'─', '┐', '│', '┘', '─', '─', ' ', '─', '├', '─', '┤', '┬', '│', '┴', '┼'}, []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.SetPos(w.Coord{X: 0, Y: s.a.GetH() - 1})
s.status.SetLogger(s.Log) 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.stPathBlock.SetType(wd.SBTypePath)
s.status.Add(s.stPathBlock) 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) 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) { func (s *ScreenHome) HandleResize(ev *tcell.EventResize) {
s.w, s.h = ev.Size() s.w, s.h = ev.Size()
s.menuLayout.HandleResize(w.Coord{X: s.w, Y: s.h - 1}.ResizeEvent()) 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.SetPos(w.Coord{X: 0, Y: s.h - 1})
s.status.HandleResize(w.Coord{X: s.w, Y: 1}.ResizeEvent()) 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) { func (s *ScreenHome) HandleTime(ev *tcell.EventTime) {
s.menuLayout.HandleTime(ev) s.menuLayout.HandleTime(ev)
s.loading.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() { func (s *ScreenHome) Draw() {
if s.doOpen { if s.doOpen {
@@ -325,7 +323,7 @@ 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()) s.changePdsList(s.pdsListing.SelectedIndex(), s.pdsListing.GetSelectedItem())
s.isLoading = false s.isLoading = false
}() }()
return true return true
@@ -385,6 +383,7 @@ func (s *ScreenHome) changePdsList(idx int, nm string) bool {
upd = s.updateStatusPathBlock(idx, nm) upd = s.updateStatusPathBlock(idx, nm)
return upd return upd
} }
func (s *ScreenHome) updateStatusPathBlock(idx int, nm string) bool { func (s *ScreenHome) updateStatusPathBlock(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))
@@ -392,10 +391,10 @@ func (s *ScreenHome) updateStatusPathBlock(idx int, nm string) bool {
} }
switch s.pdsListingTypes[idx] { switch s.pdsListingTypes[idx] {
case models.TypeNSID: case models.TypeNSID:
s.stPathBlock.SetParts([]string{nm}) s.stPathBlock.SetParts([]string{s.activePds.AtId.String(), nm})
case models.TypeRecord: case models.TypeRecord:
nsidNm := s.activePds.GetNSIDForRecordId(nm).String() nsidNm := s.activePds.GetNSIDForRecordId(nm).String()
s.stPathBlock.SetParts([]string{nsidNm, nm}) s.stPathBlock.SetParts([]string{s.activePds.AtId.String(), nsidNm, nm})
} }
return true return true
} }

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"
) )
@@ -56,6 +59,72 @@ func (w *StatusBar) Init(id string, s tcell.Style) {
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) 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) // First, all blocks that are Left Aligned (or no alignment)
x := w.x x := w.x
for i := range w.blocks { for i := range w.blocks {
@@ -65,6 +134,7 @@ func (w *StatusBar) HandleResize(ev *tcell.EventResize) {
x += w.blocks[i].Width() x += w.blocks[i].Width()
} }
} }
// Center Aligned // Center Aligned
// First, get the width of all center blocks // First, get the width of all center blocks
var cW int var cW int
@@ -88,7 +158,7 @@ func (w *StatusBar) HandleResize(ev *tcell.EventResize) {
cW = 0 cW = 0
for i := range w.blocks { for i := range w.blocks {
f, ok := w.blockFlags[w.blocks[i]] f, ok := w.blockFlags[w.blocks[i]]
if ok && (f&t.LFAlignCenter != 0) { if ok && (f&t.LFAlignRight != 0) {
cW += w.blocks[i].Width() 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) }

View File

@@ -3,6 +3,7 @@ package widgets
import ( import (
"fmt" "fmt"
"strings" "strings"
"time"
t "git.bullercodeworks.com/brian/tcell-widgets" t "git.bullercodeworks.com/brian/tcell-widgets"
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers" wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
@@ -21,35 +22,67 @@ type StatusBlock struct {
parts []string parts []string
text string text string
separator string sepR, sepL string
endR, endL string
dir int
sep, end string
active bool
} }
const ( const (
SBTypeText = iota SBTypeText = iota
SBTypePath SBTypePath
SBTypeTime
SBDirL = iota
SBDirR
) )
func NewStatusBlock(id string, s tcell.Style) *StatusBlock { func NewStatusBlock(id string, s tcell.Style) *StatusBlock {
b := StatusBlock{ b := StatusBlock{
id: id, style: s, id: id, style: s,
separator: "  ", sepR: "  ",
endR: "",
sepL: "  ",
endL: "",
} }
b.SetDirection(SBDirR)
return &b 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) SetPos(p t.Coord) { b.x, b.y = p.X, p.Y }
func (b *StatusBlock) Width() int { func (b *StatusBlock) Width() int {
switch b.tp { switch b.tp {
case SBTypePath: 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: default:
return len(b.text) return len(fmt.Sprintf("%s%s%s", b.end, b.text, b.end))
} }
} }
func (b *StatusBlock) SetType(tp int) { func (b *StatusBlock) SetType(tp int) {
switch tp { switch tp {
case SBTypeText: case SBTypePath, SBTypeTime:
b.tp = tp b.tp = tp
default: default:
b.tp = SBTypeText 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) SetText(t string) { b.text = t }
func (b *StatusBlock) Draw(screen tcell.Screen) { 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 { switch b.tp {
case SBTypeText: case SBTypeText, SBTypeTime:
b.DrawText(screen) b.DrawText(screen)
case SBTypePath: case SBTypePath:
b.DrawPath(screen) b.DrawPath(screen)
@@ -70,9 +102,27 @@ func (b *StatusBlock) Draw(screen tcell.Screen) {
} }
func (b *StatusBlock) DrawText(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) { 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))
}
} }