ChronoTomato/internal/frontend/keyhandler.go
Sebastian Mark c764deeeb7 feat: simplify command handling for start, pause, and resume
- extract command logic into a separate function
- reduce code duplication in keyhandler function

🤖
2024-10-27 22:33:36 +01:00

50 lines
1.2 KiB
Go

package frontend
import (
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
ws "github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
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 {
case <-websocket.Done:
quit <- true
return true, nil
default:
switch key.String() {
case "space":
cmd := start_pause_resume(message)
websocket.SendCmd(conn, config.Password, cmd)
case "s":
websocket.SendCmd(conn, config.Password, "stop")
case "r":
if config.PomodoroConfig != (GoTomato.GoTomatoPomodoroConfig{}) {
websocket.Send_updateSettings(conn, config.Password, config.PomodoroConfig)
}
case "q":
quit <- true
return true, nil
}
}
return false, nil
})
}