Working on the status bar
This commit is contained in:
@@ -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