package widget import ( "image" "github.com/BeatGlow/beatglow/ui" "github.com/BeatGlow/display" "github.com/BeatGlow/display/draw" "github.com/BeatGlow/display/pixel" "github.com/sirupsen/logrus" ) type Progress struct { Display display.Display Style ui.Style Position image.Point width int height int progress float64 buffer draw.Image changed bool } func NewProgress(display display.Display, at image.Point, width, height int) *Progress { logrus.WithFields(logrus.Fields{ "width": width, "height": height, "at": at, }).Debug("new progress bar") model := display.ColorModel() w := &Progress{ Display: display, Style: ui.StylesFor(model).Format, Position: at, width: width, height: height, buffer: ui.BufferFor(model, width, height), } w.Set(0) return w } func (w *Progress) Set(progress float64) { if progress < 0 { progress = 0 } if progress > 1 { progress = 1 } w.progress = progress w.changed = true } func (w *Progress) SetStyle(style ui.Style) *Progress { w.Style = style return w } func (w *Progress) Render(redraw bool) bool { if !(redraw || w.changed) { return false } var stop = 1 if w.progress > 0 { stop = 1 + int(float64(w.width-2)*w.progress+.5) } for y := 0; y < w.height; y++ { for x := 0; x < w.width; x++ { if ((x == 0 || x == w.width-1) && y != 0 && y != w.height-1) || ((y == 0 || y == w.height-1) && x != 0 && x != w.width-1) { //if (x != 0 && y == 0) || y == w.height-1 || x == 0 || x == w.width-1 || x < stop { w.buffer.Set(x, y, w.Style.Foreground) } else if x > 0 && x < stop { w.buffer.Set(x, y, pixel.Gray4{Y: 0x77}) } else { w.buffer.Set(x, y, w.Style.Background.At(x, y)) } } } draw.Draw(w.Display, w.buffer.Bounds().Add(w.Position), w.buffer, image.Point{}, draw.Src) w.changed = false return true } var _ ui.Widget = (*Progress)(nil)