tablewriter

package
v2.3.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2016 License: MIT, Apache-2.0 Imports: 9 Imported by: 0

README

ASCII Table Writer

Build Status Total views

Generate ASCII table on the fly ... Installation is simple as

go get  github.com/olekukonko/tablewriter
Features
  • Automatic Padding
  • Support Multiple Lines
  • Supports Alignment
  • Support Custom Separators
  • Automatic Alignment of numbers & percentage
  • Write directly to http , file etc via io.Writer
  • Read directly from CSV file
  • Optional row line via SetRowLine
  • Normalise table header
  • Make CSV Headers optional
  • Enable or disable table border
  • Set custom footer support
Example 1 - Basic
data := [][]string{
    []string{"A", "The Good", "500"},
    []string{"B", "The Very very Bad Man", "288"},
    []string{"C", "The Ugly", "120"},
    []string{"D", "The Gopher", "800"},
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Sign", "Rating"})

for _, v := range data {
    table.Append(v)
}
table.Render() // Send output
Output 1
+------+-----------------------+--------+
| NAME |         SIGN          | RATING |
+------+-----------------------+--------+
|  A   |       The Good        |    500 |
|  B   | The Very very Bad Man |    288 |
|  C   |       The Ugly        |    120 |
|  D   |      The Gopher       |    800 |
+------+-----------------------+--------+
data := [][]string{
    []string{"1/1/2014", "Domain name", "2233", "$10.98"},
    []string{"1/1/2014", "January Hosting", "2233", "$54.95"},
    []string{"1/4/2014", "February Hosting", "2233", "$51.00"},
    []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
table.SetFooter([]string{"", "", "Total", "$146.93"}) // Add Footer
table.SetBorder(false)                                // Set Border to false
table.AppendBulk(data)                                // Add Bulk Data
table.Render()
Output 2

    DATE   |       DESCRIPTION        |  CV2  | AMOUNT
+----------+--------------------------+-------+---------+
  1/1/2014 | Domain name              |  2233 | $10.98
  1/1/2014 | January Hosting          |  2233 | $54.95
  1/4/2014 | February Hosting         |  2233 | $51.00
  1/4/2014 | February Extra Bandwidth |  2233 | $30.00
+----------+--------------------------+-------+---------+
                                        TOTAL | $146 93
                                      +-------+---------+

Example 3 - CSV
table, _ := tablewriter.NewCSV(os.Stdout, "test_info.csv", true)
table.SetAlignment(tablewriter.ALIGN_LEFT)   // Set Alignment
table.Render()
Output 3
+----------+--------------+------+-----+---------+----------------+
|  FIELD   |     TYPE     | NULL | KEY | DEFAULT |     EXTRA      |
+----------+--------------+------+-----+---------+----------------+
| user_id  | smallint(5)  | NO   | PRI | NULL    | auto_increment |
| username | varchar(10)  | NO   |     | NULL    |                |
| password | varchar(100) | NO   |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
Example 4 - Custom Separator
table, _ := tablewriter.NewCSV(os.Stdout, "test.csv", true)
table.SetRowLine(true)         // Enable row line

// Change table lines
table.SetCenterSeparator("*")
table.SetColumnSeparator("‡")
table.SetRowSeparator("-")

table.SetAlignment(tablewriter.ALIGN_LEFT)
table.Render()
Output 4
*------------*-----------*---------*
╪ FIRST NAME ╪ LAST NAME ╪   SSN   ╪
*------------*-----------*---------*
╪ John       ╪ Barry     ╪ 123456  ╪
*------------*-----------*---------*
╪ Kathy      ╪ Smith     ╪ 687987  ╪
*------------*-----------*---------*
╪ Bob        ╪ McCornick ╪ 3979870 ╪
*------------*-----------*---------*
TODO
  • Import Directly from CSV - done
  • Support for SetFooter - done
  • Support for SetBorder - done
  • Support table with uneven rows - done
  • Support custom alignment
  • General Improvement & Optimisation
  • NewHTML Parse table from HTML

Documentation

Overview

Create & Generate text based table

Index

Constants

View Source
const (
	CENTRE = "+"
	ROW    = "-"
	COLUMN = "|"
	SPACE  = " "
)
View Source
const (
	ALIGN_DEFAULT = iota
	ALIGN_CENTRE
	ALIGN_RIGHT
	ALIGN_LEFT
)
View Source
const (
	MAX_ROW_WIDTH = 30
)

Variables

This section is empty.

Functions

func ConditionString

func ConditionString(cond bool, valid, inValid string) string

Simple Condition for string Returns value based on condition

func DisplayWidth

func DisplayWidth(str string) int

func Pad

func Pad(s, pad string, width int) string

Pad String Attempts to play string in the center

func PadLeft

func PadLeft(s, pad string, width int) string

Pad String Left position This would pace string at the right side fo the screen

func PadRight

func PadRight(s, pad string, width int) string

Pad String Right position This would pace string at the left side fo the screen

func Title

func Title(name string) string

Format Table Header Replace _ , . and spaces

func WrapString

func WrapString(s string, lim int) ([]string, int)

Wrap wraps s into a paragraph of lines of length lim, with minimal raggedness.

func WrapWords

func WrapWords(words []string, spc, lim, pen int) [][]string

WrapWords is the low-level line-breaking algorithm, useful if you need more control over the details of the text wrapping process. For most uses, WrapString will be sufficient and more convenient.

WrapWords splits a list of words into lines with minimal "raggedness", treating each rune as one unit, accounting for spc units between adjacent words on each line, and attempting to limit lines to lim units. Raggedness is the total error over all lines, where error is the square of the difference of the length of the line and lim. Too-long lines (which only happen when a single word is longer than lim units) have pen penalty units added to the error.

Types

type Table

type Table struct {
	// contains filtered or unexported fields
}

func NewCSV

func NewCSV(writer io.Writer, fileName string, hasHeader bool) (*Table, error)

Start A new table by importing from a CSV file Takes io.Writer and csv File name

func NewCSVReader

func NewCSVReader(writer io.Writer, csvReader *csv.Reader, hasHeader bool) (*Table, error)
Start a New Table Writer with csv.Reader

This enables customisation such as reader.Comma = ';' See http://golang.org/src/pkg/encoding/csv/reader.go?s=3213:3671#L94

func NewWriter

func NewWriter(writer io.Writer) *Table

Start New Table Take io.Writer Directly

func (*Table) Append

func (t *Table) Append(row []string)

Append row to table

func (*Table) AppendBulk

func (t *Table) AppendBulk(rows [][]string)

Allow Support for Bulk Append Eliminates repeated for loops

func (Table) Render

func (t Table) Render()

Render table output

func (*Table) SetAlignment

func (t *Table) SetAlignment(align int)

Set Table Alignment

func (*Table) SetAutoFormatHeaders

func (t *Table) SetAutoFormatHeaders(auto bool)

Turn header autoformatting on/off. Default is on (true).

func (*Table) SetAutoWrapText

func (t *Table) SetAutoWrapText(auto bool)

Turn automatic multiline text adjustment on/off. Default is on (true).

func (*Table) SetBorder

func (t *Table) SetBorder(border bool)

Set Table Border This would enable / disable line around the table

func (*Table) SetCenterSeparator

func (t *Table) SetCenterSeparator(sep string)

Set the center Separator

func (*Table) SetColWidth

func (t *Table) SetColWidth(width int)

Set the Default column width

func (*Table) SetColumnSeparator

func (t *Table) SetColumnSeparator(sep string)

Set the Column Separator

func (*Table) SetFooter

func (t *Table) SetFooter(keys []string)

Set table Footer

func (*Table) SetHeader

func (t *Table) SetHeader(keys []string)

Set table header

func (*Table) SetHeaderLine

func (t *Table) SetHeaderLine(line bool)

Set Header Line This would enable / disable a line after the header

func (*Table) SetRowLine

func (t *Table) SetRowLine(line bool)

Set Row Line This would enable / disable a line on each row of the table

func (*Table) SetRowSeparator

func (t *Table) SetRowSeparator(sep string)

Set the Row Separator

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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