From a5afcd2507b548acf1c242cfef1c919842692728 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 13 Nov 2024 09:43:58 +0100 Subject: [PATCH] refactor: merge client/helper package to client MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - remove helper package and move functions to client package - merge `terminalOutput()` to `view.go` - move `desktopNotifications()` to `cmd/client` 🤖 --- cmd/client/helper/terminal.go | 71 ------------------------- cmd/client/{helper => }/notification.go | 4 +- cmd/client/view.go | 68 +++++++++++++++++++++-- 3 files changed, 67 insertions(+), 76 deletions(-) delete mode 100644 cmd/client/helper/terminal.go rename cmd/client/{helper => }/notification.go (93%) diff --git a/cmd/client/helper/terminal.go b/cmd/client/helper/terminal.go deleted file mode 100644 index 325db78..0000000 --- a/cmd/client/helper/terminal.go +++ /dev/null @@ -1,71 +0,0 @@ -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(), 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 ( - 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 -} diff --git a/cmd/client/helper/notification.go b/cmd/client/notification.go similarity index 93% rename from cmd/client/helper/notification.go rename to cmd/client/notification.go index 956e6a2..7e5dfd6 100644 --- a/cmd/client/helper/notification.go +++ b/cmd/client/notification.go @@ -1,4 +1,4 @@ -package helper +package client import ( "fmt" @@ -8,7 +8,7 @@ import ( ) // Send desktop notifications on the start of each segment -func DesktopNotifications(pomodoro GoTomato.ServerMessage) { +func desktopNotifications(pomodoro GoTomato.ServerMessage) { var ( duration int notification string diff --git a/cmd/client/view.go b/cmd/client/view.go index c7e9eb7..884ea00 100644 --- a/cmd/client/view.go +++ b/cmd/client/view.go @@ -1,22 +1,25 @@ package client import ( + "fmt" "strings" "github.com/alecthomas/colour" - "git.smsvc.net/pomodoro/ChronoTomato/cmd/client/helper" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" + "github.com/charmbracelet/bubbles/progress" ) +var progressBar = progress.New(progress.WithDefaultGradient(), progress.WithoutPercentage()) + func (a app) View() string { var body string var serverStatus string body = "Waiting for server message..." if a.pomodoro != (GoTomato.ServerMessage{}) { - body = helper.TerminalOutput(a.pomodoro) - helper.DesktopNotifications(a.pomodoro) + body = terminalOutput(a.pomodoro) + desktopNotifications(a.pomodoro) } serverStatus = colour.Sprintf("^D^1disconnected: %v^R\n", client.LastErr) @@ -29,3 +32,62 @@ func (a app) View() string { return body + strings.Repeat("\n", height) + serverStatus + helpView } + +// 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 +} + +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) +}