Преглед изворни кода

Merge pull request #267 from xendke/calendar-start-day

Optionally start calendar weeks on Sunday
Svilen Markov пре 7 месеци
родитељ
комит
4bd4921131

+ 11 - 1
docs/configuration.md

@@ -1597,15 +1597,25 @@ Example:
 
 ```yaml
 - type: calendar
+  start-sunday: false
 ```
 
 Preview:
 
 ![](images/calendar-widget-preview.png)
 
+#### Properties
+
+| Name | Type | Required | Default |
+| ---- | ---- | -------- | ------- |
+| start-sunday | boolean | no | false |
+
+##### `start-sunday`
+Whether calendar weeks start on Sunday or Monday.
+
 > [!NOTE]
 >
-> There is currently no customizability available for the calendar. Extra features will be added in the future.
+> There is currently little customizability available for the calendar. Extra features will be added in the future.
 
 ### Markets
 Display a list of markets, their current value, change for the day and a small 21d chart. Data is taken from Yahoo Finance.

+ 6 - 1
internal/assets/templates/calendar.html

@@ -11,13 +11,18 @@
     </div>
 
     <div class="flex flex-wrap size-h6 margin-top-10 color-subdue">
+        {{ if .StartSunday }}
+            <div class="calendar-day">Su</div>
+        {{ end }}
         <div class="calendar-day">Mo</div>
         <div class="calendar-day">Tu</div>
         <div class="calendar-day">We</div>
         <div class="calendar-day">Th</div>
         <div class="calendar-day">Fr</div>
         <div class="calendar-day">Sa</div>
-        <div class="calendar-day">Su</div>
+        {{ if not .StartSunday }}
+            <div class="calendar-day">Su</div>
+        {{ end }}
     </div>
 
     <div class="flex flex-wrap">

+ 4 - 6
internal/feed/calendar.go

@@ -3,14 +3,12 @@ package feed
 import "time"
 
 // TODO: very inflexible, refactor to allow more customizability
-// TODO: allow changing first day of week
 // TODO: allow changing between showing the previous and next week and the entire month
-func NewCalendar(now time.Time) *Calendar {
+func NewCalendar(now time.Time, startSunday bool) *Calendar {
 	year, week := now.ISOWeek()
 	weekday := now.Weekday()
-
-	if weekday == 0 {
-		weekday = 7
+	if !startSunday {
+		weekday = (weekday + 6) % 7 // Shift Monday to 0
 	}
 
 	currentMonthDays := daysInMonth(now.Month(), year)
@@ -23,7 +21,7 @@ func NewCalendar(now time.Time) *Calendar {
 		previousMonthDays = daysInMonth(previousMonthNumber, year)
 	}
 
-	startDaysFrom := now.Day() - int(weekday+6)
+	startDaysFrom := now.Day() - int(weekday) - 7
 
 	days := make([]int, 21)
 

+ 4 - 3
internal/widget/calendar.go

@@ -10,8 +10,9 @@ import (
 )
 
 type Calendar struct {
-	widgetBase `yaml:",inline"`
-	Calendar   *feed.Calendar
+	widgetBase  `yaml:",inline"`
+	Calendar    *feed.Calendar
+	StartSunday bool `yaml:"start-sunday"`
 }
 
 func (widget *Calendar) Initialize() error {
@@ -21,7 +22,7 @@ func (widget *Calendar) Initialize() error {
 }
 
 func (widget *Calendar) Update(ctx context.Context) {
-	widget.Calendar = feed.NewCalendar(time.Now())
+	widget.Calendar = feed.NewCalendar(time.Now(), widget.StartSunday)
 	widget.withError(nil).scheduleNextUpdate()
 }