list

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2018 License: MIT Imports: 7 Imported by: 0

README

List

GoDoc

Pretty-print lists with multiple levels/indents into ASCII/Unicode strings.

  • Append Items one-by-one or as a group
  • Indent/UnIndent as you like
  • Support Items with Multiple-lines
  • Mirror output to an io.Writer object (like os.StdOut)
  • Completely customizable styles
  • Render as:
    • (ASCII/Unicode) List
    • HTML List (with custom CSS Class)
    • Markdown List
 ■ Game Of Thrones
   ■ Winter
   ■ Is
   ■ Coming
     ■ This
     ■ Is
     ■ Known
 ■ The Dark Tower
   ■ The Gunslinger

A demonstration of all the capabilities can be found here: ../cmd/demo-list

Documentation

Index

Constants

View Source
const (
	// DefaultHTMLCSSClass stores the css-class to use when none-provided via
	// SetHTMLCSSClass(cssClass string).
	DefaultHTMLCSSClass = "go-pretty-table"
)

Variables

View Source
var (
	// StyleDefault renders a List like below:
	//  * Game Of Thrones
	//    * Winter
	//    * Is
	//    * Coming
	//      * This
	//      * Is
	//      * Known
	//    * The Dark Tower
	//      * The Gunslinger
	StyleDefault = Style{
		Format:              text.FormatDefault,
		CharConnectBottom:   " ",
		CharHorizontal:      " ",
		CharItem:            "*",
		CharItemBottom:      "*",
		CharItemFirst:       "*",
		CharItemSingle:      "*",
		CharItemTop:         "*",
		CharPaddingLeft:     " ",
		CharPaddingRight:    "",
		CharVertical:        " ",
		CharVerticalConnect: " ",
		LinePrefix:          "",
		Name:                "StyleDefault",
	}

	// StyleBulletCircle renders a List like below:
	//  ● Game Of Thrones
	//    ● Winter
	//    ● Is
	//    ● Coming
	//      ● This
	//      ● Is
	//      ● Known
	//    ● The Dark Tower
	//      ● The Gunslinger
	StyleBulletCircle = Style{
		Format:              text.FormatDefault,
		CharConnectBottom:   " ",
		CharHorizontal:      " ",
		CharItem:            text.BulletCircle,
		CharItemBottom:      text.BulletCircle,
		CharItemFirst:       text.BulletCircle,
		CharItemSingle:      text.BulletCircle,
		CharItemTop:         text.BulletCircle,
		CharPaddingLeft:     " ",
		CharPaddingRight:    "",
		CharVertical:        " ",
		CharVerticalConnect: " ",
		LinePrefix:          "",
		Name:                "StyleBulletCircle",
	}

	// StyleBulletFlower renders a List like below:
	//  ✽ Game Of Thrones
	//    ✽ Winter
	//    ✽ Is
	//    ✽ Coming
	//      ✽ This
	//      ✽ Is
	//      ✽ Known
	//    ✽ The Dark Tower
	//      ✽ The Gunslinger
	StyleBulletFlower = Style{
		Format:              text.FormatDefault,
		CharConnectBottom:   " ",
		CharHorizontal:      " ",
		CharItem:            text.BulletFlower,
		CharItemBottom:      text.BulletFlower,
		CharItemFirst:       text.BulletFlower,
		CharItemSingle:      text.BulletFlower,
		CharItemTop:         text.BulletFlower,
		CharPaddingLeft:     " ",
		CharPaddingRight:    "",
		CharVertical:        " ",
		CharVerticalConnect: " ",
		LinePrefix:          "",
		Name:                "StyleBulletFlower",
	}

	// StyleBulletSquare renders a List like below:
	//  ■ Game Of Thrones
	//    ■ Winter
	//    ■ Is
	//    ■ Coming
	//      ■ This
	//      ■ Is
	//      ■ Known
	//    ■ The Dark Tower
	//      ■ The Gunslinger
	StyleBulletSquare = Style{
		Format:              text.FormatDefault,
		CharConnectBottom:   " ",
		CharHorizontal:      " ",
		CharItem:            text.BulletSquare,
		CharItemBottom:      text.BulletSquare,
		CharItemFirst:       text.BulletSquare,
		CharItemSingle:      text.BulletSquare,
		CharItemTop:         text.BulletSquare,
		CharPaddingLeft:     " ",
		CharPaddingRight:    "",
		CharVertical:        " ",
		CharVerticalConnect: " ",
		LinePrefix:          "",
		Name:                "StyleBulletSquare",
	}

	// StyleBulletStar renders a List like below:
	//  ★ Game Of Thrones
	//    ★ Winter
	//    ★ Is
	//    ★ Coming
	//      ★ This
	//      ★ Is
	//      ★ Known
	//    ★ The Dark Tower
	//      ★ The Gunslinger
	StyleBulletStar = Style{
		Format:              text.FormatDefault,
		CharConnectBottom:   " ",
		CharHorizontal:      " ",
		CharItem:            text.BulletStar,
		CharItemBottom:      text.BulletStar,
		CharItemFirst:       text.BulletStar,
		CharItemSingle:      text.BulletStar,
		CharItemTop:         text.BulletStar,
		CharPaddingLeft:     " ",
		CharPaddingRight:    "",
		CharVertical:        " ",
		CharVerticalConnect: " ",
		LinePrefix:          "",
		Name:                "StyleBulletStar",
	}

	// StyleBulletTriangle renders a List like below:
	//  ▶ Game Of Thrones
	//    ▶ Winter
	//    ▶ Is
	//    ▶ Coming
	//      ▶ This
	//      ▶ Is
	//      ▶ Known
	//    ▶ The Dark Tower
	//      ▶ The Gunslinger
	StyleBulletTriangle = Style{
		Format:              text.FormatDefault,
		CharConnectBottom:   " ",
		CharHorizontal:      " ",
		CharItem:            text.BulletTrianglePointingRight,
		CharItemBottom:      text.BulletTrianglePointingRight,
		CharItemFirst:       text.BulletTrianglePointingRight,
		CharItemSingle:      text.BulletTrianglePointingRight,
		CharItemTop:         text.BulletTrianglePointingRight,
		CharPaddingLeft:     " ",
		CharPaddingRight:    "",
		CharVertical:        " ",
		CharVerticalConnect: " ",
		LinePrefix:          "",
		Name:                "StyleBulletTriangle",
	}

	// StyleConnectedBold renders a List like below:
	//  ┏━ Game Of Thrones
	//  ┗━┳━ Winter
	//    ┣━ Is
	//    ┣━ Coming
	//    ┣━┳━ This
	//    ┃ ┣━ Is
	//    ┃ ┗━ Known
	//    ┣━ The Dark Tower
	//    ┗━━━ The Gunslinger
	StyleConnectedBold = Style{
		Format:              text.FormatDefault,
		CharConnectBottom:   text.BoxBottomLeftBold,
		CharHorizontal:      text.BoxHorizontalBold,
		CharItem:            text.BoxLeftSeparatorBold,
		CharItemBottom:      text.BoxBottomLeftBold,
		CharItemFirst:       text.BoxTopSeparatorBold,
		CharItemSingle:      text.BoxHorizontalBold,
		CharItemTop:         text.BoxTopLeftBold,
		CharPaddingLeft:     text.BoxHorizontalBold,
		CharPaddingRight:    text.BoxHorizontalBold,
		CharVertical:        text.BoxVerticalBold,
		CharVerticalConnect: text.BoxLeftSeparatorBold,
		LinePrefix:          "",
		Name:                "StyleConnectedBold",
	}

	// StyleConnectedDouble renders a List like below:
	//  ╔═ Game Of Thrones
	//  ╚═╦═ Winter
	//    ╠═ Is
	//    ╠═ Coming
	//    ╠═╦═ This
	//    ║ ╠═ Is
	//    ║ ╚═ Known
	//    ╠═ The Dark Tower
	//    ╚═══ The Gunslinger
	StyleConnectedDouble = Style{
		Format:              text.FormatDefault,
		CharConnectBottom:   text.BoxBottomLeftDouble,
		CharHorizontal:      text.BoxHorizontalDouble,
		CharItem:            text.BoxLeftSeparatorDouble,
		CharItemBottom:      text.BoxBottomLeftDouble,
		CharItemFirst:       text.BoxTopSeparatorDouble,
		CharItemSingle:      text.BoxHorizontalDouble,
		CharItemTop:         text.BoxTopLeftDouble,
		CharPaddingLeft:     text.BoxHorizontalDouble,
		CharPaddingRight:    text.BoxHorizontalDouble,
		CharVertical:        text.BoxVerticalDouble,
		CharVerticalConnect: text.BoxLeftSeparatorDouble,
		LinePrefix:          "",
		Name:                "StyleConnectedDouble",
	}

	// StyleConnectedLight renders a List like below:
	//  ┌─ Game Of Thrones
	//  └─┬─ Winter
	//    ├─ Is
	//    ├─ Coming
	//    ├─┬─ This
	//    │ ├─ Is
	//    │ └─ Known
	//    ├─ The Dark Tower
	//    └─── The Gunslinger
	StyleConnectedLight = Style{
		Format:              text.FormatDefault,
		CharConnectBottom:   text.BoxBottomLeft,
		CharHorizontal:      text.BoxHorizontal,
		CharItem:            text.BoxLeftSeparator,
		CharItemBottom:      text.BoxBottomLeft,
		CharItemFirst:       text.BoxTopSeparator,
		CharItemSingle:      text.BoxHorizontal,
		CharItemTop:         text.BoxTopLeft,
		CharPaddingLeft:     text.BoxHorizontal,
		CharPaddingRight:    text.BoxHorizontal,
		CharVertical:        text.BoxVertical,
		CharVerticalConnect: text.BoxLeftSeparator,
		LinePrefix:          "",
		Name:                "StyleConnectedLight",
	}

	// StyleConnectedRounded renders a List like below:
	//  ╭─ Game Of Thrones
	//  ╰─┬─ Winter
	//    ├─ Is
	//    ├─ Coming
	//    ├─┬─ This
	//    │ ├─ Is
	//    │ ╰─ Known
	//    ├─ The Dark Tower
	//    ╰─── The Gunslinger
	StyleConnectedRounded = Style{
		Format:              text.FormatDefault,
		CharConnectBottom:   text.BoxBottomLeftRounded,
		CharHorizontal:      text.BoxHorizontal,
		CharItem:            text.BoxLeftSeparator,
		CharItemBottom:      text.BoxBottomLeftRounded,
		CharItemFirst:       text.BoxTopSeparator,
		CharItemSingle:      text.BoxHorizontal,
		CharItemTop:         text.BoxTopLeftRounded,
		CharPaddingLeft:     text.BoxHorizontal,
		CharPaddingRight:    text.BoxHorizontal,
		CharVertical:        text.BoxVertical,
		CharVerticalConnect: text.BoxLeftSeparator,
		LinePrefix:          "",
		Name:                "StyleConnectedRounded",
	}

	// StyleMarkdown renders a List like below:
	//    * Game Of Thrones
	//      * Winter
	//      * Is
	//      * Coming
	//        * This
	//        * Is
	//        * Known
	//      * The Dark Tower
	//        * The Gunslinger
	StyleMarkdown = Style{
		Format:              text.FormatDefault,
		CharConnectBottom:   " ",
		CharHorizontal:      " ",
		CharItem:            "*",
		CharItemBottom:      "*",
		CharItemFirst:       "*",
		CharItemSingle:      "*",
		CharItemTop:         "*",
		CharPaddingLeft:     " ",
		CharPaddingRight:    "",
		CharVertical:        " ",
		CharVerticalConnect: " ",
		LinePrefix:          "  ",
		Name:                "StyleMarkdown",
	}
)

