Skip to content

Latest commit

 

History

History
499 lines (276 loc) · 8.17 KB

api.md

File metadata and controls

499 lines (276 loc) · 8.17 KB

API

This section describes the gotable APIs.

Return to the home page

github.com/liushuochen/gotable

Create a simple table

func Create(columns ...string) (*table.Table, error)

Create a simple table from struct

func CreateByStruct(v interface{}) (*table.Table, error)

Create a safe table

func CreateSafeTable(columns ...string) (*table.SafeTable, error)

Get version

func Version() string

Get version list

func Versions() []string

Specify default value

The gotable.Default constant replaces the default value stored in column. Refer to Set Default Values in the Demo section for more information.

gotable.Default

Load data from file

Currently,csv and json file are supported.

func Read(path string) (*table.Table, error)

Color control

The following constants are used in conjunction with the *table.SetColumnColor method to change the column color.

display type

Default display

gotable.TerminalDefault

Fonts are highlighted

gotable.Highlight

Underline

gotable.Underline

Font flash

gotable.Flash

color

gotable.Black
gotable.Red
gotable.Green
gotable.Yellow
gotable.Blue
gotable.Purple
gotable.Cyan
gotable.Write

Do not set the background color

gotable.NoneBackground

APIs for simple table type(*table.Table)

Clear data

The clear method is used to clear all data in the table, include columns and rows.

func (tb *Table) Clear()

Get table type

The type method returns a type of table.

func (tb *Table) Type() string

Add row

Add a row to the table. Support Map and Slice. See the Demo section for more information.

func (tb *Table) AddRow(row interface{}) error

Add a list of rows

Method AddRows add a list of rows. It returns a slice that consists of adding failed rows.

func (tb *Table) AddRows(rows []map[string]string) []map[string]string

Add column

func (tb *Table) AddColumn(column string) error

Print table

*Table implements fmt.Stringer interface, so you can use the fmt.Print, fmt.Printf functions and so on to print the contents of the table instance.

func (tb *Table) String() string

Set default value

By default, the default value for all columns is an empty string.

func (b *base) SetDefault(column string, defaultValue string)

Drop default value

func (b *base) DropDefault(column string)

Get default value

Use table method GetDefault to get default value of column. If column does not exist in the table, the method returns an empty string.

func (b *base) GetDefault(column string) string

Get default map

Use table method GetDefaults to get default map of head.

func (b *base) GetDefaults() map[string]string

Arrange: center, align left or align right

By default, the table is centered. You can set a header to be left aligned or right aligned. See the next section for more details on how to use it.

func (tb *Table) Align(column string, mode int)

Check empty

Use table method Empty to check if the table is empty.

func (tb *Table) Empty() bool

Get list of columns

Use table method GetColumns to get a list of columns.

func (b *base) GetColumns() []string

Get values map

Use table method GetValues to get the map that save values.

func (tb *Table) GetValues() []map[string]string

Check value exists

func (tb *Table) Exist(value map[string]string) bool

Get table length

func (tb *Table) Length() int

To JSON string

Use table method JSON to convert the table to JSON format. The argument indent indicates the number of indents. If the argument indent is less than or equal to 0, then the JSON method unindents.

func (tb *Table) JSON(indent int) (string, error)

To XML string

Use table method XML to convert the table to XML format. The argument indent indicates the number of indents. If the argument indent is less than or equal to 0, then the XML method unindents.

func (tb *Table) XML(indent int) string

Save the table data to a JSON file

Use table method ToJsonFile to save the table data to a JSON file.

func (tb *Table) ToJsonFile(path string, indent int) error

Save the table data to a CSV file

Use table method ToCSVFile to save the table data to a CSV file.

func (tb *Table) ToCSVFile(path string) error

Close border

Use table method CloseBorder to close table border.

func (tb *Table) CloseBorder()

Open border

Use table method OpenBorder to open table border. By default, the border property is turned on.

func (tb *Table) OpenBorder()

Has column

Table method HasColumn determine whether the column is included.

func (tb *Table) HasColumn(column string) bool

Check whether the columns of the two tables are the same

Table method EqualColumns is used to check whether the columns of two tables are the same. This method returns true if the columns of the two tables are identical (length, name, order, alignment, default), and false otherwise.

func (tb *Table) EqualColumns(other *Table) bool

Set column color

Table method SetColumnColor is used to set the color of a specific column. The first parameter specifies the name of the column to be modified. The second parameter indicates the type of font to display. Refer to the Color control section in this document for more information. The third and fourth parameters specify the font and background color.

func (tb *Table) SetColumnColor(columnName string, display, fount, background int)

Custom ending string

By default, a new blank line will print after table printing. You can designate your ending string by reset table.End.

Is simple table

Method IsSimpleTable is used to check whether the table type is simple table.

func (b *base) IsSimpleTable() bool

Is safe table

Method IsSafeTable is used to check whether the table type is safe table.

func(b *base) IsSafeTable() bool

APIs for safe table type(*table.SafeTable)

Get table type

The type method returns a type of table.

func (tb *Table) Type() string

Is simple table

Method IsSimpleTable is used to check whether the table type is simple table.

func (b *base) IsSimpleTable() bool

Is safe table

Method IsSafeTable is used to check whether the table type is safe table.

func(b *base) IsSafeTable() bool

Add row

Add a row to the safe table. Only support Map. See the Demo section for more information.

func (s *SafeTable) AddRow(row interface{}) error

Add a list of rows

Method AddRows add a list of rows. It returns a slice that consists of adding failed rows.

func (s *SafeTable) AddRows(rows []map[string]string) []map[string]string

Custom ending string

By default, a new blank line will print after table printing. You can designate your ending string by reset table.End.

Add column

func (s *SafeTable) AddColumn(column string) error

Set default value

By default, the default value for all columns is an empty string.

func (b *base) SetDefault(column string, defaultValue string)

Drop default value

func (b *base) DropDefault(column string)

Get default map

Use table method GetDefaults to get default map of head.

func (b *base) GetDefaults() map[string]string

Get table length

func (s *SafeTable) Length() int

Print table

*SafeTable implements fmt.Stringer interface, so you can use the fmt.Print, fmt.Printf functions and so on to print the contents of the table instance.

func (st *SafeTable) String() string

Get list of columns

Use table method GetColumns to get a list of columns.

func (b *base) GetColumns() []string