FString

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: May 24, 2024 License: MIT Imports: 4 Imported by: 2

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 (
	// Hellip is the ellipsis character.
	Hellip string = "..."

	// HellipLen is the length of the ellipsis character.
	HellipLen int = len(Hellip)

	// MarginLeft is the left margin of the content box.
	MarginLeft int = 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

Builder is a type that represents a builder for creating formatted strings.

func (*Builder) Build

func (b *Builder) Build() *Formatter

Build is a method of the Builder type that creates a formatter with the configuration of the builder.

Returns:

  • *Formatter: A pointer to the newly created formatter.

Information:

  • Options that are not specified will be set to their default values:
  • ==IndentConfig==
  • Nil (no indentation is used by default)
  • ==SeparatorConfig==
  • Separator: DefaultSeparator
  • HasFinalSeparator: false
  • ==DelimiterConfig (Left and Right)==
  • Nil (no delimiters are used by default)

func (*Builder) SetDelimiterLeft added in v0.2.37

func (b *Builder) SetDelimiterLeft(delimiter *DelimiterConfig)

SetDelimiterLeft is a function that sets the left delimiter configuration of the builder.

Parameters:

  • delimiter: The left delimiter configuration.

func (*Builder) SetDelimiterRight added in v0.2.37

func (b *Builder) SetDelimiterRight(delimiter *DelimiterConfig)

SetDelimiterRight is a function that sets the right delimiter configuration of the builder.

Parameters:

  • delimiter: The right delimiter configuration.

func (*Builder) SetIndentation added in v0.2.37

func (b *Builder) SetIndentation(config *IndentConfig)

SetIndentation is a function that sets the indentation configuration of the builder.

Ignore this function if you don't want to indent at all. If you want the rules of vertical indentation to be applied, without any indentation, use "" as the indentation string.

Parameters:

  • config: The indentation configuration.

func (*Builder) SetSeparator added in v0.2.37

func (b *Builder) SetSeparator(config *SeparatorConfig)

SetSeparator is a function that sets the separator configuration of the builder.

Parameters:

  • config: The separator configuration.

type DelimiterConfig

type DelimiterConfig struct {
	// Value is the string that is used as a delimiter.
	Value string

	// Inline specifies whether the delimiter should be inline.
	Inline bool
}

DelimiterConfig is a type that represents the configuration for delimiters.

func NewDelimiterConfig

func NewDelimiterConfig(value string, inline 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) String

func (c *DelimiterConfig) String() string

String is a method of fmt.Stringer interface.

Returns:

  • string: A string representation of the delimiter configuration.

type FString

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

FString is a type that represents a formatted string.

func NewFString added in v0.2.41

func NewFString() *FString

NewFString creates a new formatted string.

Returns:

  • *FString: A pointer to the newly created formatted string.

func (*FString) GetLines added in v0.2.43

func (fs *FString) GetLines() []*cb.MultiLineText

GetLines returns the lines of the formatted string.

Returns:

  • []*MultiLineText: The lines of the formatted string.

func (*FString) Traversor added in v0.2.41

func (fs *FString) Traversor(indent *IndentConfig) *Traversor

Traversor creates a traversor for the formatted string.

Parameters:

  • indent: The indentation configuration of the traversor.

Returns:

  • *Traversor: A pointer to the newly created traversor.

Behaviors:

  • If the indentation configuration is nil, the default indentation configuration is used.

type FStringer

type FStringer interface {
	// FString returns a string representation of the object.
	//
	// Parameters:
	//   - int: The current indentation level.
	//
	// Returns:
	//   - error: An error if there was a problem generating the string.
	FString(trav *Traversor) error
}

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

type Formatter added in v0.2.37

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

Formatter is a type that represents a builder for creating formatted strings.

var (
	// ArrayDefault is the default options for an array.
	// [1, 2, 3]
	ArrayDefault *Formatter = func() *Formatter {
		var builder Builder

		builder.SetDelimiterLeft(NewDelimiterConfig("[", false))
		builder.SetDelimiterRight(NewDelimiterConfig("]", false))
		builder.SetSeparator(NewSeparator(DefaultSeparator, false))

		return builder.Build()
	}()
)

func (*Formatter) Apply added in v0.2.37

func (form *Formatter) Apply(values []string) []string

Apply is a method of the Formatter type that creates a formatted string from the given values.

Parameters:

  • values: The values to format.

Returns:

  • []string: The formatted string.

func (*Formatter) ApplyString added in v0.2.37

func (form *Formatter) ApplyString(values []string) string

ApplyString is a method of the Formatter type that works like Apply but returns a single string.

Parameters:

  • values: The values to format.

Returns:

  • string: The formatted string.

type IndentConfig

type IndentConfig struct {
	// IgnoreFirst specifies whether the first line should be indented.
	IgnoreFirst bool

	// Indentation is the string that is used for indentation.
	Indentation string

	// InitialLevel is the initial indentation level.
	InitialLevel int
}

IndentConfig is a type that represents the configuration for indentation.

func NewIndentConfig

func NewIndentConfig(indentation string, initialLevel int, ignoreFirst bool) *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.
  • allowVertical: Whether vertical indentation is allowed.
  • ignoreFirst: Whether the first line should be indented.

Returns:

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

Default values:

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

func (*IndentConfig) Increase added in v0.2.41

func (config *IndentConfig) Increase(by int) *IndentConfig

Increase is a method that increases the indentation level of the configuration.

Parameters:

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

Returns:

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

Behaviors:

  • If by is negative, it is converted to a positive value.

func (*IndentConfig) String

func (c *IndentConfig) String() string

String is a method of fmt.Stringer interface.

Returns:

  • string: A string representation of the indentation configuration.

type SeparatorConfig

type SeparatorConfig struct {
	// Separator is the string that is used as a separator.
	Separator string

	// HasFinalSeparator specifies whether the last element should have a separator.
	HasFinalSeparator bool
}

SeparatorConfig is a type that represents the configuration for separators.

func NewSeparator

func NewSeparator(separator string, hasFinalSeparator 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) String

func (c *SeparatorConfig) String() string

String is a method of fmt.Stringer interface.

Returns:

  • string: A string representation of the separator configuration.

type SimplePrinter added in v0.3.5

type SimplePrinter[T comparable] struct {
	// contains filtered or unexported fields
}

SimplePrinter is a simple printer that prints a value with a name.

func NewSimplePrinter added in v0.3.5

func NewSimplePrinter[T comparable](name string, value T, fn func(T) (string, error)) *SimplePrinter[T]

NewSimplePrinter creates a new SimplePrinter with the provided name and value.

Parameters:

  • name: The name of the value.
  • value: The value to print.
  • fn: The function to use to convert the value to a string.

Returns:

  • *SimplePrinter: The new SimplePrinter.

Behaviors:

  • If the function is nil, the function uses uc.StringOf to convert the value to a string.

func (*SimplePrinter[T]) FString added in v0.3.5

func (sp *SimplePrinter[T]) FString(trav *Traversor) error

FString generates a formatted string representation of a SimplePrinter.

Format:

<name>: <value>

Parameters:

  • trav: The traversor to use for printing.

Returns:

  • error: An error if the printing fails.

type Traversor added in v0.2.41

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

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

func (*Traversor) AcceptHalfLine added in v0.3.5

func (trav *Traversor) AcceptHalfLine()

AcceptHalfLine is a function that, if there is any in-progress half-line, then said half-line is added to the source; resetting it in the process.

func (*Traversor) AddJoinedLine added in v0.3.5

func (trav *Traversor) AddJoinedLine(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 added in v0.3.5

func (trav *Traversor) AddLine(line string) error

AddLine adds a line to the traversor. If there is any in-progress half-line, then the line is appended to the half-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.ErrInvalidRuneAt if there is an invalid rune in the line.

Behaviors:

  • When line is empty, however, if there is any in-progress half-line, then the half-line is accepted as is. Otherwise, an empty line is added to the source.

func (*Traversor) AddLines added in v0.2.41

func (trav *Traversor) AddLines(lines []string) 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) AddMultiline added in v0.3.5

func (trav *Traversor) AddMultiline(mlt *cb.MultiLineText)

AddMultiline adds a multiline to the traversor. But first, it accepts any in-progress half-line.

Parameters:

  • mlt: The multiline to add.

Behaviors:

  • If the multiline is nil, then nothing is done.

func (*Traversor) AppendJoinedString added in v0.3.5

func (trav *Traversor) AppendJoinedString(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 added in v0.3.5

func (trav *Traversor) AppendRune(r rune)

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

Parameters:

  • r: The rune to append.

Behaviors:

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

func (*Traversor) AppendString added in v0.2.41

func (trav *Traversor) AppendString(str string) 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 added in v0.2.41

func (trav *Traversor) AppendStrings(strs ...string) 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:

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

func (*Traversor) ApplyIndent added in v0.3.5

func (trav *Traversor) ApplyIndent(isFirstLine bool, str string) string

ApplyIndent applies the indentation configuration to the traversor.

Parameters:

  • str: The string to apply the indentation to.

Returns:

  • string: The string with the indentation applied.

func (*Traversor) EmptyLine added in v0.2.41

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) GetIndent added in v0.2.41

func (trav *Traversor) GetIndent() string

GetIndent returns the indentation string of the traversor.

Returns:

  • string: The indentation string of the traversor.

func (*Traversor) IncreaseIndent added in v0.2.41

func (trav *Traversor) IncreaseIndent(by int) *Traversor

IncreaseIndent increases the indentation level of the traversor.

Parameters:

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

Returns:

  • *Traversor: A pointer to the new traversor.

Behaviors:

  • If by is negative, it is converted to a positive value.
  • If the traversor does not have an indentation configuration, the traversor is returned as is.

Jump to

Keyboard shortcuts

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