Documentation ¶
Index ¶
- Constants
- Variables
- func Apply[T FStringer](p *Printer, elem T) error
- func ApplyForm[T FStringer](form FormatConfig, trav *Traversor, elem T) error
- func ApplyFormMany[T FStringer](form FormatConfig, trav *Traversor, elems []T) error
- func ApplyFunc[T any](p *Printer, elem T, f FStringFunc[T]) error
- func ApplyFuncMany[T any](p *Printer, f FStringFunc[T], elems []T) error
- func ApplyMany[T FStringer](p *Printer, elems []T) error
- func ApplyTravFunc[T any](trav *Traversor, elem T, f FStringFunc[T]) error
- func ApplyTravFuncMany[T any](trav *Traversor, f FStringFunc[T], elems []T) error
- func FStringArray(format FormatConfig, values []string) (string, error)
- func Fprint(form FormatConfig, a ...interface{}) ([][][][]string, error)
- func Fprintf(form FormatConfig, format string, a ...interface{}) ([][][][]string, error)
- func Fprintln(form FormatConfig, a ...interface{}) ([][][][]string, error)
- func Print(form FormatConfig, strs ...string) ([][][][]string, error)
- func Printc(form FormatConfig, c rune) ([][][][]string, error)
- func Printj(form FormatConfig, sep string, strs ...string) ([][][][]string, error)
- func Printjln(form FormatConfig, sep string, lines ...string) ([][][][]string, error)
- func Println(form FormatConfig, lines ...string) ([][][][]string, error)
- func Stringfy(doc [][][][]string) []string
- type ConfigOption
- type Configer
- type DelimiterConfig
- type FStringFunc
- type FStringer
- type FormatConfig
- type IndentConfig
- type Printer
- type SeparatorConfig
- type SimplePrinter
- type Traversor
- func (trav *Traversor) AcceptLine()
- func (trav *Traversor) AcceptWord()
- func (trav *Traversor) AddJoinedLine(sep string, fields ...string) error
- func (trav *Traversor) AddLine(line string) error
- func (trav *Traversor) AddLines(lines []string) error
- func (trav *Traversor) AppendJoinedString(sep string, fields ...string) error
- func (trav *Traversor) AppendRune(r rune) error
- func (trav *Traversor) AppendString(str string) error
- func (trav *Traversor) AppendStrings(strs []string) error
- func (trav *Traversor) Cleanup()
- func (trav *Traversor) EmptyLine()
- func (trav *Traversor) GetConfig(options ...ConfigOption) FormatConfig
- func (trav *Traversor) Print(a ...interface{}) error
- func (trav *Traversor) Printf(format string, a ...interface{}) error
- func (trav *Traversor) Println(a ...interface{}) error
- func (trav *Traversor) Write(p []byte) (int, error)
Constants ¶
const ( // DefaultIndentation is the default indentation string. DefaultIndentation string = " " // DefaultSeparator is the default separator string. DefaultSeparator string = ", " )
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 )
const ( // NBSP is the non-breaking space rune. NBSP rune = '\u00A0' )
Variables ¶
var ( // DefaultIndentationConfig is the default indentation configuration. DefaultIndentationConfig *IndentConfig = NewIndentConfig(DefaultIndentation, 0) // DefaultSeparatorConfig is the default separator configuration. DefaultSeparatorConfig *SeparatorConfig = NewSeparator(DefaultSeparator, false) )
Functions ¶
func Apply ¶ added in v0.3.6
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 FString method.
Behaviors:
- If the formatter is nil, the function uses the nil formatter.
- If the element is nil, the function does nothing.
func ApplyForm ¶ added in v0.3.6
func ApplyForm[T FStringer](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 ¶ added in v0.3.6
func ApplyFormMany[T FStringer](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 ¶ added in v0.3.6
func ApplyFunc[T any](p *Printer, elem T, f FStringFunc[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 ¶ added in v0.3.6
func ApplyFuncMany[T any](p *Printer, f FStringFunc[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 ¶ added in v0.3.6
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 ¶ added in v0.3.6
func ApplyTravFunc[T any](trav *Traversor, elem T, f FStringFunc[T]) error
ApplyTravFunc applies a function to the printer. Useful for when you want to apply a function that does not implement the FStringer 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 ¶ added in v0.3.6
func ApplyTravFuncMany[T any](trav *Traversor, f FStringFunc[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 FStringer 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 FStringArray ¶ added in v0.3.8
func FStringArray(format FormatConfig, values []string) (string, error)
FStringArray generates a formatted string representation of an array-like object.
Parameters:
- format: The format to use for printing.
- values: The values to print.
Returns:
- string: The formatted string.
- error: An error if the printing fails.
func Fprint ¶ added in v0.3.6
func Fprint(form FormatConfig, a ...interface{}) ([][][][]string, 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 ¶ added in v0.3.6
func Fprintf(form FormatConfig, format string, a ...interface{}) ([][][][]string, 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 ¶ added in v0.3.6
func Fprintln(form FormatConfig, a ...interface{}) ([][][][]string, 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 ¶ added in v0.3.6
func Print(form FormatConfig, strs ...string) ([][][][]string, 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 ¶ added in v0.3.6
func Printc(form FormatConfig, c rune) ([][][][]string, 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 ¶ added in v0.3.6
func Printj(form FormatConfig, sep string, strs ...string) ([][][][]string, 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 ¶ added in v0.3.6
func Printjln(form FormatConfig, sep string, lines ...string) ([][][][]string, 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 ¶ added in v0.3.6
func Println(form FormatConfig, lines ...string) ([][][][]string, 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 ConfigOption ¶ added in v0.3.6
type ConfigOption func(FormatConfig)
ConfigOption is a type that represents a configuration option for a formatter.
func WithDecreasedIndent ¶ added in v0.3.6
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 ¶ added in v0.3.6
func WithIncreasedIndent() ConfigOption
WithIncreasedIndent is a function that increases the indentation level of the formatter by one.
Returns:
- ConfigOption: The configuration option.
func WithModifiedIndent ¶ added in v0.3.6
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 Configer ¶ added in v0.3.6
Configer is an interface that defines the behavior of a type that can be copied.
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 ¶ added in v0.3.6
func (c *DelimiterConfig) Copy() uc.Copier
Copy is a method of uc.Copier interface.
Returns:
- uc.Copier: A copy of the delimiter configuration.
type FStringFunc ¶ added in v0.3.6
FStringFunc 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 FStringer ¶
type FStringer interface { // FString 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. FString(trav *Traversor) error }
FStringer is an interface that defines the behavior of a type that can be converted to a string representation.
type FormatConfig ¶ added in v0.3.6
type FormatConfig [4]Configer
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), ) )
var ( // DefaultFormatter is the default formatter. // // ==IndentConfig== // - DefaultIndentationConfig // // ==SeparatorConfig== // - DefaultSeparatorConfig // // ==DelimiterConfig (Left and Right)== // - Nil (no delimiters are used by default) DefaultFormatter FormatConfig = NewFormatter( DefaultIndentationConfig, DefaultSeparatorConfig, ) )
func MergeForm ¶ added in v0.3.6
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 ¶ added in v0.3.6
func NewFormatter(options ...Configer) (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 NewIndentConfig ¶
func NewIndentConfig(str string, 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 ¶ added in v0.3.6
func (c *IndentConfig) Copy() uc.Copier
Copy is a method of uc.Copier interface.
Returns:
- uc.Copier: A copy of the indentation configuration.
func (*IndentConfig) GetIndentStr ¶ added in v0.3.6
func (c *IndentConfig) GetIndentStr() string
GetIndentStr is a method that returns the indentation string.
Returns:
- string: The indentation string.
func (*IndentConfig) GetIndentation ¶ added in v0.3.6
func (c *IndentConfig) GetIndentation() string
GetIndentation is a method that returns the applied indentation.
Returns:
- string: The applied indentation.
type Printer ¶ added in v0.3.6
type Printer struct {
// contains filtered or unexported fields
}
Printer is a type that represents a formatted string.
func NewPrinter ¶ added in v0.3.6
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 ¶ added in v0.3.6
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 ¶ added in v0.3.6
func (p *Printer) Cleanup()
Cleanup implements the Cleaner interface.
func (*Printer) GetPages ¶ added in v0.3.6
GetPages returns the pages of the printer.
Returns:
- [][][][]string: The pages of the printer.
func (*Printer) GetTraversor ¶ added in v0.3.6
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.
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 ¶ added in v0.3.6
func (c *SeparatorConfig) Copy() uc.Copier
Copy is a method of uc.Copier interface.
Returns:
- uc.Copier: A copy 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) AcceptLine ¶ added in v0.3.6
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 ¶ added in v0.3.6
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 ¶ added in v0.3.5
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
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 ¶ added in v0.2.41
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 ¶ added in v0.3.5
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
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 ¶ added in v0.2.41
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
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 ¶ added in v0.3.6
func (trav *Traversor) Cleanup()
Cleanup implements the Cleaner interface.
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) GetConfig ¶ added in v0.3.6
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 ¶ added in v0.3.6
Print is a function that writes to the traversor using the fmt.Fprint function.
Parameters:
- a: The arguments to write.
func (*Traversor) Printf ¶ added in v0.3.6
Printf is a function that writes to the traversor using the fmt.Fprintf function.
Parameters:
- format: The format string.
- a: The arguments to write.