-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
407 lines (367 loc) · 10.6 KB
/
table.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package cointop
import (
"fmt"
"math"
"net/url"
"strconv"
"strings"
"time"
"github.com/cdyfng/coind/cointop/common/humanize"
"github.com/cdyfng/coind/cointop/common/pad"
"github.com/cdyfng/coind/cointop/common/table"
)
// TableView is structure for table view
type TableView struct {
*View
}
// NewTableView returns a new table view
func NewTableView() *TableView {
return &TableView{NewView("table")}
}
// TableColumnOrder returns the default order of the table columns
func TableColumnOrder() []string {
return []string{
"rank",
"name",
"symbol",
"price",
"holdings",
"balance",
"marketcap",
"24hvolume",
"1hchange",
"7dchange",
"1dchange",
"1ychange",
"totalsupply",
"availablesupply",
"percentholdings",
"lastupdated",
}
}
const dots = "..."
// RefreshTable refreshes the table
func (ct *Cointop) RefreshTable() error {
ct.debuglog("refreshTable()")
maxX := ct.width()
ct.table = table.New().SetWidth(maxX)
ct.table.HideColumHeaders = true
if ct.State.portfolioVisible {
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
total := ct.getPortfolioTotal()
for _, coin := range ct.State.coins {
unix, _ := strconv.ParseInt(coin.LastUpdated, 10, 64)
lastUpdated := time.Unix(unix, 0).Format("15:04:05 Jan 02")
colorbalance := ct.colorscheme.TableColumnPrice
color24h := ct.colorscheme.TableColumnChange
if coin.PercentChange24H > 0 {
color24h = ct.colorscheme.TableColumnChangeUp
}
if coin.PercentChange24H < 0 {
color24h = ct.colorscheme.TableColumnChangeDown
}
name := coin.Name
star := ct.colorscheme.TableRow(" ")
if coin.Favorite {
star = ct.colorscheme.TableRowFavorite("*")
}
rank := fmt.Sprintf("%s%v", star, ct.colorscheme.TableRow(fmt.Sprintf("%6v ", coin.Rank)))
if len(name) > 20 {
name = fmt.Sprintf("%s%s", name[0:18], dots)
}
namecolor := ct.colorscheme.TableRow
if coin.Favorite {
namecolor = ct.colorscheme.TableRowFavorite
}
percentHoldings := (coin.Balance / total) * 1e2
if math.IsNaN(percentHoldings) {
percentHoldings = 0
}
ct.table.AddRow(
rank,
namecolor(pad.Right(fmt.Sprintf("%.22s", name), 21, " ")),
ct.colorscheme.TableRow(pad.Right(fmt.Sprintf("%.6s", coin.Symbol), 5, " ")),
ct.colorscheme.TableRow(fmt.Sprintf("%13s", humanize.Commaf(coin.Price))),
ct.colorscheme.TableRow(fmt.Sprintf("%15s", strconv.FormatFloat(coin.Holdings, 'f', -1, 64))),
colorbalance(fmt.Sprintf("%15s", humanize.Commaf(coin.Balance))),
color24h(fmt.Sprintf("%8.2f%%", coin.PercentChange24H)),
ct.colorscheme.TableRow(fmt.Sprintf("%10.2f%%", percentHoldings)),
ct.colorscheme.TableRow(pad.Right(fmt.Sprintf("%17s", lastUpdated), 80, " ")),
)
}
} else {
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
ct.table.AddCol("")
for _, coin := range ct.State.coins {
if coin == nil {
continue
}
unix, _ := strconv.ParseInt(coin.LastUpdated, 10, 64)
lastUpdated := time.Unix(unix, 0).Format("15:04:05 Jan 02")
namecolor := ct.colorscheme.TableRow
color1h := ct.colorscheme.TableColumnChange
color24h := ct.colorscheme.TableColumnChange
color7d := ct.colorscheme.TableColumnChange
color30d := ct.colorscheme.TableColumnChange
color1y := ct.colorscheme.TableColumnChange
if coin.Favorite {
namecolor = ct.colorscheme.TableRowFavorite
}
if coin.PercentChange1H > 0 {
color1h = ct.colorscheme.TableColumnChangeUp
}
if coin.PercentChange1H < 0 {
color1h = ct.colorscheme.TableColumnChangeDown
}
if coin.PercentChange24H > 0 {
color24h = ct.colorscheme.TableColumnChangeUp
}
if coin.PercentChange24H < 0 {
color24h = ct.colorscheme.TableColumnChangeDown
}
if coin.PercentChange7D > 0 {
color7d = ct.colorscheme.TableColumnChangeUp
}
if coin.PercentChange7D < 0 {
color7d = ct.colorscheme.TableColumnChangeDown
}
if coin.PercentChange30D > 0 {
color30d = ct.colorscheme.TableColumnChangeUp
}
if coin.PercentChange30D < 0 {
color30d = ct.colorscheme.TableColumnChangeDown
}
if coin.PercentChange1Y > 0 {
color1y = ct.colorscheme.TableColumnChangeUp
}
if coin.PercentChange1Y < 0 {
color1y = ct.colorscheme.TableColumnChangeDown
}
name := coin.Name
star := ct.colorscheme.TableRow(" ")
if coin.Favorite {
star = ct.colorscheme.TableRowFavorite("*")
}
rank := fmt.Sprintf("%s%v", star, ct.colorscheme.TableRow(fmt.Sprintf("%6v ", coin.Rank)))
if len(name) > 20 {
name = fmt.Sprintf("%s%s", name[0:18], dots)
}
symbolpadding := 5
// NOTE: this is to adjust padding by 1 because when all name rows are
// yellow it messes the spacing (need to debug)
if ct.State.filterByFavorites {
symbolpadding = 6
}
ct.table.AddRow(
rank,
namecolor(pad.Right(fmt.Sprintf("%.22s", name), 21, " ")),
ct.colorscheme.TableRow(pad.Right(fmt.Sprintf("%.6s", coin.Symbol), symbolpadding, " ")),
ct.colorscheme.TableColumnPrice(fmt.Sprintf("%12s", humanize.Commaf(coin.Price))),
ct.colorscheme.TableRow(fmt.Sprintf("%18s", humanize.Commaf(coin.MarketCap))),
ct.colorscheme.TableRow(fmt.Sprintf("%15s", humanize.Commaf(coin.Volume24H))),
color1h(fmt.Sprintf("%8.2f%%", coin.PercentChange1H)),
color24h(fmt.Sprintf("%8.2f%%", coin.PercentChange24H)),
color7d(fmt.Sprintf("%8.2f%%", coin.PercentChange7D)),
color30d(fmt.Sprintf("%8.2f%%", coin.PercentChange30D)),
color1y(fmt.Sprintf("%8.2f%%", coin.PercentChange1Y)),
ct.colorscheme.TableRow(fmt.Sprintf("%21s", humanize.Commaf(coin.TotalSupply))),
ct.colorscheme.TableRow(fmt.Sprintf("%18s", humanize.Commaf(coin.AvailableSupply))),
ct.colorscheme.TableRow(fmt.Sprintf("%18s", lastUpdated)),
// TODO: add %percent of cap
)
}
}
// highlight last row if current row is out of bounds (can happen when switching views)
currentrow := ct.HighlightedRowIndex()
if len(ct.State.coins) > currentrow {
ct.highlightRow(currentrow)
}
ct.Update(func() error {
if ct.Views.Table.Backing() == nil {
return nil
}
ct.Views.Table.Backing().Clear()
ct.table.Format().Fprint(ct.Views.Table.Backing())
go ct.RowChanged()
go ct.UpdateTableHeader()
go ct.updateMarketbar()
go ct.UpdateChart()
return nil
})
return nil
}
// UpdateTable updates the table
func (ct *Cointop) UpdateTable() error {
ct.debuglog("UpdateTable()")
ct.State.allCoinsSlugMap.Range(func(key, value interface{}) bool {
k := key.(string)
if v, ok := value.(*Coin); ok {
v.Favorite = ct.State.favorites[v.Name]
ct.State.allCoinsSlugMap.Store(k, v)
}
return true
})
if ct.State.filterByFavorites {
ct.State.coins = ct.getFavoritesSlice()
} else if ct.State.portfolioVisible {
ct.State.coins = ct.getPortfolioSlice()
} else {
// TODO: maintain state of previous sorting
if ct.State.sortBy == "holdings" {
ct.State.sortBy = "rank"
ct.State.sortDesc = false
}
ct.State.coins = ct.GetTableCoinsSlice()
}
ct.sort(ct.State.sortBy, ct.State.sortDesc, ct.State.coins, true)
go ct.RefreshTable()
return nil
}
// GetTableCoinsSlice returns a slice of the table rows
func (ct *Cointop) GetTableCoinsSlice() []*Coin {
ct.debuglog("GetTableCoinsSlice()")
sliced := []*Coin{}
start := ct.State.page * ct.State.perPage
end := start + ct.State.perPage
allCoins := ct.AllCoins()
size := len(allCoins)
if start < 0 {
start = 0
}
if end >= size-1 {
start = int(math.Floor(float64(start/100)) * 100)
end = size - 1
}
if start < 0 {
start = 0
}
if end >= size {
end = size - 1
}
if end < 0 {
end = 0
}
if start >= end {
return nil
}
if end > 0 {
sliced = allCoins[start:end]
// NOTE: restore rank
for _, coin := range sliced {
icoin, _ := ct.State.allCoinsSlugMap.Load(coin.Name)
if icoin != nil {
c, _ := icoin.(*Coin)
coin.Rank = c.Rank
}
}
}
return sliced
}
// HighlightedRowIndex returns the index of the highlighted row
func (ct *Cointop) HighlightedRowIndex() int {
ct.debuglog("HighlightedRowIndex()")
_, y := ct.Views.Table.Backing().Origin()
_, cy := ct.Views.Table.Backing().Cursor()
idx := y + cy
if idx < 0 {
idx = 0
}
if idx >= len(ct.State.coins) {
idx = len(ct.State.coins) - 1
}
return idx
}
// HighlightedRowCoin returns the coin at the index of the highlighted row
func (ct *Cointop) HighlightedRowCoin() *Coin {
ct.debuglog("HighlightedRowCoin()")
idx := ct.HighlightedRowIndex()
if len(ct.State.coins) == 0 {
return nil
}
return ct.State.coins[idx]
}
// HighlightedPageRowIndex returns the index of page row of the highlighted row
func (ct *Cointop) HighlightedPageRowIndex() int {
ct.debuglog("HighlightedPageRowIndex()")
_, cy := ct.Views.Table.Backing().Cursor()
idx := cy
if idx < 0 {
idx = 0
}
return idx
}
// RowLink returns the row url link
func (ct *Cointop) RowLink() string {
ct.debuglog("RowLink()")
coin := ct.HighlightedRowCoin()
if coin == nil {
return ""
}
return ct.api.CoinLink(coin.Name)
}
// RowLinkShort returns a shortened version of the row url link
func (ct *Cointop) RowLinkShort() string {
ct.debuglog("RowLinkShort()")
link := ct.RowLink()
if link != "" {
u, err := url.Parse(link)
if err != nil {
return ""
}
host := u.Hostname()
host = strings.Replace(host, "www.", "", -1)
path := u.EscapedPath()
parts := strings.Split(path, "/")
if len(parts) > 0 {
path = parts[len(parts)-1]
}
return fmt.Sprintf("http://%s/%s/%s", host, dots, path)
}
return ""
}
// ToggleTableFullscreen toggles the table fullscreen mode
func (ct *Cointop) ToggleTableFullscreen() error {
ct.debuglog("ToggleTableFullscreen()")
ct.State.onlyTable = !ct.State.onlyTable
if ct.State.onlyTable {
} else {
// NOTE: cached values are initial config settings.
// If the only-table config was set then toggle
// all other initial hidden views.
onlyTable, _ := ct.cache.Get("onlyTable")
if onlyTable.(bool) {
ct.State.hideMarketbar = false
ct.State.hideChart = false
ct.State.hideStatusbar = false
} else {
// NOTE: cached values store initial hidden views preferences.
hideMarketbar, _ := ct.cache.Get("hideMarketbar")
ct.State.hideMarketbar = hideMarketbar.(bool)
hideChart, _ := ct.cache.Get("hideChart")
ct.State.hideChart = hideChart.(bool)
hideStatusbar, _ := ct.cache.Get("hideStatusbar")
ct.State.hideStatusbar = hideStatusbar.(bool)
}
}
return nil
}