ChronoTomato/cmd/client/main.go
Sebastian Mark 4dcc984784 feat: add keyboard controls
- add keyboard listener for space, r, and q keys
- create shared state for server messages
- update desktop and terminal notifications to use shared state

🤖
2024-10-23 17:43:47 +02:00

59 lines
1.3 KiB
Go

package client
import (
"flag"
"os"
"os/signal"
"ChronoTomato/internal/shared"
"ChronoTomato/internal/websocket"
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
)
var interrupt = make(chan os.Signal, 1)
func Start() {
signal.Notify(interrupt, os.Interrupt)
GoTomatoUrl := flag.String("url", "ws://localhost:8080/ws", "GoTomato Server URL")
password := flag.String("password", "", "Control password for pomodoro session (optional)")
autoStart := flag.Bool("start", false, "Immediately start a Pomodoro")
flag.Parse()
conn := websocket.Connect(*GoTomatoUrl)
websocket.Send_updateSettings(conn, *password)
if *autoStart {
websocket.SendCmd(conn, "start", *password)
}
go websocket.ProcessServerMessages(conn)
keyboard.Listen(func(key keys.Key) (stop bool, err error) {
switch key.String() {
case "space":
if !shared.ServerMessage.Ongoing {
websocket.SendCmd(conn, "start", *password)
return false, nil
}
if shared.ServerMessage.Paused {
websocket.SendCmd(conn, "resume", *password)
return false, nil
} else {
websocket.SendCmd(conn, "pause", *password)
return false, nil
}
case "r":
websocket.SendCmd(conn, "stop", *password)
return false, nil
case "q":
interrupt <- os.Interrupt
return true, nil
}
return false, nil
})
websocket.WaitForDisconnect(conn, interrupt)
}