feat: add progress bar to timer

- add progress package for visual feedback
- implement calc_percentage function for duration calculation
- update TerminalOutput to include progress percentage and view

🤖
This commit is contained in:
Sebastian Mark 2024-11-10 20:31:21 +01:00
parent 94e7b41421
commit 6b90db0d6c
4 changed files with 27 additions and 4 deletions

View file

@ -4,10 +4,29 @@ import (
"fmt"
"github.com/alecthomas/colour"
"github.com/charmbracelet/bubbles/progress"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
var progressBar = progress.New(progress.WithDefaultGradient(), progress.WithoutPercentage())
func calc_percentage(message GoTomato.ServerMessage) float64 {
var duration float64
switch message.Mode {
case "Work":
duration = float64(message.Settings.Work)
case "ShortBreak":
duration = float64(message.Settings.ShortBreak)
case "LongBreak", "End":
duration = float64(message.Settings.LongBreak)
}
timeLeft := float64(message.TimeLeft)
return 1.0 - (timeLeft / duration)
}
// Return terminal output based on the passed ServerMessage
func TerminalOutput(pomodoro GoTomato.ServerMessage) string {
var (
@ -37,15 +56,16 @@ func TerminalOutput(pomodoro GoTomato.ServerMessage) string {
minutes := pomodoro.TimeLeft / 60
seconds := pomodoro.TimeLeft % 60
timerOutput = fmt.Sprintf("⏳ %02d:%02d", minutes, seconds)
timerOutput = fmt.Sprintf("⏳ %02d:%02d", minutes, seconds) + "\n"
timerOutput += progressBar.ViewAs(calc_percentage(pomodoro))
}
output += fmt.Sprintf("Session: %d/%d\n",
pomodoro.Session,
pomodoro.Settings.Sessions,
)
output += fmt.Sprintf("%s %s\n", modePrefix, pomodoro.Mode)
output += fmt.Sprintf(timerOutput)
output += fmt.Sprintf("%s %s\n", modePrefix, pomodoro.Mode) + "\n"
output += timerOutput
return output
}

View file

@ -25,7 +25,7 @@ func (a app) View() string {
}
helpView := a.help.View(a.keys)
height := 8 - strings.Count(body, "\n") - strings.Count(serverStatus, "\n") - strings.Count(helpView, "\n")
height := 10 - strings.Count(body, "\n") - strings.Count(serverStatus, "\n") - strings.Count(helpView, "\n")
return body + strings.Repeat("\n", height) + serverStatus + helpView
}