Some work

This commit is contained in:
2026-01-28 16:51:50 -06:00
parent 3f347d8342
commit c84fe6b807
7 changed files with 220 additions and 28 deletions

View File

@@ -71,9 +71,7 @@ func (w *JsonContent) Init(id string, style tcell.Style) {
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.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 {

View File

@@ -34,6 +34,9 @@ type StatusBar struct {
visible bool
keyMap *t.KeyMap
blocks []*StatusBlock
blockFlags map[*StatusBlock]t.LayoutFlag
logger func(string, ...any)
}
@@ -47,11 +50,56 @@ 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()
// 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.LFAlignCenter != 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()
}
}
}
func (w *StatusBar) GetKeyMap() *t.KeyMap { return w.keyMap }
@@ -64,33 +112,48 @@ func (w *StatusBar) HandleKey(ev *tcell.EventKey) bool {
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) 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

@@ -0,0 +1,78 @@
package widgets
import (
"fmt"
"strings"
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
separator string
}
const (
SBTypeText = iota
SBTypePath
)
func NewStatusBlock(id string, s tcell.Style) *StatusBlock {
b := StatusBlock{
id: id, style: s,
separator: "  ",
}
return &b
}
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))
default:
return len(b.text)
}
}
func (b *StatusBlock) SetType(tp int) {
switch tp {
case SBTypeText:
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) {
//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:
b.DrawText(screen)
case SBTypePath:
b.DrawPath(screen)
}
}
func (b *StatusBlock) DrawText(screen tcell.Screen) {
wh.DrawText(b.x, b.y, b.text, b.style, 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)
}