Documentation ¶
Overview ¶
Copyright © 2023 Wei Shen <shenwei356@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Copyright © 2023 Wei Shen <shenwei356@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Copyright © 2023 Wei Shen <shenwei356@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Index ¶
- Variables
- type Align
- type Column
- type LineStyle
- type RowStyle
- type Table
- func (t *Table) AddRow(row []interface{}) error
- func (t *Table) AddRowStringSlice(row []string) error
- func (t *Table) Align(align Align) (*Table, error)
- func (t *Table) AlignCenter() *Table
- func (t *Table) AlignLeft() *Table
- func (t *Table) AlignRight() *Table
- func (t *Table) ClipCell(mark string) *Table
- func (t *Table) Flush()
- func (t *Table) HasHeaders() bool
- func (t *Table) Header(headers []string) (*Table, error)
- func (t *Table) HeaderWithFormat(headers []Column) (*Table, error)
- func (t *Table) HumanizeNumbers() *Table
- func (t *Table) MaxWidth(w int) *Table
- func (t *Table) MinWidth(w int) *Table
- func (t *Table) Render(style *TableStyle) []byte
- func (t *Table) Style(style *TableStyle) *Table
- func (t *Table) WrapDelimiter(d rune) *Table
- func (t *Table) Writer(w io.Writer, bufRows uint) error
- type TableStyle
Constants ¶
This section is empty.
Variables ¶
var ErrAddRowAfterFlush = fmt.Errorf("stable: calling AddRow is not allowed after calling Flush()")
var ErrInvalidAlign = fmt.Errorf("stable: invalid align value")
ErrInvalidAlign means a invalid align value is given.
var ErrNoDataAdded = fmt.Errorf("stable: no data added")
ErrNoDataAdded means not data is added. Not used.
var ErrSetHeaderAfterDataAdded = fmt.Errorf("stable: setting header is not allowed after some data being added")
-------------------------------------------------------------------------- ErrSetHeaderAfterDataAdded means that setting header is not allowed after some data being added.
var ErrUnmatchedColumnNumber = fmt.Errorf("stable: unmatched column number")
ErrUnmatchedColumnNumber means that the column number of the newly added row is not matched with that of previous ones.
var ErrWriterRepeatedlySet = fmt.Errorf("stable: writer repeatedly set")
ErrWriterRepeatedlySet means that the writer is repeatedly set.
var StyleBold = &TableStyle{ Name: "bold", LineTop: LineStyle{"┏", "━", "┳", "┓"}, LineBelowHeader: LineStyle{"┣", "━", "╋", "┫"}, LineBetweenRows: LineStyle{"┣", "━", "╋", "┫"}, LineBottom: LineStyle{"┗", "━", "┻", "┛"}, HeaderRow: RowStyle{"┃", "┃", "┃"}, DataRow: RowStyle{"┃", "┃", "┃"}, Padding: " ", }
var StyleDouble = &TableStyle{ Name: "double", LineTop: LineStyle{"╔", "═", "╦", "╗"}, LineBelowHeader: LineStyle{"╠", "═", "╬", "╣"}, LineBetweenRows: LineStyle{"╠", "═", "╬", "╣"}, LineBottom: LineStyle{"╚", "═", "╩", "╝"}, HeaderRow: RowStyle{"║", "║", "║"}, DataRow: RowStyle{"║", "║", "║"}, Padding: " ", }
var StyleGrid = &TableStyle{ Name: "grid", LineTop: LineStyle{"+", "-", "+", "+"}, LineBelowHeader: LineStyle{"+", "=", "+", "+"}, LineBetweenRows: LineStyle{"+", "-", "+", "+"}, LineBottom: LineStyle{"+", "-", "+", "+"}, HeaderRow: RowStyle{"|", "|", "|"}, DataRow: RowStyle{"|", "|", "|"}, Padding: " ", }
var StyleLight = &TableStyle{ Name: "light", LineTop: LineStyle{"┌", "-", "┬", "┐"}, LineBelowHeader: LineStyle{"├", "=", "┼", "┤"}, LineBetweenRows: LineStyle{"├", "-", "┼", "┤"}, LineBottom: LineStyle{"└", "-", "┴", "┘"}, HeaderRow: RowStyle{"|", "|", "|"}, DataRow: RowStyle{"|", "|", "|"}, Padding: " ", }
var StylePlain = &TableStyle{ Name: "plain", HeaderRow: RowStyle{"", " ", ""}, DataRow: RowStyle{"", " ", ""}, Padding: "", }
var StyleSimple = &TableStyle{ Name: "simple", LineTop: LineStyle{"", "-", "-", ""}, LineBelowHeader: LineStyle{"", "-", "-", ""}, LineBottom: LineStyle{"", "-", "-", ""}, HeaderRow: RowStyle{"", " ", ""}, DataRow: RowStyle{"", " ", ""}, Padding: " ", }
var StyleThreeLine = &TableStyle{ Name: "3line", LineTop: LineStyle{"", "━", "━", ""}, LineBelowHeader: LineStyle{"", "-", "-", ""}, LineBottom: LineStyle{"", "━", "━", ""}, HeaderRow: RowStyle{"", " ", ""}, DataRow: RowStyle{"", " ", ""}, Padding: " ", }
Functions ¶
This section is empty.
Types ¶
type Column ¶
type Column struct { Header string // column name Align Align // text align MinWidth int // minimum width MaxWidth int // maximum width, it will be overrided by the global MaxWidth of the table HumanizeNumbers bool // add comma to numbers, for example 1000 -> 1,000 }
Column is the configuration of a column.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table is the table struct.
func (*Table) AddRowStringSlice ¶
func (*Table) Align ¶
Align sets the global text alignment. Only three values are allowed: AlignLeft, AlignCenter, AlignRight.
func (*Table) AlignCenter ¶
AlignCenter sets the global text alignment as Center.
func (*Table) AlignRight ¶
AlignRight sets the global text alignment as Right.
func (*Table) HasHeaders ¶ added in v0.1.3
HasHeaders tell whether the table has an available header line. It may return false even if you have called Header() or HeaderWithFormat(), when all headers are empty strings.
func (*Table) HeaderWithFormat ¶
HeaderWithFormat sets column names and other configuration of the column.
func (*Table) HumanizeNumbers ¶
HumanizeNumbers makes the numbers more readable by adding commas to numbers. E.g., 1000 -> 1,000.
func (*Table) Render ¶
func (t *Table) Render(style *TableStyle) []byte
Render render all data with give style.
func (*Table) Style ¶
func (t *Table) Style(style *TableStyle) *Table
Style sets the output style. If you decide to add all rows before rendering, there's no need to call this method. If you want to stream the output, please call this method before adding any rows.
func (*Table) WrapDelimiter ¶
WrapDelimiter sets the delimiter for wrapping cell text. The default value is space. Note that in streaming mode (after calling SetWriter())
func (*Table) Writer ¶
Writer sets a writer for render the table. The first bufRows rows will be used to determine the maximum width for each cell if they are not defined with MaxWidth(). bufRows should be in range of [1,1M]. If bufRows is 0, it keeps all data in buffer. So a newly added row (Addrow()) is formatted and written to the configured writer immediately. It is memory-effective for a large number of rows. And it is helpful to pipe the data in shell. Do not forget to call Flush() after adding all rows.