-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
214 lines (177 loc) · 5.32 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
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/gen2brain/beeep"
"github.com/olebedev/when"
"github.com/olebedev/when/rules/common"
"github.com/olebedev/when/rules/en"
)
const (
markName = "cli_reminder"
markValue = "1"
)
var recurConfig = make(map[int]string)
var messageConfig = make(map[int]string)
var idCounter int = 1
func main() {
helpFlag := flag.Bool("help", false, "Displays usage information")
recurFlag := flag.String("recur", "", "Set recurrence pattern (daily, weekly, monthly)")
removeRecurFlag := flag.Bool("remove-recur", false, "Remove all recurring reminders")
listRecurFlag := flag.Bool("list-recur", false, "List all recurring reminders")
removeByIDFlag := flag.Int("remove-id", 0, "Remove recurring reminder by ID")
flag.Parse()
// Handle flags first
if *helpFlag {
printUsage()
os.Exit(0)
}
if *listRecurFlag {
listRecurringReminders()
os.Exit(0)
}
if *removeByIDFlag > 0 {
removeRecurringReminderByID(*removeByIDFlag)
os.Exit(0)
}
if *removeRecurFlag {
removeAllRecurringReminders()
os.Exit(0)
}
// After flag parsing, get the remaining positional arguments
args := flag.Args()
if len(args) < 1 {
fmt.Println("Error: Insufficient arguments provided.")
printUsage()
os.Exit(1)
}
welcomeMessage()
now := time.Now()
w := when.New(nil)
w.Add(en.All...)
w.Add(common.All...)
// Parse the input date/time
timeInput := args[0]
// Determine the message based on remaining arguments
var message string
if len(args) > 1 {
message = strings.Join(args[1:], " ")
}
t, err := w.Parse(timeInput, now)
if err != nil {
fmt.Println("Error parsing time:", err)
os.Exit(1)
}
if t == nil {
fmt.Println("UwU~ Please specify a valid date/time.")
os.Exit(2)
}
if now.After(t.Time) {
fmt.Println("Cannot set a reminder in the past!")
os.Exit(3)
}
diff := time.Until(t.Time)
if os.Getenv(markName) == markValue {
time.Sleep(diff)
uwuMessage := fmt.Sprintf("UwU~ %s", message)
err = beeep.Alert("Reminder", uwuMessage, "assets/information.png")
if err != nil {
fmt.Println("Error displaying notification:", err)
os.Exit(4)
}
handleRecurrence(t.Time, *recurFlag, message)
} else {
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=%s", markName, markValue))
if err = cmd.Start(); err != nil {
fmt.Println("Error starting new process:", err)
os.Exit(5)
}
fmt.Println("UwU~ Reminder will trigger in:", diff.Round(time.Second))
os.Exit(0)
}
}
func handleRecurrence(initialTime time.Time, recurrence, message string) {
if recurrence == "" {
return
}
var nextTime time.Time
switch strings.ToLower(recurrence) {
case "daily":
nextTime = initialTime.Add(24 * time.Hour)
case "weekly":
nextTime = initialTime.Add(7 * 24 * time.Hour)
case "monthly":
nextTime = initialTime.AddDate(0, 1, 0)
default:
fmt.Println("Invalid recurrence pattern. Use daily, weekly, or monthly.")
return
}
recurConfig[idCounter] = recurrence
messageConfig[idCounter] = message
currentID := idCounter
idCounter++
diff := time.Until(nextTime)
fmt.Printf("Recurring reminder set (ID: %d) for: %s\n", currentID, nextTime.Format("2006-01-02 15:04:05"))
time.Sleep(diff)
err := beeep.Alert("Recurring Reminder", fmt.Sprintf("UwU~ %s", message), "assets/information.png")
if err != nil {
fmt.Println("Error displaying recurring notification:", err)
os.Exit(6)
}
handleRecurrence(nextTime, recurrence, message)
}
func removeAllRecurringReminders() {
if len(recurConfig) == 0 {
fmt.Println("No recurring reminders to remove.")
return
}
fmt.Println("Removing all recurring reminders...")
recurConfig = make(map[int]string) // Clear all entries
messageConfig = make(map[int]string)
fmt.Println("All recurring reminders removed successfully.")
}
func removeRecurringReminderByID(id int) {
if _, exists := recurConfig[id]; !exists {
fmt.Printf("No recurring reminder found with ID: %d\n", id)
return
}
delete(recurConfig, id)
delete(messageConfig, id)
fmt.Printf("Recurring reminder with ID: %d removed successfully.\n", id)
}
func listRecurringReminders() {
if len(recurConfig) == 0 {
fmt.Println("No recurring reminders set.")
return
}
fmt.Println("Listing all recurring reminders:")
for id, recurrence := range recurConfig {
fmt.Printf("ID: %d, Recurrence: %s, Message: %s\n", id, recurrence, messageConfig[id])
}
}
func printUsage() {
fmt.Println("CLI Reminder Tool")
fmt.Println("Usage:")
fmt.Println(" remind <date/time> <message> [flags]")
fmt.Println("Flags:")
fmt.Println(" --help Display usage information")
fmt.Println(" --recur Set recurrence pattern (daily, weekly, monthly)")
fmt.Println(" --remove-recur Remove all recurring reminders")
fmt.Println(" --list-recur List all recurring reminders")
fmt.Println(" --remove-id Remove recurring reminder by ID")
fmt.Println("Examples:")
fmt.Println(" remind \"2024-12-12 14:30\" \"Meeting with team\"")
fmt.Println(" remind \"2pm tomorrow\" \"Doctor's appointment\" --recur weekly")
fmt.Println(" remind --list-recur")
fmt.Println(" remind --remove-id 2")
fmt.Println(" remind --remove-recur")
}
func welcomeMessage() {
fmt.Println("UwU~ Welcome to the CLI Reminder Tool!")
fmt.Println("Type 'remind --help' for usage instructions, nya~!")
}