1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Add event end time to gcal module display (#877)

* Add event end time to gcal module display

* Add settings for showing end time and add test coverage

* go mod tidy
This commit is contained in:
Adriano
2020-04-23 08:32:59 -04:00
committed by GitHub
parent ad2ad7437e
commit 216124437a
6 changed files with 71 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
package gcal
import (
"fmt"
"time"
"github.com/wtfutil/wtf/utils"
@@ -97,17 +98,23 @@ func (calEvent *CalEvent) Start() time.Time {
return start
}
func (calEvent *CalEvent) Timestamp(hourFormat string) string {
func (calEvent *CalEvent) Timestamp(hourFormat string, showEndTime bool) string {
if calEvent.AllDay() {
startTime, _ := time.ParseInLocation("2006-01-02", calEvent.event.Start.Date, time.Local)
return startTime.Format(utils.FriendlyDateFormat)
}
startTime, _ := time.Parse(time.RFC3339, calEvent.event.Start.DateTime)
endTime, _ := time.Parse(time.RFC3339, calEvent.event.End.DateTime)
timeFormat := utils.MinimumTimeFormat24
if hourFormat == "12" {
timeFormat = utils.MinimumTimeFormat12
}
if showEndTime {
return fmt.Sprintf("%s-%s", startTime.Format(timeFormat), endTime.Format(timeFormat))
}
return startTime.Format(timeFormat)
}

View File

@@ -37,7 +37,8 @@ func (widget *Widget) content() (string, string, bool) {
continue
}
timestamp := fmt.Sprintf("[%s]%s", widget.eventTimeColor(calEvent), calEvent.Timestamp(widget.settings.hourFormat))
ts := calEvent.Timestamp(widget.settings.hourFormat, widget.settings.showEndTime)
timestamp := fmt.Sprintf("[%s]%s", widget.eventTimeColor(calEvent), ts)
if calEvent.AllDay() {
timestamp = ""
}

View File

@@ -0,0 +1,58 @@
package gcal
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/wtfutil/wtf/cfg"
"google.golang.org/api/calendar/v3"
)
func Test_display_content(t *testing.T) {
startTime := &calendar.EventDateTime{DateTime: "1986-04-19T01:00:00.00Z"}
endTime := &calendar.EventDateTime{DateTime: "1986-04-19T02:00:00.00Z"}
event := &calendar.Event{Summary: "Foo", Start: startTime, End: endTime}
testCases := []struct {
descriptionWanted string
events []*CalEvent
name string
settings *Settings
}{
{
name: "Event content without any events",
settings: &Settings{common: &cfg.Common{}},
events: nil,
descriptionWanted: "No calendar events",
},
{
name: "Event content with a single event, without end times displayed",
settings: &Settings{common: &cfg.Common{}, showEndTime: false},
events: []*CalEvent{NewCalEvent(event)},
descriptionWanted: "[]Saturday, Apr 19\n []01:00 []Foo[white]\n \n",
},
{
name: "Event content with a single event without showEndTime explictily set in settings",
settings: &Settings{common: &cfg.Common{}},
events: []*CalEvent{NewCalEvent(event)},
descriptionWanted: "[]Saturday, Apr 19\n []01:00 []Foo[white]\n \n",
},
{
name: "Event content with a single event with end times displayed",
settings: &Settings{common: &cfg.Common{}, showEndTime: true},
events: []*CalEvent{NewCalEvent(event)},
descriptionWanted: "[]Saturday, Apr 19\n []01:00-02:00 []Foo[white]\n \n",
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
w := &Widget{calEvents: tt.events, settings: tt.settings, err: nil}
_, description, err := w.content()
assert.Equal(t, false, err, tt.name)
assert.Equal(t, tt.descriptionWanted, description, tt.name)
})
}
}

View File

@@ -35,6 +35,7 @@ type Settings struct {
secretFile string `help:"Your Google client secret JSON file." values:"A string representing a file path to the JSON secret file."`
showAllDay bool `help:"Whether or not to display all-day events" values:"true or false" optional:"true" default:"true"`
showDeclined bool `help:"Whether or not to display events youve declined to attend." values:"true or false" optional:"true"`
showEndTime bool `help:"Display the end time of events, in addition to start time." values:"true or false" optional:"true" default:"false"`
withLocation bool `help:"Whether or not to show the location of the appointment." values:"true or false"`
timezone string `help:"The time zone used to display calendar event times." values:"A valid TZ database time zone string" optional:"true"`
calendarReadLevel string `help:"The calender read level specifies level you want to read events. Default: writer " values:"reader, writer" optional:"true"`
@@ -54,6 +55,7 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
multiCalendar: ymlConfig.UBool("multiCalendar", false),
secretFile: ymlConfig.UString("secretFile", ""),
showAllDay: ymlConfig.UBool("showAllDay", true),
showEndTime: ymlConfig.UBool("showEndTime", false),
showDeclined: ymlConfig.UBool("showDeclined", false),
withLocation: ymlConfig.UBool("withLocation", true),
timezone: ymlConfig.UString("timezone", ""),