ruler

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SORT_ASCENDING  = '^'
	SORT_DESCENDING = 'v'
)
View Source
const UNSET byte = 0

Variables

View Source
var (
	HintStyles = map[string]*HintStyle{
		"": &HintStyle{
			AlignLeft:    AlignMarker{Left: ":"},
			AlignRight:   AlignMarker{Right: ":"},
			AlignCenter:  AlignMarker{Center: ":"},
			AlignNumber:  AlignMarker{Center: "."},
			AlignVersion: AlignMarker{Center: ".."},
		},

		"none": &HintStyle{
			FlagsMask: 0xff,
		},

		"markdown": &HintStyle{
			FlagsMask:   ^model.AlignmentMask,
			AlignLeft:   AlignMarker{Left: ":"},
			AlignRight:  AlignMarker{Right: ":"},
			AlignCenter: AlignMarker{Left: ":", Right: ":"},
			Replace: map[model.ColumnFlags]model.ColumnFlags{

				model.AlignNumericFlag: model.AlignRightFlag,
				model.AlignVersionFlag: model.AlignLeftFlag,
			},
		},
	}
)

Functions

This section is empty.

Types

type AlignMarker added in v0.3.2

type AlignMarker struct {
	Left   string
	Center string
	Right  string
}

type Decoder

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

Decoder parses rulers from input lines

The input is expected to be either

  • a ruler template, or
  • a row being parsed from an existing PSV table

nil is returned if the input cannot be parsed.

Expected use while parsing a document:

rulerDec := NewDecoder()
rowDec   := NewRowDecoder()

for scanner.Scan() {
    line := scanner.Text()
    prefix, content := SplitPrefix(line)
    doc.SetTablePrefixOnce(prefix)
    if ruler := rulerDec.Decode(content); ruler != nil {
        doc.AppendRuler(ruler)
        continue
    }
    if row := rowDec.Decode(content); row != nil {
        doc.AppendRow(row)
        continue
    }
    // line is not part of the table
}

A Ruler may also be created via &model.Ruler{}, in which case it will use the default template of "| -"

func NewDecoder

func NewDecoder() *Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(input string) (*model.Ruler, []model.ColumnFlags)

Decode attempts to decode a line of text into a Ruler and possible ColumnSet if the ruler includes column behaviour hints.

No errors are returned. If the line cannot be parsed, it is not a ruler and should be parsed by another line parser.

func (*Decoder) Decode2Pass added in v0.3.2

func (d *Decoder) Decode2Pass(input string) (*model.Ruler, []model.ColumnFlags)

Decode2Pass attempts to decode a line of text into a Ruler and possible ColumnSet if the ruler includes column behaviour hints.

No errors are returned. If the line cannot be parsed, it is not a ruler and should be parsed by another line parser.

type Encoder

type Encoder struct {
	*HintStyle
}

func (*Encoder) Encode

func (enc *Encoder) Encode(
	buf *bytes.Buffer,
	ruler *model.Ruler,
	cws []model.ColumnWidth,
)

Encode encodes a ruler according to the column alignments and widths provided.

func (*Encoder) EncodeWithHints

func (enc *Encoder) EncodeWithHints(
	buf *bytes.Buffer,
	ruler *model.Ruler,
	cws []model.ColumnWidth,
	cfs []model.ColumnFlags,
)

EncodeWithHints encodes a ruler for a set number of columns, and includes additional alignment hints, according to the configured hint-styles.

Rulers with column hints have a general form of:

[padding] <left> <line> <center> <line> <right> <order> [<padding>]

func (*Encoder) MinWidth

func (enc *Encoder) MinWidth(cf model.ColumnFlags) int

MinWidth returns the minimum width of a field of a Ruler with columm alignment hints.

type HintStyle

type HintStyle struct {
	FlagsMask    model.ColumnFlags                       // mask to remove unsupported flags, 0: everything supported, 0xff: nothing supported
	Replace      map[model.ColumnFlags]model.ColumnFlags // map of equivalent flags
	AlignLeft    AlignMarker
	AlignRight   AlignMarker
	AlignCenter  AlignMarker
	AlignNumber  AlignMarker
	AlignVersion AlignMarker
}

REFACTOR: 2025-03-12 move to EncodingStyle see also cmd/psv/flags.go

type RulerBuilder

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

func (*RulerBuilder) IsSorted added in v0.3.2

func (rb *RulerBuilder) IsSorted() bool

func (*RulerBuilder) SetAlignment

func (rb *RulerBuilder) SetAlignment(alignmentFlags model.ColumnFlags)

func (*RulerBuilder) SetSort

func (rb *RulerBuilder) SetSort(sortFlags model.ColumnFlags)

func (*RulerBuilder) SetSortPriority

func (rb *RulerBuilder) SetSortPriority(sortPriority int)

Jump to

Keyboard shortcuts

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