From c8a69e9146a163108b1674abb0078f7f68eca671 Mon Sep 17 00:00:00 2001 From: joce Date: Tue, 15 Feb 2022 20:57:51 -0500 Subject: [PATCH] Show % in the header in red if < 0 --- cnn_market.go | 8 ++++---- layout.go | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/cnn_market.go b/cnn_market.go index f7d0648..06923d1 100644 --- a/cnn_market.go +++ b/cnn_market.go @@ -156,16 +156,16 @@ func (market *Market) extract(snippet []byte) *Market { market.Yield[`change`] = matches[11] market.Oil[`latest`] = matches[12] - market.Oil[`change`] = matches[13] + market.Oil[`change`] = matches[13] + `%` market.Yen[`latest`] = matches[14] - market.Yen[`change`] = matches[15] + market.Yen[`change`] = matches[15] + `%` market.Euro[`latest`] = matches[16] - market.Euro[`change`] = matches[17] + market.Euro[`change`] = matches[17] + `%` market.Gold[`latest`] = matches[18] - market.Gold[`change`] = matches[19] + market.Gold[`change`] = matches[19] + `%` market.Tokyo[`change`] = matches[20] market.Tokyo[`latest`] = matches[21] diff --git a/layout.go b/layout.go index 4d88d2e..b0da111 100644 --- a/layout.go +++ b/layout.go @@ -9,6 +9,7 @@ import ( "fmt" "reflect" "regexp" + "strconv" "strings" "text/template" "time" @@ -210,7 +211,7 @@ func (layout *Layout) pad(str string, width int) string { func buildMarketTemplate() *template.Template { markup := `Dow {{.Dow.change}} ({{.Dow.percent}}) at {{.Dow.latest}} S&P 500 {{.Sp500.change}} ({{.Sp500.percent}}) at {{.Sp500.latest}} NASDAQ {{.Nasdaq.change}} ({{.Nasdaq.percent}}) at {{.Nasdaq.latest}} Tokyo {{.Tokyo.change}} ({{.Tokyo.percent}}) at {{.Tokyo.latest}} HK {{.HongKong.change}} ({{.HongKong.percent}}) at {{.HongKong.latest}} London {{.London.change}} ({{.London.percent}}) at {{.London.latest}} Frankfurt {{.Frankfurt.change}} ({{.Frankfurt.percent}}) at {{.Frankfurt.latest}} {{if .IsClosed}}U.S. markets closed{{end}} -10-Year Yield {{.Yield.latest}}% ({{.Yield.change}}) Euro ${{.Euro.latest}} ({{.Euro.change}}%) Yen ¥{{.Yen.latest}} ({{.Yen.change}}%) Oil ${{.Oil.latest}} ({{.Oil.change}}%) Gold ${{.Gold.latest}} ({{.Gold.change}}%)` +10-Year Yield {{.Yield.latest}} ({{.Yield.change}}) Euro ${{.Euro.latest}} ({{.Euro.change}}) Yen ¥{{.Yen.latest}} ({{.Yen.change}}) Oil ${{.Oil.latest}} ({{.Oil.change}}) Gold ${{.Gold.latest}} ({{.Gold.change}})` return template.Must(template.New(`market`).Parse(markup)) } @@ -231,8 +232,17 @@ func buildQuotesTemplate() *template.Template { //----------------------------------------------------------------------------- func highlight(collections ...map[string]string) { for _, collection := range collections { - if collection[`change`][0:1] != `-` { - collection[`change`] = `` + collection[`change`] + `` + change := collection[`change`] + if change[len(change)-1:] == `%` { + change = change[0:len(change)-1] + } + adv, err := strconv.ParseFloat(change, 64) + if err == nil { + if adv < 0.0 { + collection[`change`] = `` + collection[`change`] + `` + } else if adv > 0.0 { + collection[`change`] = `` + collection[`change`] + `` + } } } }