Documentation
¶
Index ¶
- Constants
- Variables
- type BorderStyle
- type Cell
- type DividerStyle
- type OptionFunc
- func WithBorderFlags(flags uint8) OptionFunc
- func WithCSVOutput(csv bool) OptionFunc
- func WithCellPadding(padding string) OptionFunc
- func WithColMaxWidth(width int) OptionFunc
- func WithColumnWidths(widths ...int) OptionFunc
- func WithOverflowFlag(flag OverflowFlag) OptionFunc
- func WithShowRowNumber(show bool) OptionFunc
- func WithSortColumn(col int, ascending bool) OptionFunc
- func WithStyle(style Style) OptionFunc
- func WithTrimSpace(trim bool) OptionFunc
- func WithoutBorder() OptionFunc
- type Options
- type OverflowFlag
- type Row
- type Style
- type Table
- func (t *Table) AddHead(name string) *Table
- func (t *Table) AddRow(cols ...any) *Table
- func (t *Table) ConfigStyle(fn func(s *Style)) *Table
- func (t *Table) Format()
- func (t *Table) PrependHead(name string) *Table
- func (t *Table) Render() string
- func (t *Table) SetHeads(names ...string) *Table
- func (t *Table) SetRows(rs any) *Table
- func (t *Table) WithOptions(fns ...OptionFunc) *Table
- func (t *Table) WithStyle(style Style) *Table
Constants ¶
const ( BorderNone uint8 = 0 BorderTop uint8 = 1 << iota BorderBottom BorderLeft BorderRight BorderHeader // 显示表头边框 BorderRows // 显示行边框 // BorderDefault display head, top and bottom borders BorderDefault = BorderTop | BorderBottom | BorderHeader // BorderBody display body borders BorderBody = BorderTop | BorderBottom | BorderLeft | BorderRight | BorderRows // BorderAll display all borders BorderAll = BorderTop | BorderBottom | BorderLeft | BorderRight | BorderHeader | BorderRows )
BorderFlags for controlling table border display
Variables ¶
var ( /* StyleMySql - MySql-like table style +-----+------------+-----------+--------+ | # | FIRST NAME | LAST NAME | SALARY | +-----+------------+-----------+--------+ | 1 | Arya | Stark | 3000 | | 20 | Jon | Snow | 2000 | | 300 | Tyrion | Lannister | 5000 | +-----+------------+-----------+--------+ */ StyleMySql = Style{ HeadColor: "info", BorderFlags: BorderAll, Border: BorderStyleDefault, Divider: DividerStyle{ Left: '+', Right: '+', Intersect: '+', }, } // StyleSimple - Simple table style StyleSimple = Style{ HeadColor: "info", BorderFlags: BorderDefault, Border: BorderStyleDefault, Divider: DividerStyle{ Left: '|', Right: '|', Intersect: '+', }, } // StyleMarkdown - Markdown table style StyleMarkdown = Style{ HeadColor: "info", BorderFlags: BorderAll, Border: BorderStyle{ TopLeft: 0, Top: 0, TopIntersect: 0, TopRight: 0, Right: '|', Cell: '|', Left: '|', Center: '-', BottomRight: 0, Bottom: 0, BottomLeft: 0, BottomIntersect: 0, }, Divider: DividerStyle{ Left: '|', Right: '|', Intersect: '|', }, } // StyleBold - Bold table style with thick borders: StyleBold = Style{ HeadColor: "info", BorderFlags: BorderDefault, Border: BorderStyle{ TopLeft: '┏', Top: '━', TopIntersect: '┳', TopRight: '┓', Right: '┃', Cell: '┃', Left: '┃', Center: '━', BottomRight: '┛', Bottom: '━', BottomLeft: '┗', BottomIntersect: '┻', }, Divider: DividerStyle{ Left: '┣', Right: '┫', Intersect: '╋', }, } // StyleRounded - Rounded corner table style: StyleRounded = Style{ HeadColor: "info", BorderFlags: BorderDefault, Border: BorderStyle{ TopLeft: '╭', Top: '─', TopIntersect: '┬', TopRight: '╮', Right: '│', Cell: '│', Left: '│', Center: '─', BottomRight: '╯', Bottom: '─', BottomLeft: '╰', BottomIntersect: '┴', }, Divider: DividerStyle{ Left: '├', Right: '┤', Intersect: '┼', }, } // StyleDouble - Double line table style: StyleDouble = Style{ HeadColor: "info", BorderFlags: BorderDefault, Border: BorderStyle{ TopLeft: '╔', Top: '═', TopIntersect: '╦', TopRight: '╗', Right: '║', Cell: '║', Left: '║', Center: '═', BottomRight: '╝', Bottom: '═', BottomLeft: '╚', BottomIntersect: '╩', }, Divider: DividerStyle{ Left: '╠', Right: '╣', Intersect: '╬', }, } // StyleMinimal - Minimal table style with light borders: StyleMinimal = Style{ HeadColor: "info", BorderFlags: BorderAll, Border: BorderStyle{ TopLeft: '┌', Top: '─', TopIntersect: '┬', TopRight: '┐', Right: '│', Cell: '│', Left: '│', Center: '─', BottomRight: '┘', Bottom: '─', BottomLeft: '└', BottomIntersect: '┴', }, Divider: DividerStyle{ Left: '├', Right: '┤', Intersect: '┼', }, } )
var (
BorderStyleDefault = BorderStyle{
TopLeft: '+',
Top: '-',
TopIntersect: '+',
TopRight: '+',
Right: '|',
Cell: '|',
Left: '|',
Center: '-',
BottomRight: '+',
Bottom: '-',
BottomLeft: '+',
BottomIntersect: '+',
}
)
var StyleBoldBorder = Style{ HeadColor: "info", BorderFlags: BorderDefault, Border: BorderStyle{ TopLeft: '┏', Top: '━', TopIntersect: '┯', TopRight: '┓', Right: '┃', Cell: '│', Left: '┃', Center: '─', BottomRight: '┛', Bottom: '━', BottomLeft: '┗', BottomIntersect: '┷', }, Divider: DividerStyle{ Left: '┃', Right: '┃', Intersect: '┼', }, }
StyleBoldBorder - table style with bold top and bottom lines: ━━━┯━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━┯━━━━━━━━━━
# │ pid │ name │ status │ cpu
───┼───────┼─────────────────┼──────────┼──────────
0 │ 992 │ chrome │ Sleeping │ 6.988768 2 │ 13973 │ qemu-system-x86 │ Sleeping │ 4.996551
━━━┷━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━┷━━━━━━━━━━
Functions ¶
This section is empty.
Types ¶
type BorderStyle ¶ added in v3.1.1
type BorderStyle struct {
TopLeft, Top, TopIntersect, TopRight rune
// Right - right border char
Right rune
Center rune // eg: ─
// Cell - column separator char. 列分隔符
Cell rune
Left rune
BottomLeft, Bottom, BottomIntersect, BottomRight rune
}
BorderStyle for table
type Cell ¶ added in v3.1.1
type Cell struct {
// Width custom set width of the cell.
Width int
// Wrap when true wraps the contents of the cell when the length exceeds the width
//
// default: auto - will inherit from the table options
Wrap OverflowFlag
// Align when true aligns contents to the right
Align strutil.PosFlag
// Val is the cell data
Val any
// contains filtered or unexported fields
}
Cell represents a column in a row
type DividerStyle ¶ added in v3.1.1
type DividerStyle struct {
Left rune // eg: ├
Right rune // eg: ┤
// Intersect 交叉 eg: ┼
Intersect rune
}
DividerStyle defines table divider style. 定义表格分隔器样式
type OptionFunc ¶ added in v3.3.0
type OptionFunc func(opts *Options)
OptionFunc define
func WithBorderFlags ¶ added in v3.3.0
func WithBorderFlags(flags uint8) OptionFunc
WithBorderFlags set border flags
func WithCSVOutput ¶ added in v3.3.0
func WithCSVOutput(csv bool) OptionFunc
WithCSVOutput enable/disable CSV output
func WithCellPadding ¶ added in v3.3.0
func WithCellPadding(padding string) OptionFunc
WithCellPadding set column padding
func WithColMaxWidth ¶ added in v3.3.0
func WithColMaxWidth(width int) OptionFunc
WithColMaxWidth set column max width
func WithColumnWidths ¶ added in v3.3.0
func WithColumnWidths(widths ...int) OptionFunc
WithColumnWidths set column widths by index
func WithOverflowFlag ¶ added in v3.3.0
func WithOverflowFlag(flag OverflowFlag) OptionFunc
WithOverflowFlag set overflow handling flag
func WithShowRowNumber ¶ added in v3.3.0
func WithShowRowNumber(show bool) OptionFunc
WithShowRowNumber enable/disable row number display
func WithSortColumn ¶ added in v3.3.0
func WithSortColumn(col int, ascending bool) OptionFunc
WithSortColumn set sort column and direction
func WithTrimSpace ¶ added in v3.3.0
func WithTrimSpace(trim bool) OptionFunc
WithTrimSpace enable/disable trim space
func WithoutBorder ¶ added in v3.3.0
func WithoutBorder() OptionFunc
WithoutBorder set border to none
type Options ¶ added in v3.1.1
type Options struct {
Style
// Alignment column 内容对齐方式
Alignment strutil.PosFlag
// StructTag struct tag name on input struct data. Default: json
StructTag string
// ColMaxWidth column max width.
// - 0: auto
// - 超出宽度时,将对内容进行处理 OverflowFlag
ColMaxWidth int
// CellPadding column value padding left and right.
// - 默认L,R填充一个空格
CellPadding string
// OverflowFlag 内容溢出处理方式 0: 默认截断, 1: 换行
OverflowFlag OverflowFlag
// ShowRowNumber 显示行号,将会多一个列
ShowRowNumber bool
// ColumnWidths 自定义设置列宽. 按顺序设置,不设置/为0时,将根据内容自动计算
//
// start index is 0
ColumnWidths []int
// SortColumn sort rows by column index value.
// - -1: 不排序
// - 0+: 按指定索引列排序
SortColumn int
// SortAscending sort direction, true for ascending, false for descending
SortAscending bool
// TrimSpace trim spaces from cell values. default: true
TrimSpace bool
// CSVOutput output table in CSV format TODO
CSVOutput bool
}
Options struct
func (*Options) HasBorderFlag ¶ added in v3.3.0
HasBorderFlag check border flag
type OverflowFlag ¶ added in v3.3.0
type OverflowFlag uint8
OverflowFlag for handling content overflow
const ( OverflowAuto OverflowFlag = iota // auto: default is cut OverflowCut // 截断 OverflowWrap // 换行 )
OverflowFlag values
type Row ¶ added in v3.1.1
type Row struct {
// Cells is the group of cell for the row
Cells []*Cell
// Height is the height of the row.
//
// Defaults to 0 - the height of the tallest cell(最高的单元格的高度)
Height int
// contains filtered or unexported fields
}
Row represents a row in a table
type Style ¶ added in v3.1.1
type Style struct {
Border BorderStyle
Divider DividerStyle
// BorderFlags control which borders to show using bit flags
// e.g. BorderFlags = BorderTop | BorderBottom | BorderHeader
BorderFlags uint8
// TitleColor - table title color.
// allow: red, green, blue, yellow, magenta, cyan, white, gray, black
TitleColor string
HeadColor string
RowColor string
// FirstColor - first column color
FirstColor string
}
Style for table
type Table ¶
type Table struct {
showcom.Base // use for internal
// Title for the table
Title string
// Heads the table head data
Heads []*Cell
// Heads []string
// Rows table data rows
Rows []*Row
// contains filtered or unexported fields
}
Table a cli Table show
func (*Table) ConfigStyle ¶ added in v3.3.0
ConfigStyle config the table style
func (*Table) PrependHead ¶ added in v3.3.0
PrependHead prepend head column
func (*Table) WithOptions ¶ added in v3.1.1
func (t *Table) WithOptions(fns ...OptionFunc) *Table
WithOptions for table