Compare commits

...

11 commits

Author SHA1 Message Date
a649a3d9fd refactor: rename desktop.go to notification.go
🤖
2024-10-27 22:49:28 +01:00
c764deeeb7 feat: simplify command handling for start, pause, and resume
- extract command logic into a separate function
- reduce code duplication in keyhandler function

🤖
2024-10-27 22:33:36 +01:00
e0000382e9 fix: move package variable to frontend.Handler 2024-10-27 22:33:36 +01:00
e8b463c59c format: update import statements
- clean up and streamline import statements across multiple files

🤖
2024-10-27 22:33:36 +01:00
f26f12ef98 format: improve variable declaration style
- change variable declarations to use a grouped format

🤖
2024-10-27 22:33:36 +01:00
912dfa62f2 format: improve code structure for better readability
🤖
2024-10-27 22:33:36 +01:00
c2310f7735 refactor: improve cli parameter handling
- streamline `main` functions
- refactor cli parameter handling
2024-10-27 22:33:36 +01:00
34d4206f65 fix: use websocket constant to check IsCloseError 2024-10-27 22:33:36 +01:00
f54e8486f1 break: configure via file OR cli parameters
no more defaults when no parameter passed!
2024-10-27 22:33:36 +01:00
4f9bd664a3 fix: remove ununsed return statements in keyhandler() 2024-10-27 22:33:36 +01:00
bbc9977f1c 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
2024-10-27 22:33:30 +01:00
10 changed files with 142 additions and 108 deletions

View file

@ -4,75 +4,42 @@ 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"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
var (
config ChronoTomato.Config
parameter_url = flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)")
parameter_password = flag.String("password", "", "Control password for pomodoro session")
configfile = flag.String("config", "", "Path to config file")
)
func Start() {
flag.Parse()
cursor.Hide()
defer cursor.Show()
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)")
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)
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
// read cli parameters if no config file passed
if *configfile == "" {
config.URL = *parameter_url
config.Password = *parameter_password
} 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
}
// otherwise read config
config = helper.ParseConfig(*configfile)
}
return false, nil
})
conn := websocket.Connect(config.URL)
channel := make(chan GoTomato.ServerMessage, 2)
go websocket.ProcessServerMessages(conn, channel)
frontend.Handler(conn, config, channel)
websocket.Disconnect(conn)
}

View file

@ -0,0 +1,50 @@
package frontend
import (
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
ws "github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/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) {
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":
cmd := start_pause_resume(message)
websocket.SendCmd(conn, config.Password, cmd)
case "s":
websocket.SendCmd(conn, config.Password, "stop")
case "r":
if config.PomodoroConfig != (GoTomato.GoTomatoPomodoroConfig{}) {
websocket.Send_updateSettings(conn, config.Password, config.PomodoroConfig)
}
case "q":
quit <- true
return true, nil
}
}
return false, nil
})
}

25
internal/frontend/main.go Normal file
View file

@ -0,0 +1,25 @@
package frontend
import (
"github.com/gorilla/websocket"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
func Handler(conn *websocket.Conn, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) {
var message 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
}
}
}

View file

@ -1,16 +1,18 @@
package notifications
package frontend
import (
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
"fmt"
"github.com/gen2brain/beeep"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
func DesktopNotifications() {
var duration int
var notification string
func desktopNotifications(pomodoro GoTomato.ServerMessage) {
pomodoro := &shared.ServerMessage
var (
duration int
notification string
)
mode := pomodoro.Mode
session := pomodoro.Session

View file

@ -1,17 +1,18 @@
package notifications
package frontend
import (
"fmt"
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
"github.com/fatih/color"
"strings"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
func TerminalOutput() {
var modePrefix string
var timerOutput string
pomodoro := &shared.ServerMessage
func terminalOutput(pomodoro GoTomato.ServerMessage) {
var (
modePrefix string
timerOutput string
)
fmt.Print("\033[H\033[2J") // Clears the screen
@ -31,7 +32,6 @@ func TerminalOutput() {
modePrefix = " "
if pomodoro.Paused {
modePrefix = " "
} else {
}
minutes := pomodoro.TimeLeft / 60
seconds := pomodoro.TimeLeft % 60

View file

@ -1,32 +1,32 @@
package helper
import (
"git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
"github.com/charmbracelet/log"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
"strings"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
)
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:])
}
yamlFile, err := os.ReadFile(filename)
if err != nil {
log.Warn("Error opening config file!", "reason", err)
log.Warn("Using defaults")
return models.ConfigFile{
URL: "ws://localhost:8080/ws",
}
log.Fatal("Error opening config file!", "reason", err)
}
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
return config
return config
}

View file

@ -1,7 +0,0 @@
package shared
import (
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
var ServerMessage models.ServerMessage

View file

@ -3,22 +3,23 @@ package websocket
import (
"encoding/json"
"fmt"
"git.smsvc.net/pomodoro/ChronoTomato/internal/notifications"
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
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)
for {
_, message, err := conn.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, 1000) {
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
// Ignore normal closure and exit gracefully
return
}
@ -28,14 +29,12 @@ 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
}
}

View file

@ -2,14 +2,14 @@ package websocket
import (
"encoding/json"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
func sendClientCommand(conn *websocket.Conn, msg models.ClientCommand) {
func sendClientCommand(conn *websocket.Conn, msg GoTomato.ClientCommand) {
messageBytes, err := json.Marshal(msg)
if err != nil {
log.Error("Error marshalling!", "reason", err)
return
@ -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,

View file

@ -1,11 +1,9 @@
package models
import (
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
import 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"`
}