2024-10-22 11:13:41 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
2024-10-23 11:16:30 +00:00
|
|
|
"ChronoTomato/internal/shared"
|
2024-10-22 11:13:41 +00:00
|
|
|
"ChronoTomato/internal/websocket"
|
2024-10-23 11:16:30 +00:00
|
|
|
"atomicgo.dev/keyboard"
|
|
|
|
"atomicgo.dev/keyboard/keys"
|
2024-10-22 11:13:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2024-10-23 11:18:33 +00:00
|
|
|
websocket.SendCmd(conn, *password, "start")
|
2024-10-22 11:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go websocket.ProcessServerMessages(conn)
|
2024-10-23 11:48:16 +00:00
|
|
|
pomodoro := &shared.ServerMessage
|
2024-10-22 11:13:41 +00:00
|
|
|
|
2024-10-23 11:16:30 +00:00
|
|
|
keyboard.Listen(func(key keys.Key) (stop bool, err error) {
|
|
|
|
switch key.String() {
|
|
|
|
case "space":
|
2024-10-23 11:48:16 +00:00
|
|
|
if !pomodoro.Ongoing {
|
2024-10-23 11:18:33 +00:00
|
|
|
websocket.SendCmd(conn, *password, "start")
|
2024-10-23 11:16:30 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2024-10-23 11:48:16 +00:00
|
|
|
if pomodoro.Paused {
|
2024-10-23 11:18:33 +00:00
|
|
|
websocket.SendCmd(conn, *password, "resume")
|
2024-10-23 11:16:30 +00:00
|
|
|
return false, nil
|
|
|
|
} else {
|
2024-10-23 11:18:33 +00:00
|
|
|
websocket.SendCmd(conn, *password, "pause")
|
2024-10-23 11:16:30 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
case "r":
|
2024-10-23 11:18:33 +00:00
|
|
|
websocket.SendCmd(conn, *password, "stop")
|
2024-10-23 11:16:30 +00:00
|
|
|
return false, nil
|
|
|
|
case "q":
|
|
|
|
interrupt <- os.Interrupt
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
|
2024-10-22 11:13:41 +00:00
|
|
|
websocket.WaitForDisconnect(conn, interrupt)
|
|
|
|
}
|