package widgets import ( "fmt" "strings" "time" 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 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, 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(fmt.Sprintf("%s%s", strings.Join(b.parts, b.sep), b.end)) default: return len(fmt.Sprintf("%s%s%s", b.end, b.text, b.end)) } } func (b *StatusBlock) SetType(tp int) { switch tp { case SBTypePath, SBTypeTime: 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) { switch b.tp { case SBTypeText, SBTypeTime: b.DrawText(screen) case SBTypePath: b.DrawPath(screen) } } func (b *StatusBlock) DrawText(screen tcell.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) { 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)) } }