summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--go.work1
-rw-r--r--tui/go.mod3
-rw-r--r--tui/progressbar.go64
3 files changed, 68 insertions, 0 deletions
diff --git a/go.work b/go.work
index 4963ab5..0c4ec81 100644
--- a/go.work
+++ b/go.work
@@ -6,4 +6,5 @@ use (
./gpt2
./llm
./tensor
+ ./tui
)
diff --git a/tui/go.mod b/tui/go.mod
new file mode 100644
index 0000000..af968c1
--- /dev/null
+++ b/tui/go.mod
@@ -0,0 +1,3 @@
+module go.jknobloc.com/x/tui
+
+go 1.25.0
diff --git a/tui/progressbar.go b/tui/progressbar.go
new file mode 100644
index 0000000..73a0c6a
--- /dev/null
+++ b/tui/progressbar.go
@@ -0,0 +1,64 @@
+package tui
+
+import (
+ "fmt"
+ "strings"
+ "time"
+)
+
+type ProgressBar struct {
+ title string
+ width int
+ steps int
+ n int
+ start time.Time
+}
+
+func NewProgressBar(title string, width, steps int, start time.Time) *ProgressBar {
+ pb := &ProgressBar{
+ title: title,
+ width: width,
+ steps: steps,
+ start: start,
+ }
+
+ pb.Print()
+
+ return pb
+}
+
+func (p *ProgressBar) Increment() {
+ p.n++
+}
+
+func (p *ProgressBar) Update(steps int) {
+ p.n = steps
+}
+
+func (p *ProgressBar) Print() {
+ fmt.Print(p.String())
+}
+
+func (p *ProgressBar) Finish() {
+ fmt.Println()
+}
+
+func (p *ProgressBar) String() string {
+ progress := int(float64(p.n) / float64(p.steps) * float64(p.width))
+ percentage := int(float64(p.n) / float64(p.steps) * 100)
+
+ lap := time.Since(p.start)
+
+ duration := fmt.Sprintf("%02d:%02d:%02d", int(lap.Hours()), int(lap.Minutes())%60, int(lap.Seconds())%60)
+
+ return fmt.Sprintf(
+ "\r[%s] %-20s %3d%% [%s%s] %d/%d",
+ duration,
+ p.title,
+ percentage,
+ strings.Repeat("=", min(progress, p.width)),
+ strings.Repeat("-", max(0, p.width-progress)),
+ p.n,
+ p.steps,
+ )
+}