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 c30829392d
commit ae238e5aaf
4 changed files with 26 additions and 6 deletions

View file

@ -3,11 +3,27 @@ package helper
import ( import (
"fmt" "fmt"
"github.com/alecthomas/colour"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/alecthomas/colour"
"github.com/charmbracelet/bubbles/progress"
) )
var prg = progress.New(progress.WithDefaultGradient())
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)
}
return 1.0 - float64(message.TimeLeft)/duration
}
// Return terminal output based on the passed ServerMessage // Return terminal output based on the passed ServerMessage
func TerminalOutput(pomodoro GoTomato.ServerMessage) string { func TerminalOutput(pomodoro GoTomato.ServerMessage) string {
var ( var (
@ -37,15 +53,16 @@ func TerminalOutput(pomodoro GoTomato.ServerMessage) string {
minutes := pomodoro.TimeLeft / 60 minutes := pomodoro.TimeLeft / 60
seconds := pomodoro.TimeLeft % 60 seconds := pomodoro.TimeLeft % 60
timerOutput = fmt.Sprintf("⏳ %02d:%02d", minutes, seconds) timerOutput = fmt.Sprintf("⏳ %02d:%02d", minutes, seconds) + "\n"
timerOutput += prg.ViewAs(calc_percentage(pomodoro))
} }
output += fmt.Sprintf("Session: %d/%d\n", output += fmt.Sprintf("Session: %d/%d\n",
pomodoro.Session, pomodoro.Session,
pomodoro.Settings.Sessions, pomodoro.Settings.Sessions,
) )
output += fmt.Sprintf("%s %s\n", modePrefix, pomodoro.Mode) output += fmt.Sprintf("%s %s\n", modePrefix, pomodoro.Mode) + "\n"
output += fmt.Sprintf(timerOutput) output += timerOutput
return output return output
} }

View file

@ -25,7 +25,7 @@ func (a app) View() string {
} }
helpView := a.help.View(a.keys) 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 return body + strings.Repeat("\n", height) + serverStatus + helpView
} }

1
go.mod
View file

@ -15,6 +15,7 @@ require (
require ( require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/harmonica v0.2.0 // indirect
github.com/charmbracelet/lipgloss v1.0.0 // indirect github.com/charmbracelet/lipgloss v1.0.0 // indirect
github.com/charmbracelet/x/ansi v0.4.5 // indirect github.com/charmbracelet/x/ansi v0.4.5 // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect github.com/charmbracelet/x/term v0.2.0 // indirect

2
go.sum
View file

@ -8,6 +8,8 @@ github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQW
github.com/charmbracelet/bubbles v0.20.0/go.mod h1:39slydyswPy+uVOHZ5x/GjwVAFkCsV8IIVy+4MhzwwU= github.com/charmbracelet/bubbles v0.20.0/go.mod h1:39slydyswPy+uVOHZ5x/GjwVAFkCsV8IIVy+4MhzwwU=
github.com/charmbracelet/bubbletea v1.2.1 h1:J041h57zculJKEKf/O2pS4edXGIz+V0YvojvfGXePIk= github.com/charmbracelet/bubbletea v1.2.1 h1:J041h57zculJKEKf/O2pS4edXGIz+V0YvojvfGXePIk=
github.com/charmbracelet/bubbletea v1.2.1/go.mod h1:viLoDL7hG4njLJSKU2gw7kB3LSEmWsrM80rO1dBJWBI= github.com/charmbracelet/bubbletea v1.2.1/go.mod h1:viLoDL7hG4njLJSKU2gw7kB3LSEmWsrM80rO1dBJWBI=
github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/lipgloss v1.0.0 h1:O7VkGDvqEdGi93X+DeqsQ7PKHDgtQfF8j8/O2qFMQNg= github.com/charmbracelet/lipgloss v1.0.0 h1:O7VkGDvqEdGi93X+DeqsQ7PKHDgtQfF8j8/O2qFMQNg=
github.com/charmbracelet/lipgloss v1.0.0/go.mod h1:U5fy9Z+C38obMs+T+tJqst9VGzlOYGj4ri9reL3qUlo= github.com/charmbracelet/lipgloss v1.0.0/go.mod h1:U5fy9Z+C38obMs+T+tJqst9VGzlOYGj4ri9reL3qUlo=
github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM= github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM=