From 5e44a01699c8d412f54864addf223a76e4ec150a Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Tue, 29 Sep 2020 10:06:43 -0700 Subject: [PATCH] Sort exchange rates alphabetically and group same base rates by row color Before this, exchange rates were read from a map, which meant the display order was random. This ensures that the list of exchange rates displays in a consistent order and visually denotes rates for the same currency. Signed-off-by: Chris Cummer --- modules/exchangerates/widget.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/modules/exchangerates/widget.go b/modules/exchangerates/widget.go index c243daa2..59e85a07 100644 --- a/modules/exchangerates/widget.go +++ b/modules/exchangerates/widget.go @@ -3,6 +3,7 @@ package exchangerates import ( "fmt" "regexp" + "sort" "github.com/rivo/tview" "github.com/wtfutil/wtf/view" @@ -56,15 +57,26 @@ func (widget *Widget) content() (string, string, bool) { return widget.CommonSettings().Title, widget.err.Error(), false } + // Sort the bases alphabetically to ensure consistent display ordering + bases := []string{} + for base := range widget.settings.rates { + bases = append(bases, base) + } + sort.Strings(bases) + out := "" - idx := 0 - for base, rates := range widget.settings.rates { + + for idx, base := range bases { + rates := widget.settings.rates[base] + + rowColor := widget.CommonSettings().RowColor(idx) + for _, cur := range rates { rate := widget.rates[base][cur] out += fmt.Sprintf( "[%s]1 %s = %s %s[white]\n", - widget.CommonSettings().RowColor(idx), + rowColor, base, widget.formatConversionRate(rate), cur,