Compare commits
6 commits
36dca57830
...
aaad07bc77
Author | SHA1 | Date | |
---|---|---|---|
aaad07bc77 | |||
4985dc207a | |||
809b6e07da | |||
359017968d | |||
aef9bdfff7 | |||
9c4d43a4d3 |
10 changed files with 119 additions and 53 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1,3 @@
|
|||
ChronoTomato
|
||||
|
||||
dist/
|
||||
|
|
40
.goreleaser.yaml
Normal file
40
.goreleaser.yaml
Normal file
|
@ -0,0 +1,40 @@
|
|||
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
|
||||
|
||||
version: 2
|
||||
|
||||
before:
|
||||
hooks:
|
||||
- rm -fr ./dist
|
||||
- go mod tidy
|
||||
|
||||
builds:
|
||||
- goos:
|
||||
- linux
|
||||
goarch:
|
||||
- amd64
|
||||
- arm64
|
||||
env:
|
||||
- CGO_ENABLED=0
|
||||
ldflags:
|
||||
- -s -w -X git.smsvc.net/pomodoro/ChronoTomato/cmd/client.version={{.Version}}
|
||||
|
||||
upx:
|
||||
- enabled: true
|
||||
compress: best
|
||||
lzma: true
|
||||
|
||||
changelog:
|
||||
use: gitea
|
||||
|
||||
archives:
|
||||
- format: binary
|
||||
|
||||
release:
|
||||
gitea:
|
||||
owner: pomodoro
|
||||
name: ChronoTomato
|
||||
mode: replace
|
||||
|
||||
gitea_urls:
|
||||
download: http://git.smsvc.net
|
||||
api: http://git.smsvc.net/api/v1
|
|
@ -2,6 +2,8 @@ package client
|
|||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
|
||||
|
@ -13,6 +15,7 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
version = "devel"
|
||||
config ChronoTomato.Config
|
||||
client websocket.Client
|
||||
)
|
||||
|
@ -49,10 +52,14 @@ func (a app) waitForChannelSignal() tea.Cmd {
|
|||
func (a app) Init() tea.Cmd {
|
||||
client = websocket.Connect(config.URL)
|
||||
client.Password = config.Password
|
||||
if client.LastErr != nil {
|
||||
helper.Logger.Fatal(client.LastErr)
|
||||
}
|
||||
|
||||
go client.ProcessServerMessages(a.channel)
|
||||
|
||||
return tea.Batch(
|
||||
tea.ClearScreen,
|
||||
tea.EnterAltScreen,
|
||||
a.waitForChannelSignal(),
|
||||
)
|
||||
}
|
||||
|
@ -64,9 +71,15 @@ func Start() {
|
|||
parameter_url = flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)")
|
||||
parameter_password = flag.String("password", "", "Control password for pomodoro session")
|
||||
parameter_configfile = flag.String("config", defaultConfigFile, "Path to config file")
|
||||
showVersion = flag.Bool("version", false, "Show Version")
|
||||
)
|
||||
flag.Parse()
|
||||
|
||||
if *showVersion {
|
||||
fmt.Println("ChronoTomato", version)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// read passed config file or try to use default config
|
||||
if *parameter_configfile != defaultConfigFile {
|
||||
config = helper.ParseConfig(*parameter_configfile)
|
||||
|
|
|
@ -3,22 +3,29 @@ package client
|
|||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/colour"
|
||||
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/cmd/client/helper"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
)
|
||||
|
||||
func (a app) View() string {
|
||||
var body string
|
||||
var serverStatus string
|
||||
|
||||
body = "Waiting for server message..."
|
||||
if a.pomodoro != (GoTomato.ServerMessage{}) {
|
||||
body = helper.TerminalOutput(a.pomodoro)
|
||||
helper.DesktopNotifications(a.pomodoro)
|
||||
} else {
|
||||
body = "Waiting for first server message..."
|
||||
}
|
||||
|
||||
serverStatus = colour.Sprintf("^D^1disconnected: %v^R\n", client.LastErr)
|
||||
if client.Connected() {
|
||||
serverStatus = colour.Sprintf("^D^2connected to %s^R\n", client.Server)
|
||||
}
|
||||
|
||||
helpView := a.help.View(a.keys)
|
||||
height := 8 - strings.Count(body, "\n") - strings.Count(helpView, "\n")
|
||||
height := 8 - strings.Count(body, "\n") - strings.Count(serverStatus, "\n") - strings.Count(helpView, "\n")
|
||||
|
||||
return body + strings.Repeat("\n", height) + helpView
|
||||
return body + strings.Repeat("\n", height) + serverStatus + helpView
|
||||
}
|
||||
|
|
8
go.mod
8
go.mod
|
@ -3,10 +3,10 @@ module git.smsvc.net/pomodoro/ChronoTomato
|
|||
go 1.23.1
|
||||
|
||||
require (
|
||||
git.smsvc.net/pomodoro/GoTomato v0.0.8
|
||||
git.smsvc.net/pomodoro/GoTomato v0.2.0
|
||||
github.com/alecthomas/colour v0.1.0
|
||||
github.com/charmbracelet/bubbles v0.20.0
|
||||
github.com/charmbracelet/bubbletea v1.1.2
|
||||
github.com/charmbracelet/bubbletea v1.2.1
|
||||
github.com/charmbracelet/log v0.4.0
|
||||
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
|
@ -15,8 +15,8 @@ require (
|
|||
|
||||
require (
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||
github.com/charmbracelet/lipgloss v0.13.0 // indirect
|
||||
github.com/charmbracelet/x/ansi v0.4.0 // indirect
|
||||
github.com/charmbracelet/lipgloss v1.0.0 // indirect
|
||||
github.com/charmbracelet/x/ansi v0.4.5 // indirect
|
||||
github.com/charmbracelet/x/term v0.2.0 // indirect
|
||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
|
||||
github.com/go-logfmt/logfmt v0.6.0 // indirect
|
||||
|
|
16
go.sum
16
go.sum
|
@ -1,19 +1,19 @@
|
|||
git.smsvc.net/pomodoro/GoTomato v0.0.8 h1:ePD6T14pFtrtXybHypabpDs4MURdOnPkm8nQzzEteVA=
|
||||
git.smsvc.net/pomodoro/GoTomato v0.0.8/go.mod h1:LaKrPdnFB5v4RpltExKasW67TmjJlmhDDvmoEYWv0P4=
|
||||
git.smsvc.net/pomodoro/GoTomato v0.2.0 h1:/Wkft5ESsKNJIKiu6OJ7sKvY8LhW6acpTcMy89DLd0s=
|
||||
git.smsvc.net/pomodoro/GoTomato v0.2.0/go.mod h1:LaKrPdnFB5v4RpltExKasW67TmjJlmhDDvmoEYWv0P4=
|
||||
github.com/alecthomas/colour v0.1.0 h1:nOE9rJm6dsZ66RGWYSFrXw461ZIt9A6+nHgL7FRrDUk=
|
||||
github.com/alecthomas/colour v0.1.0/go.mod h1:QO9JBoKquHd+jz9nshCh40fOfO+JzsoXy8qTHF68zU0=
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
|
||||
github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE=
|
||||
github.com/charmbracelet/bubbles v0.20.0/go.mod h1:39slydyswPy+uVOHZ5x/GjwVAFkCsV8IIVy+4MhzwwU=
|
||||
github.com/charmbracelet/bubbletea v1.1.2 h1:naQXF2laRxyLyil/i7fxdpiz1/k06IKquhm4vBfHsIc=
|
||||
github.com/charmbracelet/bubbletea v1.1.2/go.mod h1:9HIU/hBV24qKjlehyj8z1r/tR9TYTQEag+cWZnuXo8E=
|
||||
github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=
|
||||
github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY=
|
||||
github.com/charmbracelet/bubbletea v1.2.1 h1:J041h57zculJKEKf/O2pS4edXGIz+V0YvojvfGXePIk=
|
||||
github.com/charmbracelet/bubbletea v1.2.1/go.mod h1:viLoDL7hG4njLJSKU2gw7kB3LSEmWsrM80rO1dBJWBI=
|
||||
github.com/charmbracelet/lipgloss v1.0.0 h1:O7VkGDvqEdGi93X+DeqsQ7PKHDgtQfF8j8/O2qFMQNg=
|
||||
github.com/charmbracelet/lipgloss v1.0.0/go.mod h1:U5fy9Z+C38obMs+T+tJqst9VGzlOYGj4ri9reL3qUlo=
|
||||
github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM=
|
||||
github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM=
|
||||
github.com/charmbracelet/x/ansi v0.4.0 h1:NqwHA4B23VwsDn4H3VcNX1W1tOmgnvY1NDx5tOXdnOU=
|
||||
github.com/charmbracelet/x/ansi v0.4.0/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
|
||||
github.com/charmbracelet/x/ansi v0.4.5 h1:LqK4vwBNaXw2AyGIICa5/29Sbdq58GbGdFngSexTdRM=
|
||||
github.com/charmbracelet/x/ansi v0.4.5/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
|
||||
github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0=
|
||||
github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"github.com/gorilla/websocket"
|
||||
"time"
|
||||
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
||||
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||
)
|
||||
|
||||
|
@ -13,30 +12,24 @@ type Client ChronoTomato.GoTomatoClient // New websocket client
|
|||
// Connects to websocket
|
||||
func Connect(url string) Client {
|
||||
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
|
||||
if err != nil {
|
||||
helper.Logger.Fatal("Dial error!", "reason", err)
|
||||
|
||||
return Client{Conn: conn, Server: url, LastErr: err}
|
||||
}
|
||||
|
||||
helper.Logger.Info("Connected ó°–ź ", "host", url)
|
||||
time.Sleep(time.Second)
|
||||
|
||||
return Client{Conn: conn}
|
||||
func (c Client) Connected() bool {
|
||||
return c.Conn != nil
|
||||
}
|
||||
|
||||
// Disconnects from websocket
|
||||
func (c Client) Disconnect() {
|
||||
select {
|
||||
case <-Done:
|
||||
// session closed by remote
|
||||
return
|
||||
default:
|
||||
// Cleanly close the connection by sending a close message and then
|
||||
// waiting (with timeout) for the server to close the connection.
|
||||
if c.Connected() {
|
||||
c.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
||||
select {
|
||||
case <-Done:
|
||||
case <-time.After(time.Second):
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,10 @@ package websocket
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
)
|
||||
|
||||
|
@ -12,30 +13,35 @@ var Done = make(chan struct{})
|
|||
|
||||
// Receives websocket messages and writes them to a channel.
|
||||
// Closes the channel if websocket closes.
|
||||
func (c Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
|
||||
var serverMessage GoTomato.ServerMessage
|
||||
func (c *Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
|
||||
var serverMessage, prevMessage GoTomato.ServerMessage
|
||||
|
||||
defer close(Done)
|
||||
|
||||
for {
|
||||
_, message, err := c.Conn.ReadMessage()
|
||||
if err != nil {
|
||||
// On normal closure exit gracefully
|
||||
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
|
||||
// Ignore normal closure and exit gracefully
|
||||
return
|
||||
}
|
||||
// Log any other errors
|
||||
helper.Logger.Error("Read error!", "reason", err)
|
||||
close(channel)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(message, &serverMessage)
|
||||
if err != nil {
|
||||
helper.Logger.Error("Error unmarshalling!", "reason", err)
|
||||
// Try to reconnect on unexpected disconnect
|
||||
for {
|
||||
channel <- prevMessage // send previous ServerMessage to update view
|
||||
time.Sleep(time.Second)
|
||||
*c = Connect(c.Server)
|
||||
if c.Connected() {
|
||||
break
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
err = json.Unmarshal(message, &serverMessage)
|
||||
c.LastErr = err
|
||||
|
||||
channel <- serverMessage
|
||||
prevMessage = serverMessage
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,27 +4,30 @@ import (
|
|||
"encoding/json"
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
)
|
||||
|
||||
// Sends a message to the clients websocket
|
||||
func sendClientCommand(c Client, msg GoTomato.ClientCommand) {
|
||||
func sendClientCommand(c *Client, msg GoTomato.ClientCommand) {
|
||||
if !c.Connected() {
|
||||
// noop if client is not connected
|
||||
return
|
||||
}
|
||||
messageBytes, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
helper.Logger.Error("Error marshalling!", "reason", err)
|
||||
c.LastErr = err
|
||||
return
|
||||
}
|
||||
|
||||
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
|
||||
if err != nil {
|
||||
helper.Logger.Error("Write error!", "reason", err)
|
||||
c.LastErr = err
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Sends the passed command to the server
|
||||
func (c Client) SendCmd(cmd string) {
|
||||
func (c *Client) SendCmd(cmd string) {
|
||||
message := GoTomato.ClientCommand{
|
||||
Command: cmd,
|
||||
Password: c.Password,
|
||||
|
@ -34,7 +37,7 @@ func (c Client) SendCmd(cmd string) {
|
|||
}
|
||||
|
||||
// Sends the new PomodoroConfig to the server
|
||||
func (c Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
|
||||
func (c *Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
|
||||
message := GoTomato.ClientCommand{
|
||||
Command: "updateSettings",
|
||||
Password: c.Password,
|
||||
|
|
|
@ -4,6 +4,8 @@ import "github.com/gorilla/websocket"
|
|||
|
||||
// Represents a GoTomato client
|
||||
type GoTomatoClient struct {
|
||||
Conn *websocket.Conn // The websocket connection of the client
|
||||
Server string // The GoTomato server
|
||||
Password string // Pomodoro password
|
||||
Conn *websocket.Conn // The websocket connection of the client
|
||||
LastErr error // Last client error
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue