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"
|
"atomicgo.dev/cursor"
|
||||||
"flag"
|
"flag"
|
||||||
|
|
||||||
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/frontend"
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
|
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
|
||||||
|
|
||||||
"atomicgo.dev/keyboard"
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
"atomicgo.dev/keyboard/keys"
|
|
||||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
cursor.Hide()
|
cursor.Hide()
|
||||||
defer cursor.Show()
|
defer cursor.Show()
|
||||||
|
|
||||||
|
channel := make(chan GoTomato.ServerMessage, 2)
|
||||||
|
|
||||||
parameter_url := flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)")
|
parameter_url := flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)")
|
||||||
parameter_password := flag.String("password", "", "Control password for pomodoro session (optional)")
|
parameter_password := flag.String("password", "", "Control password for pomodoro session (optional)")
|
||||||
configfile := flag.String("config", "~/.config/ChronoTomato.yml", "path to config file (optional)")
|
configfile := flag.String("config", "~/.config/ChronoTomato.yml", "path to config file (optional)")
|
||||||
|
@ -29,50 +29,14 @@ func Start() {
|
||||||
url = config.URL
|
url = config.URL
|
||||||
}
|
}
|
||||||
|
|
||||||
password := *parameter_password
|
if *parameter_password != "" {
|
||||||
if password == "" {
|
config.Password = *parameter_password
|
||||||
password = config.Password
|
|
||||||
}
|
}
|
||||||
|
|
||||||
conn := websocket.Connect(url)
|
conn := websocket.Connect(url)
|
||||||
|
|
||||||
go websocket.ProcessServerMessages(conn)
|
go websocket.ProcessServerMessages(conn, channel)
|
||||||
|
frontend.Handler(conn, config, channel)
|
||||||
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
|
|
||||||
})
|
|
||||||
|
|
||||||
websocket.Disconnect(conn)
|
websocket.Disconnect(conn)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
package notifications
|
package frontend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
"github.com/gen2brain/beeep"
|
"github.com/gen2brain/beeep"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DesktopNotifications() {
|
func desktopNotifications(pomodoro GoTomato.ServerMessage) {
|
||||||
|
|
||||||
var duration int
|
var duration int
|
||||||
var notification string
|
var notification string
|
||||||
|
|
||||||
pomodoro := &shared.ServerMessage
|
|
||||||
|
|
||||||
mode := pomodoro.Mode
|
mode := pomodoro.Mode
|
||||||
session := pomodoro.Session
|
session := pomodoro.Session
|
||||||
sessions := pomodoro.PomodoroSettings.Sessions
|
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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TerminalOutput() {
|
func terminalOutput(pomodoro GoTomato.ServerMessage) {
|
||||||
var modePrefix string
|
var modePrefix string
|
||||||
var timerOutput string
|
var timerOutput string
|
||||||
|
|
||||||
pomodoro := &shared.ServerMessage
|
|
||||||
|
|
||||||
fmt.Print("\033[H\033[2J") // Clears the screen
|
fmt.Print("\033[H\033[2J") // Clears the screen
|
||||||
|
|
||||||
// header
|
// header
|
||||||
|
@ -31,7 +29,6 @@ func TerminalOutput() {
|
||||||
modePrefix = " "
|
modePrefix = " "
|
||||||
if pomodoro.Paused {
|
if pomodoro.Paused {
|
||||||
modePrefix = " "
|
modePrefix = " "
|
||||||
} else {
|
|
||||||
}
|
}
|
||||||
minutes := pomodoro.TimeLeft / 60
|
minutes := pomodoro.TimeLeft / 60
|
||||||
seconds := pomodoro.TimeLeft % 60
|
seconds := pomodoro.TimeLeft % 60
|
|
@ -1,7 +1,7 @@
|
||||||
package helper
|
package helper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"os"
|
"os"
|
||||||
|
@ -9,8 +9,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseConfig(filename string) models.ConfigFile {
|
func ParseConfig(filename string) ChronoTomato.Config {
|
||||||
var config models.ConfigFile
|
var config ChronoTomato.Config
|
||||||
if strings.HasPrefix(filename, "~/") {
|
if strings.HasPrefix(filename, "~/") {
|
||||||
dirname, _ := os.UserHomeDir()
|
dirname, _ := os.UserHomeDir()
|
||||||
filename = filepath.Join(dirname, filename[2:])
|
filename = filepath.Join(dirname, filename[2:])
|
||||||
|
@ -19,7 +19,7 @@ func ParseConfig(filename string) models.ConfigFile {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Error opening config file!", "reason", err)
|
log.Warn("Error opening config file!", "reason", err)
|
||||||
log.Warn("Using defaults")
|
log.Warn("Using defaults")
|
||||||
return models.ConfigFile{
|
return ChronoTomato.Config{
|
||||||
URL: "ws://localhost:8080/ws",
|
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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/notifications"
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
|
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Done = make(chan struct{})
|
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)
|
defer close(Done)
|
||||||
|
|
||||||
|
@ -28,14 +29,14 @@ func ProcessServerMessages(conn *websocket.Conn) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(message, &shared.ServerMessage)
|
err = json.Unmarshal(message, &serverMessage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error unmarshalling!", "reason", err)
|
log.Error("Error unmarshalling!", "reason", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
notifications.DesktopNotifications()
|
channel <- serverMessage
|
||||||
notifications.TerminalOutput()
|
channel <- serverMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,12 +2,12 @@ package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
"github.com/gorilla/websocket"
|
"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)
|
messageBytes, err := json.Marshal(msg)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -23,7 +23,7 @@ func sendClientCommand(conn *websocket.Conn, msg models.ClientCommand) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendCmd(conn *websocket.Conn, pwd string, cmd string) {
|
func SendCmd(conn *websocket.Conn, pwd string, cmd string) {
|
||||||
message := models.ClientCommand{
|
message := GoTomato.ClientCommand{
|
||||||
Command: cmd,
|
Command: cmd,
|
||||||
Password: pwd,
|
Password: pwd,
|
||||||
}
|
}
|
||||||
|
@ -31,8 +31,8 @@ func SendCmd(conn *websocket.Conn, pwd string, cmd string) {
|
||||||
sendClientCommand(conn, message)
|
sendClientCommand(conn, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Send_updateSettings(conn *websocket.Conn, pwd string, settings models.GoTomatoPomodoroConfig) {
|
func Send_updateSettings(conn *websocket.Conn, pwd string, settings GoTomato.GoTomatoPomodoroConfig) {
|
||||||
message := models.ClientCommand{
|
message := GoTomato.ClientCommand{
|
||||||
Command: "updateSettings",
|
Command: "updateSettings",
|
||||||
Password: pwd,
|
Password: pwd,
|
||||||
PomodoroSettings: settings,
|
PomodoroSettings: settings,
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
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"`
|
URL string `yaml:"url"`
|
||||||
Password string `yaml:"password"`
|
Password string `yaml:"password"`
|
||||||
PomodoroConfig models.GoTomatoPomodoroConfig `yaml:"config"`
|
PomodoroConfig GoTomato.GoTomatoPomodoroConfig `yaml:"config"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue