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
36
internal/frontend/desktop.go
Normal file
36
internal/frontend/desktop.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package frontend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"github.com/gen2brain/beeep"
|
||||
)
|
||||
|
||||
func desktopNotifications(pomodoro GoTomato.ServerMessage) {
|
||||
|
||||
var duration int
|
||||
var notification string
|
||||
|
||||
mode := pomodoro.Mode
|
||||
session := pomodoro.Session
|
||||
sessions := pomodoro.PomodoroSettings.Sessions
|
||||
|
||||
switch mode {
|
||||
case "Work":
|
||||
duration = pomodoro.PomodoroSettings.Work
|
||||
notification = fmt.Sprintf("Session %d/%d: %s %0.f minutes", session, sessions, mode, float32(duration)/60)
|
||||
case "ShortBreak":
|
||||
duration = pomodoro.PomodoroSettings.ShortBreak
|
||||
notification = fmt.Sprintf("Session %d/%d: Take a %0.f minute break", session, sessions, float32(duration)/60)
|
||||
case "LongBreak":
|
||||
duration = pomodoro.PomodoroSettings.LongBreak
|
||||
notification = fmt.Sprintf("Long Break: Take a %0.f minute break", float32(duration)/60)
|
||||
case "End":
|
||||
duration = 0
|
||||
notification = fmt.Sprintf("Pomodoro sessions complete! Great job! 🎉")
|
||||
}
|
||||
|
||||
if pomodoro.TimeLeft == duration { // start of segment
|
||||
beeep.Alert("🍅 Pomodoro Timer", notification, "")
|
||||
}
|
||||
}
|
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
|
||||
}
|
||||
}
|
||||
}
|
49
internal/frontend/terminal.go
Normal file
49
internal/frontend/terminal.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package frontend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"github.com/fatih/color"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func terminalOutput(pomodoro GoTomato.ServerMessage) {
|
||||
var modePrefix string
|
||||
var timerOutput string
|
||||
|
||||
fmt.Print("\033[H\033[2J") // Clears the screen
|
||||
|
||||
// header
|
||||
color.Blue("Work: %d ◊ Break: %d ◊ Longbreak: %d\n\n",
|
||||
pomodoro.PomodoroSettings.Work/60,
|
||||
pomodoro.PomodoroSettings.ShortBreak/60,
|
||||
pomodoro.PomodoroSettings.LongBreak/60,
|
||||
)
|
||||
|
||||
//body
|
||||
switch pomodoro.Mode {
|
||||
case "Idle":
|
||||
modePrefix = " "
|
||||
timerOutput = ""
|
||||
default:
|
||||
modePrefix = " "
|
||||
if pomodoro.Paused {
|
||||
modePrefix = " "
|
||||
}
|
||||
minutes := pomodoro.TimeLeft / 60
|
||||
seconds := pomodoro.TimeLeft % 60
|
||||
|
||||
timerOutput = fmt.Sprintf("⏳ %02d:%02d", minutes, seconds)
|
||||
}
|
||||
|
||||
fmt.Printf("Session: %d/%d\n",
|
||||
pomodoro.Session,
|
||||
pomodoro.PomodoroSettings.Sessions,
|
||||
)
|
||||
fmt.Printf("%s %s\n", modePrefix, pomodoro.Mode)
|
||||
fmt.Printf(timerOutput)
|
||||
|
||||
//footer
|
||||
fmt.Printf(strings.Repeat("\n", 3))
|
||||
color.White("space: start/pause/resume • s: stop • r: reset pomodoro • q: quit")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue