CString

package
v0.4.9 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultIndentation is the default indentation string.
	DefaultIndentation string = "   "

	// DefaultSeparator is the default separator string.
	DefaultSeparator string = ", "
)
View Source
const (
	// ConfInd_Idx is the index for the indentation configuration.
	ConfInd_Idx = iota

	// ConfDelL_Idx is the index for the left delimiter configuration.
	ConfDelL_Idx

	// ConfDelR_Idx is the index for the right delimiter configuration.
	ConfDelR_Idx

	// ConfSep_Idx is the index for the separator configuration.
	ConfSep_Idx

	// ConfStyle_Idx is the index for the style configuration.
	ConfStyle_Idx
)
View Source
const (
	// NBSP is the non-breaking space rune.
	NBSP rune = '\u00A0'
)

Variables

This section is empty.

Functions

func Apply

func Apply[T CStringer](p *Printer, elem T) error

Apply applies a format to a stringer.

Parameters:

  • p: The printer to use.
  • elem: The element to format.

Returns:

  • error: An error if the formatting fails.

Errors:

  • *ErrInvalidParameter: If the printer is nil.
  • *ErrFinalization: If the finalization of the page fails.
  • any error returned by the element's CString method.

Behaviors:

  • If the formatter is nil, the function uses the nil formatter.
  • If the element is nil, the function does nothing.

func ApplyForm

func ApplyForm[T CStringer](form FormatConfig, trav *Traversor, elem T) error

ApplyForm is a function that applies the format to an element.

Parameters:

  • form: The formatter to use for formatting.
  • trav: The traversor to use for formatting.
  • elem: The element to format.

Returns:

  • error: An error if the formatting fails.

Behaviors:

  • If the traversor is nil, the function does nothing.

func ApplyFormMany

func ApplyFormMany[T CStringer](form FormatConfig, trav *Traversor, elems []T) error

ApplyFormMany is a function that applies the format to multiple elements at once.

Parameters:

  • form: The formatter to use for formatting.
  • trav: The traversor to use for formatting.
  • elems: The elements to format.

Returns:

  • error: An error if type Errors.ErrAt if the formatting fails on a specific element.

Behaviors:

  • If the traversor is nil, the function does nothing.

func ApplyFunc

func ApplyFunc[T any](p *Printer, elem T, f CStringFunc[T]) error

ApplyFunc applies a format function to the printer.

Parameters:

  • p: The printer to use.
  • elem: The element to apply the function to.
  • f: The function to apply.

Returns:

  • error: An error if the function fails.

Errors:

  • *ErrInvalidParameter: If the printer is nil.
  • any error returned by the function.

func ApplyFuncMany

func ApplyFuncMany[T any](p *Printer, f CStringFunc[T], elems []T) error

ApplyFuncMany applies a format function to the printer.

Parameters:

  • p: The printer to use.
  • f: The function to apply.
  • elems: The elements to apply the function to.

Returns:

  • error: An error if the function fails.

Errors:

  • *ErrInvalidParameter: If the printer is nil.
  • *Errors.ErrAt: If an error occurs on a specific element.
  • any error returned by the function.

func ApplyMany

func ApplyMany[T CStringer](p *Printer, elems []T) error

ApplyMany applies a format to a stringer.

Parameters:

  • p: The printer to use.
  • elems: The elements to format.

Returns:

  • error: An error if the formatting fails.

Errors:

  • *ErrInvalidParameter: If the printer is nil.
  • *ErrFinalization: If the finalization of the page fails.
  • *Errors.ErrAt: If an error occurs on a specific element.

Behaviors:

  • If the formatter is nil, the function uses the nil formatter.
  • If an element is nil, the function skips the element.
  • If all elements are nil, the function does nothing.

func ApplyTravFunc

func ApplyTravFunc[T any](trav *Traversor, elem T, f CStringFunc[T]) error

ApplyTravFunc applies a function to the printer. Useful for when you want to apply a function that does not implement the CStringer interface.

Parameters:

  • trav: The traversor to use.
  • elem: The element to apply the function to.
  • f: The function to apply.

Returns:

  • error: An error if the function fails.

Errors:

  • *ErrFinalization: If the finalization of the page fails.
  • any error returned by the function.

func ApplyTravFuncMany

func ApplyTravFuncMany[T any](trav *Traversor, f CStringFunc[T], elems []T) error

ApplyTravFuncMany applies a function to the printer. Useful for when you want to apply a function that does not implement the CStringer interface.

Parameters:

  • trav: The traversor to use.
  • f: The function to apply.
  • elems: The elements to apply the function to.

Returns:

  • error: An error if the function fails.

Errors:

  • *ErrFinalization: If the finalization of the page fails.
  • *Errors.ErrAt: If an error occurs on a specific element.
  • any error returned by the function.

func Fprint

func Fprint(form FormatConfig, style tcell.Style, a ...interface{}) ([][][][][]*Unit, error)

Fprint prints a formatted string.

Parameters:

  • form: The formatter to use.
  • a: The elements to print.

Returns:

  • [][][][]string: The pages of the formatted strings.
  • error: An error if the printing fails.

func Fprintf

func Fprintf(form FormatConfig, style tcell.Style, format string, a ...interface{}) ([][][][][]*Unit, error)

Fprintf prints a formatted string.

Parameters:

  • form: The formatter to use.
  • format: The format string.
  • a: The elements to print.

Returns:

  • [][][][]string: The pages of the formatted strings.
  • error: An error if the printing fails.

func Fprintln

func Fprintln(form FormatConfig, style tcell.Style, a ...interface{}) ([][][][][]*Unit, error)

Fprintln prints a formatted string with a newline.

Parameters:

  • form: The formatter to use.
  • a: The elements to print.

Returns:

  • [][][][]string: The pages of the formatted strings.
  • error: An error if the printing fails.

func Print

func Print(form FormatConfig, style tcell.Style, strs ...string) ([][][][][]*Unit, error)

Print prints a string.

Parameters:

  • form: The formatter to use.
  • strs: The strings to print.

Returns:

  • [][][][]string: The pages of the formatted strings.
  • error: An error if the printing fails.

func Printc

func Printc(form FormatConfig, style tcell.Style, c rune) ([][][][][]*Unit, error)

Printc prints a character.

Parameters:

  • form: The formatter to use.
  • c: The character to print.

Returns:

  • [][][][]string: The pages of the formatted character.
  • error: An error if the printing fails.

func Printj

func Printj(form FormatConfig, style tcell.Style, sep string, strs ...string) ([][][][][]*Unit, error)

Printj prints a joined string.

Parameters:

  • form: The formatter to use.
  • sep: The separator to use.
  • strs: The strings to join.

Returns:

  • [][][][]string: The pages of the formatted strings.
  • error: An error if the printing fails.

func Printjln

func Printjln(form FormatConfig, style tcell.Style, sep string, lines ...string) ([][][][][]*Unit, error)

Printjln prints a joined string with a newline.

Parameters:

  • form: The formatter to use.
  • sep: The separator to use.
  • lines: The lines to join.

Returns:

  • [][][][]string: The pages of the formatted strings.
  • error: An error if the printing fails.

func Println

func Println(form FormatConfig, style tcell.Style, lines ...string) ([][][][][]*Unit, error)

Println prints a string with a newline.

Parameters:

  • form: The formatter to use.
  • lines: The lines to print.

Returns:

  • [][][][]string: The pages of the formatted strings.
  • error: An error if the printing fails.

Types

type CStringFunc

type CStringFunc[T any] func(trav *Traversor, elem T) error

CStringFunc is a function that generates a formatted string representation of an object.

Parameters:

  • trav: The traversor to use for printing.
  • elem: The element to print.

Returns:

  • error: An error if there was a problem generating the string.

type CStringer

type CStringer interface {
	// CString returns a string representation of the object.
	//
	// Parameters:
	//   - trav: The traversor to use for printing.
	//
	// Returns:
	//   - error: An error if there was a problem generating the string.
	CString(trav *Traversor) error
}

CStringer is an interface that defines the behavior of a type that can be converted to a string representation.

type ConfigOption

type ConfigOption func(FormatConfig)

ConfigOption is a type that represents a configuration option for a formatter.

func WithDecreasedIndent

func WithDecreasedIndent() ConfigOption

WithDecreasedIndent is a function that decreases the indentation level of the formatter by one.

Returns:

  • ConfigOption: The configuration option.

Behaviors:

  • If the indentation level is already 0, it is not decreased.

func WithIncreasedIndent

func WithIncreasedIndent() ConfigOption

WithIncreasedIndent is a function that increases the indentation level of the formatter by one.

Returns:

  • ConfigOption: The configuration option.

func WithModifiedIndent

func WithModifiedIndent(by int) ConfigOption

WithModifiedIndent is a function that modifies the indentation level of the formatter by a specified amount relative to the current indentation level.

Parameters:

  • by: The amount by which to modify the indentation level.

Returns:

  • ConfigOption: The configuration option.

Behaviors:

  • Negative values will decrease the indentation level while positive values will increase it. If the value is 0, then nothing is done and when the indentation level is 0, it is not decreased.

type DelimiterConfig

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

DelimiterConfig is a type that represents the configuration for delimiters.

func NewDelimiterConfig

func NewDelimiterConfig(str string, isInline, left bool) *DelimiterConfig

NewDelimiterConfig is a function that creates a new delimiter configuration.

Parameters:

  • value: The string that is used as a delimiter.
  • inline: Whether the delimiter should be inline.

Returns:

  • *DelimiterConfig: A pointer to the new delimiter configuration.

Default values:

  • ==DelimiterConfig==
  • Value: ""
  • Inline: true

func (*DelimiterConfig) Copy

func (c *DelimiterConfig) Copy() *DelimiterConfig

Copy is a method of uc.Copier interface.

Returns:

  • *DelimiterConfig: A copy of the delimiter configuration.

type FormatConfig

type FormatConfig [5]any

FormatConfig is a type that represents a configuration for formatting. [Indentation] [Left Delimiter] [Right Delimiter] [Separator]

var (
	// ArrayLikeFormat is the default options for an array-like object.
	// [1, 2, 3]
	ArrayLikeFormat FormatConfig = NewFormatter(
		NewDelimiterConfig("[", false, true),
		NewDelimiterConfig("]", false, false),
		NewSeparator(DefaultSeparator, false),
	)
)

func DefaultFormatter

func DefaultFormatter(style tcell.Style) FormatConfig

DefaultFormatter is the default formatter.

Parameters:

  • style: The style of the formatter.

==IndentConfig==

  • DefaultIndentationConfig

==SeparatorConfig==

  • DefaultSeparatorConfig

==DelimiterConfig (Left and Right)==

  • Nil (no delimiters are used by default)

func MergeForm

func MergeForm(form1, form2 FormatConfig) FormatConfig

MergeForm is a function that merges the given formatter with the current one; prioritizing the values of the first formatter.

Parameters:

  • form1: The first formatter.
  • form2: The second formatter.

Returns:

  • FormatConfig: A pointer to the new formatter.

func NewFormatter

func NewFormatter(options ...any) (form FormatConfig)

NewFormatter is a function that creates a new formatter with the given configuration.

Parameters:

  • options: The configuration for the formatter.

Returns:

  • form: A pointer to the new formatter.

Behaviors:

  • The function panics if an invalid configuration type is given. (i.e., not IndentConfig, DelimiterConfig, or SeparatorConfig)

type IndentConfig

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

IndentConfig is a type that represents the configuration for indentation.

func DefaultIndentationConfig

func DefaultIndentationConfig(style tcell.Style) *IndentConfig

DefaultIndentationConfig is the default indentation configuration.

Parameters:

  • style: The style of the indentation.

==IndentConfig==

  • DefaultIndentation
  • 0

Returns:

  • *IndentConfig: A pointer to the default indentation configuration.

func NewIndentConfig

func NewIndentConfig(units []*Unit, initialLevel int) *IndentConfig

NewIndentConfig is a function that creates a new indentation configuration.

Parameters:

  • indentation: The string that is used for indentation.
  • initialLevel: The initial indentation level.

Returns:

  • *IndentConfig: A pointer to the new indentation configuration.

Default values:

	==IndentConfig==
  - Indentation: DefaultIndentation
  - InitialLevel: 0

Behaviors:

  • If initialLevel is negative, it is set to 0.

func (*IndentConfig) Copy

func (c *IndentConfig) Copy() *IndentConfig

Copy is a method of uc.Copier interface.

Returns:

  • *IndentConfig: A copy of the indentation configuration.

func (*IndentConfig) GetIndentStr

func (c *IndentConfig) GetIndentStr() []*Unit

GetIndentStr is a method that returns the indentation string.

Returns:

  • string: The indentation string.

func (*IndentConfig) GetIndentation

func (c *IndentConfig) GetIndentation() []*Unit

