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+
3039type 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, \n too 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, \n by 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 )
0 commit comments