feat: simplify command handling for start, pause, and resume
- extract command logic into a separate function
- reduce code duplication in keyhandler function
🤖
This commit is contained in:
parent
e0000382e9
commit
c764deeeb7
1 changed files with 13 additions and 9 deletions
|
@ -11,6 +11,17 @@ import (
|
||||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/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) {
|
func keyhandler(conn *ws.Conn, config ChronoTomato.Config, message *GoTomato.ServerMessage, quit chan bool) {
|
||||||
keyboard.Listen(func(key keys.Key) (stop bool, err error) {
|
keyboard.Listen(func(key keys.Key) (stop bool, err error) {
|
||||||
select {
|
select {
|
||||||
|
@ -20,15 +31,8 @@ func keyhandler(conn *ws.Conn, config ChronoTomato.Config, message *GoTomato.Ser
|
||||||
default:
|
default:
|
||||||
switch key.String() {
|
switch key.String() {
|
||||||
case "space":
|
case "space":
|
||||||
if !message.Ongoing {
|
cmd := start_pause_resume(message)
|
||||||
websocket.SendCmd(conn, config.Password, "start")
|
websocket.SendCmd(conn, config.Password, cmd)
|
||||||
break
|
|
||||||
}
|
|
||||||
if message.Paused {
|
|
||||||
websocket.SendCmd(conn, config.Password, "resume")
|
|
||||||
} else {
|
|
||||||
websocket.SendCmd(conn, config.Password, "pause")
|
|
||||||
}
|
|
||||||
case "s":
|
case "s":
|
||||||
websocket.SendCmd(conn, config.Password, "stop")
|
websocket.SendCmd(conn, config.Password, "stop")
|
||||||
case "r":
|
case "r":
|
||||||
|
|
Loading…
Reference in a new issue