-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.go
More file actions
75 lines (63 loc) · 1.66 KB
/
Copy pathdisplay.go
File metadata and controls
75 lines (63 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"os"
"strings"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
)
func getTable(stocks []ChartResponse) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Ticker", "Last Price", "Change", "Change %", "Prev. Close", "Currency"})
for _, stock := range stocks {
row := getRow(stock)
t.AppendRow(row)
}
t.SetColumnConfigs([]table.ColumnConfig{
{
Name: "Change",
Transformer: text.Transformer(func(val interface{}) string {
return getColoredChangeCell(val, "")
}),
},
{
Name: "Change %",
Transformer: text.Transformer(func(val interface{}) string {
return getColoredChangeCell(val, "%")
}),
},
})
t.SetStyle(table.StyleColoredCyanWhiteOnBlack)
t.Render()
}
func getRow(stock ChartResponse) table.Row {
data := stock.Chart.Result[0].Meta
diff := data.RegularMarketPrice - data.PreviousClose
ticker := data.Symbol
lastPrice := data.RegularMarketPrice
change := appendPlus(diff)
changePercent := appendPlus(diff / data.PreviousClose * 100)
currency := data.Currency
previousClose := data.PreviousClose
return table.Row{ticker, lastPrice, change, changePercent, previousClose, currency}
}
func getColoredChangeCell(val interface{}, postfix string) string {
strVal, ok := val.(string)
if !ok {
return "0.00" + postfix
}
var color text.Color
if strings.Contains(strVal, "-") {
color = text.FgRed
} else if strings.Contains(strVal, "+") {
color = text.FgGreen
}
return text.Colors{color}.Sprint(strVal + postfix)
}
func appendPlus(num float64) string {
if num >= 0 {
return fmt.Sprintf("+%.2f", num)
}
return fmt.Sprintf("%.2f", num)
}