Functions

This section is empty.

Types

type List

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

List helps print a 2-dimensional array in a human readable pretty-List.

func (*List) AppendItem

func (l *List) AppendItem(item interface{})

AppendItem appends the item to the List of items to render.

func (*List) AppendItems

func (l *List) AppendItems(items []interface{})

AppendItems appends the items to the List of items to render.

func (*List) Indent

func (l *List) Indent()

Indent indents the following items to appear right-shifted.

func (*List) Length

func (l *List) Length() int

Length returns the number of items to be rendered.

func (*List) Render

func (l *List) Render() string

Render renders the List in a human-readable "pretty" format. Example:

  • Game Of Thrones
  • Winter
  • Is
  • Coming
  • This
  • Is
  • Known
  • The Dark Tower
  • The Gunslinger

func (*List) RenderHTML

func (l *List) RenderHTML() string

RenderHTML renders the List in the HTML format. Example:

<ul class="go-pretty-table">
  <li>Game Of Thrones</li>
  <ul class="go-pretty-table-1">
    <li>Winter</li>
    <li>Is</li>
    <li>Coming</li>
    <ul class="go-pretty-table-2">
      <li>This</li>
      <li>Is</li>
      <li>Known</li>
    </ul>
  </ul>
  <li>The Dark Tower</li>
  <ul class="go-pretty-table-1">
    <li>The Gunslinger</li>
  </ul>