GetIndentation is a method that returns the applied indentation.

Returns:

  • string: The applied indentation.

type Printer

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

Printer is a type that represents a formatted string.

func NewPrinter

func NewPrinter(form FormatConfig) *Printer

NewPrinter creates a new printer.

Parameters:

  • form: The formatter to use.

Returns:

  • *Printer: The new printer.

Behaviors:

  • If the formatter is nil, the function uses the formatter with nil values.

func NewPrinterFromConfig

func NewPrinterFromConfig(opts ...any) *Printer

NewPrinterFromConfig creates a new printer from a configuration.

Parameters:

  • opts: The configuration to use.

Returns:

  • *Printer: The new printer.

Behaviors:

  • If the configuration is nil, the function uses the default configuration.
  • Panics if an invalid configuration type is given (i.e., not IndentConfig, DelimiterConfig, or SeparatorConfig).

func (*Printer) Cleanup

func (p *Printer) Cleanup()

Cleanup implements the Cleaner interface.

func (*Printer) GetPages

func (p *Printer) GetPages() [][][][][]*Unit

GetPages returns the pages of the printer.

Returns:

  • [][][][]string: The pages of the printer.

func (*Printer) GetTraversor

func (p *Printer) GetTraversor() *Traversor

GetTraversor returns a traversor for the printer.

Returns:

  • *Traversor: The traversor for the printer.

type SeparatorConfig

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

SeparatorConfig is a type that represents the configuration for separators.

var (
	// DefaultSeparatorConfig is the default separator configuration.
	DefaultSeparatorConfig *SeparatorConfig = NewSeparator(DefaultSeparator, false)
)

func NewSeparator

func NewSeparator(sep string, includeFinal bool) *SeparatorConfig

NewSeparator is a function that creates a new separator configuration.

Parameters:

  • separator: The string that is used as a separator.
  • hasFinalSeparator: Whether the last element should have a separator.

Returns:

  • *SeparatorConfig: A pointer to the new separator configuration.

Default values:

	==SeparatorConfig==
  - Separator: DefaultSeparator
  - HasFinalSeparator: false

func (*SeparatorConfig) Copy

func (c *SeparatorConfig) Copy() *SeparatorConfig

Copy is a method of uc.Copier interface.

Returns:

  • *SeparatorConfig: A copy of the separator configuration.

type StyleConfig

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

StyleConfig is a type that represents the configuration for styles.

func NewStyleConfig

func NewStyleConfig(defaultStyle tcell.Style) *StyleConfig

NewStyleConfig is a function that creates a new style configuration.

Parameters:

  • defaultStyle: The default style to use.

Returns:

  • *StyleConfig: A pointer to the new style configuration.

func (*StyleConfig) Copy

func (c *StyleConfig) Copy() *StyleConfig

Copy is a method of uc.Copier interface.

Returns:

  • *StyleConfig: A copy of the style configuration.

type Traversor

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

Traversor is a type that represents a traversor for a formatted string.

func (*Traversor) AcceptLine

func (trav *Traversor) AcceptLine()

AcceptLine is a function that accepts the current line of the traversor.

Behaviors:

  • This also accepts the current word if any.

func (*Traversor) AcceptWord

func (trav *Traversor) AcceptWord()

AcceptWord is a function that, if there is any in-progress word, then said word is added to the source.

func (*Traversor) AddJoinedLine

func (trav *Traversor) AddJoinedLine(style tcell.Style, sep string, fields ...string) error

AddJoinedLine adds a joined line to the traversor. This is a more efficient way to do the same as AddLine(strings.Join(fields, sep)).

Parameters:

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

Returns:

  • error: An error of type *Errors.ErrInvalidRuneAt if there is an invalid rune in the line.

Behaviors:

  • If fields is empty, then nothing is done.

func (*Traversor) AddLine

func (trav *Traversor) AddLine(line string, style tcell.Style) error

AddLine adds a line to the traversor. If there is any in-progress line, then the line is appended to the line before accepting it. Otherwise, a new line with the line is added to the source.

Parameters:

  • line: The line to add.

Returns:

  • error: An error of type *Errors.ErrAt if there is an error adding the line.

Behaviors:

  • If line is empty, then an empty line is added to the source.

func (*Traversor) AddLines

func (trav *Traversor) AddLines(lines []string, style tcell.Style) error

AddLines adds multiple lines to the traversor in a more efficient way than adding each line individually.

