2018-03-29 22:02:24 +00:00
|
|
|
package table
|
|
|
|
|
2018-03-30 17:29:26 +00:00
|
|
|
// FormatFn format function
|
2018-03-29 22:02:24 +00:00
|
|
|
type FormatFn func(interface{}) string
|
|
|
|
|
2018-03-30 17:29:26 +00:00
|
|
|
// Col struct
|
2018-03-29 22:02:24 +00:00
|
|
|
type Col struct {
|
|
|
|
name string
|
|
|
|
hide bool
|
|
|
|
format string
|
|
|
|
formatFn FormatFn
|
|
|
|
align Align
|
|
|
|
width int
|
|
|
|
perc float32
|
|
|
|
minWidth int
|
|
|
|
minWidthPerc int
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:29:26 +00:00
|
|
|
// Cols columns
|
2018-03-29 22:02:24 +00:00
|
|
|
type Cols []*Col
|
|
|
|
|
2018-03-30 17:29:26 +00:00
|
|
|
// Hide hide
|
2018-03-29 22:02:24 +00:00
|
|
|
func (c *Col) Hide() *Col {
|
|
|
|
c.hide = true
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:29:26 +00:00
|
|
|
// SetFormatFn set format function
|
|
|
|
func (c *Col) SetFormatFn(f FormatFn) *Col {
|
|
|
|
c.formatFn = f
|
2018-03-29 22:02:24 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:29:26 +00:00
|
|
|
// SetFormat sets format
|
|
|
|
func (c *Col) SetFormat(f string) *Col {
|
|
|
|
c.format = f
|
2018-03-29 22:02:24 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:29:26 +00:00
|
|
|
// AlignLeft align left
|
2018-03-29 22:02:24 +00:00
|
|
|
func (c *Col) AlignLeft() *Col {
|
|
|
|
c.align = AlignLeft
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:29:26 +00:00
|
|
|
// AlignRight align right
|
2018-03-29 22:02:24 +00:00
|
|
|
func (c *Col) AlignRight() *Col {
|
|
|
|
c.align = AlignRight
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:29:26 +00:00
|
|
|
// AlignCenter align center
|
2018-03-29 22:02:24 +00:00
|
|
|
func (c *Col) AlignCenter() *Col {
|
|
|
|
c.align = AlignCenter
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:29:26 +00:00
|
|
|
// SetWidth set width
|
2018-03-29 22:02:24 +00:00
|
|
|
func (c *Col) SetWidth(w int) *Col {
|
|
|
|
c.minWidth = w
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:29:26 +00:00
|
|
|
// SetWidthPerc set width percentage
|
2018-03-29 22:02:24 +00:00
|
|
|
func (c *Col) SetWidthPerc(w int) *Col {
|
|
|
|
c.minWidthPerc = w
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:29:26 +00:00
|
|
|
// Index index
|
2018-03-29 22:02:24 +00:00
|
|
|
func (c Cols) Index(n string) int {
|
|
|
|
for i := range c {
|
|
|
|
if c[i].name == n {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|