tvxwidgets/gauge_am.go

102 lines
2.3 KiB
Go
Raw Normal View History

2021-12-21 18:13:01 +11:00
package tvxwidgets
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// ActivityModeGauge represents activity mode gauge permitive.
2021-12-21 18:13:01 +11:00
type ActivityModeGauge struct {
*tview.Box
// counter value
counter int
// pgBgColor: progress block background color
pgBgColor tcell.Color
}
// NewActivityModeGauge returns new activity mode gauge permitive.
func NewActivityModeGauge() *ActivityModeGauge {
gauge := &ActivityModeGauge{
Box: tview.NewBox(),
counter: 0,
pgBgColor: tcell.ColorBlue,
}
2021-12-21 18:13:01 +11:00
return gauge
}
// Draw draws this primitive onto the screen.
func (g *ActivityModeGauge) Draw(screen tcell.Screen) {
g.Box.DrawForSubclass(screen, g)
x, y, width, height := g.Box.GetInnerRect()
tickStr := g.tickStr(width)
2021-12-21 18:13:01 +11:00
for i := 0; i < height; i++ {
2021-12-21 19:57:34 +11:00
tview.Print(screen, tickStr, x, y+i, width, tview.AlignLeft, g.pgBgColor)
2021-12-21 18:13:01 +11:00
}
}
// Focus is called when this primitive receives focus.
func (g *ActivityModeGauge) Focus(delegate func(p tview.Primitive)) { //nolint:revive
2021-12-21 18:13:01 +11:00
}
// HasFocus returns whether or not this primitive has focus.
2021-12-21 18:13:01 +11:00
func (g *ActivityModeGauge) HasFocus() bool {
return g.Box.HasFocus()
}
// GetRect return primitive current rect.
2021-12-21 18:13:01 +11:00
func (g *ActivityModeGauge) GetRect() (int, int, int, int) {
return g.Box.GetRect()
}
// SetRect sets rect for this primitive.
func (g *ActivityModeGauge) SetRect(x, y, width, height int) {
g.Box.SetRect(x, y, width, height)
}
// SetPgBgColor sets progress block background color.
2021-12-21 18:13:01 +11:00
func (g *ActivityModeGauge) SetPgBgColor(color tcell.Color) {
g.pgBgColor = color
}
// Pulse pulse update the gauge progress bar.
2021-12-21 18:13:01 +11:00
func (g *ActivityModeGauge) Pulse() {
g.counter++
2021-12-21 18:13:01 +11:00
}
// Reset resets the gauge counter (set to 0).
func (g *ActivityModeGauge) Reset() {
g.counter = 0
}
2021-12-21 18:13:01 +11:00
func (g *ActivityModeGauge) tickStr(max int) string {
var (
prgHeadStr string
prgEndStr string
prgStr string
)
2021-12-21 18:13:01 +11:00
if g.counter >= max-4 {
g.counter = 0
}
2021-12-21 18:13:01 +11:00
hWidth := 0
2021-12-21 18:13:01 +11:00
for i := 0; i < g.counter; i++ {
prgHeadStr += fmt.Sprintf("[%s::]%s", getColorName(tview.Styles.PrimitiveBackgroundColor), prgCell)
2021-12-21 18:13:01 +11:00
hWidth++
}
2021-12-21 18:13:01 +11:00
prgStr = prgCell + prgCell + prgCell + prgCell
2021-12-21 18:13:01 +11:00
for i := 0; i < max+hWidth+4; i++ {
prgEndStr += fmt.Sprintf("[%s::]%s", getColorName(tview.Styles.PrimitiveBackgroundColor), prgCell)
2021-12-21 18:13:01 +11:00
}
return fmt.Sprintf("%s[%s::]%s%s", prgHeadStr, getColorName(g.pgBgColor), prgStr, prgEndStr)
}