Skip to content

Commit 7d0a2cf

Browse files
committed
Support specifying columns to show
1 parent 0382742 commit 7d0a2cf

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Options:
7272
too frequent refresh may cause your IP banned by their servers
7373
-c, --config-file string Config file path, refer to "token_ticker.example.yaml" for the format,
7474
by default token-ticker uses "token_ticker.yml" in current directory or $HOME as config file
75+
-s, --show strings Only show comma-separated columns (default [Symbol,Price,%Change(1h),%Change(24h),Source,Updated])
7576
-p, --proxy string Proxy used when sending HTTP request
7677
(eg. "http://localhost:7777", "https://localhost:7777", "socks5://localhost:1080")
7778
-t, --timeout int HTTP request timeout in seconds (default 20)

exchange/okex.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (client *okexClient) GetSymbolPrice(symbol string) (*SymbolPrice, error) {
135135
percentChange1h = (respJSON.Ticker.Last - price1hAgo) / price1hAgo * 100
136136
}
137137

138-
time.Sleep(time.Second) // ZB limits 1 req/sec for Kline
138+
time.Sleep(time.Second) // Limit 1 req/sec for Kline
139139
price24hAgo, err := client.GetKlinePrice(symbol, "3min", 492) // Why not 480?
140140
if err != nil {
141141
logrus.Warnf("%s - Failed to get price 24 hours ago, error: %v\n", client.GetName(), err)

main.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ var (
2727
Rev string
2828
)
2929

30+
const (
31+
colSymbol = "Symbol"
32+
colPrice = "Price"
33+
colChange1hPct = "%Change(1h)"
34+
colChange24hPct = "%Change(24h)"
35+
colSource = "Source"
36+
colUpdated = "Updated"
37+
)
38+
3039
type exchangeConfig struct {
3140
Name string
3241
Tokens []string
@@ -111,19 +120,41 @@ func renderTable(symbolPriceList []*SymbolPrice, writer *uilive.Writer) {
111120
table := tablewriter.NewWriter(writer)
112121
table.SetAutoFormatHeaders(false)
113122
table.SetAutoWrapText(false)
114-
headers := []string{color.YellowString("Symbol"), color.YellowString("Price"),
115-
color.YellowString("%Change(1h)"), color.YellowString("%Change(24h)"),
116-
color.YellowString("Source"), color.YellowString("Updated")}
117-
table.SetHeader(headers)
123+
headers := viper.GetStringSlice("show")
124+
formattedHeaders := make([]string, len(headers))
125+
for i, hdr := range headers {
126+
formattedHeaders[i] = color.YellowString(hdr)
127+
}
128+
table.SetHeader(formattedHeaders)
118129
table.SetRowLine(true)
119130
table.SetCenterSeparator(faint("-"))
120131
table.SetColumnSeparator(faint("|"))
121132
table.SetRowSeparator(faint("-"))
122133

123134
// Fill in data
124135
for _, sp := range symbolPriceList {
125-
table.Append([]string{sp.Symbol, sp.Price, highlightChange(sp.PercentChange1h),
126-
highlightChange(sp.PercentChange24h), sp.Source, sp.UpdateAt.Local().Format("15:04:05")})
136+
var columns []string
137+
for _, hdr := range headers {
138+
switch strings.ToLower(hdr) {
139+
case strings.ToLower(colSymbol):
140+
columns = append(columns, sp.Symbol)
141+
case strings.ToLower(colPrice):
142+
columns = append(columns, sp.Price)
143+
case strings.ToLower(colChange1hPct):
144+
columns = append(columns, highlightChange(sp.PercentChange1h))
145+
case strings.ToLower(colChange24hPct):
146+
columns = append(columns, highlightChange(sp.PercentChange24h))
147+
case strings.ToLower(colSource):
148+
columns = append(columns, sp.Source)
149+
case strings.ToLower(colUpdated):
150+
columns = append(columns, sp.UpdateAt.Local().Format("15:04:05"))
151+
default:
152+
fmt.Fprintf(os.Stderr, "Unknown column: %s\n", hdr)
153+
os.Exit(1)
154+
}
155+
156+
}
157+
table.Append(columns)
127158
}
128159

129160
table.Render()
@@ -177,13 +208,15 @@ func init() {
177208
showHelp := pflag.BoolP("help", "h", false, "Show usage message")
178209
pflag.CommandLine.MarkHidden("help")
179210
pflag.BoolP("debug", "d", false, "Enable debug mode")
180-
showList := pflag.BoolP("list-exchanges", "l", false, "List supported exchanges")
211+
showExchanges := pflag.BoolP("list-exchanges", "l", false, "List supported exchanges")
181212
pflag.IntP("refresh", "r", 0, "Auto refresh on every specified seconds, "+
182213
"note every exchange has a rate limit, \ntoo frequent refresh may cause your IP banned by their servers")
183214
var configFile string
184215
pflag.StringVarP(&configFile, "config-file", "c", "", "Config file path, "+
185216
"refer to \"token_ticker.example.yaml\" for the format, \nby default token-ticker uses \"token_ticker.yml\" "+
186217
"in current directory or $HOME as config file")
218+
pflag.StringSliceP("show", "s", []string{colSymbol, colPrice, colChange1hPct, colChange24hPct, colSource, colUpdated},
219+
"Only show comma-separated columns")
187220
pflag.StringP("proxy", "p", "", "Proxy used when sending HTTP request \n(eg. "+
188221
"\"http://localhost:7777\", \"https://localhost:7777\", \"socks5://localhost:1080\")")
189222
pflag.IntP("timeout", "t", 20, "HTTP request timeout in seconds")
@@ -204,7 +237,7 @@ func init() {
204237
os.Exit(0)
205238
}
206239

207-
if *showList {
240+
if *showExchanges {
208241
fmt.Fprintln(os.Stderr, "Supported exchanges:")
209242
for _, name := range ListExchanges() {
210243
fmt.Fprintf(os.Stderr, " %s\n", name)

token_ticker.example.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
## Running in debug mode
1414
# debug: true
1515

16+
# Specify columns to show
17+
# show:
18+
# - Symbol
19+
# - Price
20+
# - "%Change(1h)"
21+
# - "%Change(24h)"
22+
# - Source
23+
# - Updated
24+
1625
exchanges:
1726
## Exchanges are identified by name, following are supported exchanges
1827
- name: CoinMarketCap

0 commit comments

Comments
 (0)