File: progress.go

package info (click to toggle)
golang-github-gcla-gowid 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,456 kB
  • sloc: makefile: 4
file content (200 lines) | stat: -rw-r--r-- 5,260 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2019-2022 Graham Clark. All rights reserved.  Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.

// Package progress provides a simple progress bar.
package progress

import (
	"fmt"

	"github.com/gcla/gowid"
	"github.com/gcla/gowid/gwutil"
	"github.com/gcla/gowid/widgets/hpadding"
	"github.com/gcla/gowid/widgets/styled"
	"github.com/gcla/gowid/widgets/text"
)

//======================================================================

// IWidget - if your widget implements progress.IWidget, you will be able to render it using the
// progress.Render() function.
//
type IWidget interface {
	gowid.IWidget
	// Text should return the string to be displayed inside the progress bar e.g. "50%"
	Text() string
	// Progress returns the number of units completed.
	Progress() int
	// Target returns the number of units required overall.
	Target() int
	// Normal is used to render the incomplete part of the progress bar.
	Normal() gowid.ICellStyler
	// Complete is used to render the complete part of the progress bar.
	Complete() gowid.ICellStyler
}

// For callback registration
type ProgressCB struct{}
type TargetCB struct{}

// Widget is the concrete type of a progressbar widget.
type Widget struct {
	Current, Done    int
	normal, complete gowid.ICellStyler
	Callbacks        *gowid.Callbacks
	gowid.RejectUserInput
	gowid.NotSelectable
}

// Options is used for passing arguments to the progressbar initializer, New().
type Options struct {
	Normal, Complete gowid.ICellStyler
	Target, Current  int
}

// New will return an initialized progressbar Widget/
func New(args Options) *Widget {
	if args.Target == 0 {
		args.Target = 100
	}
	res := &Widget{
		Current:   args.Current,
		Done:      args.Target,
		normal:    args.Normal,
		complete:  args.Complete,
		Callbacks: gowid.NewCallbacks(),
	}
	var _ IWidget = res
	return res
}

func (w *Widget) String() string {
	return fmt.Sprintf("progress")
}

func (w *Widget) Text() string {
	var percent int
	if w.Done == 0 {
		percent = 100
	} else {
		percent = gwutil.Min(100, gwutil.Max(0, w.Current*100/w.Done))
	}
	return fmt.Sprintf("%d %%", percent)
}

func (w *Widget) OnSetProgress(f gowid.IWidgetChangedCallback) {
	gowid.AddWidgetCallback(w.Callbacks, ProgressCB{}, f)
}

func (w *Widget) RemoveOnSetProgress(f gowid.IIdentity) {
	gowid.RemoveWidgetCallback(w.Callbacks, ProgressCB{}, f)
}

func (w *Widget) OnSetTarget(f gowid.IWidgetChangedCallback) {
	gowid.AddWidgetCallback(w.Callbacks, TargetCB{}, f)
}

func (w *Widget) RemoveOnSetTarget(f gowid.IIdentity) {
	gowid.RemoveWidgetCallback(w.Callbacks, TargetCB{}, f)
}

func (w *Widget) SetProgress(app gowid.IApp, current int) {
	w.Current = current
	if w.Current > w.Done {
		w.Current = w.Done
	} else if w.Current < 0 {
		w.Current = 0
	}
	gowid.RunWidgetCallbacks(w.Callbacks, ProgressCB{}, app, w)
}

func (w *Widget) SetTarget(app gowid.IApp, target int) {
	w.Done = target
	if w.Done < 0 {
		w.Done = 0
	}
	if w.Current > w.Done {
		w.Current = w.Done
	}
	gowid.RunWidgetCallbacks(w.Callbacks, TargetCB{}, app, w)
}

func (w *Widget) Progress() int {
	return w.Current
}

func (w *Widget) Target() int {
	return w.Done
}

func (w *Widget) Normal() gowid.ICellStyler {
	return w.normal
}

func (w *Widget) Complete() gowid.ICellStyler {
	return w.complete
}

func (w *Widget) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
	return gowid.CalculateRenderSizeFallback(w, size, focus, app)
}

func (w *Widget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
	return Render(w, size, focus, app)
}

//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

// Render will render a progressbar IWidget.
func Render(w IWidget, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
	flow, isFlow := size.(gowid.IRenderFlowWith)
	if !isFlow {
		panic(gowid.WidgetSizeError{Widget: w, Size: size, Required: "gowid.IRenderFlowWith"})
	}
	cols := flow.FlowColumns()

	barCanvas :=
		styled.New(
			text.New(gwutil.StringOfLength(' ', cols)),
			w.Normal(),
		).Render(

			gowid.RenderBox{C: cols, R: 1}, gowid.NotSelected, app)

	fnorm, _, _ := w.Normal().GetStyle(app)
	percentStyle := gowid.MakePaletteEntry(fnorm, gowid.NoColor{})

	fcomp, bcomp, scomp := w.Complete().GetStyle(app)
	fcompCol := gowid.IColorToTCell(fcomp, gowid.ColorNone, app.GetColorMode())
	bcompCol := gowid.IColorToTCell(bcomp, gowid.ColorNone, app.GetColorMode())

	cur, done := w.Progress(), w.Target()
	var cutoff int
	if done == 0 {
		cutoff = cols
	} else {
		cutoff = (cur * cols) / done
	}
	for i := 0; i < cutoff; i++ {
		barCanvas.SetCellAt(i, 0, barCanvas.CellAt(i, 0).WithForegroundColor(fcompCol).WithBackgroundColor(bcompCol).WithStyle(scomp))
	}

	percent := hpadding.New(
		styled.New(
			text.New(w.Text()),
			percentStyle,
		),
		gowid.HAlignMiddle{}, gowid.RenderFixed{},
	)
	percentCanvas := percent.Render(gowid.RenderBox{C: cols, R: 1}, gowid.NotSelected, app)
	barCanvas.MergeUnder(percentCanvas, 0, 0, false)

	return barCanvas
}

//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: