runes

package
v0.1.26 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2024 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToUtf8 added in v0.1.26

func BytesToUtf8(data []byte) ([]rune, error)

BytesToUtf8 is a function that converts bytes to runes. When error occurs, the function returns the runes decoded so far and the error.

Parameters:

  • data: The bytes to convert.

Returns:

  • []rune: The runes.
  • error: An error if the bytes are not valid UTF-8.

Errors:

  • *ErrInvalidUTF8Encoding: If the bytes are not valid UTF-8.

func EitherOrString added in v0.1.26

func EitherOrString(elems []rune, quote bool) string

EitherOrString is a function that returns a string representation of a slice of strings. Empty strings are ignored.

Parameters:

  • values: The values to convert to a string.

Returns:

  • string: The string representation.

Example:

EitherOrString([]rune{'a', 'b', 'c'}, false) // "either a, b or c"

func IndicesOf added in v0.1.26

func IndicesOf(data []rune, sep rune, exclude_sep bool) []int

Indices returns the indices of the separator in the data.

Parameters:

  • data: The data.
  • sep: The separator.
  • exclude_sep: Whether the separator is inclusive. If set to true, the indices will point to the character right after the separator. Otherwise, the indices will point to the separator itself.

Returns:

  • []int: The indices.

func Join added in v0.1.26

func Join(data [][]rune, sep rune) []rune

Join is a function that joins the data. Returns nil if the data is empty.

Parameters:

  • data: The data to join.
  • sep: The separator to use.

Returns:

  • []rune: The joined data.

func JoinSize added in v0.1.26

func JoinSize(data [][]rune) int

JoinSize returns the number of runes in the data.

Parameters:

  • data: The data to join.

Returns:

  • int: The number of runes.

func NormalizeRunes added in v0.1.26

func NormalizeRunes(chars []rune) ([]rune, error)

NormalizeRunes is a function that converts '\r\n' to '\n'.

Parameters:

  • chars: The characters to convert.

Returns:

  • []rune: The normalized characters.
  • error: An error if the characters are not valid UTF-8.

Errors:

  • *ErrUnexpectedChar: If the characters are not valid UTF-8.

func Repeat added in v0.1.26

func Repeat(char rune, count int) []rune

Repeat is a function that repeats the character.

Parameters:

  • char: The character to repeat.
  • count: The number of times to repeat the character.

Returns:

  • []rune: The repeated character. Returns nil if count is less than 0.

func StringToUtf8 added in v0.1.26

func StringToUtf8(str string) ([]rune, error)

StringToUtf8 converts a string to a slice of runes. When error occurs, the function returns the runes decoded so far and the error.

Parameters:

  • str: The string to convert.

Returns:

  • runes: The slice of runes.
  • error: An error of if the string is not valid UTF-8.

Errors:

  • *ErrInvalidUTF8Encoding: If the string is not valid UTF-8.

Types

type BoxBorderType

type BoxBorderType int

BoxBorderType is the type of the box border.

const (
	// BtNormal is the normal box border type.
	BtNormal BoxBorderType = iota

	// BtTriple is the triple box border type.
	BtTriple

	// BtQuadruple is the quadruple box border type.
	BtQuadruple

	// BtDouble is the double box border type.
	BtDouble

	// BtRounded is like BtNormal but with rounded corners.
	BtRounded
)

type BoxStyle

type BoxStyle struct {
	// LineType is the type of the line.
	LineType BoxBorderType

	// IsHeavy is whether the line is heavy or not.
	// Only applicable to BtNormal, BtTriple, and BtQuadruple.
	IsHeavy bool

	// Padding is the padding of the box.
	// [Top, Right, Bottom, Left]
	Padding [4]int
}

BoxStyle is the style of the box.

var (
	// DefaultBoxStyle is the default box style.
	DefaultBoxStyle *BoxStyle
)

func NewBoxStyle

func NewBoxStyle(line_type BoxBorderType, is_heavy bool, padding [4]int) *BoxStyle

NewBoxStyle creates a new box style.

Negative padding are set to 0.

Parameters:

  • line_type: The line type.
  • is_heavy: Whether the line is heavy or not.
  • padding: The padding of the box. [Top, Right, Bottom, Left]

Returns:

  • *BoxStyle: The new box style. Never returns nil.

func (BoxStyle) Apply added in v0.1.5

func (bs BoxStyle) Apply(table *RuneTable) error

Apply draws a box around a content that is specified in a table.

Format: If the content is [['H', 'e', 'l', 'l', 'o'], ['W', 'o', 'r', 'l', 'd']], the box will be:

┏━━━━━━━┓
┃ Hello ┃
┃ World ┃
┗━━━━━━━┛

Parameters:

  • table: The table that contains the content to be drawn.

Returns:

  • error: An error if the content could not be processed.

Behaviors:

  • If the box style is nil, the default box style will be used.

Each string of the content represents a row in the box.

func (BoxStyle) Corners

func (bs BoxStyle) Corners() [4]rune

Corners gets the corners of the box.

Returns:

  • [4]rune: The corners. [TopLeft, TopRight, BottomLeft, BottomRight]

func (BoxStyle) SideBorder

func (bs BoxStyle) SideBorder() rune

SideBorder gets the side border of the box.

It also applies to the left border as they are the same.

Returns:

  • string: The side border.

func (BoxStyle) TopBorder

func (bs BoxStyle) TopBorder() rune

