Working on the status bar
This commit is contained in:
@@ -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) }
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user