ChronoTomato/cmd/client/helper/terminal.go
Sebastian Mark e6c1ca9deb 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

🤖
2024-11-10 22:44:44 +01:00

69 lines
1.6 KiB
Go

package helper
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())
func calc_percentage(message GoTomato.ServerMessage) float64 {
modeDuration := map[string]int{
"Work": message.Settings.Work,
"ShortBreak": message.Settings.ShortBreak,
"LongBreak": message.Settings.LongBreak,
"End": message.Settings.LongBreak,
}
duration := float64(modeDuration[message.Mode])
timeLeft := float64(message.TimeLeft)
return 1.0 - (timeLeft / duration)
}
// Return terminal output based on the passed ServerMessage
func TerminalOutput(pomodoro GoTomato.ServerMessage) string {
var (
output string
modePrefix string
timerOutput string
)
// header
output += colour.Sprintf("^D^4Work: %d ◊ Break: %d ◊ Longbreak: %d^R\n\n",
pomodoro.Settings.Work/60,
pomodoro.Settings.ShortBreak/60,
pomodoro.Settings.LongBreak/60,
)
//body
switch pomodoro.Mode {
case "Idle":
modePrefix = " "
timerOutput = ""
default:
modePrefix = " "
if pomodoro.Paused {
modePrefix = " "
}
minutes := pomodoro.TimeLeft / 60
seconds := pomodoro.TimeLeft % 60
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) + "\n"
output += timerOutput
return output
}