-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmodel.go
153 lines (125 loc) · 3.11 KB
/
model.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package cli
import (
"errors"
"strconv"
"strings"
"time"
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/plutov/ultrafocus/hosts"
)
type TickMsg time.Time
type sessionState uint
func doTick() tea.Cmd {
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
return TickMsg(t)
})
}
const (
menuView sessionState = iota
blacklistView
timerView
)
type model struct {
textarea textarea.Model
textinput textinput.Model
fatalErr error
status hosts.FocusStatus
domains []string
commandsListSelection int
minutesLeft int
state sessionState
}
func NewModel() model {
domains, status, err := hosts.ExtractDomainsFromHostsFile()
if len(domains) == 0 {
domains = hosts.DefaultDomains
}
return model{
textarea: GetTextareaModel(),
textinput: GetInputModel(),
domains: domains,
state: menuView,
status: status,
fatalErr: err,
}
}
func (m model) Init() tea.Cmd {
if m.fatalErr != nil {
return tea.Quit
}
return doTick()
}
func (m *model) getCommandsList() []command {
if m.status == hosts.FocusStatusOn {
return []command{commandFocusOff, commandConfigureBlacklist}
}
return []command{commandFocusOn, commandFocusOnWithTimer, commandConfigureBlacklist}
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
var cmd tea.Cmd
m.textarea, cmd = m.textarea.Update(msg)
cmds = append(cmds, cmd)
m.textinput, cmd = m.textinput.Update(msg)
cmds = append(cmds, cmd)
switch msg := msg.(type) {
case TickMsg:
if m.status == hosts.FocusStatusOn && m.minutesLeft > 0 {
m.minutesLeft--
if m.minutesLeft == 0 {
m = commandFocusOff.Run(m)
}
}
return m, doTick()
case tea.KeyMsg:
commands := m.getCommandsList()
switch msg.String() {
case "up", "k":
if m.state == menuView && m.commandsListSelection > 0 {
m.commandsListSelection--
}
case "down", "j":
if m.state == menuView && m.commandsListSelection < len(commands)-1 {
m.commandsListSelection++
}
case "enter", " ":
if m.state == menuView {
m = commands[m.commandsListSelection].Run(m)
if m.fatalErr != nil {
return m, tea.Quit
}
}
case "ctrl+c", "q":
return m, tea.Quit
case "esc":
if m.state == blacklistView {
domains := strings.Split(m.textarea.Value(), "\n")
domains = hosts.CleanDomainsList(domains)
if err := hosts.WriteDomainsToHostsFile(domains, m.status); err != nil {
m.fatalErr = err
return m, tea.Quit
}
m.commandsListSelection = 0
m.domains = domains
m.state = menuView
m.textarea.Blur()
}
if m.state == timerView {
minutesStr := m.textinput.Value()
minutes, err := strconv.Atoi(minutesStr)
if err != nil || minutes <= 0 {
m.fatalErr = errors.New("Invalid number of minutes")
return m, tea.Quit
}
m = commandFocusOn.Run(m)
m.minutesLeft = minutes
m.commandsListSelection = 0
m.state = menuView
m.textinput.Blur()
}
}
}
return m, tea.Batch(cmds...)
}