mirror of
https://codeberg.org/Kankys/doxa.git
synced 2026-11-11 06:44:36 +01:00
87 lines
2.0 KiB
Go
87 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
// TimerStats holds persistent statistics and settings
|
|
type TimerStats struct {
|
|
TotalPomodoros int `json:"total_pomodoros"`
|
|
TodayPomodoros int `json:"today_pomodoros"`
|
|
LastDate string `json:"last_date"`
|
|
FocusDuration int `json:"focus_duration"` // minutes
|
|
ShortBreak int `json:"short_break"` // minutes
|
|
LongBreak int `json:"long_break"` // minutes
|
|
LongBreakAfter int `json:"long_break_after"` // after how many pomodoros
|
|
}
|
|
|
|
func getStoragePath() string {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "doxa_timer.json"
|
|
}
|
|
configDir := filepath.Join(home, ".doxa-timer")
|
|
if _, err := os.Stat(configDir); os.IsNotExist(err) {
|
|
os.MkdirAll(configDir, 0755)
|
|
}
|
|
return filepath.Join(configDir, "doxa_timer.json")
|
|
}
|
|
|
|
func loadStats() TimerStats {
|
|
path := getStoragePath()
|
|
file, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return defaultStats()
|
|
}
|
|
var stats TimerStats
|
|
if err := json.Unmarshal(file, &stats); err != nil {
|
|
return defaultStats()
|
|
}
|
|
// Reset today's count if it's a new day
|
|
today := time.Now().Format("2006-01-02")
|
|
if stats.LastDate != today {
|
|
stats.TodayPomodoros = 0
|
|
stats.LastDate = today
|
|
}
|
|
// Ensure sane defaults if values are zero
|
|
if stats.FocusDuration == 0 {
|
|
stats.FocusDuration = 25
|
|
}
|
|
if stats.ShortBreak == 0 {
|
|
stats.ShortBreak = 5
|
|
}
|
|
if stats.LongBreak == 0 {
|
|
stats.LongBreak = 15
|
|
}
|
|
if stats.LongBreakAfter == 0 {
|
|
stats.LongBreakAfter = 4
|
|
}
|
|
return stats
|
|
}
|
|
|
|
func saveStats(stats TimerStats) {
|
|
path := getStoragePath()
|
|
data, err := json.MarshalIndent(stats, "", " ")
|
|
if err != nil {
|
|
fmt.Println("Chyba při ukládání dat:", err)
|
|
return
|
|
}
|
|
os.WriteFile(path, data, 0644)
|
|
}
|
|
|
|
func defaultStats() TimerStats {
|
|
return TimerStats{
|
|
TotalPomodoros: 0,
|
|
TodayPomodoros: 0,
|
|
LastDate: time.Now().Format("2006-01-02"),
|
|
FocusDuration: 25,
|
|
ShortBreak: 5,
|
|
LongBreak: 15,
|
|
LongBreakAfter: 4,
|
|
}
|
|
}
|