1
1
package cli
2
2
3
3
import (
4
+ "errors"
5
+ "strconv"
4
6
"strings"
7
+ "time"
5
8
6
9
"github.com/charmbracelet/bubbles/textarea"
10
+ "github.com/charmbracelet/bubbles/textinput"
7
11
tea "github.com/charmbracelet/bubbletea"
8
12
"github.com/plutov/ultrafocus/hosts"
9
13
)
10
14
15
+ type TickMsg time.Time
16
+
11
17
type sessionState uint
12
18
19
+ func doTick () tea.Cmd {
20
+ return tea .Tick (time .Second , func (t time.Time ) tea.Msg {
21
+ return TickMsg (t )
22
+ })
23
+ }
24
+
13
25
const (
14
26
menuView sessionState = iota
15
27
blacklistView
28
+ timerView
16
29
)
17
30
18
31
type model struct {
19
32
textarea textarea.Model
33
+ textinput textinput.Model
20
34
fatalErr error
21
35
status hosts.FocusStatus
22
36
domains []string
23
37
commandsListSelection int
38
+ minutesLeft int
24
39
state sessionState
25
40
}
26
41
@@ -32,11 +47,12 @@ func NewModel() model {
32
47
}
33
48
34
49
return model {
35
- textarea : GetTextareModel (),
36
- domains : domains ,
37
- state : menuView ,
38
- status : status ,
39
- fatalErr : err ,
50
+ textarea : GetTextareaModel (),
51
+ textinput : GetInputModel (),
52
+ domains : domains ,
53
+ state : menuView ,
54
+ status : status ,
55
+ fatalErr : err ,
40
56
}
41
57
}
42
58
@@ -45,15 +61,15 @@ func (m model) Init() tea.Cmd {
45
61
return tea .Quit
46
62
}
47
63
48
- return nil
64
+ return doTick ()
49
65
}
50
66
51
67
func (m * model ) getCommandsList () []command {
52
68
if m .status == hosts .FocusStatusOn {
53
69
return []command {commandFocusOff , commandConfigureBlacklist }
54
70
}
55
71
56
- return []command {commandFocusOn , commandConfigureBlacklist }
72
+ return []command {commandFocusOn , commandFocusOnWithTimer , commandConfigureBlacklist }
57
73
}
58
74
59
75
func (m model ) Update (msg tea.Msg ) (tea.Model , tea.Cmd ) {
@@ -63,8 +79,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
63
79
m .textarea , cmd = m .textarea .Update (msg )
64
80
cmds = append (cmds , cmd )
65
81
82
+ m .textinput , cmd = m .textinput .Update (msg )
83
+ cmds = append (cmds , cmd )
84
+
66
85
switch msg := msg .(type ) {
67
86
87
+ case TickMsg :
88
+ if m .status == hosts .FocusStatusOn && m .minutesLeft > 0 {
89
+ m .minutesLeft --
90
+ if m .minutesLeft == 0 {
91
+ m = commandFocusOff .Run (m )
92
+ }
93
+ }
94
+
95
+ return m , doTick ()
68
96
case tea.KeyMsg :
69
97
commands := m .getCommandsList ()
70
98
switch msg .String () {
@@ -103,6 +131,21 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
103
131
m .state = menuView
104
132
m .textarea .Blur ()
105
133
}
134
+ if m .state == timerView {
135
+ minutesStr := m .textinput .Value ()
136
+ minutes , err := strconv .Atoi (minutesStr )
137
+ if err != nil || minutes <= 0 {
138
+ m .fatalErr = errors .New ("Invalid number of minutes" )
139
+ return m , tea .Quit
140
+ }
141
+
142
+ m = commandFocusOn .Run (m )
143
+
144
+ m .minutesLeft = minutes
145
+ m .commandsListSelection = 0
146
+ m .state = menuView
147
+ m .textinput .Blur ()
148
+ }
106
149
}
107
150
}
108
151
0 commit comments