Code Monkey home page Code Monkey logo

cli-table's Introduction

cli-table

CircleCI Coverage Status GoDoc

Fancy ASCII tables for your CLI with full ANSI support.

The cli-table package provides a simple API for rendering ASCII tables. Its main features are:

  • headers and header groups; header groups may span multiple header columns.
  • user-selectable cell padding.
  • per-column and per header group alignment selection (left, center, right).
  • ANSI support. ANSI characters are properly handled and do not mess up width calculations.
  • tables are rendered to any output sink implementing io.Writer

Getting started

package main

import (
	"os"

	"github.com/geckoboard/cli-table"
)

func main() {
	t := table.New(3)

	// Set headers
	t.SetHeader(0, "left", table.AlignLeft)
	t.SetHeader(1, "center", table.AlignCenter)
	t.SetHeader(2, "right", table.AlignRight)

	// Optionally define header groups
	t.AddHeaderGroup(2, "left group", table.AlignCenter)
	t.AddHeaderGroup(1, "right group", table.AlignRight)

	// Append single row
	t.Append([]string{"1", "2", "3"})

	// Or append a bunch of rows which may contain ANSI characters
	t.Append(
		[]string{"1", "2", "3"},
		[]string{"\033[33mfour\033[0m", "five", "six"},
	)

	// Render table and strip out ANSI characters
	t.Write(os.Stdout, table.StripAnsi)

	// If your terminal does support ANSI characters you can use:
	// t.Write(os.Stdout, table.PreserveAnsi)
}

Running this example would render the following output:

+---------------+---------------+
|  left group   |   right group |
+---------------+---------------+
| left | center |         right |
+------+--------+---------------+
| 1    |   2    |             3 |
| 1    |   2    |             3 |
| four |  five  |           six |
+------+--------+---------------+

cli-table's People

Contributors

achilleasa avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.