From c764deeeb77b16a83d164bf8677b36dc2b989752 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 27 Oct 2024 21:57:05 +0100 Subject: [PATCH] feat: simplify command handling for start, pause, and resume MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - extract command logic into a separate function - reduce code duplication in keyhandler function 🤖 --- internal/frontend/keyhandler.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/internal/frontend/keyhandler.go b/internal/frontend/keyhandler.go index 58aad16..0c0ed45 100644 --- a/internal/frontend/keyhandler.go +++ b/internal/frontend/keyhandler.go @@ -11,6 +11,17 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) +func start_pause_resume(message *GoTomato.ServerMessage) string { + if !message.Ongoing { + return "start" + } + if message.Paused { + return "resume" + } else { + return "pause" + } +} + func keyhandler(conn *ws.Conn, config ChronoTomato.Config, message *GoTomato.ServerMessage, quit chan bool) { keyboard.Listen(func(key keys.Key) (stop bool, err error) { select { @@ -20,15 +31,8 @@ func keyhandler(conn *ws.Conn, config ChronoTomato.Config, message *GoTomato.Ser default: switch key.String() { case "space": - if !message.Ongoing { - websocket.SendCmd(conn, config.Password, "start") - break - } - if message.Paused { - websocket.SendCmd(conn, config.Password, "resume") - } else { - websocket.SendCmd(conn, config.Password, "pause") - } + cmd := start_pause_resume(message) + websocket.SendCmd(conn, config.Password, cmd) case "s": websocket.SendCmd(conn, config.Password, "stop") case "r":