63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package progress
|
|
|
|
import (
|
|
"io"
|
|
|
|
"gitea.unprism.ru/yotia/display-test/components"
|
|
)
|
|
|
|
type handler struct {
|
|
bar components.ProgressBar
|
|
checkpointN int // Number of check points
|
|
curP int
|
|
}
|
|
|
|
// Handles progress bar update
|
|
// You set the amount of
|
|
type Handler interface {
|
|
Checkpoint() // Increments current progress
|
|
// SetProgress(n int) // Do not want to make it public because there is no places it is needed yet
|
|
Finish() // Set progress to end
|
|
GetBar() components.ProgressBar
|
|
Reset()
|
|
|
|
io.Closer
|
|
}
|
|
|
|
func NewHandler(bar components.ProgressBar, checkpointN int) Handler {
|
|
return &handler{
|
|
bar: bar,
|
|
checkpointN: checkpointN,
|
|
}
|
|
}
|
|
|
|
func (h *handler) GetBar() components.ProgressBar {
|
|
return h.bar
|
|
}
|
|
|
|
func (h *handler) Checkpoint() {
|
|
h.curP = min(h.curP+1, h.checkpointN)
|
|
h.bar.SetProgress(float64(h.curP) / float64(h.checkpointN))
|
|
}
|
|
|
|
func (h *handler) SetProgress(n int) {
|
|
h.curP = min(max(n, 0), h.checkpointN)
|
|
h.bar.SetProgress(float64(h.curP) / float64(h.checkpointN))
|
|
}
|
|
|
|
func (h *handler) Finish() {
|
|
h.curP = h.checkpointN
|
|
h.bar.SetProgress(1)
|
|
}
|
|
|
|
func (h *handler) Reset() {
|
|
h.curP = 0
|
|
h.bar.SetProgress(0)
|
|
}
|
|
|
|
func (h *handler) Close() error {
|
|
h.curP = 0
|
|
h.checkpointN = 0
|
|
return h.bar.Close()
|
|
}
|