Show % in the header in red if < 0

master
joce 3 years ago
parent 5fbeb457d5
commit c8a69e9146
  1. 8
      cnn_market.go
  2. 16
      layout.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]

@ -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 := `<yellow>Dow</> {{.Dow.change}} ({{.Dow.percent}}) at {{.Dow.latest}} <yellow>S&P 500</> {{.Sp500.change}} ({{.Sp500.percent}}) at {{.Sp500.latest}} <yellow>NASDAQ</> {{.Nasdaq.change}} ({{.Nasdaq.percent}}) at {{.Nasdaq.latest}}
<yellow>Tokyo</> {{.Tokyo.change}} ({{.Tokyo.percent}}) at {{.Tokyo.latest}} <yellow>HK</> {{.HongKong.change}} ({{.HongKong.percent}}) at {{.HongKong.latest}} <yellow>London</> {{.London.change}} ({{.London.percent}}) at {{.London.latest}} <yellow>Frankfurt</> {{.Frankfurt.change}} ({{.Frankfurt.percent}}) at {{.Frankfurt.latest}} {{if .IsClosed}}<right>U.S. markets closed</right>{{end}}
<yellow>10-Year Yield</> {{.Yield.latest}}% ({{.Yield.change}}) <yellow>Euro</> ${{.Euro.latest}} ({{.Euro.change}}%) <yellow>Yen</> ¥{{.Yen.latest}} ({{.Yen.change}}%) <yellow>Oil</> ${{.Oil.latest}} ({{.Oil.change}}%) <yellow>Gold</> ${{.Gold.latest}} ({{.Gold.change}}%)`
<yellow>10-Year Yield</> {{.Yield.latest}} ({{.Yield.change}}) <yellow>Euro</> ${{.Euro.latest}} ({{.Euro.change}}) <yellow>Yen</> ¥{{.Yen.latest}} ({{.Yen.change}}) <yellow>Oil</> ${{.Oil.latest}} ({{.Oil.change}}) <yellow>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`] = `<green>` + 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`] = `<red>` + collection[`change`] + `</>`
} else if adv > 0.0 {
collection[`change`] = `<green>` + collection[`change`] + `</>`
}
}
}
}

Loading…
Cancel
Save