| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"log" |
| 5 |
"strings" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/charmbracelet/bubbles/progress" |
| 9 |
tea "github.com/charmbracelet/bubbletea" |
| 10 |
"github.com/charmbracelet/huh" |
| 11 |
"github.com/charmbracelet/lipgloss" |
| 12 |
) |
| 13 |
|
| 14 |
const ( |
| 15 |
focusColor = "#2EF8BB" |
| 16 |
breakColor = "#FF5F87" |
| 17 |
) |
| 18 |
|
| 19 |
var ( |
| 20 |
focusTitleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(focusColor)).MarginRight(1).SetString("Focus Mode") |
| 21 |
breakTitleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(breakColor)).MarginRight(1).SetString("Break Mode") |
| 22 |
pausedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(breakColor)).MarginRight(1).SetString("Continue?") |
| 23 |
helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240")).MarginTop(2) |
| 24 |
sidebarStyle = lipgloss.NewStyle().MarginLeft(3).Padding(1, 3).Border(lipgloss.RoundedBorder()).BorderForeground(helpStyle.GetForeground()) |
| 25 |
) |
| 26 |
|
| 27 |
var baseTimerStyle = lipgloss.NewStyle().Padding(1, 2) |
| 28 |
|
| 29 |
type mode int |
| 30 |
|
| 31 |
const ( |
| 32 |
Initial mode = iota |
| 33 |
Focusing |
| 34 |
Paused |
| 35 |
Breaking |
| 36 |
) |
| 37 |
|
| 38 |
type Model struct { |
| 39 |
form *huh.Form |
| 40 |
quitting bool |
| 41 |
|
| 42 |
lastTick time.Time |
| 43 |
startTime time.Time |
| 44 |
|
| 45 |
mode mode |
| 46 |
|
| 47 |
focusTime time.Duration |
| 48 |
breakTime time.Duration |
| 49 |
|
| 50 |
progress progress.Model |
| 51 |
} |
| 52 |
|
| 53 |
func (m Model) Init() tea.Cmd { |
| 54 |
return m.form.Init() |
| 55 |
} |
| 56 |
|
| 57 |
const tickInterval = time.Second / 2 |
| 58 |
|
| 59 |
type tickMsg time.Time |
| 60 |
|
| 61 |
func tickCmd(t time.Time) tea.Msg { |
| 62 |
return tickMsg(t) |
| 63 |
} |
| 64 |
|
| 65 |
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 66 |
var cmds []tea.Cmd |
| 67 |
|
| 68 |
switch msg := msg.(type) { |
| 69 |
case tickMsg: |
| 70 |
cmds = append(cmds, tea.Tick(tickInterval, tickCmd)) |
| 71 |
case tea.KeyMsg: |
| 72 |
switch msg.String() { |
| 73 |
case "q": |
| 74 |
switch m.mode { |
| 75 |
case Focusing: |
| 76 |
m.mode = Paused |
| 77 |
m.startTime = time.Now() |
| 78 |
m.progress.FullColor = breakColor |
| 79 |
case Paused: |
| 80 |
m.mode = Breaking |
| 81 |
m.startTime = time.Now() |
| 82 |
case Breaking: |
| 83 |
m.quitting = true |
| 84 |
return m, tea.Quit |
| 85 |
} |
| 86 |
case "ctrl+c": |
| 87 |
m.quitting = true |
| 88 |
return m, tea.Interrupt |
| 89 |
default: |
| 90 |
if m.mode == Paused { |
| 91 |
m.mode = Breaking |
| 92 |
m.startTime = time.Now() |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
f, cmd := m.form.Update(msg) |
| 99 |
m.form = f.(*huh.Form) |
| 100 |
cmds = append(cmds, cmd) |
| 101 |
if m.form.State != huh.StateCompleted { |
| 102 |
return m, tea.Batch(cmds...) |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
if m.startTime.IsZero() { |
| 107 |
m.startTime = time.Now() |
| 108 |
m.focusTime = m.form.Get("focus").(time.Duration) |
| 109 |
m.breakTime = m.form.Get("break").(time.Duration) |
| 110 |
m.mode = Focusing |
| 111 |
cmds = append(cmds, tea.Tick(tickInterval, tickCmd)) |
| 112 |
} |
| 113 |
|
| 114 |
switch m.mode { |
| 115 |
case Focusing: |
| 116 |
if time.Now().After(m.startTime.Add(m.focusTime)) { |
| 117 |
m.mode = Paused |
| 118 |
m.startTime = time.Now() |
| 119 |
m.progress.FullColor = breakColor |
| 120 |
} |
| 121 |
case Breaking: |
| 122 |
if time.Now().After(m.startTime.Add(m.breakTime)) { |
| 123 |
m.quitting = true |
| 124 |
return m, tea.Quit |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return m, tea.Batch(cmds...) |
| 129 |
} |
| 130 |
|
| 131 |
func (m Model) View() string { |
| 132 |
if m.quitting { |
| 133 |
return "" |
| 134 |
} |
| 135 |
|
| 136 |
if m.form.State != huh.StateCompleted { |
| 137 |
return m.form.View() |
| 138 |
} |
| 139 |
|
| 140 |
var s strings.Builder |
| 141 |
|
| 142 |
elapsed := time.Now().Sub(m.startTime) |
| 143 |
var percent float64 |
| 144 |
switch m.mode { |
| 145 |
case Focusing: |
| 146 |
percent = float64(elapsed) / float64(m.focusTime) |
| 147 |
s.WriteString(focusTitleStyle.String()) |
| 148 |
s.WriteString(elapsed.Round(time.Second).String()) |
| 149 |
s.WriteString("\n\n") |
| 150 |
s.WriteString(m.progress.ViewAs(percent)) |
| 151 |
s.WriteString(helpStyle.Render("Press 'q' to skip")) |
| 152 |
case Paused: |
| 153 |
s.WriteString(pausedStyle.String()) |
| 154 |
s.WriteString("\n\nFocus time is done, time to take a break.") |
| 155 |
s.WriteString(helpStyle.Render("press any key to continue.\n")) |
| 156 |
case Breaking: |
| 157 |
percent = float64(elapsed) / float64(m.breakTime) |
| 158 |
s.WriteString(breakTitleStyle.String()) |
| 159 |
s.WriteString(elapsed.Round(time.Second).String()) |
| 160 |
s.WriteString("\n\n") |
| 161 |
s.WriteString(m.progress.ViewAs(percent)) |
| 162 |
s.WriteString(helpStyle.Render("press 'q' to quit")) |
| 163 |
} |
| 164 |
|
| 165 |
return baseTimerStyle.Render(s.String()) |
| 166 |
} |
| 167 |
|
| 168 |
func NewModel() Model { |
| 169 |
theme := huh.ThemeCharm() |
| 170 |
theme.Focused.Base.Border(lipgloss.HiddenBorder()) |
| 171 |
theme.Focused.Title.Foreground(lipgloss.Color(focusColor)) |
| 172 |
theme.Focused.SelectSelector.Foreground(lipgloss.Color(focusColor)) |
| 173 |
theme.Focused.SelectedOption.Foreground(lipgloss.Color("15")) |
| 174 |
theme.Focused.Option.Foreground(lipgloss.Color("7")) |
| 175 |
|
| 176 |
form := huh.NewForm( |
| 177 |
huh.NewGroup( |
| 178 |
huh.NewSelect[time.Duration](). |
| 179 |
Title("Focus Time"). |
| 180 |
Key("focus"). |
| 181 |
Options( |
| 182 |
huh.NewOption("25 minutes", 25*time.Minute), |
| 183 |
huh.NewOption("30 minutes", 30*time.Minute), |
| 184 |
huh.NewOption("45 minutes", 45*time.Minute), |
| 185 |
huh.NewOption("1 hour", time.Hour), |
| 186 |
), |
| 187 |
), |
| 188 |
huh.NewGroup( |
| 189 |
huh.NewSelect[time.Duration](). |
| 190 |
Title("Break Time"). |
| 191 |
Key("break"). |
| 192 |
Options( |
| 193 |
huh.NewOption("5 minutes", 5*time.Minute), |
| 194 |
huh.NewOption("10 minutes", 10*time.Minute), |
| 195 |
huh.NewOption("15 minutes", 15*time.Minute), |
| 196 |
huh.NewOption("20 minutes", 20*time.Minute), |
| 197 |
), |
| 198 |
), |
| 199 |
).WithShowHelp(false).WithTheme(theme) |
| 200 |
|
| 201 |
progress := progress.New() |
| 202 |
progress.FullColor = focusColor |
| 203 |
progress.SetSpringOptions(1, 1) |
| 204 |
|
| 205 |
return Model{ |
| 206 |
form: form, |
| 207 |
progress: progress, |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
func main() { |
| 212 |
m := NewModel() |
| 213 |
mm, err := tea.NewProgram(&m).Run() |
| 214 |
m = mm.(Model) |
| 215 |
if err != nil { |
| 216 |
log.Fatal(err) |
| 217 |
} |
| 218 |
} |