Skip to content

Commit

Permalink
Added sorting and fixed negative color gradient
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Dec 27, 2020
1 parent a0948aa commit ab9f14d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
25 changes: 4 additions & 21 deletions internal/ui/component/watchlist/watchlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ const (
maxPercentChangeColorGradient = 10
)

const (
PositivePriceChange = iota
NegativePriceChange
NeutralPriceChange
)

type Model struct {
Width int
Quotes []quote.Quote
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

}
10 changes: 8 additions & 2 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ui

import (
"fmt"
"sort"
"ticker-tape/internal/quote"
"ticker-tape/internal/ui/component/watchlist"
"time"
Expand Down Expand Up @@ -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)),
}
})
}
Expand All @@ -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)),
}
}
}
Expand Down Expand Up @@ -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
}

0 comments on commit ab9f14d

Please sign in to comment.