From 1ca444d3e659b61317ea62588930a0a5156934c5 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Sat, 20 Apr 2024 22:58:30 -0600 Subject: imworkingon: Initial go at daily statuses --- cmd/generate/calendar.go | 122 +++++++++++++++++++++++++++++++++++++ cmd/generate/imworkingon.html.tmpl | 72 ++++++++++++++++++++++ cmd/generate/main.go | 40 +++++++++++- cmd/generate/src_mastodon.go | 52 ++++++++++++++++ 4 files changed, 283 insertions(+), 3 deletions(-) create mode 100644 cmd/generate/calendar.go create mode 100644 cmd/generate/src_mastodon.go (limited to 'cmd/generate') diff --git a/cmd/generate/calendar.go b/cmd/generate/calendar.go new file mode 100644 index 0000000..29c3318 --- /dev/null +++ b/cmd/generate/calendar.go @@ -0,0 +1,122 @@ +package main + +import ( + "time" +) + +////////////////////////////////////////////////////////////////////// + +type Date struct { + Year int + Month time.Month + Day int +} + +func DateOf(t time.Time) Date { + y, m, d := t.Date() + return Date{Year: y, Month: m, Day: d} +} + +func (d Date) Time() time.Time { + return time.Date(d.Year, d.Month, d.Day, 0, 0, 0, 0, time.Local) +} + +func (d Date) AddDays(delta int) Date { + return DateOf(d.Time().AddDate(0, 0, delta)) +} + +func (d Date) Weekday() time.Weekday { + return d.Time().Weekday() +} + +func (a Date) Cmp(b Date) int { + switch { + case a.Year < b.Year: + return -1 + case a.Year > b.Year: + return 1 + } + switch { + case a.Month < b.Month: + return -1 + case a.Month > b.Month: + return 1 + } + switch { + case a.Day < b.Day: + return -1 + case a.Day > b.Day: + return 1 + } + return 0 +} + +////////////////////////////////////////////////////////////////////// + +type CalendarDay[T any] struct { + Date + Data T +} + +////////////////////////////////////////////////////////////////////// + +// keyed by time.Weekday +type CalendarWeek[T any] [7]CalendarDay[T] + +////////////////////////////////////////////////////////////////////// + +// must be sorted, must be non-sparse +type Calendar[T any] []CalendarWeek[T] + +func (c Calendar[T]) NumWeekdaysInMonth(weekday time.Weekday, target Date) int { + num := 0 + for _, w := range c { + if w[weekday].Date == (Date{}) { + continue + } + switch { + case w[weekday].Year == target.Year: + switch { + case w[weekday].Month == target.Month: + num++ + case w[weekday].Month > target.Month: + return num + } + case w[weekday].Year > target.Year: + return num + } + } + return num +} + +////////////////////////////////////////////////////////////////////// + +func BuildCalendar[T any](things []T, dateOfThing func(T) Date) Calendar[T] { + if len(things) == 0 { + return nil + } + + newestDate := DateOf(time.Now().Local()) + + oldestDate := dateOfThing(things[0]) + byDate := make(map[Date]T, len(things)) + for _, thing := range things { + date := dateOfThing(thing) + if oldestDate.Cmp(date) > 0 { + oldestDate = date + } + byDate[date] = thing + } + + var ret Calendar[T] + for date := oldestDate; date.Cmp(newestDate) <= 0; date = date.AddDays(1) { + if len(ret) == 0 || date.Weekday() == 0 { + ret = append(ret, CalendarWeek[T]{}) + } + ret[len(ret)-1][date.Weekday()] = CalendarDay[T]{ + Date: date, + Data: byDate[date], + } + } + return ret +} diff --git a/cmd/generate/imworkingon.html.tmpl b/cmd/generate/imworkingon.html.tmpl index fb24ac6..1be3960 100644 --- a/cmd/generate/imworkingon.html.tmpl +++ b/cmd/generate/imworkingon.html.tmpl @@ -81,6 +81,78 @@ {{- end }} +
+

Daily statuses

+

Posted daily on Mastodon.

+ +
Calendar view + + + + + + + + + + + + + + + + {{- $cal := .StandupCalendar }} + {{- $curSunMonth := 0 }} + {{- $curSatMonth := 0 }} + {{- range $i, $week := reverse .StandupCalendar }} + + {{- $sun := (index $week time.Sunday) }} + {{- if not $sun.Day }} + + {{- else if ne $sun.Month $curSunMonth }} + + {{- $curSunMonth = $sun.Month }} + {{- end }} + {{- range $day := $week }} + {{- if not $day.Day }} + + {{- else if not $day.Data }} + + {{- else }} + + {{- end }} + + {{- end }} + {{- $sat := (index $week time.Saturday) }} + {{- if not $sat.Day }} + + {{- else if ne $sat.Month $curSatMonth }} + + {{- $curSatMonth = $sat.Month }} + {{- end }} + {{- end }} + + +
SuMTuWThFS
+ {{ $sun.Month }} {{ $sun.Year }} + + {{ $day.Day }} + + + {{ $day.Day }} + + + {{ $sat.Month }} {{ $sat.Year }} +
+
+ + {{- range $status := .Standups }} + + {{- end }} +