-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
124 lines (100 loc) · 2.71 KB
/
utils.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
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
)
func showHelper() string {
helpText := `
-------------------Usage-------------------
- Press 's' to start work session.
s <minutes> to start work session for <minutes> minutes
- Press 'b' to take a break.
b <minutes> to take break for <minutes> minutes
- Press 'l' to list all completed today's sessions.
l YYYY-MM-DD to list completed sessions on that date.
- Press 'q' to quit.
`
return helpText
}
func checkValidMinute(m *model, command string) (int, bool) {
if command == "s" || command == "b" {
return 0, true
}
spacing := command[1:]
if !strings.HasPrefix(spacing, " ") {
m.err = "Invalid command"
return 0, false
}
numOfMinutesStr := strings.TrimSpace(command[2:])
numOfMinutes, err := strconv.Atoi(numOfMinutesStr)
if err != nil {
m.err = "Invalid number of minutes"
return 0, false
}
return numOfMinutes, true
}
func loadSessions() []session {
data, err := os.ReadFile("db.json")
if err != nil {
return []session{}
}
sessions := []session{}
err = json.Unmarshal(data, &sessions)
if err != nil {
return []session{}
}
return sessions
}
func saveSessions(sessions []session) error {
data, err := json.MarshalIndent(sessions, "", " ")
if err != nil {
return fmt.Errorf("Error marshal data: %v", err.Error())
}
err = os.WriteFile("db.json", data, 0644)
if err != nil {
return fmt.Errorf("Error writing to file: %v", err.Error())
}
return nil
}
func printSessions(sessions []session, differentDate bool, date time.Time) string {
printingResult := ""
if !differentDate {
today := time.Now()
todaySessions := getCorrectSession(sessions, today)
printingResult = "Today's Completed Sessions:\n"
printingResult += printHelper(todaySessions)
} else {
differentDateSessions := getCorrectSession(sessions, date)
printingResult = fmt.Sprintf("Completed sessions on %v:\n", date.Format(time.DateOnly))
printingResult += printHelper(differentDateSessions)
}
return printingResult
}
func getCorrectSession(sessions []session, date time.Time) []session {
resultSessions := []session{}
for _, s := range sessions {
if s.StartTime.Add(s.Duration).Format(time.DateOnly) == date.Format(time.DateOnly) {
resultSessions = append(resultSessions, s)
}
}
return resultSessions
}
func printHelper(sessions []session) string {
resultPrinting := ""
if len(sessions) == 0 {
return "\nYou haven't completed any session 😕\n"
}
for _, s := range sessions {
resultPrinting += fmt.Sprintf(
"Pomodoro session: duration of %f minutes from %v to %v\n",
s.Duration.Minutes(),
s.StartTime.Format("15:04"),
s.EndTime.Format("15:04"),
)
}
return resultPrinting
}