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 }