You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cointop/cointop/table_header.go

110 lines
3.3 KiB
Go

package cointop
import (
"fmt"
"strings"
"github.com/miguelmota/cointop/pkg/ui"
)
// TableHeaderView is structure for table header view
type TableHeaderView = ui.View
// NewTableHeaderView returns a new table header view
func NewTableHeaderView() *TableHeaderView {
var view *TableHeaderView = ui.NewView("table_header")
return view
}
// UpdateTableHeader renders the table header
func (ct *Cointop) UpdateTableHeader() error {
ct.debuglog("UpdateTableHeader()")
6 years ago
type t struct {
colorfn func(a ...interface{}) string
displaytext string
padleft int
padright int
arrow string
}
6 years ago
5 years ago
baseColor := ct.colorscheme.TableHeaderSprintf()
offset := 0
lb := "["
rb := "]"
noSort := ct.IsPriceAlertsVisible()
if noSort {
offset = 2
lb = ""
rb = ""
}
possibleHeaders := map[string]*t{
"rank": {baseColor, fmt.Sprintf("%sr%sank", lb, rb), 0, 1 + offset, " "},
"name": {baseColor, fmt.Sprintf("%sn%same", lb, rb), 0, 11 + offset, " "},
"symbol": {baseColor, fmt.Sprintf("%ss%symbol", lb, rb), 4, 0 + offset, " "},
"target_price": {baseColor, fmt.Sprintf("%st%sarget price", lb, rb), 2, 0 + offset, " "},
"price": {baseColor, fmt.Sprintf("%sp%srice", lb, rb), 2, 0 + offset, " "},
"frequency": {baseColor, "frequency", 1, 0, " "},
"holdings": {baseColor, fmt.Sprintf("%sh%soldings", lb, rb), 5, 0 + offset, " "},
"balance": {baseColor, fmt.Sprintf("%sb%salance", lb, rb), 5, 0, " "},
"marketcap": {baseColor, fmt.Sprintf("%sm%sarket cap", lb, rb), 5, 0 + offset, " "},
"24h_volume": {baseColor, fmt.Sprintf("24H %sv%solume", lb, rb), 3, 0 + offset, " "},
"1h_change": {baseColor, fmt.Sprintf("%s1%sH%%", lb, rb), 5, 0 + offset, " "},
"24h_change": {baseColor, fmt.Sprintf("%s2%s4H%%", lb, rb), 3, 0 + offset, " "},
"7d_change": {baseColor, fmt.Sprintf("%s7%sD%%", lb, rb), 4, 0 + offset, " "},
"total_supply": {baseColor, fmt.Sprintf("%st%sotal supply", lb, rb), 7, 0 + offset, " "},
"available_supply": {baseColor, fmt.Sprintf("%sa%svailable supply", lb, rb), 1, 0 + offset, " "},
"percent_holdings": {baseColor, fmt.Sprintf("%s%%%sholdings", lb, rb), 2, 0 + offset, " "},
"last_updated": {baseColor, fmt.Sprintf("last %su%spdated", lb, rb), 3, 0, " "},
}
6 years ago
for k := range possibleHeaders {
possibleHeaders[k].arrow = " "
5 years ago
if ct.State.sortBy == k {
possibleHeaders[k].colorfn = ct.colorscheme.TableHeaderColumnActiveSprintf()
5 years ago
if ct.State.sortDesc {
possibleHeaders[k].arrow = "▼"
} else {
possibleHeaders[k].arrow = "▲"
}
}
}
6 years ago
var cols []string
switch ct.State.selectedView {
case PortfolioView:
cols = ct.GetPortfolioTableHeaders()
case PriceAlertsView:
cols = ct.GetPriceAlertsTableHeaders()
default:
cols = ct.GetCoinsTableHeaders()
6 years ago
}
var headers []string
for _, v := range cols {
s, ok := possibleHeaders[v]
6 years ago
if !ok {
continue
}
var str string
d := s.arrow + s.displaytext
if v == "price" || v == "balance" {
4 years ago
d = s.arrow + ct.CurrencySymbol() + s.displaytext
6 years ago
}
str = fmt.Sprintf(
"%s%s%s",
strings.Repeat(" ", s.padleft),
s.colorfn(d),
strings.Repeat(" ", s.padright),
)
headers = append(headers, str)
}
ct.UpdateUI(func() error {
return ct.Views.TableHeader.Update(strings.Join(headers, ""))
})
return nil
}