list

package
v4.3.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2019 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

Overview

Example
lw := NewWriter()
// append a tree
lw.AppendItem("George. R. R. Martin")
lw.Indent()
lw.AppendItem("A Song of Ice and Fire")
lw.Indent()
lw.AppendItems([]interface{}{
	"Arya Stark",
	"Bran Stark",
	"Rickon Stark",
	"Robb Stark",
	"Sansa Stark",
	"Jon Snow",
})
lw.UnIndent()
lw.UnIndent()
// append another tree
lw.AppendItem("Stephen King")
lw.Indent()
lw.AppendItem("The Dark Tower")
lw.Indent()
lw.AppendItems([]interface{}{
	"Jake Chambers",
	"Randal Flagg",
	"Roland Deschain",
})
lw.UnIndent()
lw.AppendItem("the shawshank redemption")
lw.Indent()
lw.AppendItems([]interface{}{
	"andy dufresne",
	"byron hadley",
	"ellis boyd redding",
	"samuel norton",
})
// customize rendering
lw.SetStyle(StyleConnectedLight)
lw.Style().CharItemTop = "├"
lw.Style().Format = text.FormatTitle
// render it
fmt.Printf("Simple List:\n%s", lw.Render())
Output:

Simple List:
├ George. R. R. Martin
│  └─ A Song Of Ice And Fire
│     ├─ Arya Stark
│     ├─ Bran Stark
│     ├─ Rickon Stark
│     ├─ Robb Stark
│     ├─ Sansa Stark
│     └─ Jon Snow
└─ Stephen King
   ├─ The Dark Tower
   │  ├─ Jake Chambers
   │  ├─ Randal Flagg
   │  └─ Roland Deschain
   └─ The Shawshank Redemption
      ├─ Andy Dufresne
      ├─ Byron Hadley
      ├─ Ellis Boyd Redding
      └─ Samuel Norton

Index

Examples

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,
		CharItemSingle:   "*",
		CharItemTop:      "*",
		CharItemFirst:    "*",
		CharItemMiddle:   "*",
		CharItemVertical: "  ",
		CharItemBottom:   "*",
		CharNewline:      "\n",
		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,
		CharItemSingle:   "●",
		CharItemTop:      "●",
		CharItemFirst:    "●",
		CharItemMiddle:   "●",
		CharItemVertical: "  ",
		CharItemBottom:   "●",
		CharNewline:      "\n",
		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,
		CharItemSingle:   "✽",
		CharItemTop:      "✽",
		CharItemFirst:    "✽",
		CharItemMiddle:   "✽",
		CharItemVertical: "  ",
		CharItemBottom:   "✽",
		CharNewline:      "\n",
		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,
		CharItemSingle:   "■",
		CharItemTop:      "■",
		CharItemFirst:    "■",
		CharItemMiddle:   "■",
		CharItemVertical: "  ",
		CharItemBottom:   "■",
		CharNewline:      "\n",
		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,
		CharItemSingle:   "★",
		CharItemTop:      "★",
		CharItemFirst:    "★",
		CharItemMiddle:   "★",
		CharItemVertical: "  ",
		CharItemBottom:   "★",
		CharNewline:      "\n",
		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,
		CharItemSingle:   "▶",
		CharItemTop:      "▶",
		CharItemFirst:    "▶",
		CharItemMiddle:   "▶",
		CharItemVertical: "  ",
		CharItemBottom:   "▶",
		CharNewline:      "\n",
		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,
		CharItemSingle:   "━━",
		CharItemTop:      "┏━",
		CharItemFirst:    "┣━",
		CharItemMiddle:   "┣━",
		CharItemVertical: "┃  ",
		CharItemBottom:   "┗━",
		CharNewline:      "\n",
		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,
		CharItemSingle:   "══",
		CharItemTop:      "╔═",
		CharItemFirst:    "╠═",
		CharItemMiddle:   "╠═",
		CharItemVertical: "║  ",
		CharItemBottom:   "╚═",
		CharNewline:      "\n",
		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,
		CharItemSingle:   "──",
		CharItemTop:      "┌─",
		CharItemFirst:    "├─",
		CharItemMiddle:   "├─",
		CharItemVertical: "│  ",
		CharItemBottom:   "└─",
		CharNewline:      "\n",
		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,
		CharItemSingle:   "──",
		CharItemTop:      "╭─",
		CharItemFirst:    "├─",
		CharItemMiddle:   "├─",
		CharItemVertical: "│  ",
		CharItemBottom:   "╰─",
		CharNewline:      "\n",
		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,
		CharItemSingle:   "*",
		CharItemTop:      "*",
		CharItemFirst:    "*",
		CharItemMiddle:   "*",
		CharItemVertical: "  ",
		CharItemBottom:   "*",
		CharNewline:      "<br/>",
		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 added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

func (l *List) Reset()

Reset sets the List to its initial state.

func (*List) SetHTMLCSSClass added in v1.0.0

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 added in v1.0.0

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 added in v1.0.0

func (l *List) UnIndent()

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

type Style

type Style struct {
	Format           text.Format // formatting for the Text
	CharItemSingle   string      // the bullet for a single-item list
	CharItemTop      string      // the bullet for the top-most item
	CharItemFirst    string      // the bullet for the first item
	CharItemMiddle   string      // the bullet for non-first/non-last item
	CharItemVertical string      // the vertical connector from one bullet to the next
	CharItemBottom   string      // the bullet for the bottom-most item
	CharNewline      string      // new-line character to use
	LinePrefix       string      // prefix for every single line
	Name             string      // name of the Style
}

Style declares how to render the List (items).

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