ChronoTomato/cmd/client/main.go

75 lines
1.7 KiB
Go
Raw Normal View History

2024-10-22 11:13:41 +00:00
package client
import (
"flag"
"os"
"os/signal"
"ChronoTomato/internal/helper"
"ChronoTomato/internal/shared"
2024-10-22 11:13:41 +00:00
"ChronoTomato/internal/websocket"
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
2024-10-22 11:13:41 +00:00
)
var interrupt = make(chan os.Signal, 1)
func Start() {
signal.Notify(interrupt, os.Interrupt)
parameter_url := flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)")
parameter_password := flag.String("password", "", "Control password for pomodoro session (optional)")
configfile := flag.String("config", "~/.config/ChronoTomato.yml", "path to config file (optional)")
2024-10-22 11:13:41 +00:00
flag.Parse()
config := helper.ParseConfig(*configfile)
url := *parameter_url
if url == "" {
url = config.URL
}
password := *parameter_password
if password == "" {
password = config.Password
}
conn := websocket.Connect(url)
2024-10-22 11:13:41 +00:00
if config.PomodoroConfig != (models.GoTomatoPomodoroConfig{}) {
websocket.Send_updateSettings(conn, config.Password, config.PomodoroConfig)
2024-10-22 11:13:41 +00:00
}
go websocket.ProcessServerMessages(conn)
pomodoro := &shared.ServerMessage
2024-10-22 11:13:41 +00:00
keyboard.Listen(func(key keys.Key) (stop bool, err error) {
switch key.String() {
case "space":
if !pomodoro.Ongoing {
websocket.SendCmd(conn, password, "start")
return false, nil
}
if pomodoro.Paused {
websocket.SendCmd(conn, password, "resume")
return false, nil
} else {
websocket.SendCmd(conn, password, "pause")
return false, nil
}
case "r":
websocket.SendCmd(conn, password, "stop")
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)
}