package dialogs
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.brettb.xyz/goinv/client/internal/ui/style"
|
|
"github.com/gdamore/tcell/v2"
|
|
"github.com/rivo/tview"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
const (
|
|
prgCell = "▉"
|
|
prgMinWidth = 40
|
|
)
|
|
|
|
type ProgressDialog struct {
|
|
*tview.Box
|
|
logger *zap.Logger
|
|
x int
|
|
y int
|
|
width int
|
|
height int
|
|
counterValue int
|
|
display bool
|
|
}
|
|
|
|
func NewProgressDialog(logger *zap.Logger) *ProgressDialog {
|
|
return &ProgressDialog{
|
|
logger: logger,
|
|
Box: tview.NewBox().
|
|
SetBorder(true).
|
|
SetBorderColor(style.DialogBorderColor),
|
|
display: false,
|
|
}
|
|
}
|
|
|
|
func (d *ProgressDialog) SetTitle(title string) {
|
|
d.Box.SetTitle(title)
|
|
}
|
|
|
|
func (d *ProgressDialog) Draw(screen tcell.Screen) {
|
|
if !d.display || d.height < 3 {
|
|
return
|
|
}
|
|
|
|
d.Box.DrawForSubclass(screen, d)
|
|
x, y, width, _ := d.Box.GetInnerRect()
|
|
tickStr := d.tickStr(width)
|
|
tview.Print(screen, tickStr, x, y, width, tview.AlignLeft, style.PrgBarBgColor)
|
|
}
|
|
|
|
func (d *ProgressDialog) SetRect(x, y, width, height int) {
|
|
d.x = x
|
|
d.y = y
|
|
d.width = width
|
|
|
|
if d.width > prgMinWidth {
|
|
d.width = prgMinWidth
|
|
spaceWidth := (width - d.width) / 2
|
|
d.x = x + spaceWidth
|
|
}
|
|
|
|
if height > 3 {
|
|
d.height = 3
|
|
spaceHeight := (height - d.height) / 2
|
|
d.y = y + spaceHeight
|
|
}
|
|
|
|
d.Box.SetRect(d.x, d.y, d.width, d.height)
|
|
}
|
|
|
|
func (d *ProgressDialog) Hide() {
|
|
d.display = false
|
|
}
|
|
|
|
func (d *ProgressDialog) Display() {
|
|
d.counterValue = 0
|
|
d.display = true
|
|
}
|
|
|
|
func (d *ProgressDialog) IsDisplay() bool {
|
|
return d.display
|
|
}
|
|
|
|
func (d *ProgressDialog) Focus(delegate func(tview.Primitive)) {}
|
|
|
|
func (d *ProgressDialog) HasFocus() bool {
|
|
return d.Box.HasFocus()
|
|
}
|
|
|
|
func (d *ProgressDialog) InputHandler() func(*tcell.EventKey, func(tview.Primitive)) {
|
|
return d.WrapInputHandler(func(e *tcell.EventKey, setFocus func(tview.Primitive)) {
|
|
d.logger.Sugar().Debugf("progress dialog event %v received", e)
|
|
})
|
|
}
|
|
|
|
func (d *ProgressDialog) tickStr(max int) string {
|
|
barColor := style.GetColorHex(style.PrgBarColor)
|
|
counter := d.counterValue
|
|
|
|
if counter < max-4 {
|
|
d.counterValue++
|
|
} else {
|
|
d.counterValue = 0
|
|
}
|
|
|
|
prgHeadStr := ""
|
|
hWidth := 0
|
|
prgEndStr := ""
|
|
prgStr := ""
|
|
|
|
for i := 0; i < d.counterValue; i++ {
|
|
prgHeadStr += fmt.Sprintf("[black::]%s", prgCell)
|
|
hWidth++
|
|
}
|
|
|
|
prgStr = strings.Repeat(prgCell, 4)
|
|
|
|
for i := 0; i < max+hWidth; i++ {
|
|
prgEndStr += fmt.Sprintf("[black::]%s", prgCell)
|
|
}
|
|
|
|
progress := fmt.Sprintf("%s[%s::]%s%s", prgHeadStr, barColor, prgStr, prgEndStr)
|
|
|
|
return progress
|
|
}
|