diff --git a/cointop/cointop.go b/cointop/cointop.go index 7c66707..c6b828a 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -32,6 +32,7 @@ type Cointop struct { headerviewname string tableview *gocui.View tableviewname string + tablecolumnorder []string table *table.Table maxtablewidth int statusbarview *gocui.View @@ -115,9 +116,22 @@ func Run() { "6H": time.Duration(6 * time.Hour), "1H": time.Duration(1 * time.Hour), }, - selectedchartrange: "7D", - headerviewname: "header", - tableviewname: "table", + selectedchartrange: "7D", + headerviewname: "header", + tableviewname: "table", + tablecolumnorder: []string{ + "rank", + "name", + "symbol", + "price", + "marketcap", + "24hvolume", + "1hchange", + "7dchange", + "totalsupply", + "availablesupply", + "lastupdated", + }, statusbarviewname: "statusbar", searchfieldviewname: "searchfield", helpviewname: "help", diff --git a/cointop/sort.go b/cointop/sort.go index 8053340..ab2a7d5 100644 --- a/cointop/sort.go +++ b/cointop/sort.go @@ -1,28 +1,15 @@ package cointop import ( + "sort" + "github.com/miguelmota/cointop/pkg/gocui" - "github.com/miguelmota/cointop/pkg/slice" ) -var colorder = []string{ - "rank", - "name", - "symbol", - "price", - "marketcap", - "24hvolume", - "1hchange", - "7dchange", - "totalsupply", - "availablesupply", - "lastupdated", -} - func (ct *Cointop) sort(sortby string, desc bool, list []*coin) { ct.sortby = sortby ct.sortdesc = desc - slice.Sort(list[:], func(i, j int) bool { + sort.Slice(list[:], func(i, j int) bool { if ct.sortdesc { i, j = j, i } @@ -73,7 +60,7 @@ func (ct *Cointop) sortfn(sortby string, desc bool) func(g *gocui.Gui, v *gocui. } func (ct *Cointop) getSortColIndex() int { - for i, col := range colorder { + for i, col := range ct.tablecolumnorder { if ct.sortby == col { return i } @@ -96,27 +83,27 @@ func (ct *Cointop) sortDesc() error { } func (ct *Cointop) sortPrevCol() error { - nextsortby := colorder[0] + nextsortby := ct.tablecolumnorder[0] i := ct.getSortColIndex() k := i - 1 if k < 0 { k = 0 } - nextsortby = colorder[k] + nextsortby = ct.tablecolumnorder[k] ct.sort(nextsortby, ct.sortdesc, ct.coins) ct.updateTable() return nil } func (ct *Cointop) sortNextCol() error { - nextsortby := colorder[0] - l := len(colorder) + nextsortby := ct.tablecolumnorder[0] + l := len(ct.tablecolumnorder) i := ct.getSortColIndex() k := i + 1 if k > l-1 { k = l - 1 } - nextsortby = colorder[k] + nextsortby = ct.tablecolumnorder[k] ct.sort(nextsortby, ct.sortdesc, ct.coins) ct.updateTable() return nil diff --git a/cointop/table.go b/cointop/table.go index 4d0e710..910b961 100644 --- a/cointop/table.go +++ b/cointop/table.go @@ -64,9 +64,7 @@ func (ct *Cointop) refreshTable() error { star = "*" } rank := fmt.Sprintf("%s%v", color.Yellow(star), color.White(fmt.Sprintf("%6v ", coin.Rank))) - lastchar := len(name) - if lastchar > 20 { - lastchar = 20 + if len(name) > 20 { name = fmt.Sprintf("%s%s", name[0:18], dots) } ct.table.AddRow( diff --git a/cointop/update.go b/cointop/update.go index 9e81ca2..aafa0cf 100644 --- a/cointop/update.go +++ b/cointop/update.go @@ -1,6 +1,8 @@ package cointop -import "github.com/miguelmota/cointop/pkg/gocui" +import ( + "github.com/miguelmota/cointop/pkg/gocui" +) // update update view func (ct *Cointop) update(f func()) { diff --git a/pkg/slice/slice.go b/pkg/slice/slice.go deleted file mode 100644 index 150d38a..0000000 --- a/pkg/slice/slice.go +++ /dev/null @@ -1,40 +0,0 @@ -// Package slice provides a slice sorting function. -package slice - -import ( - "fmt" - "reflect" - "sort" - - "go4.org/reflectutil" -) - -// Sort sorts the provided slice using the function less. -// If slice is not a slice, Sort panics. -func Sort(slice interface{}, less func(i, j int) bool) { - sort.Sort(SortInterface(slice, less)) -} - -// SortInterface returns a sort.Interface to sort the provided slice -// using the function less. -func SortInterface(slice interface{}, less func(i, j int) bool) sort.Interface { - sv := reflect.ValueOf(slice) - if sv.Kind() != reflect.Slice { - panic(fmt.Sprintf("slice.Sort called with non-slice value of type %T", slice)) - } - return &funcs{ - length: sv.Len(), - less: less, - swap: reflectutil.Swapper(slice), - } -} - -type funcs struct { - length int - less func(i, j int) bool - swap func(i, j int) -} - -func (f *funcs) Len() int { return f.length } -func (f *funcs) Less(i, j int) bool { return f.less(i, j) } -func (f *funcs) Swap(i, j int) { f.swap(i, j) }