pull/5/head
Miguel Mota 6 years ago
parent 19684e3207
commit 459b83ad5e

@ -0,0 +1 @@
gocui table

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

@ -5,6 +5,7 @@ import (
"strings"
)
// AlignLeft align left
func AlignLeft(s string, n int) string {
if len(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)))
}
// AlignRight align right
func AlignRight(s string, n int) string {
if len(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)
}
// AlignCenter align center
func AlignCenter(s string, n int) string {
if len(s) > n {
return s[:n]

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

@ -1,21 +1,26 @@
package table
// Row row
type Row struct {
table *Table
values []interface{}
strValues []string
}
// Rows rows
type Rows []*Row
// Len count
func (r Rows) Len() int {
return len(r)
}
// Swap swap rows
func (r Rows) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
// Less less
func (r Rows) Less(i, j int) bool {
sortOrder := r[i].table.sort
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 {
if fn != nil {
return fn(a, b)
} else {
switch a.(type) {
case int:
return a.(int) < b.(int)
case string:
return a.(string) < b.(string)
}
}
switch a.(type) {
case int:
return a.(int) < b.(int)
case string:
return a.(string) < b.(string)
}
return false
}

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

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

Loading…
Cancel
Save