feat: refactor shared.Message to a channel
- remove `shared` package and shared.Message - rename `notifications` package to `frontend` - introduce a channel send the received ServerMessages to the frontend handler(s) - move keyhandler to goroutine - simplify password handling in Start function - update import names when importing from GoTomoto
This commit is contained in:
parent
cc24dd6775
commit
bbc9977f1c
10 changed files with 108 additions and 82 deletions
|
@ -4,19 +4,19 @@ import (
|
|||
"atomicgo.dev/cursor"
|
||||
"flag"
|
||||
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/frontend"
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
|
||||
|
||||
"atomicgo.dev/keyboard"
|
||||
"atomicgo.dev/keyboard/keys"
|
||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
)
|
||||
|
||||
func Start() {
|
||||
cursor.Hide()
|
||||
defer cursor.Show()
|
||||
|
||||
channel := make(chan GoTomato.ServerMessage, 2)
|
||||
|
||||
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)")
|
||||
|
@ -29,50 +29,14 @@ func Start() {
|
|||
url = config.URL
|
||||
}
|
||||
|
||||
password := *parameter_password
|
||||
if password == "" {
|
||||
password = config.Password
|
||||
if *parameter_password != "" {
|
||||
config.Password = *parameter_password
|
||||
}
|
||||
|
||||
conn := websocket.Connect(url)
|
||||
|
||||
go websocket.ProcessServerMessages(conn)
|
||||
|
||||
pomodoro := &shared.ServerMessage
|
||||
keyboard.Listen(func(key keys.Key) (stop bool, err error) {
|
||||
select {
|
||||
case <-websocket.Done:
|
||||
return true, nil
|
||||
default:
|
||||
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 "s":
|
||||
websocket.SendCmd(conn, password, "stop")
|
||||
return false, nil
|
||||
case "r":
|
||||
|
||||
if config.PomodoroConfig != (models.GoTomatoPomodoroConfig{}) {
|
||||
websocket.Send_updateSettings(conn, password, config.PomodoroConfig)
|
||||
}
|
||||
return false, nil
|
||||
case "q":
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
})
|
||||
go websocket.ProcessServerMessages(conn, channel)
|
||||
frontend.Handler(conn, config, channel)
|
||||
|
||||
websocket.Disconnect(conn)
|
||||
}
|
||||
|
|
|
@ -1,17 +1,16 @@
|
|||
package notifications
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
|
||||
"fmt"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"github.com/gen2brain/beeep"
|
||||
)
|
||||
|
||||
func DesktopNotifications() {
|
||||
func desktopNotifications(pomodoro GoTomato.ServerMessage) {
|
||||
|
||||
var duration int
|
||||
var notification string
|
||||
|
||||
pomodoro := &shared.ServerMessage
|
||||
|
||||
mode := pomodoro.Mode
|
||||
session := pomodoro.Session
|
||||
sessions := pomodoro.PomodoroSettings.Sessions
|
48
internal/frontend/keyhandler.go
Normal file
48
internal/frontend/keyhandler.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package frontend
|
||||
|
||||
import (
|
||||
"atomicgo.dev/keyboard"
|
||||
"atomicgo.dev/keyboard/keys"
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
|
||||
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
ws "github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
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":
|
||||
if !message.Ongoing {
|
||||
websocket.SendCmd(conn, config.Password, "start")
|
||||
return false, nil
|
||||
}
|
||||
if message.Paused {
|
||||
websocket.SendCmd(conn, config.Password, "resume")
|
||||
return false, nil
|
||||
} else {
|
||||
websocket.SendCmd(conn, config.Password, "pause")
|
||||
return false, nil
|
||||
}
|
||||
case "s":
|
||||
websocket.SendCmd(conn, config.Password, "stop")
|
||||
return false, nil
|
||||
case "r":
|
||||
if config.PomodoroConfig != (GoTomato.GoTomatoPomodoroConfig{}) {
|
||||
websocket.Send_updateSettings(conn, config.Password, config.PomodoroConfig)
|
||||
}
|
||||
return false, nil
|
||||
case "q":
|
||||
quit <- true
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
})
|
||||
}
|
24
internal/frontend/main.go
Normal file
24
internal/frontend/main.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package frontend
|
||||
|
||||
import (
|
||||
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var message GoTomato.ServerMessage
|
||||
|
||||
func Handler(conn *websocket.Conn, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) {
|
||||
keyhandler_quit := make(chan bool, 1)
|
||||
go keyhandler(conn, config, &message, keyhandler_quit)
|
||||
|
||||
for {
|
||||
select {
|
||||
case message = <-channel:
|
||||
desktopNotifications(message)
|
||||
terminalOutput(message)
|
||||
case <-keyhandler_quit:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,18 +1,16 @@
|
|||
package notifications
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"github.com/fatih/color"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func TerminalOutput() {
|
||||
func terminalOutput(pomodoro GoTomato.ServerMessage) {
|
||||
var modePrefix string
|
||||
var timerOutput string
|
||||
|
||||
pomodoro := &shared.ServerMessage
|
||||
|
||||
fmt.Print("\033[H\033[2J") // Clears the screen
|
||||
|
||||
// header
|
||||
|
@ -31,7 +29,6 @@ func TerminalOutput() {
|
|||
modePrefix = " "
|
||||
if pomodoro.Paused {
|
||||
modePrefix = " "
|
||||
} else {
|
||||
}
|
||||
minutes := pomodoro.TimeLeft / 60
|
||||
seconds := pomodoro.TimeLeft % 60
|
|
@ -1,7 +1,7 @@
|
|||
package helper
|
||||
|
||||
import (
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||
"github.com/charmbracelet/log"
|
||||
"gopkg.in/yaml.v3"
|
||||
"os"
|
||||
|
@ -9,8 +9,8 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
func ParseConfig(filename string) models.ConfigFile {
|
||||
var config models.ConfigFile
|
||||
func ParseConfig(filename string) ChronoTomato.Config {
|
||||
var config ChronoTomato.Config
|
||||
if strings.HasPrefix(filename, "~/") {
|
||||
dirname, _ := os.UserHomeDir()
|
||||
filename = filepath.Join(dirname, filename[2:])
|
||||
|
@ -19,7 +19,7 @@ func ParseConfig(filename string) models.ConfigFile {
|
|||
if err != nil {
|
||||
log.Warn("Error opening config file!", "reason", err)
|
||||
log.Warn("Using defaults")
|
||||
return models.ConfigFile{
|
||||
return ChronoTomato.Config{
|
||||
URL: "ws://localhost:8080/ws",
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
package shared
|
||||
|
||||
import (
|
||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
)
|
||||
|
||||
var ServerMessage models.ServerMessage
|
|
@ -3,15 +3,16 @@ package websocket
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/notifications"
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var Done = make(chan struct{})
|
||||
|
||||
func ProcessServerMessages(conn *websocket.Conn) {
|
||||
func ProcessServerMessages(conn *websocket.Conn, channel chan<- GoTomato.ServerMessage) {
|
||||
|
||||
var serverMessage GoTomato.ServerMessage
|
||||
|
||||
defer close(Done)
|
||||
|
||||
|
@ -28,14 +29,14 @@ func ProcessServerMessages(conn *websocket.Conn) {
|
|||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(message, &shared.ServerMessage)
|
||||
err = json.Unmarshal(message, &serverMessage)
|
||||
if err != nil {
|
||||
log.Error("Error unmarshalling!", "reason", err)
|
||||
continue
|
||||
}
|
||||
|
||||
notifications.DesktopNotifications()
|
||||
notifications.TerminalOutput()
|
||||
channel <- serverMessage
|
||||
channel <- serverMessage
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,12 +2,12 @@ package websocket
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
func sendClientCommand(conn *websocket.Conn, msg models.ClientCommand) {
|
||||
func sendClientCommand(conn *websocket.Conn, msg GoTomato.ClientCommand) {
|
||||
messageBytes, err := json.Marshal(msg)
|
||||
|
||||
if err != nil {
|
||||
|
@ -23,7 +23,7 @@ func sendClientCommand(conn *websocket.Conn, msg models.ClientCommand) {
|
|||
}
|
||||
|
||||
func SendCmd(conn *websocket.Conn, pwd string, cmd string) {
|
||||
message := models.ClientCommand{
|
||||
message := GoTomato.ClientCommand{
|
||||
Command: cmd,
|
||||
Password: pwd,
|
||||
}
|
||||
|
@ -31,8 +31,8 @@ func SendCmd(conn *websocket.Conn, pwd string, cmd string) {
|
|||
sendClientCommand(conn, message)
|
||||
}
|
||||
|
||||
func Send_updateSettings(conn *websocket.Conn, pwd string, settings models.GoTomatoPomodoroConfig) {
|
||||
message := models.ClientCommand{
|
||||
func Send_updateSettings(conn *websocket.Conn, pwd string, settings GoTomato.GoTomatoPomodoroConfig) {
|
||||
message := GoTomato.ClientCommand{
|
||||
Command: "updateSettings",
|
||||
Password: pwd,
|
||||
PomodoroSettings: settings,
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
)
|
||||
|
||||
type ConfigFile struct {
|
||||
type Config struct {
|
||||
URL string `yaml:"url"`
|
||||
Password string `yaml:"password"`
|
||||
PomodoroConfig models.GoTomatoPomodoroConfig `yaml:"config"`
|
||||
PomodoroConfig GoTomato.GoTomatoPomodoroConfig `yaml:"config"`
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue