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

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 <chriscummer@me.com>
This commit is contained in:
Chris Cummer 2020-09-29 10:06:43 -07:00
parent 787d27b498
commit 5e44a01699

View File

@ -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,