</ul>

func (*List) RenderMarkdown

func (l *List) RenderMarkdown() string

RenderMarkdown renders the List in the Markdown format. Example:

  • Game Of Thrones
  • Winter
  • Is
  • Coming
  • This
  • Is
  • Known
  • The Dark Tower
  • The Gunslinger

func (*List) Reset

func (l *List) Reset()

Reset sets the List to its initial state.

func (*List) SetHTMLCSSClass

func (l *List) SetHTMLCSSClass(cssClass string)

SetHTMLCSSClass sets the the HTML CSS Class to use on the <ul> node when rendering the List in HTML format. Recursive lists would use a numbered index suffix. For ex., if the cssClass is set as "foo"; the <ul> for level 0 would have the class set as "foo"; the <ul> for level 1 would have "foo-1".

func (*List) SetOutputMirror

func (l *List) SetOutputMirror(mirror io.Writer)

SetOutputMirror sets an io.Writer for all the Render functions to "Write" to in addition to returning a string.

func (*List) SetStyle

func (l *List) SetStyle(style Style)

SetStyle overrides the DefaultStyle with the provided one.

func (*List) Style

func (l *List) Style() *Style

Style returns the current style.

func (*List) UnIndent

func (l *List) UnIndent()

UnIndent un-indents the following items to appear left-shifted.

type Style

type Style struct {
	Format              text.Format
	CharConnectBottom   string
	CharHorizontal      string
	CharItem            string
	CharItemBottom      string
	CharItemFirst       string
	CharItemSingle      string
	CharItemTop         string
	CharPaddingLeft     string
	CharPaddingRight    string
	CharVertical        string
	CharVerticalConnect string
	LinePrefix          string
	Name                string
}

Style declares how to render the List.

type Writer

type Writer interface {
	AppendItem(item interface{})
	AppendItems(items []interface{})
	Indent()
	Length() int
	Render() string
	RenderHTML() string
	RenderMarkdown() string
	Reset()
	SetHTMLCSSClass(cssClass string)
	SetOutputMirror(mirror io.Writer)
	SetStyle(style Style)
	Style() *Style
	UnIndent()
}

Writer declares the interfaces that can be used to setup and render a list.

func NewWriter

func NewWriter() Writer

NewWriter initializes and returns a Writer.

Jump to

Keyboard shortcuts

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