-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
346 lines (308 loc) · 7.67 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package main
import (
"fmt"
"os"
"strings"
"wakarizer/wakatime"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
ini "gopkg.in/ini.v1"
)
type Status int
type Msg[T any] struct{ value T }
const (
STARTING Status = iota
GOTTEN_HOME_DIR
ASKING_API_KEY
SETUP_ASKING_LANGUAGE_TYPES
ASKING_LANGUAGE_TYPES
VALIDATE_GETTING_LANGUAGES
START_MAIN_ACTIVITY
INSIDE_MAIN_ACTIVITY
)
type LanguagesInfo struct {
wakatime_cfg string
languages []string
language_index int
}
func NewLanguagesInfo() LanguagesInfo {
return LanguagesInfo{
language_index: 0,
}
}
func (l *LanguagesInfo) getKey() string {
cfg, err := ini.Load(l.wakatime_cfg)
if err != nil {
fmt.Printf("Couldn't load cfg file from %s\n", l.wakatime_cfg)
os.Exit(1)
}
return cfg.Section("settings").Key("api_key").String()
}
func (l *LanguagesInfo) setLanguages(languages []string) {
l.languages = languages
l.language_index = 0
}
func (l *LanguagesInfo) updateLanguageIndex() {
max := len(l.languages)
l.language_index += 1
if l.language_index >= max {
l.language_index = 0
}
}
// Perform the actual wakatime hearbeat
func (l *LanguagesInfo) doHeartBeat() {
key := l.getKey()
for {
wakatime.Execute(l.languages[l.language_index], key)
l.updateLanguageIndex()
}
}
// Returns true if api-key field is set
// otherwise returns false
func (l *LanguagesInfo) checkIfApiKeySet() tea.Msg {
cfg, err := ini.Load(l.wakatime_cfg)
if err != nil {
//probably file doesn't exist so create it
f, err := os.OpenFile(l.wakatime_cfg, os.O_CREATE, 0777)
f.Close()
cfg, err = ini.Load(l.wakatime_cfg)
if err != nil {
fmt.Printf("Error occured with loading config file: %s, with error: %s", l.wakatime_cfg, err)
return tea.Quit
}
}
if !cfg.HasSection("settings") {
cfg.NewSection("settings")
cfg.SaveTo(l.wakatime_cfg)
}
settings := cfg.Section("settings")
var result Msg[bool]
if !settings.HasKey("api_key") {
result.value = false
} else {
result.value = true
}
return result
}
func (l *LanguagesInfo) checkIfLangsInputEmpty() tea.Msg {
var result Msg[bool]
for _, v := range l.languages {
if len(v) == 0 {
result.value = false
return result
}
}
result.value = true
return result
}
func (l *LanguagesInfo) setApiKey(m model) {
cfg, err := ini.Load(l.wakatime_cfg)
if err != nil {
fmt.Printf("Error, %s occured trying to open %s", err, l.wakatime_cfg)
os.Exit(1)
}
cfg.Section("settings").NewKey("api_key", m.text_input.Value())
if cfg.SaveTo(l.wakatime_cfg) != nil {
fmt.Printf("Failed to save %s file", l.wakatime_cfg)
os.Exit(1)
}
}
var LangInfo LanguagesInfo
func init() {
LangInfo = NewLanguagesInfo()
}
type model struct {
text_input textinput.Model
state Status
spinner spinner.Model
}
func newModel() model {
spin := spinner.New()
spin.Spinner = spinner.Dot
spin.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("#11EE11"))
return model{state: STARTING,
text_input: textinput.New(),
spinner: spin,
}
}
func getHomeDir() tea.Msg {
home, err := os.UserHomeDir()
if err != nil {
fmt.Println("Couldn't get home dir of your os")
return tea.Quit
}
var result Msg[string]
result.value = home + string(os.PathSeparator) + ".wakarizer.cfg"
return result
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
//handle key presses
case tea.KeyMsg:
{
switch msg.Type {
case tea.KeyCtrlC:
return m, tea.Quit
case tea.KeyEnter:
{
switch m.state {
case ASKING_API_KEY:
{
m.state = SETUP_ASKING_LANGUAGE_TYPES
LangInfo.setApiKey(m)
return m, textinput.Blink
}
case ASKING_LANGUAGE_TYPES:
{
m.state = VALIDATE_GETTING_LANGUAGES
LangInfo.setLanguages(strings.Split(m.text_input.Value(), " "))
return m, LangInfo.checkIfLangsInputEmpty
}
}
}
case tea.KeyCtrlV:
{
if m.state == ASKING_API_KEY || m.state == ASKING_LANGUAGE_TYPES {
return m, textinput.Paste
}
}
}
}
case Msg[string]:
{
switch m.state {
case STARTING:
{
m.state = GOTTEN_HOME_DIR
LangInfo.wakatime_cfg = msg.value
return m, LangInfo.checkIfApiKeySet
}
}
}
case Msg[bool]:
{
switch m.state {
case GOTTEN_HOME_DIR:
{
if msg.value == true {
m.state = SETUP_ASKING_LANGUAGE_TYPES
return m, textinput.Blink
} else {
m.state = ASKING_API_KEY
m.text_input.EchoMode = textinput.EchoPassword
m.text_input.Placeholder = "Enter Wakatime Api Key"
m.text_input.Width = 100
m.text_input.CharLimit = 100
m.text_input.Focus()
return m, textinput.Blink
}
}
case VALIDATE_GETTING_LANGUAGES:
{
if msg.value == true {
m.state = START_MAIN_ACTIVITY
return m, m.spinner.Tick
} else {
m.state = ASKING_LANGUAGE_TYPES
return m, textinput.Blink
}
}
}
}
}
switch m.state {
//do this when starting
case STARTING:
return m, getHomeDir
case ASKING_API_KEY, ASKING_LANGUAGE_TYPES:
{
input, cmd := m.text_input.Update(msg)
m.text_input = input
return m, cmd
}
case SETUP_ASKING_LANGUAGE_TYPES:
{
m.state = ASKING_LANGUAGE_TYPES
m.text_input = textinput.New()
m.text_input.Placeholder = "rs ts java...example of how to enter file extentions"
m.text_input.Width = 100
m.text_input.Focus()
return m, textinput.Blink
}
case START_MAIN_ACTIVITY:
{
go LangInfo.doHeartBeat()
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
m.state = INSIDE_MAIN_ACTIVITY
return m, cmd
}
case INSIDE_MAIN_ACTIVITY:
{
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
}
return m, nil
}
func (m model) showMainTitle() string {
return lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FFFFFF")).
Background(lipgloss.Color("#EE1111")).
Border(lipgloss.DoubleBorder(), true).
Render("\nWelcome to Wakarizer\n by @Borwe [email protected]\n")
}
func (m model) showTitle(title string) string {
return lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#EEEEEE")).
Render("\n" + title)
}
func (m model) showFooter(title string) string {
return lipgloss.NewStyle().Bold(true).
Foreground(lipgloss.Color("#EE1111")).
Render(title)
}
func (m model) View() string {
string_return := m.showMainTitle()
switch m.state {
case STARTING:
string_return += m.showTitle("\nLoading....")
case GOTTEN_HOME_DIR:
string_return += m.showTitle(fmt.Sprintf("\nGotten config @ %s", LangInfo.wakatime_cfg))
case ASKING_API_KEY:
{
string_return += fmt.Sprintf("%s\n\n%s", m.showTitle("\nWhat is your api key?"), m.text_input.View())
}
case ASKING_LANGUAGE_TYPES:
string_return += fmt.Sprintf("%s\n\n%s",
m.showTitle("\nWhat Languages You want to wakarize, give atleast 1? \n"+
"(write the extention, and seperate with spaces for others\n"+
"eg: rs ts java\n"+
"This is for rust, typescript and java)"), m.text_input.View())
case START_MAIN_ACTIVITY, INSIDE_MAIN_ACTIVITY:
{
string_return += fmt.Sprintf("\nLANGS/EXTENTIONS: %s", LangInfo.languages)
string_return += fmt.Sprintf("\n %s -> %s %s\n", m.spinner.View(), "Doing Language: ", LangInfo.languages[LangInfo.language_index])
}
}
if m.state == ASKING_API_KEY || m.state == ASKING_LANGUAGE_TYPES {
string_return += "\nPress ctrl+v to paste"
}
string_return += m.showFooter("\nPress ctrl+c to quit")
return string_return
}
func main() {
p := tea.NewProgram(newModel())
if _, err := p.Run(); err != nil {
fmt.Printf("Sorry, failed to start program, with error %s", err)
os.Exit(1)
}
}