Former-commit-id: 459b83ad5e
pull/15/head
Miguel Mota 6 years ago
parent 75b86fcee4
commit 03c5eb158b

@ -0,0 +1 @@
gocui table

@ -1,9 +1,13 @@
package table package table
// Align int
type Align int type Align int
const ( const (
// AlignLeft int
AlignLeft = Align(iota) AlignLeft = Align(iota)
// AlignRight int
AlignRight AlignRight
// AlignCenter int
AlignCenter AlignCenter
) )

@ -5,6 +5,7 @@ import (
"strings" "strings"
) )
// AlignLeft align left
func AlignLeft(s string, n int) string { func AlignLeft(s string, n int) string {
if len(s) > n { if len(s) > n {
return s[:n] return s[:n]
@ -13,6 +14,7 @@ func AlignLeft(s string, n int) string {
return fmt.Sprintf("%s%s", s, strings.Repeat(" ", n-len(s))) return fmt.Sprintf("%s%s", s, strings.Repeat(" ", n-len(s)))
} }
// AlignRight align right
func AlignRight(s string, n int) string { func AlignRight(s string, n int) string {
if len(s) > n { if len(s) > n {
return s[:n] return s[:n]
@ -21,6 +23,7 @@ func AlignRight(s string, n int) string {
return fmt.Sprintf("%s%s", strings.Repeat(" ", n-len(s)), s) return fmt.Sprintf("%s%s", strings.Repeat(" ", n-len(s)), s)
} }
// AlignCenter align center
func AlignCenter(s string, n int) string { func AlignCenter(s string, n int) string {
if len(s) > n { if len(s) > n {
return s[:n] return s[:n]

@ -1,7 +1,9 @@
package table package table
// FormatFn format function
type FormatFn func(interface{}) string type FormatFn func(interface{}) string
// Col struct
type Col struct { type Col struct {
name string name string
hide bool hide bool
@ -14,48 +16,58 @@ type Col struct {
minWidthPerc int minWidthPerc int
} }
// Cols columns
type Cols []*Col type Cols []*Col
// Hide hide
func (c *Col) Hide() *Col { func (c *Col) Hide() *Col {
c.hide = true c.hide = true
return c return c
} }
func (c *Col) SetFormat(f string) *Col { // SetFormatFn set format function
c.format = f func (c *Col) SetFormatFn(f FormatFn) *Col {
c.formatFn = f
return c return c
} }
func (c *Col) SetFormatFn(f FormatFn) *Col { // SetFormat sets format
c.formatFn = f func (c *Col) SetFormat(f string) *Col {
c.format = f
return c return c
} }
// AlignLeft align left
func (c *Col) AlignLeft() *Col { func (c *Col) AlignLeft() *Col {
c.align = AlignLeft c.align = AlignLeft
return c return c
} }
// AlignRight align right
func (c *Col) AlignRight() *Col { func (c *Col) AlignRight() *Col {
c.align = AlignRight c.align = AlignRight
return c return c
} }
// AlignCenter align center
func (c *Col) AlignCenter() *Col { func (c *Col) AlignCenter() *Col {
c.align = AlignCenter c.align = AlignCenter
return c return c
} }
// SetWidth set width
func (c *Col) SetWidth(w int) *Col { func (c *Col) SetWidth(w int) *Col {
c.minWidth = w c.minWidth = w
return c return c
} }
// SetWidthPerc set width percentage
func (c *Col) SetWidthPerc(w int) *Col { func (c *Col) SetWidthPerc(w int) *Col {
c.minWidthPerc = w c.minWidthPerc = w
return c return c
} }
// Index index
func (c Cols) Index(n string) int { func (c Cols) Index(n string) int {
for i := range c { for i := range c {
if c[i].name == n { if c[i].name == n {

@ -1,21 +1,26 @@
package table package table
// Row row
type Row struct { type Row struct {
table *Table table *Table
values []interface{} values []interface{}
strValues []string strValues []string
} }
// Rows rows
type Rows []*Row type Rows []*Row
// Len count
func (r Rows) Len() int { func (r Rows) Len() int {
return len(r) return len(r)
} }
// Swap swap rows
func (r Rows) Swap(i, j int) { func (r Rows) Swap(i, j int) {
r[i], r[j] = r[j], r[i] r[i], r[j] = r[j], r[i]
} }
// Less less
func (r Rows) Less(i, j int) bool { func (r Rows) Less(i, j int) bool {
sortOrder := r[i].table.sort sortOrder := r[i].table.sort
var k int var k int
@ -52,13 +57,12 @@ func gt(a interface{}, b interface{}, fn SortFn) bool {
func lt(a interface{}, b interface{}, fn SortFn) bool { func lt(a interface{}, b interface{}, fn SortFn) bool {
if fn != nil { if fn != nil {
return fn(a, b) return fn(a, b)
} else { }
switch a.(type) { switch a.(type) {
case int: case int:
return a.(int) < b.(int) return a.(int) < b.(int)
case string: case string:
return a.(string) < b.(string) return a.(string) < b.(string)
}
} }
return false return false
} }

@ -1,14 +1,21 @@
package table package table
// SortOrder int
type SortOrder int type SortOrder int
// SortFn sort function
type SortFn func(interface{}, interface{}) bool type SortFn func(interface{}, interface{}) bool
const ( const (
// SortNone sort none
SortNone SortOrder = iota SortNone SortOrder = iota
// SortAsc sort ascendinge
SortAsc SortAsc
// SortDesc sort descending
SortDesc SortDesc
) )
// SortBy sort by
type SortBy struct { type SortBy struct {
index int index int
order SortOrder order SortOrder

@ -9,6 +9,7 @@ import (
"github.com/miguelmota/cointop/table/align" "github.com/miguelmota/cointop/table/align"
) )
// Table table
type Table struct { type Table struct {
cols Cols cols Cols
rows Rows rows Rows
@ -17,27 +18,32 @@ type Table struct {
HideColumHeaders bool HideColumHeaders bool
} }
// New new table
func New() *Table { func New() *Table {
return &Table{} return &Table{}
} }
// SetWidth set table width
func (t *Table) SetWidth(w int) *Table { func (t *Table) SetWidth(w int) *Table {
t.width = w t.width = w
return t return t
} }
// AddCol add column
func (t *Table) AddCol(n string) *Col { func (t *Table) AddCol(n string) *Col {
c := &Col{name: n} c := &Col{name: n}
t.cols = append(t.cols, c) t.cols = append(t.cols, c)
return c return c
} }
// AddRow add row
func (t *Table) AddRow(v ...interface{}) *Row { func (t *Table) AddRow(v ...interface{}) *Row {
r := &Row{table: t, values: v, strValues: make([]string, len(v))} r := &Row{table: t, values: v, strValues: make([]string, len(v))}
t.rows = append(t.rows, r) t.rows = append(t.rows, r)
return r return r
} }
// SortAscFn sort ascending function
func (t *Table) SortAscFn(n string, fn SortFn) *Table { func (t *Table) SortAscFn(n string, fn SortFn) *Table {
i := t.cols.Index(n) i := t.cols.Index(n)
s := SortBy{index: i, order: SortAsc, sortFn: fn} s := SortBy{index: i, order: SortAsc, sortFn: fn}
@ -45,10 +51,12 @@ func (t *Table) SortAscFn(n string, fn SortFn) *Table {
return t return t
} }
// SortAsc sort ascending
func (t *Table) SortAsc(n string) *Table { func (t *Table) SortAsc(n string) *Table {
return t.SortAscFn(n, nil) return t.SortAscFn(n, nil)
} }
// SortDescFn sort descending function
func (t *Table) SortDescFn(n string, fn SortFn) *Table { func (t *Table) SortDescFn(n string, fn SortFn) *Table {
i := t.cols.Index(n) i := t.cols.Index(n)
s := SortBy{index: i, order: SortDesc, sortFn: fn} s := SortBy{index: i, order: SortDesc, sortFn: fn}
@ -56,10 +64,12 @@ func (t *Table) SortDescFn(n string, fn SortFn) *Table {
return t return t
} }
// SortDesc sort descending
func (t *Table) SortDesc(n string) *Table { func (t *Table) SortDesc(n string) *Table {
return t.SortDescFn(n, nil) return t.SortDescFn(n, nil)
} }
// Sort sort
func (t *Table) Sort() *Table { func (t *Table) Sort() *Table {
if len(t.sort) > 0 { if len(t.sort) > 0 {
sort.Sort(t.rows) sort.Sort(t.rows)
@ -98,6 +108,7 @@ func (t *Table) normalizeColWidthPerc() {
} }
} }
// Format format table
func (t *Table) Format() *Table { func (t *Table) Format() *Table {
for _, c := range t.cols { for _, c := range t.cols {
c.width = len(c.name) + 1 c.width = len(c.name) + 1
@ -158,6 +169,7 @@ func (t *Table) Format() *Table {
return t return t
} }
// Fprint write
func (t *Table) Fprint(w io.Writer) { func (t *Table) Fprint(w io.Writer) {
if !t.HideColumHeaders { if !t.HideColumHeaders {
for _, c := range t.cols { for _, c := range t.cols {

Loading…
Cancel
Save