feat: use bubbletea framework

- implement TUI in bubbletea
  - split components into separate files
- remove now unused functions
- restructure files
This commit is contained in:
Sebastian Mark 2024-10-31 07:37:50 +01:00
parent 9656db5335
commit 8a0ef32c91
14 changed files with 269 additions and 187 deletions

View file

@ -1,40 +0,0 @@
package frontend
import (
"github.com/eiannone/keyboard"
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
// sends start/pause/resume based on the state of the pomodoro
func start_pause_resume(message GoTomato.ServerMessage) string {
if !message.Ongoing {
return "start"
}
if message.Paused {
return "resume"
} else {
return "pause"
}
}
// reads a KeyEvent and sends the matching command
func menuHandler(key keyboard.KeyEvent, client websocket.Client, config ChronoTomato.Config, message GoTomato.ServerMessage) bool {
switch key.Rune {
case 0: // space
cmd := start_pause_resume(message)
client.SendCmd(cmd)
case 115: // s
client.SendCmd("stop")
case 114: // r
if config.PomodoroConfig != (GoTomato.PomodoroConfig{}) {
client.SendSettingsUpdate(config.PomodoroConfig)
}
case 113: // q
return false
}
return true
}

View file

@ -1,36 +0,0 @@
package frontend
import (
"github.com/eiannone/keyboard"
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
// Update the terminal and send desktop notifications until the websocket if closed or "quit"
func UpdateLoop(client websocket.Client, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) {
var message GoTomato.ServerMessage
// listen for key events
keysEvents, _ := keyboard.GetKeys(1)
defer keyboard.Close()
for {
select {
case message = <-channel:
// for every received message
desktopNotifications(message)
terminalOutput(message)
case keypress := <-keysEvents:
// react to key pressed
if !menuHandler(keypress, client, config, message) {
return
}
case <-websocket.Done:
// connection closed
return
}
}
}

View file

@ -1,39 +0,0 @@
package frontend
import (
"fmt"
"github.com/gen2brain/beeep"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
// Send desktop notifications on the start of each segment
func desktopNotifications(pomodoro GoTomato.ServerMessage) {
var (
duration int
notification string
)
mode := pomodoro.Mode
session := pomodoro.Session
sessions := pomodoro.Settings.Sessions
switch mode {
case "Work":
duration = pomodoro.Settings.Work
notification = fmt.Sprintf("Session %d/%d: %s %0.f minutes", session, sessions, mode, float32(duration)/60)
case "ShortBreak":
duration = pomodoro.Settings.ShortBreak
notification = fmt.Sprintf("Session %d/%d: Take a %0.f minute break", session, sessions, float32(duration)/60)
case "LongBreak":
duration = pomodoro.Settings.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, "")
}
}

View file

@ -1,54 +0,0 @@
package frontend
import (
"fmt"
"github.com/fatih/color"
"strings"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
// Update the terminal output based on the passed ServerMessage
func terminalOutput(pomodoro GoTomato.ServerMessage) {
var (
modePrefix string
timerOutput string
)
// Clear the screen
fmt.Print("\033[H\033[2J")
// header
color.Blue("Work: %d ◊ Break: %d ◊ Longbreak: %d\n\n",
pomodoro.Settings.Work/60,
pomodoro.Settings.ShortBreak/60,
pomodoro.Settings.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.Settings.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")
}

View file

@ -10,7 +10,7 @@ import (
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
)
// Expands the `~` in a passed filename
// Expands the "~" in a passed filename
func expandUnixPath(filename string) string {
if strings.HasPrefix(filename, "~/") {
dirname, _ := os.UserHomeDir()

View file

@ -12,13 +12,14 @@ type Client ChronoTomato.GoTomatoClient // New websocket client
// Connects to websocket
func Connect(url string) Client {
log.Info("Connected 󰖟 ", "host", url)
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
log.Fatal("Dial error!", "reason", err)
}
log.Info("Connected 󰖟 ", "host", url)
time.Sleep(time.Second)
return Client{Conn: conn}
}

View file

@ -2,7 +2,6 @@ package websocket
import (
"encoding/json"
"fmt"
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
@ -11,7 +10,8 @@ import (
var Done = make(chan struct{})
// Receives websocket messages and write them to `channel`
// 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
@ -25,8 +25,8 @@ func (c Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
return
}
// Log any other errors
fmt.Println()
log.Error("Read error!", "reason", err)
close(channel)
return
}