Compare commits

..

No commits in common. "a649a3d9fdc07ba680ab5ea08ae255b00ca4fcf8" and "cc24dd6775cd92574a9a47cceafe49e9edc89407" have entirely different histories.

10 changed files with 108 additions and 142 deletions

View file

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

View file

@ -1,50 +0,0 @@
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
})
}

View file

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

View file

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

View file

@ -1,18 +1,17 @@
package frontend package notifications
import ( import (
"fmt" "fmt"
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
"github.com/fatih/color" "github.com/fatih/color"
"strings" "strings"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
func terminalOutput(pomodoro GoTomato.ServerMessage) { func TerminalOutput() {
var ( var modePrefix string
modePrefix string var timerOutput string
timerOutput string
) pomodoro := &shared.ServerMessage
fmt.Print("\033[H\033[2J") // Clears the screen fmt.Print("\033[H\033[2J") // Clears the screen
@ -32,6 +31,7 @@ func terminalOutput(pomodoro GoTomato.ServerMessage) {
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

7
internal/shared/state.go Normal file
View file

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

View file

@ -3,23 +3,22 @@ package websocket
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"git.smsvc.net/pomodoro/ChronoTomato/internal/notifications"
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
var Done = make(chan struct{}) var Done = make(chan struct{})
func ProcessServerMessages(conn *websocket.Conn, channel chan<- GoTomato.ServerMessage) { func ProcessServerMessages(conn *websocket.Conn) {
var serverMessage GoTomato.ServerMessage
defer close(Done) defer close(Done)
for { for {
_, message, err := conn.ReadMessage() _, message, err := conn.ReadMessage()
if err != nil { if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure) { if websocket.IsCloseError(err, 1000) {
// Ignore normal closure and exit gracefully // Ignore normal closure and exit gracefully
return return
} }
@ -29,12 +28,14 @@ func ProcessServerMessages(conn *websocket.Conn, channel chan<- GoTomato.ServerM
return return
} }
err = json.Unmarshal(message, &serverMessage) err = json.Unmarshal(message, &shared.ServerMessage)
if err != nil { if err != nil {
log.Error("Error unmarshalling!", "reason", err) log.Error("Error unmarshalling!", "reason", err)
continue continue
} }
channel <- serverMessage notifications.DesktopNotifications()
notifications.TerminalOutput()
} }
} }

View file

@ -2,14 +2,14 @@ package websocket
import ( import (
"encoding/json" "encoding/json"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
func sendClientCommand(conn *websocket.Conn, msg GoTomato.ClientCommand) { func sendClientCommand(conn *websocket.Conn, msg models.ClientCommand) {
messageBytes, err := json.Marshal(msg) messageBytes, err := json.Marshal(msg)
if err != nil { if err != nil {
log.Error("Error marshalling!", "reason", err) log.Error("Error marshalling!", "reason", err)
return return
@ -23,7 +23,7 @@ func sendClientCommand(conn *websocket.Conn, msg GoTomato.ClientCommand) {
} }
func SendCmd(conn *websocket.Conn, pwd string, cmd string) { func SendCmd(conn *websocket.Conn, pwd string, cmd string) {
message := GoTomato.ClientCommand{ message := models.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 GoTomato.GoTomatoPomodoroConfig) { func Send_updateSettings(conn *websocket.Conn, pwd string, settings models.GoTomatoPomodoroConfig) {
message := GoTomato.ClientCommand{ message := models.ClientCommand{
Command: "updateSettings", Command: "updateSettings",
Password: pwd, Password: pwd,
PomodoroSettings: settings, PomodoroSettings: settings,

View file

@ -1,9 +1,11 @@
package models package models
import GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" import (
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
type Config struct { type ConfigFile struct {
URL string `yaml:"url"` URL string `yaml:"url"`
Password string `yaml:"password"` Password string `yaml:"password"`
PomodoroConfig GoTomato.GoTomatoPomodoroConfig `yaml:"config"` PomodoroConfig models.GoTomatoPomodoroConfig `yaml:"config"`
} }