-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
168 lines (142 loc) · 3.91 KB
/
main.go
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
package main
import (
"fmt"
"math"
"os"
"strconv"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/fogleman/ease"
"github.com/lucasb-eyer/go-colorful"
)
const (
progressBarWidth = 50
progressFullChar = "█"
progressEmptyChar = "░"
progressColor1 = "#B14FFF"
progressColor2 = "#00FFA3"
)
// General stuff for styling the view
var (
subtleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241"))
progressEmpty = subtleStyle.Render(progressEmptyChar)
mainStyle = lipgloss.NewStyle().MarginLeft(2)
// Gradient colors we'll use for the progress bar
ramp = makeRampStyles(progressColor1, progressColor2, progressBarWidth)
progressMaxValue float64 = 1
progressCurrentValue float64 = 0
)
type model struct {
Chosen bool
Frames int
Progress float64
Loaded bool
}
func main() {
// Check if a command line argument is provided
if len(os.Args) != 2 {
fmt.Println("Usage: go run . <progressMaxValue>")
return
}
// Convert the argument to a float64
var err error
progressMaxValue, err = strconv.ParseFloat(os.Args[1], 64)
if progressMaxValue < 0 || progressMaxValue > 1 {
fmt.Println("Invalid progressMaxValue. progressMaxValue needs to be between 0 and 1.")
return
}
if err != nil {
fmt.Println("Invalid progressMaxValue. It should be a number.")
return
}
initialModel := model{false, int(math.Round(progressCurrentValue * 100)), progressCurrentValue, false}
p := tea.NewProgram(initialModel)
if _, err := p.Run(); err != nil {
fmt.Println("could not start program:", err)
}
}
type (
frameMsg struct{}
)
func frame() tea.Cmd {
return tea.Tick(time.Second/60, func(time.Time) tea.Msg {
return frameMsg{}
})
}
func (m model) Init() tea.Cmd {
return frame()
}
// Main update function.
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Make sure these keys always quit
if msg, ok := msg.(tea.KeyMsg); ok {
k := msg.String()
if k == "q" || k == "esc" || k == "ctrl+c" {
return m, tea.Quit
}
}
return updateChosen(msg, m)
}
// The main view, which just calls the appropriate sub-view
func (m model) View() string {
s := chosenView(m)
return mainStyle.Render("\n" + s + "\n\n")
}
// Update loop for the second view after a choice has been made
func updateChosen(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
switch msg.(type) {
case frameMsg:
if !m.Loaded {
m.Frames++
m.Progress = ease.Linear(float64(m.Frames) / 100) // Adjusted the calculation for the new progressMaxValue
if m.Progress >= progressMaxValue {
m.Progress = progressMaxValue
m.Loaded = true
return m, tea.Quit
}
return m, frame()
}
}
return m, nil
}
// The second view, after a task has been chosen
func chosenView(m model) string {
return progressbar(m.Progress) + "%"
}
func progressbar(percent float64) string {
w := float64(progressBarWidth)
fullSize := int(math.Round(w * percent))
var fullCells string
for i := 0; i < fullSize; i++ {
fullCells += ramp[i].Render(progressFullChar)
}
emptySize := int(w) - fullSize
emptyCells := strings.Repeat(progressEmpty, emptySize)
return fmt.Sprintf("%s%s %3.0f", fullCells, emptyCells, math.Round(percent*100))
}
// Utils
// Generate a blend of colors.
func makeRampStyles(colorA, colorB string, steps float64) (s []lipgloss.Style) {
cA, _ := colorful.Hex(colorA)
cB, _ := colorful.Hex(colorB)
for i := 0.0; i < steps; i++ {
c := cA.BlendLuv(cB, i/steps)
s = append(s, lipgloss.NewStyle().Foreground(lipgloss.Color(colorToHex(c))))
}
return
}
// Convert a colorful.Color to a hexadecimal format.
func colorToHex(c colorful.Color) string {
return fmt.Sprintf("#%s%s%s", colorFloatToHex(c.R), colorFloatToHex(c.G), colorFloatToHex(c.B))
}
// Helper function for converting colors to hex. Assumes a value between 0 and
// 1.
func colorFloatToHex(f float64) (s string) {
s = strconv.FormatInt(int64(f*255), 16)
if len(s) == 1 {
s = "0" + s
}
return
}