Skip to content

Commit 1e7dc2a

Browse files
fix: ignore matches with no teams + change initial cursor strategy
1 parent 72d5aec commit 1e7dc2a

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

internal/timeutil/day.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ func IsToday(date time.Time) bool {
1616
return compareDay(date, today)
1717
}
1818

19+
func IsBeforeToday(date time.Time) bool {
20+
now := time.Now()
21+
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
22+
return date.Before(today)
23+
}
24+
1925
// IsTomorrow returns true if the given date is tomorrow, false otherwise.
2026
func IsTomorrow(date time.Time) bool {
2127
tomorrow := time.Now().AddDate(0, 0, 1)

internal/timeutil/day_test.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ func TestIsYesterday(t *testing.T) {
1717
want bool
1818
}{
1919
{
20-
name: "with yesterday date returns true",
20+
name: "yesterday returns true",
2121
date: time.Now().AddDate(0, 0, -1),
2222
want: true,
2323
},
2424
{
25-
name: "with Faker birthday returns false",
25+
name: "Faker birthday returns false",
2626
date: goatBirthday,
2727
want: false,
2828
},
@@ -43,12 +43,12 @@ func TestIsToday(t *testing.T) {
4343
want bool
4444
}{
4545
{
46-
name: "with current date returns true",
46+
name: "current date returns true",
4747
date: time.Now(),
4848
want: true,
4949
},
5050
{
51-
name: "with Faker birthday returns false",
51+
name: "Faker birthday returns false",
5252
date: goatBirthday,
5353
want: false,
5454
},
@@ -62,19 +62,44 @@ func TestIsToday(t *testing.T) {
6262
}
6363
}
6464

65+
func TestIsBeforeToday(t *testing.T) {
66+
tt := []struct {
67+
name string
68+
date time.Time
69+
want bool
70+
}{
71+
{
72+
name: "yesterday returns true",
73+
date: time.Now().AddDate(0, 0, -1),
74+
want: true,
75+
},
76+
{
77+
name: "today returns false",
78+
date: time.Now(),
79+
want: false,
80+
},
81+
}
82+
for _, tc := range tt {
83+
t.Run(tc.name, func(t *testing.T) {
84+
got := timeutil.IsBeforeToday(tc.date)
85+
require.Equal(t, tc.want, got)
86+
})
87+
}
88+
}
89+
6590
func TestIsTomorrow(t *testing.T) {
6691
tt := []struct {
6792
name string
6893
date time.Time
6994
want bool
7095
}{
7196
{
72-
name: "with tomorrow date returns true",
97+
name: "tomorrow returns true",
7398
date: time.Now().AddDate(0, 0, 1),
7499
want: true,
75100
},
76101
{
77-
name: "with Faker birthday returns false",
102+
name: "Faker birthday returns false",
78103
date: goatBirthday,
79104
want: false,
80105
},

internal/ui/matches.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/charmbracelet/lipgloss"
1414
"github.com/charmbracelet/x/ansi"
1515
"github.com/matthieugusmini/go-lolesports"
16-
1716
"github.com/matthieugusmini/rift/internal/timeutil"
1817
)
1918

@@ -93,15 +92,25 @@ func newMatchList(events []lolesports.Event, width, height int) list.Model {
9392
l.SetSpinner(spinner.MiniDot)
9493
l.SetShowHelp(false)
9594

96-
// We use the first match of the day as the starting position for the list cursor.
97-
firstTodayMatchIndex := slices.IndexFunc(events, func(event lolesports.Event) bool {
98-
return timeutil.IsToday(event.StartTime)
99-
})
100-
l.Select(firstTodayMatchIndex)
95+
cursorStartingPos := indexMatchListInitialCursor(events)
96+
l.Select(cursorStartingPos)
10197

10298
return l
10399
}
104100

101+
func indexMatchListInitialCursor(events []lolesports.Event) int {
102+
// The starting position of the cursor is the first match of the first day
103+
// with a match starting from today.
104+
cursorStartingPos := slices.IndexFunc(events, func(event lolesports.Event) bool {
105+
return !timeutil.IsBeforeToday(event.StartTime)
106+
})
107+
// Fallback to 0 but it should never happen. Right?
108+
if cursorStartingPos == -1 {
109+
return 0
110+
}
111+
return cursorStartingPos
112+
}
113+
105114
type matchItemStyles struct {
106115
// Item
107116
normalItem lipgloss.Style

internal/ui/schedule_page.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,11 @@ func filterMatchEvents(events []lolesports.Event) []lolesports.Event {
528528
continue
529529
}
530530

531+
// Matches does not always have teams somehow.
532+
if len(event.Match.Teams) != 2 {
533+
continue
534+
}
535+
531536
matches = append(matches, event)
532537
}
533538

0 commit comments

Comments
 (0)