From ab9f14dcfaee0bc909a892e21d2de9c488e97297 Mon Sep 17 00:00:00 2001 From: Ani Channarasappa Date: Sun, 27 Dec 2020 17:01:56 -0500 Subject: [PATCH] Added sorting and fixed negative color gradient --- internal/ui/component/watchlist/watchlist.go | 25 ++++---------------- internal/ui/ui.go | 10 ++++++-- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/internal/ui/component/watchlist/watchlist.go b/internal/ui/component/watchlist/watchlist.go index 0040354..e4956a1 100644 --- a/internal/ui/component/watchlist/watchlist.go +++ b/internal/ui/component/watchlist/watchlist.go @@ -24,12 +24,6 @@ const ( maxPercentChangeColorGradient = 10 ) -const ( - PositivePriceChange = iota - NegativePriceChange - NeutralPriceChange -) - type Model struct { Width int Quotes []quote.Quote @@ -110,10 +104,10 @@ func priceText(change float64, changePercent float64) string { } if change > 0.0 { - return stylePricePositive(change)("↑ " + convertFloatToString(change) + " (" + convertFloatToString(changePercent) + "%)") + return stylePricePositive(changePercent)("↑ " + convertFloatToString(change) + " (" + convertFloatToString(changePercent) + "%)") } - return stylePriceNegative(change)("↓ " + convertFloatToString(change) + " (" + convertFloatToString(changePercent) + "%)") + return stylePriceNegative(changePercent)("↓ " + convertFloatToString(change) + " (" + convertFloatToString(changePercent) + "%)") } // util @@ -145,19 +139,8 @@ func getNormalizedPercentWithMax(percent float64, maxPercent float64) float64 { absolutePercent := math.Abs(percent) if absolutePercent > maxPercent { - return 1 + return 1.0 } - - return percent / maxPercent - -} - -// Accepts a start color, end color, and percent and returns the color at the position between them -func getColorHex(startColorHex string, endColorHex string, normalizedPercent float64) string { - - c1, _ := colorful.Hex(startColorHex) - c2, _ := colorful.Hex(endColorHex) - - return c1.BlendHsv(c2, normalizedPercent).Hex() + return math.Abs(percent / maxPercent) } diff --git a/internal/ui/ui.go b/internal/ui/ui.go index 02a55c6..8727311 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -2,6 +2,7 @@ package ui import ( "fmt" + "sort" "ticker-tape/internal/quote" "ticker-tape/internal/ui/component/watchlist" "time" @@ -33,7 +34,7 @@ type Model struct { func (m Model) updateQuotes() tea.Cmd { return tea.Tick(time.Second*time.Duration(m.requestInterval), func(t time.Time) tea.Msg { return QuoteMsg{ - quotes: m.requestQuotes(m.symbols), + quotes: sortQuotes(m.requestQuotes(m.symbols)), } }) } @@ -58,7 +59,7 @@ func (m Model) Init() tea.Cmd { m.watchlist = watchlist.NewModel() return func() tea.Msg { return QuoteMsg{ - quotes: m.requestQuotes(m.symbols), + quotes: sortQuotes(m.requestQuotes(m.symbols)), } } } @@ -118,3 +119,8 @@ func (m Model) View() string { func footer(elementWidth int) string { return footerHighlightStyle(" 🚀 ticker-tape ") + helpStyle(" q: exit") } + +func sortQuotes(q []quote.Quote) []quote.Quote { + sort.Slice(q, func(i, j int) bool { return q[i].RegularMarketChangePercent < q[j].RegularMarketChangePercent }) + return q +}