table

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: MIT Imports: 4 Imported by: 1

README

table

This package provides support for writing text tables.

Examples

package main

import (
	"fmt"
	
	"github.com/mithrandie/go-text"
	"github.com/mithrandie/go-text/table"
)

func main() {
	header := []table.Field{
		table.NewField("c1", text.Centering),
		table.NewField("c2", text.Centering),
		table.NewField("c3", text.Centering),
	}
	
	recordSet := [][]table.Field{
		{
			table.NewField("1", text.RightAligned),
			table.NewField("abc", text.LeftAligned),
			table.NewField("true", text.NotAligned),
		},
		{
			table.NewField("2", text.RightAligned),
			table.NewField("def", text.LeftAligned),
			table.NewField("true", text.NotAligned),
		},
		{
			table.NewField("3", text.RightAligned),
			table.NewField("ghi", text.LeftAligned),
			table.NewField("true", text.NotAligned),
		},
	}
	
	alignments := []text.FieldAlignment{
		text.RightAligned,
		text.LeftAligned,
		text.NotAligned,
	}
	
	e := table.NewEncoder(table.GFMTable, len(recordSet))
	e.LineBreak = text.LF
	e.EastAsianEncoding = true
	e.CountDiacriticalSign = false
	e.WithoutHeader = false
    
	
	e.SetHeader(header)
	for _, record := range recordSet {
		e.AppendRecord(record)
	}
	e.SetFieldAlignments(alignments)
	
	encoded, _ := e.Encode()
	fmt.Println(encoded)
}

Documentation

Overview

Package table is a Go library to write text tables.

Index

Constants

View Source
const (
	VLine                    = '|'
	HLine                    = '-'
	CrossLine                = '+'
	PadChar                  = ' '
	AlignSign                = ':'
	MarkdownLineBreak        = "<br />"
	EscapeChar               = '\\'
	BoxHorizontal            = '─'
	BoxVirtical              = '│'
	BoxVirticalAndHorizontal = '┼'
	BoxVirticalAndRIght      = '├'
	BoxVirticalAndLeft       = '┤'
	BoxDownAndRight          = '┌'
	BoxDownAndLeft           = '┐'
	BoxDownAndHorizontal     = '┬'
	BoxUpAndRight            = '└'
	BoxUpAndLeft             = '┘'
	BoxUpAndHorizontal       = '┴'
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DrawingCharacterSet added in v1.4.2

type DrawingCharacterSet struct {
	Horizontal            []byte
	Virtical              []byte
	VirticalAndHorizontal []byte
	VirticalAndRight      []byte
	VirticalAndLeft       []byte
	DownAndRight          []byte
	DownAndLeft           []byte
	DownAndHorizontal     []byte
	UpAndRight            []byte
	UpAndLeft             []byte
	UpAndHorizontal       []byte
}

func NewBoxDrawingCharacterSet added in v1.4.2

func NewBoxDrawingCharacterSet() *DrawingCharacterSet

func NewPlainDrawingCharacterSet added in v1.4.2

func NewPlainDrawingCharacterSet() *DrawingCharacterSet

type Encoder

type Encoder struct {
	Format               Format
	LineBreak            text.LineBreak
	EastAsianEncoding    bool
	CountDiacriticalSign bool
	CountFormatCode      bool
	Encoding             text.Encoding

	// Plain Table or Box Table
	DrawingCharacterSet *DrawingCharacterSet

	// GFM or Org Table
	WithoutHeader bool
	// contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(format Format, recordCounts int) *Encoder

func (*Encoder) AppendRecord

func (e *Encoder) AppendRecord(record []Field)

func (*Encoder) Encode

func (e *Encoder) Encode() (string, error)

func (*Encoder) SetFieldAlignments

func (e *Encoder) SetFieldAlignments(alignments []text.FieldAlignment)

func (*Encoder) SetHeader

func (e *Encoder) SetHeader(header []Field)

type Field

type Field struct {
	Contents  string
	Alignment text.FieldAlignment

	Lines []string
	Width int
}

func NewField

func NewField(contents string, alignment text.FieldAlignment) Field

type Format

type Format int
const (
	PlainTable Format = iota
	GFMTable
	OrgTable
	BoxTable
)

Jump to

Keyboard shortcuts

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