From 0681a04607ab9502135b6535975af8a4488f07f6 Mon Sep 17 00:00:00 2001 From: fawn Date: Sat, 18 May 2024 03:07:34 +0300 Subject: [PATCH] Add custom sorting to the twitch channels widget this allows the channels to be sorted as defined in the config while still keeping the live channels on the top. the wording could probably be improved, "custom" is too broad but i'm not sure what else to call it. --- docs/configuration.md | 4 ++++ internal/feed/twitch.go | 6 ++++++ internal/widget/twitch-channels.go | 12 +++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/docs/configuration.md b/docs/configuration.md index e698fdd..92dab52 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1063,6 +1063,7 @@ Preview: | ---- | ---- | -------- | ------- | | channels | array | yes | | | collapse-after | integer | no | 5 | +| sort-by | string | no | viewers | ##### `channels` A list of channels to display. @@ -1070,6 +1071,9 @@ A list of channels to display. ##### `collapse-after` How many channels are visible before the "SHOW MORE" button appears. Set to `-1` to never collapse. +##### `sort-by` +Can be used to specify the order in which the channels are displayed. Possible values are `viewers` and `custom`. + ### Twitch top games Display a list of games with the most viewers on Twitch. diff --git a/internal/feed/twitch.go b/internal/feed/twitch.go index 1ff82ee..1ce9354 100644 --- a/internal/feed/twitch.go +++ b/internal/feed/twitch.go @@ -44,6 +44,12 @@ func (channels TwitchChannels) SortByViewers() { }) } +func (channels TwitchChannels) SortByLive() { + sort.SliceStable(channels, func(i, j int) bool { + return channels[i].IsLive && !channels[j].IsLive + }) +} + type twitchOperationResponse struct { Data json.RawMessage Extensions struct { diff --git a/internal/widget/twitch-channels.go b/internal/widget/twitch-channels.go index 3f36669..3107081 100644 --- a/internal/widget/twitch-channels.go +++ b/internal/widget/twitch-channels.go @@ -14,6 +14,7 @@ type TwitchChannels struct { ChannelsRequest []string `yaml:"channels"` Channels []feed.TwitchChannel `yaml:"-"` CollapseAfter int `yaml:"collapse-after"` + SortBy string `yaml:"sort-by"` } func (widget *TwitchChannels) Initialize() error { @@ -23,6 +24,10 @@ func (widget *TwitchChannels) Initialize() error { widget.CollapseAfter = 5 } + if widget.SortBy != "viewers" && widget.SortBy != "custom" { + widget.SortBy = "viewers" + } + return nil } @@ -33,7 +38,12 @@ func (widget *TwitchChannels) Update(ctx context.Context) { return } - channels.SortByViewers() + if widget.SortBy == "viewers" { + channels.SortByViewers() + } else if widget.SortBy == "custom" { + channels.SortByLive() + } + widget.Channels = channels }