TopBorder gets the top border of the box.

It also applies to the bottom border as they are the same.

Returns:

  • string: The top border.

type ErrInvalidUTF8Encoding added in v0.1.26

type ErrInvalidUTF8Encoding struct {
	// At is the index of the invalid UTF-8 encoding.
	At int
}

ErrInvalidUTF8Encoding is an error type for invalid UTF-8 encoding.

func NewErrInvalidUTF8Encoding added in v0.1.26

func NewErrInvalidUTF8Encoding(at int) *ErrInvalidUTF8Encoding

NewErrInvalidUTF8Encoding creates a new ErrInvalidUTF8Encoding error.

Parameters:

  • at: The index of the invalid UTF-8 encoding.

Returns:

  • *ErrInvalidUTF8Encoding: A pointer to the newly created error.

func (ErrInvalidUTF8Encoding) Error added in v0.1.26

func (e ErrInvalidUTF8Encoding) Error() string

Error implements the error interface.

Message:

"invalid UTF-8 encoding at index {At}"

type ErrUnexpectedChar added in v0.1.26

type ErrUnexpectedChar struct {
	// Expected is the expected character.
	Expecteds []rune

	// Previous is the previous character.
	Previous rune

	// Got is the current character.
	Got *rune
}

ErrUnexpectedChar is an error that occurs when an unexpected character is encountered.

func NewErrUnexpectedChar added in v0.1.26

func NewErrUnexpectedChar(previous rune, expecteds []rune, got *rune) *ErrUnexpectedChar

NewErrUnexpectedChar creates a new ErrUnexpectedChar error.

Parameters:

  • previous: the previous character.
  • expecteds: the expected characters.
  • got: the current character.

Returns:

  • *ErrUnexpectedChar: the error. Never returns nil.

func (ErrUnexpectedChar) Error added in v0.1.26

func (e ErrUnexpectedChar) Error() string

Error implements the error interface.

Message:

"expected {expected} after {previous}, got {got} instead".

type RuneTable

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

RuneTable is a table of runes.

func (*RuneTable) AlignRightEdge

func (rt *RuneTable) AlignRightEdge() (int, bool)

AlignRightEdge aligns the right edge of the table.

Returns:

  • int: The right most edge.
  • bool: True if the receiver is not nil, false otherwise.

func (*RuneTable) AppendBottomRow

func (rt *RuneTable) AppendBottomRow(row []rune) bool

AppendBottomRow appends a row to the bottom of the table.

Parameters:

  • row: The row to append.

Returns:

  • bool: True if the receiver is not nil, false otherwise.

func (RuneTable) Byte added in v0.1.5

func (rt RuneTable) Byte() []byte

Byte returns the byte representation of the table.

Returns:

  • []byte: The byte representation of the table.

func (*RuneTable) FromBytes added in v0.1.5

func (rt *RuneTable) FromBytes(lines [][]byte) error

FromBytes initializes the RuneTable from a slice of slice of bytes.

Parameters:

  • lines: The slice of slice of bytes.

Returns:

  • error: An error if the table could not be initialized.

Errors:

  • *ints.ErrAt if a line is not proper UTF-8 encoding.
  • *errors.NilReceiver if the receiver is nil.

func (*RuneTable) FromRunes added in v0.1.5

func (rt *RuneTable) FromRunes(lines [][]rune) error

FromRunes initializes the RuneTable from a slice of slice of runes.

Parameters:

  • lines: The slice of slice of runes.

Returns:

  • error: An error of type *errors.NilReceiver if the receiver is nil.

func (*RuneTable) FromStrings added in v0.1.5

func (rt *RuneTable) FromStrings(lines []string) error

FromStrings initializes the RuneTable from a slice of strings.

Parameters:

  • lines: The slice of strings.

Returns:

  • error: An error if the table could not be initialized.

Errors:

  • *errors.ErrAt if a string is not properly UTF-8 encoded.
  • *errors.NilReceiver if the receiver is nil.

func (*RuneTable) PrefixEachRow

func (rt *RuneTable) PrefixEachRow(prefix []rune) bool

PrefixEachRow prefixes each row with the given prefix.

Parameters:

  • prefix: The prefix to add to each row.

Returns:

  • bool: True if the receiver is not nil, false otherwise.

func (*RuneTable) PrependTopRow

func (rt *RuneTable) PrependTopRow(row []rune) bool

PrependTopRow prepends a row to the top of the table.

Parameters:

  • row: The row to prepend.

Returns:

  • bool: True if the receiver is not nil, false otherwise.

func (RuneTable) RightMostEdge

func (rt RuneTable) RightMostEdge() int

RightMostEdge gets the right most edge of the content.

Parameters:

  • content: The content.

Returns:

  • int: The right most edge.

func (RuneTable) Rune added in v0.1.5

func (rt RuneTable) Rune() []rune

Rune returns the rune representation of the table.

Returns:

  • []rune: The rune representation of the table.

func (RuneTable) String

func (rt RuneTable) String() string

String implements the fmt.Stringer interface.

func (*RuneTable) SuffixEachRow

func (rt *RuneTable) SuffixEachRow(suffix []rune) bool

SuffixEachRow suffixes each row with the given suffix.

Parameters:

  • suffix: The suffix to add to each row.

Returns:

  • bool: True if the receiver is not nil, false otherwise.

Jump to

Keyboard shortcuts

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