ruler

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 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: ".", FillLine: LeftOnly},
			AlignVersion: AlignMarker{Center: "..", FillLine: RightOnly},
		},

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

		"markdown": &HintStyle{
			KeepEmpty:   true,
			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,
			},
		},
	}
)

TODO: 2025-03-13 relocate, make configurable

Functions

This section is empty.

Types

type AlignMarker added in v0.3.2

type AlignMarker struct {
	Left     string // marker before all horizontal lines
	Center   string // marker between horizontal lines
	Right    string // marker after all horizontal lines
	FillLine        // add horizontal line to left, right or both sides
}

func (AlignMarker) FillLines added in v0.3.3

func (am AlignMarker) FillLines(l, r int) (int, int)

FillLines ensures that the minimum required number of horizontal line characters are included in the resulting ruler.

| pattern |    fill    | min left | min right |          rule           |
| ---:--- | ----:----- | ---:---- | ----:---- | ----------------------- |
|   l--   |    auto    |    1     |           | left + right >= 1       |
|   --r   |    auto    |          |     1     | left + right >= 1       |
|   -c-   |    auto    |    1     |     1     | left >= 1, right >= 1   |
|   -c-   | left only  |   (1)    |           | left >= 1 if right == 0 |
|   -c-   | right only |          |    (1)    | right >= 1 if left == 0 |
|   l-r   |    auto    |    1     |           | left + right >= 1       |

func (AlignMarker) MinWidth added in v0.3.3

func (am AlignMarker) MinWidth() int

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
}

Layout construction:

  • cw = ColumnWidth
  • am = alignMarker
  • sm = sortMarker
  • p = padding if ruler is padded

Layout:

absolute column width |<------------------------- cw.Chars ------------------------->|
numeric alignment                       |<----- cw.Left ----->|<----- cw.Right ----->|
version alignment     |<----- cw.Left ----->|<----- cw.Right ----->|
column hints          |p am.Left ---------------- am.Center ----------- am.Right sm p|

func (*Encoder) CollapseEmptyColumn added in v0.3.3

func (enc *Encoder) CollapseEmptyColumn() bool

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) EncodeColumn added in v0.3.3

func (enc *Encoder) EncodeColumn(
	buf *bytes.Buffer,
	ruler *model.Ruler,
	cw model.ColumnWidth,
)

func (*Encoder) EncodeColumnWithHints added in v0.3.3

func (enc *Encoder) EncodeColumnWithHints(
	buf *bytes.Buffer,
	ruler *model.Ruler,
	cw model.ColumnWidth,
	cf model.ColumnFlags,
)

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 FillLine added in v0.3.3

type FillLine byte
const (
	AutoFill FillLine = iota
	LeftOnly
	RightOnly
)

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
	KeepEmpty    bool // default: allow empty columns to be collapsed to `||`, true: minimum column width is MinWidth() + padding
}

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

func (*HintStyle) HasSortFlags added in v0.3.3

func (s *HintStyle) HasSortFlags() bool

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