Skip to content

Commit

Permalink
fix: fixed a bug regarding sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
Odas0R committed Nov 14, 2024
1 parent efd61cb commit 6383d67
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/charmbracelet/x/term v0.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down
12 changes: 6 additions & 6 deletions pomo.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ var App = &cli.App{
}

var session Session
if err := session.Current(); err != nil {
if err := session.Get(); err != nil {
return err
}

if session.isRunning() {
if session.Type == WorkSession {
ok := InputConfirm("[WARNING]: A session is already running, do you want to reset it?")
if ok {
if err := session.Remove(); err != nil {
if err := session.Delete(); err != nil {
return err
}
} else {
Expand Down Expand Up @@ -152,15 +152,15 @@ var App = &cli.App{
}

var session Session
if err := session.Current(); err != nil {
if err := session.Get(); err != nil {
return err
}

if session.isRunning() {
if session.Type == BreakSession {
ok := InputConfirm("[WARNING]: A session is already running, do you want to reset it?")
if ok {
if err := session.Remove(); err != nil {
if err := session.Delete(); err != nil {
return err
}
} else {
Expand Down Expand Up @@ -196,7 +196,7 @@ var App = &cli.App{
Usage: "stop the pomodoro countdown",
Action: func(_ *cli.Context) error {
var session Session
if err := session.Current(); err != nil {
if err := session.Get(); err != nil {
return err
}

Expand All @@ -216,7 +216,7 @@ var App = &cli.App{
Usage: "print current to standard output",
Action: func(_ *cli.Context) error {
var session Session
if err := session.Current(); err != nil {
if err := session.Get(); err != nil {
return err
}

Expand Down
35 changes: 26 additions & 9 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"path/filepath"
"strings"
"time"

"github.com/google/uuid"
)

// create a enum for the session type
Expand All @@ -18,6 +20,7 @@ const (
)

type Session struct {
ID uuid.UUID
StartTime time.Time
EndTime time.Time
Duration time.Duration
Expand All @@ -36,6 +39,7 @@ func (s *Session) Elapsed() time.Duration {
}

func (s *Session) Start(conf Conf, dur time.Duration, mode SessionType) error {
s.ID = uuid.New()
s.StartTime = time.Now()
s.Duration = dur
s.EndTime = time.Time{} // empty time
Expand Down Expand Up @@ -70,7 +74,7 @@ func (s *Session) Start(conf Conf, dur time.Duration, mode SessionType) error {
return nil
}

func (s *Session) Current() error {
func (s *Session) Get() error {
sessionPath, err := sessionPath()
if err != nil {
return err
Expand All @@ -96,7 +100,8 @@ func (s *Session) Current() error {

func (s *Session) String() string {
return fmt.Sprintf(
"type=%s start=%s end=%s duration=%s | %s",
"id=%s type=%s start=%s end=%s duration=%s | %s",
s.ID,
s.Type,
s.StartTime.Format(time.RFC3339),
s.EndTime.Format(time.RFC3339),
Expand All @@ -120,6 +125,8 @@ func (s *Session) Scan(line string) error {
}
key, value := keyValue[0], keyValue[1]
switch key {
case "id":
s.ID = uuid.MustParse(value)
case "type":
s.Type = SessionType(value)
case "duration":
Expand All @@ -135,11 +142,15 @@ func (s *Session) Scan(line string) error {
}
s.StartTime = t
case "end":
t, err := time.Parse(time.RFC3339, value)
if err != nil {
return err
if value == "" {
s.EndTime = time.Time{}
} else {
t, err := time.Parse(time.RFC3339, value)
if err != nil {
return err
}
s.EndTime = t
}
s.EndTime = t
}
}
s.File = parts[1]
Expand All @@ -161,7 +172,7 @@ func (s *Session) Save() error {
found := false

for i, line := range lines {
start := fmt.Sprintf("start=%s", s.StartTime.Format(time.RFC3339))
start := fmt.Sprintf("id=%s", s.ID)
if strings.Contains(line, start) {
lines[i] = s.String()
found = true
Expand All @@ -181,7 +192,13 @@ func (s *Session) Stop() error {
return s.Save()
}

func (s *Session) Remove() error {
func (s *Session) Reset() error {
s.StartTime = time.Now()
s.EndTime = time.Time{}
return s.Save()
}

func (s *Session) Delete() error {
sessionPath, err := sessionPath()
if err != nil {
return err
Expand All @@ -197,7 +214,7 @@ func (s *Session) Remove() error {
}

for i, line := range lines {
start := fmt.Sprintf("start=%s", s.StartTime)
start := fmt.Sprintf("id=%s", s.ID)
if strings.Contains(line, start) {
lines = append(lines[:i], lines[i+1:]...)
break
Expand Down
16 changes: 2 additions & 14 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (m model) Init() tea.Cmd {
return tick()
}


func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
Expand Down Expand Up @@ -128,27 +127,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tick()

case "r": // Reset current timer
// Stop current session
if err := m.session.Stop(); err != nil {
if err := m.session.Reset(); err != nil {
fmt.Printf("Failed to stop session: %v\n", err)
}
// Start a new session with same duration and type
var newSession Session
if err := newSession.Start(conf, m.session.Duration, m.session.Type); err != nil {
fmt.Printf("Failed to reset session: %v\n", err)
}
m.session = newSession
m.notified = false
}
return m, tick()

case time.Time:
remaining := m.session.Elapsed()
if !m.notified && remaining <= 0 && m.session.isRunning() {
if err := m.session.Stop(); err != nil {
fmt.Printf("Failed to stop session: %v\n", err)
}

title := "Pomo Timer"
message := "Time to take a break!"
if m.session.Type == BreakSession {
Expand Down Expand Up @@ -228,7 +216,7 @@ func (m model) View() string {

func StartUI() error {
var session Session
if err := session.Current(); err != nil {
if err := session.Get(); err != nil {
return fmt.Errorf("failed to get current session: %v", err)
}

Expand Down

0 comments on commit 6383d67

Please sign in to comment.