Parameters:

  • lines: The lines to add.

Returns:

  • error: An error of type *Errors.ErrAt if there is an error adding a line.

Behaviors:

  • If there are no lines, then nothing is done.

func (*Traversor) AppendJoinedString

func (trav *Traversor) AppendJoinedString(style tcell.Style, sep string, fields ...string) error

AppendJoinedString appends a joined string to the half-line of the traversor.

Parameters:

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

Returns:

  • error: An error of type *Errors.ErrInvalidRuneAt if there is an invalid rune in the string.

Behaviors:

  • This is equivalent to calling AppendString(strings.Join(fields, sep)).

func (*Traversor) AppendRune

func (trav *Traversor) AppendRune(r rune, style tcell.Style) error

AppendRune appends a rune to the half-line of the traversor.

Parameters:

  • r: The rune to append.

Returns:

  • error: An error if the rune could not be appended.

Errors:

  • *Errors.ErrInvalidRune: If the rune is invalid.

Behaviors:

  • If the half-line is nil, then a new half-line is created.

func (*Traversor) AppendString

func (trav *Traversor) AppendString(str string, style tcell.Style) error

AppendString appends a string to the half-line of the traversor.

Parameters:

  • str: The string to append.

Returns:

  • error: An error of type *Errors.ErrInvalidRuneAt if there is an invalid rune in the string.

Behaviors:

  • IF str is empty: nothing is done.

func (*Traversor) AppendStrings

func (trav *Traversor) AppendStrings(strs []string, style tcell.Style) error

AppendStrings appends multiple strings to the half-line of the traversor.

Parameters:

  • strs: The strings to append.

Returns:

  • error: An error of type *Errors.ErrAt if there is an error appending a string.

Behaviors:

  • This is equivalent to calling AppendString for each string in strs but more efficient.

func (*Traversor) Cleanup

func (trav *Traversor) Cleanup()

Cleanup implements the Cleaner interface.

func (*Traversor) EmptyLine

func (trav *Traversor) EmptyLine()

EmptyLine adds an empty line to the traversor. This is a more efficient way to do the same as AddLine("") or AddLines([]string{""}).

Behaviors:

  • If the half-line is not empty, then the half-line is added to the source (half-line is reset) and an empty line is added to the source.

func (*Traversor) GetConfig

func (trav *Traversor) GetConfig(options ...ConfigOption) FormatConfig

GetConfig is a method that returns a copy of the configuration of the traversor.

Parameters:

  • options: The options to apply to the configuration.

Returns:

  • FormatConfig: A copy of the configuration of the traversor.

func (*Traversor) Print

func (trav *Traversor) Print(a ...interface{}) error

Print is a function that writes to the traversor using the fmt.Fprint function.

Parameters:

  • a: The arguments to write.

func (*Traversor) Printf

func (trav *Traversor) Printf(format string, a ...interface{}) error

Printf is a function that writes to the traversor using the fmt.Fprintf function.

Parameters:

  • format: The format string.
  • a: The arguments to write.

func (*Traversor) Println

func (trav *Traversor) Println(a ...interface{}) error

Println is a function that writes to the traversor using the fmt.Fprintln function.

Parameters:

  • a: The arguments to write.

func (*Traversor) Write

func (trav *Traversor) Write(p []byte) (int, error)

Write implements the io.Writer interface for the traversor.

type Unit

type Unit struct {
	// Content is the content of the unit.
	Content string

	// Style is the style of the unit.
	Style tcell.Style
}

Unit is a unit of content that can be displayed.

func NewUnit

func NewUnit(content string, style tcell.Style) *Unit

NewUnit is a function that creates a new unit.

Parameters:

  • content: The content of the new unit.
  • style: The style of the new unit.

Returns:

  • *Unit: The new unit.

func ReduceUnitSequence

func ReduceUnitSequence(units []*Unit) []*Unit

func (*Unit) Copy

func (u *Unit) Copy() *Unit

Copy is a method that creates a copy of the unit.

Returns:

  • *Unit: The copy of the unit.

func (*Unit) Merge

func (u *Unit) Merge(other *Unit) bool

Merge is a method that merges the content of the unit with another unit if the styles are the same.

Parameters:

  • other: The other unit to merge with.

Returns:

  • bool: True if the merge was successful, false otherwise.

Jump to

Keyboard shortcuts

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