feat: refactor to use local pointer for ServerMessage

- replace `shared.ServerMessage` with local pomodoro pointer
- update conditions to use `pomodoro` instead of `shared.ServerMessage`

🤖
This commit is contained in:
Sebastian Mark 2024-10-23 13:48:16 +02:00
parent a61da7d280
commit 98013780da
3 changed files with 24 additions and 23 deletions

View file

@ -29,15 +29,16 @@ func Start() {
} }
go websocket.ProcessServerMessages(conn) go websocket.ProcessServerMessages(conn)
pomodoro := &shared.ServerMessage
keyboard.Listen(func(key keys.Key) (stop bool, err error) { keyboard.Listen(func(key keys.Key) (stop bool, err error) {
switch key.String() { switch key.String() {
case "space": case "space":
if !shared.ServerMessage.Ongoing { if !pomodoro.Ongoing {
websocket.SendCmd(conn, *password, "start") websocket.SendCmd(conn, *password, "start")
return false, nil return false, nil
} }
if shared.ServerMessage.Paused { if pomodoro.Paused {
websocket.SendCmd(conn, *password, "resume") websocket.SendCmd(conn, *password, "resume")
return false, nil return false, nil
} else { } else {

View file

@ -7,31 +7,31 @@ import (
) )
func DesktopNotifications() { func DesktopNotifications() {
msg := &shared.ServerMessage
var duration int var duration int
var notification string var notification string
mode := msg.Mode pomodoro := &shared.ServerMessage
session := msg.Session
sessions := msg.PomodoroSettings.Sessions
switch msg.Mode { mode := pomodoro.Mode
session := pomodoro.Session
sessions := pomodoro.PomodoroSettings.Sessions
switch pomodoro.Mode {
case "Work": case "Work":
duration = msg.PomodoroSettings.Work duration = pomodoro.PomodoroSettings.Work
notification = fmt.Sprintf("Session %d/%d: %s %0.f minutes", session, sessions, mode, float32(duration)/60) notification = fmt.Sprintf("Session %d/%d: %s %0.f minutes", session, sessions, mode, float32(duration)/60)
case "ShortBreak": case "ShortBreak":
duration = msg.PomodoroSettings.ShortBreak duration = pomodoro.PomodoroSettings.ShortBreak
notification = fmt.Sprintf("Session %d/%d: Take a %0.f minute break", session, sessions, float32(duration)/60) notification = fmt.Sprintf("Session %d/%d: Take a %0.f minute break", session, sessions, float32(duration)/60)
case "LongBreak": case "LongBreak":
duration = msg.PomodoroSettings.LongBreak duration = pomodoro.PomodoroSettings.LongBreak
notification = fmt.Sprintf("Long Break: Take a %0.f minute break", float32(duration)/60) notification = fmt.Sprintf("Long Break: Take a %0.f minute break", float32(duration)/60)
case "End": case "End":
duration = 0 duration = 0
notification = fmt.Sprintf("Pomodoro sessions complete! Great job! 🎉") notification = fmt.Sprintf("Pomodoro sessions complete! Great job! 🎉")
} }
if msg.TimeLeft == duration { // start of segment if pomodoro.TimeLeft == duration { // start of segment
beeep.Alert("🍅 Pomodoro Timer", notification, "") beeep.Alert("🍅 Pomodoro Timer", notification, "")
} }
} }

View file

@ -9,29 +9,29 @@ import (
func TerminalOutput() { func TerminalOutput() {
var timerOutput string var timerOutput string
msg := &shared.ServerMessage pomodoro := &shared.ServerMessage
fmt.Print("\033[H\033[2J") // Clears the screen fmt.Print("\033[H\033[2J") // Clears the screen
fmt.Printf("Work: %d | Break: %d | Longbreak: %d\n", fmt.Printf("Work: %d | Break: %d | Longbreak: %d\n",
msg.PomodoroSettings.Work/60, pomodoro.PomodoroSettings.Work/60,
msg.PomodoroSettings.ShortBreak/60, pomodoro.PomodoroSettings.ShortBreak/60,
msg.PomodoroSettings.LongBreak/60, pomodoro.PomodoroSettings.LongBreak/60,
) )
fmt.Printf("Session: %d/%d\n", fmt.Printf("Session: %d/%d\n",
msg.Session, pomodoro.Session,
msg.PomodoroSettings.Sessions, pomodoro.PomodoroSettings.Sessions,
) )
fmt.Printf("\n▶ %s\n", fmt.Printf("▶ %s\n",
msg.Mode, pomodoro.Mode,
) )
switch msg.Mode { switch pomodoro.Mode {
case "Idle": case "Idle":
timerOutput = "" timerOutput = ""
default: default:
minutes := msg.TimeLeft / 60 minutes := pomodoro.TimeLeft / 60
seconds := msg.TimeLeft % 60 seconds := pomodoro.TimeLeft % 60
timerOutput = fmt.Sprintf("⏳ %02d:%02d", minutes, seconds) timerOutput = fmt.Sprintf("⏳ %02d:%02d", minutes, seconds)
} }