Compare commits
6 Commits
7d2b9764c3
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cf9f50f583 | ||
|
|
d17993453e | ||
|
|
572f129ddb | ||
|
|
d05789b36b | ||
|
|
5f38a36027 | ||
|
|
d7d888453b |
13
gcal.go
13
gcal.go
@@ -71,11 +71,18 @@ func (gcal *GCal) EventsToday(ctx context.Context) ([]Event, error) {
|
||||
d = t2.Sub(t)
|
||||
}
|
||||
|
||||
result = append(result, Event{
|
||||
Name: events.Items[i].Summary,
|
||||
result = append(result,
|
||||
Event{
|
||||
Name: "Soon: " + events.Items[i].Summary,
|
||||
Time: t.Add(-2 * time.Minute),
|
||||
Duration: 0,
|
||||
},
|
||||
Event{
|
||||
Name: "Now: " + events.Items[i].Summary,
|
||||
Time: t,
|
||||
Duration: d,
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
44
main.go
44
main.go
@@ -34,9 +34,19 @@ func main() {
|
||||
}
|
||||
|
||||
for alert := range alerts {
|
||||
if err := alertAt(ctx, *ntfy, time.Now(), alert, time.Now().Format("15:04")); err != nil {
|
||||
if ctx.Err() != nil {
|
||||
break
|
||||
}
|
||||
if err := alertAt(ctx, *ntfy, time.Now(), alert, time.Now().Format("15:04")); err != nil && ctx.Err() == nil {
|
||||
panic(err)
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if err := ctx.Err(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,18 +58,38 @@ func alerts(ctx context.Context, gcal bool, args []string) (chan string, error)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return alertsAfter(ctx, duration)
|
||||
msg := "alerting after " + duration.String()
|
||||
if len(args) > 1 {
|
||||
msg = fmt.Sprintf("%s (%s)", args[1], duration.String())
|
||||
}
|
||||
return alertsAfter(ctx, duration, msg)
|
||||
}
|
||||
|
||||
func alertsAfter(ctx context.Context, dur time.Duration) (chan string, error) {
|
||||
func alertsAfter(ctx context.Context, dur time.Duration, msg string) (chan string, error) {
|
||||
ch := make(chan string)
|
||||
deadline := time.Now().Add(dur)
|
||||
go func() {
|
||||
defer close(ch)
|
||||
var prev string
|
||||
for ctx.Err() == nil && time.Now().Before(deadline) {
|
||||
seconds := int(time.Until(deadline).Seconds())
|
||||
|
||||
cur := fmt.Sprintf("%v", time.Duration(seconds)*time.Second)
|
||||
if prev != "" {
|
||||
fmt.Printf("\r%s\r", strings.Repeat(" ", len(prev)))
|
||||
}
|
||||
fmt.Printf("%s", cur)
|
||||
prev = cur
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-time.After(dur):
|
||||
case <-time.After(time.Second):
|
||||
}
|
||||
ch <- "alerting after " + dur.String()
|
||||
}
|
||||
if ctx.Err() == nil {
|
||||
fmt.Println(msg)
|
||||
}
|
||||
ch <- msg
|
||||
}()
|
||||
return ch, nil
|
||||
}
|
||||
@@ -100,10 +130,11 @@ func alertsGCal(ctx context.Context) (chan string, error) {
|
||||
if err := refresh(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
case <-time.After(time.Until(events[0].Time.Add(-2 * time.Minute))):
|
||||
case <-time.After(time.Until(events[0].Time)):
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case ch <- events[0].Name:
|
||||
if events[0].Duration > 0 {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-time.After(events[0].Duration):
|
||||
@@ -113,6 +144,7 @@ func alertsGCal(ctx context.Context) (chan string, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
events = events[1:]
|
||||
case <-ctx.Done():
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user