Skip to content

Commit 64ba76c

Browse files
committed
feat: add short session option in confirmation dialog
- add 's' key in confirmation dialog to start a 2min short session - short sessions extend current session without counting as a new session - refactor session start logic to eliminate code duplication Closes #5
1 parent 8a5cab7 commit 64ba76c

File tree

5 files changed

+58
-12
lines changed

5 files changed

+58
-12
lines changed

ui/confirm/confirm.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type ConfirmChoice int
4949
const (
5050
Confirm ConfirmChoice = iota
5151
Cancel
52+
ShortSession
5253
)
5354

5455
type ChoiceMsg struct {
@@ -121,6 +122,9 @@ func (m *Model) HandleKeys(msg tea.KeyMsg) tea.Cmd {
121122
}
122123
return m.Choice(Cancel)
123124

125+
case key.Matches(msg, Keys.ShortSession):
126+
return m.Choice(ShortSession)
127+
124128
case key.Matches(msg, Keys.Quit):
125129
m.quitting = true
126130
return m.Choice(Cancel)

ui/confirm/keys.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ import (
55
)
66

77
type KeyMap struct {
8-
Toggle key.Binding
9-
Confirm key.Binding
10-
Cancel key.Binding
11-
Submit key.Binding
12-
Quit key.Binding
8+
Toggle key.Binding
9+
Confirm key.Binding
10+
Cancel key.Binding
11+
Submit key.Binding
12+
ShortSession key.Binding
13+
Quit key.Binding
1314
}
1415

1516
func (k KeyMap) ShortHelp() []key.Binding {
1617
return []key.Binding{
1718
k.Toggle,
1819
k.Submit,
19-
k.Confirm,
20-
k.Cancel,
20+
k.ShortSession,
21+
k.Quit,
2122
}
2223
}
2324

@@ -42,6 +43,10 @@ var Keys = KeyMap{
4243
key.WithKeys("enter"),
4344
key.WithHelp("enter", "submit"),
4445
),
46+
ShortSession: key.NewBinding(
47+
key.WithKeys("s"),
48+
key.WithHelp("s", "short session"),
49+
),
4550
Quit: key.NewBinding(
4651
key.WithKeys("ctrl+c", "q"),
4752
key.WithHelp("q", "quit"),

ui/handlers.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/Bahaaio/pomo/actions"
8+
"github.com/Bahaaio/pomo/config"
89
"github.com/Bahaaio/pomo/ui/confirm"
910
"github.com/charmbracelet/bubbles/key"
1011
"github.com/charmbracelet/bubbles/progress"
@@ -57,6 +58,8 @@ func (m *Model) handleConfirmChoice(msg confirm.ChoiceMsg) tea.Cmd {
5758
switch msg.Choice {
5859
case confirm.Confirm:
5960
return m.nextSession()
61+
case confirm.ShortSession:
62+
return m.shortSession()
6063
case confirm.Cancel:
6164
return m.Quit()
6265
}
@@ -132,18 +135,36 @@ func (m *Model) handleCompletion() tea.Cmd {
132135
m.recordSession()
133136
actions.RunPostActions(&m.currentTask).Wait()
134137

138+
// show confirmation dialog if configured to do so
135139
if m.shouldAskToContinue {
136140
m.sessionState = ShowingConfirm
137141
return nil
138142
}
139143

144+
// else, quit
140145
return m.Quit()
141146
}
142147

143-
// updates model with next task and starts the timer
148+
// starts session with the opposite task type (work <-> break)
144149
func (m *Model) nextSession() tea.Cmd {
145-
m.currentTaskType = m.currentTaskType.Opposite()
146-
m.currentTask = *m.currentTaskType.GetTask()
150+
nextTaskType := m.currentTaskType.Opposite()
151+
return m.startSession(nextTaskType, *nextTaskType.GetTask(), false)
152+
}
153+
154+
// starts a short session of the current task type
155+
func (m *Model) shortSession() tea.Cmd {
156+
shortTask := m.currentTask
157+
shortTask.Duration = 2 * time.Minute // TODO: make configurable
158+
shortTask.Title = "short " + m.currentTaskType.GetTask().Title
159+
160+
return m.startSession(m.currentTaskType, shortTask, true)
161+
}
162+
163+
// initializes and starts a new session with the given task
164+
func (m *Model) startSession(taskType config.TaskType, task config.Task, isShortSession bool) tea.Cmd {
165+
m.isShortSession = isShortSession
166+
m.currentTaskType = taskType
167+
m.currentTask = task
147168

148169
m.elapsed = 0
149170
m.duration = m.currentTask.Duration
@@ -158,6 +179,12 @@ func (m *Model) nextSession() tea.Cmd {
158179

159180
// records the current session into the session summary
160181
func (m *Model) recordSession() {
182+
// short sessions extend the current session without incrementing the count
183+
if m.isShortSession {
184+
m.sessionSummary.AddDuration(m.currentTaskType, m.elapsed)
185+
return
186+
}
187+
161188
m.sessionSummary.AddSession(m.currentTaskType, m.elapsed)
162189
}
163190

ui/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Model struct {
3232
currentTaskType config.TaskType
3333
currentTask config.Task
3434
sessionSummary summary.SessionSummary
35+
isShortSession bool
3536

3637
// ASCII art
3738
useTimerArt bool

ui/summary/session_summary.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,19 @@ type SessionSummary struct {
2525
func (t *SessionSummary) AddSession(taskType config.TaskType, elapsed time.Duration) {
2626
if taskType == config.WorkTask {
2727
t.totalWorkSessions++
28-
t.totalWorkDuration += elapsed
2928
} else {
3029
t.totalBreakSessions++
31-
t.totalBreakDuration += elapsed
30+
}
31+
32+
t.AddDuration(taskType, elapsed)
33+
}
34+
35+
// AddDuration adds a duration to the summary based on the task type.
36+
func (t *SessionSummary) AddDuration(taskType config.TaskType, duration time.Duration) {
37+
if taskType == config.WorkTask {
38+
t.totalWorkDuration += duration
39+
} else {
40+
t.totalBreakDuration += duration
3241
}
3342
}
3443

0 commit comments

Comments
 (0)