Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Calendar Events implemented. #174

Open
wants to merge 6 commits into
base: release/v0.7.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
)

require (
github.com/arran4/golang-ical v0.3.1 // indirect
github.com/PuerkitoBio/goquery v1.10.0 // indirect
github.com/andybalholm/cascadia v1.3.3 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
14 changes: 12 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
github.com/PuerkitoBio/goquery v1.10.0 h1:6fiXdLuUvYs2OJSvNRqlNPoBm6YABE226xrbavY5Wv4=
github.com/PuerkitoBio/goquery v1.10.0/go.mod h1:TjZZl68Q3eGHNBA8CWaxAN7rOU1EbDz3CWuolcO5Yu4=
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
github.com/arran4/golang-ical v0.3.1 h1:v13B3eQZ9VDHTAvT6M11vVzxYgcYmjyPBE2eAZl3VZk=
github.com/arran4/golang-ical v0.3.1/go.mod h1:LZWxF8ZIu/sjBVUCV0udiVPrQAgq3V0aa0RfbO99Qkk=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
github.com/andybalholm/cascadia v1.3.3/go.mod h1:xNd9bqTn98Ln4DwST8/nG+H0yuB8Hmgu1YHNnWw0GeA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -13,6 +14,9 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mmcdole/gofeed v1.3.0 h1:5yn+HeqlcvjMeAI4gu6T+crm7d0anY85+M+v6fIFNG4=
github.com/mmcdole/gofeed v1.3.0/go.mod h1:9TGv2LcJhdXePDzxiuMnukhV2/zb6VtnZt1mS+SjkLE=
github.com/mmcdole/goxpp v1.1.1 h1:RGIX+D6iQRIunGHrKqnA2+700XMCnNv0bAOOv5MUhx8=
Expand All @@ -22,10 +26,12 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
Expand Down Expand Up @@ -113,5 +119,9 @@ golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxb
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
58 changes: 52 additions & 6 deletions internal/glance/static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -1066,16 +1066,62 @@ details[open] .summary::after {
}

.calendar-day {
width: calc(100% / 7);
text-align: center;
padding: 0.6rem 0;
width: calc(100% / 7);
text-align: center;
display: flex;
flex-direction: column;
align-items: center;
padding: 0.6rem 0;
}


.calendar-day-today {
border-radius: var(--border-radius);
background-color: hsl(var(--bghs), calc(var(--scheme) (var(--scheme) (var(--bgl)) + 6%)));
color: var(--color-text-highlight);
border-radius: var(--border-radius);
background-color: hsl(
var(--bghs),
calc(var(--scheme) (var(--scheme) (var(--bgl)) + 6%))
);
color: var(--color-text-highlight);
}

.calendar-isevent {
color: var(--color-primary);
cursor: pointer;
}
.tooltip {
position: relative;
display: inline-block;
width: 0;
height: 0;
}
.tooltiptext {
transition: 0.5s;
}
/* Tooltip text */
.tooltip .tooltiptext {
visibility: hidden;
width: 150px;
background-color: hsl(
var(--bghs),
calc(var(--scheme) (var(--scheme) (var(--bgl)) + 6%))
);
color: var(--color-text-highlight);
text-align: center;
padding: 5px 0;
border-radius: 6px;
opacity: 0;

position: absolute;
z-index: 1;
bottom: 100%;
left: 50%;
margin-bottom: 5px;
margin-left: -75px;
}

.calendar-day:hover .tooltiptext {
visibility: visible;
opacity: 0.8;
}

.dns-stats-totals {
Expand Down
20 changes: 16 additions & 4 deletions internal/glance/templates/calendar.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,22 @@
{{ end }}
</div>

<div class="flex flex-wrap">
{{ range .Calendar.Days }}
<div class="calendar-day{{ if eq . $.Calendar.CurrentDay }} calendar-day-today{{ end }}">{{ . }}</div>
{{ end }}
<div class="flex flex-wrap justify-center items-center">
{{ range .Calendar.Days }}
<div class="calendar-day{{ if eq .Day $.Calendar.CurrentDay }} calendar-day-today{{ end }} {{ if .IsEvent}} calendar-isevent{{ end }}">
{{ if .IsEvent}}
<div class="tooltip">
<span class="tooltiptext">
{{ range .Events }}
{{ .EventHover }}
<br>
{{ end }}
</span>
</div>
{{ end }}
<span>{{ .Day }}</span>
</div>
{{ end }}
</div>
</div>
{{ end }}
82 changes: 75 additions & 7 deletions internal/glance/widget-calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,34 @@ package glance

import (
"context"
"fmt"
"html/template"
"log"
"net/http"
"os"
"strings"
"time"

ics "github.com/arran4/golang-ical"
)

var calendarWidgetTemplate = mustParseTemplate("calendar.html", "widget-base.html")

type CalendarEvent struct {
StartedDay time.Time
EventHover string
}
type CalendayDay struct {
Day int
IsEvent bool
Events []CalendarEvent
}

type calendarWidget struct {
widgetBase `yaml:",inline"`
Calendar *calendar
StartSunday bool `yaml:"start-sunday"`
Icsurl string
}

func (widget *calendarWidget) initialize() error {
Expand All @@ -21,7 +39,8 @@ func (widget *calendarWidget) initialize() error {
}

func (widget *calendarWidget) update(ctx context.Context) {
widget.Calendar = newCalendar(time.Now(), widget.StartSunday)
widget.Calendar = newCalendar(time.Now(), widget.StartSunday, widget.Icsurl)
fmt.Println(widget.Calendar.Days)
widget.withError(nil).scheduleNextUpdate()
}

Expand All @@ -34,13 +53,14 @@ type calendar struct {
CurrentWeekNumber int
CurrentMonthName string
CurrentYear int
Days []int
Days []CalendayDay
Icsurl string `yaml:"icsurl"`
}

// TODO: very inflexible, refactor to allow more customizability
// TODO: allow changing between showing the previous and next week and the entire month
func newCalendar(now time.Time, startSunday bool) *calendar {
year, week := now.ISOWeek()
func newCalendar(now time.Time, startSunday bool, icsurl string) *calendar {
year, week := now.Year(), int(now.Weekday())
weekday := now.Weekday()
if !startSunday {
weekday = (weekday + 6) % 7 // Shift Monday to 0
Expand All @@ -58,18 +78,40 @@ func newCalendar(now time.Time, startSunday bool) *calendar {

startDaysFrom := now.Day() - int(weekday) - 7

days := make([]int, 21)
days := make([]CalendayDay, 21)
events, _ := ReadPublicIcs(icsurl)

for i := 0; i < 21; i++ {
day := startDaysFrom + i
month := now.Month()

if day < 1 {
day = previousMonthDays + day
month -= 1

} else if day > currentMonthDays {
day = day - currentMonthDays
}
month += 1

days[i] = day
}
if events != nil {
for _, event := range events {
var dayEvent CalendarEvent
startAt, err := event.GetStartAt()
if err != nil {
log.Panic(err)
}
fmt.Println(year)
// fmt.Println(startAt.Day() == day && startAt.Month() == month)
if startAt.Day() == day && startAt.Month() == month && startAt.Year() == year {
dayEvent.StartedDay = startAt
dayEvent.EventHover = event.GetProperty("SUMMARY").Value
days[i].IsEvent = true
days[i].Events = append(days[i].Events, dayEvent)
}
}
}
days[i].Day = day
}

return &calendar{
Expand All @@ -84,3 +126,29 @@ func newCalendar(now time.Time, startSunday bool) *calendar {
func daysInMonth(m time.Month, year int) int {
return time.Date(year, m+1, 0, 0, 0, 0, 0, time.UTC).Day()
}

func ParseEventsFromFile(file string) []*ics.VEvent {
eventString, err := os.ReadFile(file)
if err != nil {
log.Panic(err)
}
cal, err := ics.ParseCalendar(strings.NewReader(string(eventString)))
if err != nil {
log.Panic(err)
}
events := cal.Events()
return events
}
func ReadPublicIcs(url string) ([]*ics.VEvent, error) {
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
cal, err := ics.ParseCalendar(response.Body)
if err != nil {
return nil, err
}
events := cal.Events()
return events, nil
}