-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (110 loc) · 2.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
"os"
"time"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type tickMsg time.Duration
type model struct {
spinner spinner.Model
timeLeft time.Duration
totalTime time.Duration
mode string
gomos int
quitting bool
}
var quitKeys = key.NewBinding(
key.WithKeys("q", "esc", "ctrl+c"),
key.WithHelp("", "press q to quit"),
)
func initialModel() model {
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
return model{
spinner: s,
timeLeft: 25 * time.Minute,
totalTime: 25 * time.Minute,
mode: "Work",
gomos: 0,
}
}
func (m model) Init() tea.Cmd {
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
return tickMsg(1 * time.Second)
})
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
if key.Matches(msg, quitKeys) {
m.quitting = true
return m, tea.Quit
}
return m, nil
case tickMsg:
m.timeLeft -= time.Duration(msg)
// newPercent := float64(100 - int((m.timeLeft*100)/m.totalTime))
if m.timeLeft <= 0 {
if m.mode == "Work" {
m.gomos++
if m.gomos%4 == 0 {
m.mode = "Long Rest"
m.timeLeft = 15 * time.Minute
m.totalTime = 15 * time.Minute
} else {
m.mode = "Rest"
m.timeLeft = 5 * time.Minute
m.totalTime = 5 * time.Minute
}
} else {
m.mode = "Work"
m.timeLeft = 25 * time.Minute
m.totalTime = 25 * time.Minute
}
}
return m, tea.Tick(time.Second, func(t time.Time) tea.Msg {
return tickMsg(1 * time.Second)
})
default:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
}
func (m model) View() string {
// adding lipgloss styles
modeStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("205")).
Bold(true)
timeStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("42")).
Bold(true)
helpStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("240")).
Faint(true)
spinnerStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("226"))
str := fmt.Sprintf(
"\n\n %s %s %s %s \n\n",
spinnerStyle.Render(m.spinner.View()),
modeStyle.Render(m.mode),
timeStyle.Render(fmt.Sprintf("Time Left: %s", m.timeLeft)),
helpStyle.Render("Press q to quit"),
)
if m.quitting {
return str + "\n"
}
return str
}
func main() {
p := tea.NewProgram(initialModel())
if _, err := p.Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}