Help us shape the future of Go! Take the 2026 Go Developer Survey

cliview

package module
v0.0.0-...-5471ce6 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2015 License: MIT Imports: 9 Imported by: 1

README

Build Status

CLI Views

Print complicated data objects in tree view or table view, with customizable formatting and styling support.

Usage

import (
	cv "github.com/easeway/go-cliview"
)

func ShowTable(data []interface{}) {
	cv.Table{
		Columns: []cv.Column{
			cv.Column{Title: "ID", Field: "id", Width: 16},
			cv.Column{Title: "Name", Field: "name", MaxWidth: 20},
			cv.Column{Title: "Age", Field: "age", MaxWidth: 3, Align: cv.AlignRight},
			cv.Column{Title: "Home Address", Field: "home", Width: -50},		// 50% width
			cv.Column{Title: "Office Address", Field: "office", Width: -50},	// 50% width
		},
		MaxWidth: 80,
	}.HideColumns("Age", "Home Address").Print(data)
}

func ShowJSON(obj map[string]interface{}) {
	cv.NewTree().Print(obj)
}

Features

  • Print complicated object in Tree view
    • Left padding support
    • Customizable indent
    • Formatting values
    • Styling map keys and values
  • Print array in Table view
    • Customizable column headers
    • Fixed column width
    • Automatically decide column width using actual data
    • Column width in percentage (specified as negative values)
    • Alignment: left, middle, right
    • Auto-ellipsis text
    • Maximum table width (for percentage widths only)
    • Formatting values
    • Styling headers, cells

Views Details

Tree view

Usage
func ShowInTreeView(data interface{}) {
	cv.NewTree().Print(data)
}

or

func ShowInTreeView(data interface{}) {
	tv := &cv.Tree{
		Output: cv.Output{		// Output is optional
			Padding: 10,		// left paddings
			Writer: os.Stderr,	// specify output, default is os.Stdout
			Formatter: func (class string, data interface{},, formatter cv.FormatterFunc) string {
				// custom formatter
				// 'class' can be:
				//    - 'tree:key:path'
				//    - 'tree:val:path'
				// here path is from top of "data", e.g.
				//
				// root:
				//     key1:
				//       - name: Jack
				//       - name: Alice
			    //
				// path for "Jack" is 'tree:val:root/key1/0/name',
				// path for "Alice" is 'tree:val:root/key1/1/name',
				// path for "key1" is 'tree:key:root/key1'
				//
				// if you don't know how to format, simply call chained "formatter"
				// return formatter(class, data, nil)
				//
				// When class is 'tree:key:...', it is used for filtering keys rather than renaming keys
				// (renaming keys should use Styler). If an empty string is returned, the key is skipped.
				...
			},
			Styler: func (class, text string, data interface{}) string {
				// class is similar to formatter
				...
			},
		},
		Indent: cv.DefaultIndent,	// override indent, DefaultIndent is 4
	}
	tv.Print(data)
}

Table view

Usage
func ShowInTableView(data []map[string]interface{}) {
	tv := &cv.Table{
		Output: cv.Output{		// Output is optional
			Padding: 10,		// left paddings
			Writer: os.Stderr,	// specify output, default is os.Stdout
			Formatter: func (class string, data interface{}, formatter cv.FormatterFunc) string {
				// custom formatter
				// 'class' can be: 'table:row:field'
				...
			},
			Styler: func (class, text string, data interface{}) string {
				// class is similar to formatter, it can also be
				// 'table:head:field'
				...
			},
		},
		Columns: []cv.Column{	// required, define the columns
			cv.Column{
				Title: "Display Title",
				Field: "key to fetch data",
				Width: 10,		// >0 for fixed width
								// <0 used as percentage
								// =0 auto decided from data
				MaxWidth: 10,	// limit the maximum column Width
				Align: cv.AlignLeft,	// this is default
										// can be cv.AlignRight, cv.AlignMiddle
				Fetcher: func(col cv.Column, row map[string]interface{}) interface{} {
					// optional function for fetching cell data with special logic.
					// with this function, the column can be a virtual column which doesn't
					// require the Field in the table data but calculated from other fields
				},
				Formatter: ...,  // same as Output.Formatter but operates on column level
			},
			...
		},
		MaxWidth: 80,			// maximum table width
	}
	tv.Print(data)
}

License

X11/MIT

Documentation

Index

Constants

View Source
const (
	AlignLeft   = 0
	AlignMiddle = 1
	AlignRight  = 2
)
View Source
const (
	DefaultIndent = 4
)

Variables

View Source
var (
	BorderFull = string(runesBorderFull)

	BorderCompact = string(runesBorderCompact)
)

Functions

func PaddingBuffer

func PaddingBuffer(padding int) *bytes.Buffer

func PaddingString

func PaddingString(padding int) string

Types

type Column

type Column struct {
	Title     string // title to be displayed
	Field     string // field name for retrieving data
	Width     int    // column width, >0 fixed, =0 auto, <0 percentage
	MaxWidth  int    // maximum column width
	Align     int
	Fetcher   func(column Column, row map[string]interface{}) interface{}
	Formatter FormatterFunc
	Styler    StylerFunc
}

type FormatterFunc

type FormatterFunc func(class string, data interface{}, formatter FormatterFunc) string

type KeyRankFunc

type KeyRankFunc func(path, key string) int

func ArrayKeyRanker

func ArrayKeyRanker(keys []string) KeyRankFunc

func PatternArrayKeyRanker

func PatternArrayKeyRanker(dict map[string][]string) KeyRankFunc

func SuffixArrayKeyRanker

func SuffixArrayKeyRanker(dict map[string][]string) KeyRankFunc

type Output

type Output struct {
	Padding   int
	Writer    io.Writer
	Styler    StylerFunc
	Formatter FormatterFunc
}

func (*Output) Format

func (o *Output) Format(class string, data interface{}) string

func (*Output) Out

func (o *Output) Out() io.Writer

func (*Output) PaddingBuffer

func (o *Output) PaddingBuffer() *bytes.Buffer

func (*Output) PaddingString

func (o *Output) PaddingString() string

func (*Output) Styling

func (o *Output) Styling(class, text string, data interface{}, styler StylerFunc) string

type StylerFunc

type StylerFunc func(class, text string, data interface{}) string

type Table

type Table struct {
	Output

	Border string // border characters
	// top: LT T TS RT
	// row: L S R
	// bot: LB B BS RB
	// row-splitter: LS C CS RS
	// head: LH SH RH
	// head-splitter: LSH CH CSH RSH
	Columns  []Column // Column definitions
	MaxWidth int      // maximum table width
	// contains filtered or unexported fields
}

func (*Table) HideColumns

func (tv *Table) HideColumns(names ...string) *Table

func (*Table) Print

func (tv *Table) Print(data []map[string]interface{})

type Tree

type Tree struct {
	Output
	Indent    int
	KeyRanker KeyRankFunc
}

func NewTree

func NewTree() *Tree

func (*Tree) Print

func (tv *Tree) Print(obj interface{})

func (*Tree) RankKey

func (tv *Tree) RankKey(path, key string) uint

Jump to

Keyboard shortcuts

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