-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (118 loc) · 3.43 KB
/
main.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
"os"
"slices"
"time"
"github.com/NimbleMarkets/ntcharts/linechart/timeserieslinechart"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
)
func main() {
var chartPeriod string = "24h"
var currency = Currency{name: "USD", rate: 1.0, symbol: "$"}
var cryptoSymbol string
if len(os.Args) <= 1 {
printHelp()
}
if slices.Contains(os.Args, "-h") || slices.Contains(os.Args, "--help") {
printHelp()
os.Exit(0)
}
if slices.Contains(os.Args, "-p") || slices.Contains(os.Args, "--period") {
chartPeriod = getArgValue(os.Args, "-p", "--period")
}
cryptoSymbol = os.Args[1]
var cryptoData, err = getCoinData(cryptoSymbol)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var cryptoChart, chartErr = getChart(cryptoSymbol, chartPeriod)
if chartErr != nil {
fmt.Println(chartErr)
os.Exit(1)
}
printCoinData(cryptoData, cryptoChart, currency)
}
func printCoinData(coin Crypto, chartData [][]float64, currency Currency) {
var style = lipgloss.NewStyle().
Bold(true)
fmt.Print(style.Render(coin.Name + " (" + coin.Symbol + "): "))
var priceColor string = "#067213"
if coin.PriceChangePercentageDay < 0 {
priceColor = "#ff0000"
}
style = lipgloss.NewStyle().
Background(lipgloss.Color(priceColor))
fmt.Println(style.Render(FormatPrice(currency.symbol, coin.Price)))
printChart(chartData)
dataTable := table.New().
Border(lipgloss.NormalBorder()).
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("99"))).
Width(70).
StyleFunc(func(row, col int) lipgloss.Style {
if row%2 == 0 {
return lipgloss.NewStyle().
Align(lipgloss.Left).
MarginLeft(3).Bold(true)
} else {
return lipgloss.NewStyle().
Align(lipgloss.Left).
MarginLeft(3).
Bold(false)
}
})
dataTable.Row("Market Cap", FormatPrice(currency.symbol, coin.MarketCap))
dataTable.Row("Volume", FormatPrice(currency.symbol, coin.Volume))
dataTable.Row("Max supply", FormatPrice(coin.Symbol, coin.TotalSupply))
dataTable.Row("Circulating supply", FormatPrice(coin.Symbol, coin.CirculatingSupply))
dataTable.Row("% of supply in circulation", fmt.Sprintf("%.2f %%", (coin.CirculatingSupply/coin.TotalSupply)*100))
dataTable.Row("Homepage", coin.Website)
// style = lipgloss.NewStyle().
// Align(lipgloss.Center)
fmt.Println(dataTable.String())
}
func printChart(chart [][]float64) {
var minPrice float64 = chart[0][1]
var maxPrice float64 = 0
for i := 0; i < len(chart); i++ {
if chart[i][1] < minPrice {
minPrice = chart[i][1]
} else if chart[i][1] > maxPrice {
maxPrice = chart[i][1]
}
}
slc := timeserieslinechart.New(70, 15, timeserieslinechart.WithYRange(minPrice, maxPrice))
for i := 0; i < len(chart); i++ {
slc.Push(timeserieslinechart.TimePoint{Time: time.Unix(int64(chart[i][0]), 0), Value: chart[i][1]})
}
slc.DrawBraille()
var style = lipgloss.NewStyle().
Foreground(lipgloss.Color("99")).
Margin(1)
fmt.Println(style.Render(slc.View()))
}
func getArgValue(args []string, shortFlag, longFlag string) string {
for i, arg := range args {
if arg == shortFlag || arg == longFlag {
if i+1 < len(args) {
return args[i+1]
}
}
}
return ""
}
func printHelp() {
fmt.Println(
`
Usage: tcoin <coin>
Options:
-h, --help Display this help message
-p, --period Period of the chart (24h, 1w, 1m, 3m, 6m, 1y, all)
Example: tcoin bitcoin
Example: tcoin ethereum -p 1y
Example: tcoin monero --period 1m
`)
os.Exit(0)
}