table

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
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: '┼',
		},
	}
)
View Source
var (
	BorderStyleDefault = BorderStyle{

		TopLeft:      '+',
		Top:          '-',
		TopIntersect: '+',
		TopRight:     '+',

		Right:  '|',
		Cell:   '|',
		Left:   '|',
		Center: '-',

		BottomRight:     '+',
		Bottom:          '-',
		BottomLeft:      '+',
		BottomIntersect: '+',
	}
)
View Source
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

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

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

func NewCell

func NewCell(val any) *Cell

NewCell creates a new cell with the given value

func (*Cell) Init

func (c *Cell) Init(opts *Options)

Init for one cell

func (*Cell) String

func (c *Cell) String() string

String returns the string formatted representation of the cell

type DividerStyle

type DividerStyle struct {
	Left  rune // eg: ├
	Right rune // eg: ┤
	// Intersect 交叉 eg: ┼
	Intersect rune
}

DividerStyle defines table divider style. 定义表格分隔器样式

type OptionFunc

type OptionFunc func(opts *Options)

OptionFunc define

func WithBorderFlags

func WithBorderFlags(flags uint8) OptionFunc

WithBorderFlags set border flags

func WithCSVOutput

func WithCSVOutput(csv bool) OptionFunc

WithCSVOutput enable/disable CSV output

func WithCellPadding

func WithCellPadding(padding string) OptionFunc

WithCellPadding set column padding

func WithColMaxWidth

func WithColMaxWidth(width int) OptionFunc

WithColMaxWidth set column max width

func WithColumnWidths

func WithColumnWidths(widths ...int) OptionFunc

WithColumnWidths set column widths by index

func WithOverflowFlag

func WithOverflowFlag(flag OverflowFlag) OptionFunc

WithOverflowFlag set overflow handling flag

func WithShowRowNumber

func WithShowRowNumber(show bool) OptionFunc

WithShowRowNumber enable/disable row number display

func WithSortColumn

func WithSortColumn(col int, ascending bool) OptionFunc

WithSortColumn set sort column and direction

func WithStyle

func WithStyle(style Style) OptionFunc

WithStyle set table style

func WithTrimSpace

func WithTrimSpace(trim bool) OptionFunc

WithTrimSpace enable/disable trim space

func WithoutBorder

func WithoutBorder() OptionFunc

WithoutBorder set border to none

type Options

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 NewOptions

func NewOptions() *Options

NewOptions create default options

func (*Options) HasBorderFlag

func (opts *Options) HasBorderFlag(flag uint8) bool

HasBorderFlag check border flag

type OverflowFlag

type OverflowFlag uint8

OverflowFlag for handling content overflow

const (
	OverflowAuto OverflowFlag = iota // auto: default is cut
	OverflowCut                      // 截断
	OverflowWrap                     // 换行
)

OverflowFlag values

type Row

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

func (*Row) Init

func (r *Row) Init(opts *Options)

Init for one row: calc cell width and height, row height

type Style

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 New

func New(title string, fns ...OptionFunc) *Table

New create table

func (*Table) AddHead

func (t *Table) AddHead(name string) *Table

AddHead add head column to table

func (*Table) AddRow

func (t *Table) AddRow(cols ...any) *Table

AddRow data to table

func (*Table) ConfigStyle

func (t *Table) ConfigStyle(fn func(s *Style)) *Table

ConfigStyle config the table style

func (*Table) Format

func (t *Table) Format()

Format as string

func (*Table) PrependHead

func (t *Table) PrependHead(name string) *Table

PrependHead prepend head column

func (*Table) Render

func (t *Table) Render() string

Render formatted message with newline

func (*Table) SetHeads

func (t *Table) SetHeads(names ...string) *Table

SetHeads column names to table

func (*Table) SetRows

func (t *Table) SetRows(rs any) *Table

SetRows to table

func (*Table) WithOptions

func (t *Table) WithOptions(fns ...OptionFunc) *Table

WithOptions for table

func (*Table) WithStyle

func (t *Table) WithStyle(style Style) *Table

WithStyle set table style

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL