Documentation ¶
Index ¶
- func CalculateNumberOfLines(text []string, width int) (int, error)
- func FindContentIndexes(op_token, cl_token string, tokens []string) (result [2]int, err error)
- func FirstInstanceOfWS(chars []rune, from_idx, to_idx int) int
- func FitString(s string, width int) string
- func GenerateID(size int) (string, error)
- func LastInstanceOfWS(chars []rune, from_idx, to_idx int) int
- func ReplaceSuffix(str, suffix string) (string, bool)
- func ToUTF8Runes(s string) (runes []rune, err error)
- type BoxBorderType
- type BoxStyle
- type ErrInvalidUTF8Encoding
- type ErrLinesGreaterThanWords
- type ErrLongerSuffix
- type ErrNeverOpened
- type ErrNoCandidateFound
- type ErrNoClosestWordFound
- type ErrTokenNotFound
- type LavenshteinTable
- type RuneTable
- func (rt *RuneTable) AlignRightEdge() int
- func (rt *RuneTable) AppendBottomRow(row []rune)
- func (rt *RuneTable) GetRightMostEdge() int
- func (rt *RuneTable) PrefixEachRow(prefix []rune)
- func (rt *RuneTable) PrependTopRow(row []rune)
- func (rt *RuneTable) String() string
- func (rt *RuneTable) SuffixEachRow(suffix []rune)
- type TextSplit
- func (ts *TextSplit) Copy() uc.Copier
- func (ts *TextSplit) GetFirstLine() []string
- func (ts *TextSplit) GetFurthestRightEdge() (int, bool)
- func (ts *TextSplit) GetHeight() int
- func (ts *TextSplit) GetLines() []string
- func (ts *TextSplit) GetRunes() [][]rune
- func (ts *TextSplit) InsertWord(word string) bool
- func (ts *TextSplit) InsertWords(words []string) int
- type TokenType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalculateNumberOfLines ¶
CalculateNumberOfLines is a function that calculates the minimum number of lines needed to fit a given text within a specified width.
Parameters:
- text: The slice of strings representing the text to calculate the number of lines for.
- width: The width to fit the text within.
Returns:
- int: The calculated number of lines needed to fit the text within the width.
- error: An error if it occurs during the calculation.
Errors:
- *uc.ErrInvalidParameter: If the width is less than or equal to 0.
- *ErrLinesGreaterThanWords: If the calculated number of lines is greater than the number of words in the text.
The function calculates the total length of the text (Tl) and uses a mathematical formula to estimate the minimum number of lines needed to fit the text within the given width. The formula is explained in detail in the comments within the function.
It also returns the calculated number of lines when it errors out
func FindContentIndexes ¶
FindContentIndexes searches for the positions of opening and closing tokens in a slice of strings.
Parameters:
- op_token: The string that marks the beginning of the content.
- cl_token: The string that marks the end of the content.
- tokens: The slice of strings in which to search for the tokens.
Returns:
- result: An array of two integers representing the start and end indexes of the content.
- err: Any error that occurred while searching for the tokens.
Errors:
- *uc.ErrInvalidParameter: If the openingToken or closingToken is an empty string.
- *ErrTokenNotFound: If the opening or closing token is not found in the content.
- *ErrNeverOpened: If the closing token is found without any corresponding opening token.
Behaviors:
- The first index of the content is inclusive, while the second index is exclusive.
- This function returns a partial result when errors occur. ([-1, -1] if errors occur before finding the opening token, [index, 0] if the opening token is found but the closing token is not found.
func FirstInstanceOfWS ¶ added in v0.3.37
FirstInstanceOfWS finds the first instance of whitespace in the characters.
Parameters:
- chars: The characters.
- from_idx: The starting index. (inclusive)
- to_idx: The ending index. (exclusive)
Returns:
- int: The index of the first whitespace character. -1 if not found.
Behaviors:
- If from_idx < 0, from_idx is set to 0.
- If to_idx >= len(chars), to_idx is set to len(chars) - 1.
- If from_idx > to_idx, from_idx and to_idx are swapped.
FIXME: Remove this function once MyGoLib is updated.
func FitString ¶ added in v0.2.41
FitString fits a string to the specified width by adding spaces to the end of the string until the width is reached.
Parameters:
- width: The width to fit the string to.
Returns:
- string: The string with spaces added to the end to fit the width.
Behaviors:
- If the width is less than 0, it is set to 0.
- If the width is less than the length of the string, the string is truncated to fit the width.
- If the width is greater than the length of the string, spaces are added to the end of the string until the width is reached.
func GenerateID ¶ added in v0.2.14
GenerateID generates a random ID of the specified size (in bytes).
Parameters:
- size: The size of the ID to generate (in bytes).
Returns:
- string: The generated ID.
- error: An error if the ID cannot be generated.
Errors:
- *uc.ErrInvalidParameter: If the size is less than 1.
- Any error returned by the rand.Read function.
Behaviors:
- The function uses the crypto/rand package to generate a random ID of the specified size.
- The ID is returned as a hexadecimal string.
func LastInstanceOfWS ¶ added in v0.3.37
LastInstanceOfWS finds the last instance of whitespace in the characters.
Parameters:
- chars: The characters.
- from_idx: The starting index. (inclusive)
- to_idx: The ending index. (exclusive)
Returns:
- int: The index of the last whitespace character. -1 if not found.
Behaviors:
- If from_idx < 0, from_idx is set to 0.
- If to_idx >= len(chars), to_idx is set to len(chars) - 1.
- If from_idx > to_idx, from_idx and to_idx are swapped.
func ReplaceSuffix ¶
ReplaceSuffix replaces the end of the given string with the provided suffix.
Parameters:
- str: The original string.
- suffix: The suffix to replace the end of the string.
Returns:
- string: The resulting string after replacing the end with the suffix.
- bool: A boolean indicating if the operation was successful. (i.e., if the suffix is shorter than the string).
Behaviors:
- For quick error, use the *ErrLongerSuffix error type of this package.
Examples:
const ( str string = "hello world" suffix string = "Bob" ) result, err := ReplaceSuffix(str, suffix) if err != nil { fmt.Println(err) } else { fmt.Println(result) // Output: hello woBob }
func ToUTF8Runes ¶ added in v0.2.33
ToUTF8Runes converts a string to a slice of runes.
Parameters:
- s: The string to convert.
Returns:
- runes: The slice of runes.
- error: An error of type *common.ErrAt if an invalid UTF-8 encoding is found.
Behaviors:
- An empty string returns a nil slice with no errors.
- The function stops at the first invalid UTF-8 encoding; returning an error and the runes found up to that point.
- The function converts '\r\n' to '\n'.
Types ¶
type BoxBorderType ¶ added in v0.3.36
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 ¶ added in v0.3.36
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 ¶ added in v0.3.36
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.
func (*BoxStyle) ApplyStrings ¶ added in v0.3.36
DrawBox draws a box around the content.
Format: If the content is ["Hello", "World"], the box will be:
┏━━━━━━━┓ ┃ Hello ┃ ┃ World ┃ ┗━━━━━━━┛
Parameters:
- content: The content.
Returns:
- *RuneTable: The content in a box.
- error: An error if the content could not be processed.
Behaviors:
- If the box style is nil, the default box style will be used.
func (*BoxStyle) GetCorners ¶ added in v0.3.36
GetCorners gets the corners of the box.
Returns:
- [4]rune: The corners. [TopLeft, TopRight, BottomLeft, BottomRight]
func (*BoxStyle) GetSideBorder ¶ added in v0.3.36
GetSideBorder 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) GetTopBorder ¶ added in v0.3.36
GetTopBorder 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.2.33
type ErrInvalidUTF8Encoding struct{}
ErrInvalidUTF8Encoding is an error type for invalid UTF-8 encoding.
func NewErrInvalidUTF8Encoding ¶ added in v0.2.33
func NewErrInvalidUTF8Encoding() *ErrInvalidUTF8Encoding
NewErrInvalidUTF8Encoding creates a new ErrInvalidUTF8Encoding error.
Returns:
- *ErrInvalidUTF8Encoding: A pointer to the newly created error.
func (*ErrInvalidUTF8Encoding) Error ¶ added in v0.2.33
func (e *ErrInvalidUTF8Encoding) Error() string
Error implements the error interface.
Message: "invalid UTF-8 encoding"
type ErrLinesGreaterThanWords ¶ added in v0.2.43
type ErrLinesGreaterThanWords struct { // NumberOfLines is the number of lines in the text. NumberOfLines int // NumberOfWords is the number of words in the text. NumberOfWords int }
ErrLinesGreaterThanWords is an error type that is returned when the number of lines in a text is greater than the number of words.
func NewErrLinesGreaterThanWords ¶ added in v0.2.43
func NewErrLinesGreaterThanWords(numberOfLines, numberOfWords int) *ErrLinesGreaterThanWords
NewErrLinesGreaterThanWords is a constructor of ErrLinesGreaterThanWords.
Parameters:
- numberOfLines: The number of lines in the text.
- numberOfWords: The number of words in the text.
Returns:
- *ErrLinesGreaterThanWords: A pointer to the newly created error.
func (*ErrLinesGreaterThanWords) Error ¶ added in v0.2.43
func (e *ErrLinesGreaterThanWords) Error() string
Error implements the error interface.
Message: "number of lines ({NumberOfLines}) is greater than the number of words ({NumberOfWords})"
type ErrLongerSuffix ¶ added in v0.2.32
type ErrLongerSuffix struct { // Str is the string that is shorter than the suffix. Str string // Suffix is the Suffix that is longer than the string. Suffix string }
ErrLongerSuffix is a struct that represents an error when the suffix is longer than the string.
func NewErrLongerSuffix ¶ added in v0.2.32
func NewErrLongerSuffix(str, suffix string) *ErrLongerSuffix
NewErrLongerSuffix is a constructor of ErrLongerSuffix.
Parameters:
- str: The string that is shorter than the suffix.
- suffix: The suffix that is longer than the string.
Returns:
- *ErrLongerSuffix: A pointer to the newly created error.
func (*ErrLongerSuffix) Error ¶ added in v0.2.32
func (e *ErrLongerSuffix) Error() string
Error implements the error interface.
Message: "suffix {Suffix} is longer than the string {Str}"
type ErrNeverOpened ¶
type ErrNeverOpened struct { // OpeningToken is the opening token that was never closed. OpeningToken string // ClosingToken is the closing token that was found without a corresponding // opening token. ClosingToken string }
ErrNeverOpened is a struct that represents an error when a closing token is found without a corresponding opening token.
func NewErrNeverOpened ¶ added in v0.2.25
func NewErrNeverOpened(openingToken, closingToken string) *ErrNeverOpened
NewErrNeverOpened is a constructor of ErrNeverOpened.
Parameters:
- openingToken: The opening token that was never closed.
- closingToken: The closing token that was found without a corresponding opening token.
Returns:
- *ErrNeverOpened: A pointer to the newly created error.
func (*ErrNeverOpened) Error ¶
func (e *ErrNeverOpened) Error() string
Error implements the error interface.
Message:
- "closing token {ClosingToken} found without a corresponding opening token {OpeningToken}".
type ErrNoCandidateFound ¶ added in v0.2.43
type ErrNoCandidateFound struct{}
ErrNoCandidateFound is an error type that is returned when no candidate is found.
func NewErrNoCandidateFound ¶ added in v0.2.43
func NewErrNoCandidateFound() *ErrNoCandidateFound
NewErrNoCandidateFound is a constructor of ErrNoCandidateFound.
Returns:
- *ErrNoCandidateFound: A pointer to the newly created error.
func (*ErrNoCandidateFound) Error ¶ added in v0.2.43
func (e *ErrNoCandidateFound) Error() string
Error implements the error interface.
Message: "no candidate found"
type ErrNoClosestWordFound ¶ added in v0.3.36
type ErrNoClosestWordFound struct{}
ErrNoClosestWordFound is an error when no closest word is found.
func NewErrNoClosestWordFound ¶ added in v0.3.36
func NewErrNoClosestWordFound() *ErrNoClosestWordFound
NewErrNoClosestWordFound creates a new ErrNoClosestWordFound.
Returns:
- *ErrNoClosestWordFound: The new ErrNoClosestWordFound.
func (*ErrNoClosestWordFound) Error ¶ added in v0.3.36
func (e *ErrNoClosestWordFound) Error() string
Error implements the error interface.
Message: "no closest word was found"
type ErrTokenNotFound ¶ added in v0.2.32
type ErrTokenNotFound struct { // Token is the token that was not found in the content. Token string // Type is the type of the token (opening or closing). Type TokenType }
ErrTokenNotFound is a struct that represents an error when a token is not found in the content.
func NewErrTokenNotFound ¶ added in v0.2.32
func NewErrTokenNotFound(token string, tokenType TokenType) *ErrTokenNotFound
NewErrTokenNotFound is a constructor of ErrTokenNotFound.
Parameters:
- token: The token that was not found in the content.
- tokenType: The type of the token (opening or closing).
Returns:
- *ErrTokenNotFound: A pointer to the newly created error.
func (*ErrTokenNotFound) Error ¶ added in v0.2.32
func (e *ErrTokenNotFound) Error() string
Error implements the error interface.
Message: "{Type} token {Token} is not in the content"
type LavenshteinTable ¶ added in v0.3.36
type LavenshteinTable struct {
// contains filtered or unexported fields
}
LevenshteinTable is a table of words for the Levenshtein distance.
func NewLevenshteinTable ¶ added in v0.3.36
func NewLevenshteinTable(words ...string) (*LavenshteinTable, error)
NewLevenshteinTable creates a new Levenshtein table with the given words.
Parameters:
- words: The words to add to the table.
Returns:
- *LevenshteinTable: The new Levenshtein table.
func (*LavenshteinTable) AddWord ¶ added in v0.3.36
func (lt *LavenshteinTable) AddWord(word string) bool
AddWord adds a word to the table.
Parameters:
- word: The word to add.
Returns:
- bool: Whether the word was added successfully.
If this fails, it means that the word could not be converted to UTF-8 runes.
func (*LavenshteinTable) GetClosest ¶ added in v0.3.36
func (lt *LavenshteinTable) GetClosest(target []rune) (string, error)
GetClosest gets the closest word to a target.
Parameters:
- target: The target.
Returns:
- string: The closest word.
- error: The error if any occurs.
Errors:
- *common.ErrInvalidParameter: If the target is empty.
- *ErrNoClosestWordFound: If no closest word is found.
type RuneTable ¶ added in v0.3.36
type RuneTable struct {
// contains filtered or unexported fields
}
RuneTable is a table of runes.
func NewRuneTable ¶ added in v0.3.36
NewRuneTable creates a new RuneTable with the given lines.
Parameters:
- lines: The lines to add to the table.
Returns:
- *RuneTable: The new RuneTable.
- error: An error if any.
func (*RuneTable) AlignRightEdge ¶ added in v0.3.36
AlignRightEdge aligns the right edge of the table.
Returns:
- int: The right most edge.
func (*RuneTable) AppendBottomRow ¶ added in v0.3.36
AppendBottomRow appends a row to the bottom of the table.
Parameters:
- row: The row to append.
func (*RuneTable) GetRightMostEdge ¶ added in v0.3.36
GetRightMostEdge gets the right most edge of the content.
Parameters:
- content: The content.
Returns:
- int: The right most edge.
func (*RuneTable) PrefixEachRow ¶ added in v0.3.36
PrefixEachRow prefixes each row with the given prefix.
Parameters:
- prefix: The prefix to add to each row.
func (*RuneTable) PrependTopRow ¶ added in v0.3.36
PrependTopRow prepends a row to the top of the table.
Parameters:
- row: The row to prepend.
func (*RuneTable) SuffixEachRow ¶ added in v0.3.36
SuffixEachRow suffixes each row with the given suffix.
Parameters:
- suffix: The suffix to add to each row.
type TextSplit ¶ added in v0.2.43
type TextSplit struct {
// contains filtered or unexported fields
}
TextSplit represents a split text with a maximum width and height.
func NewTextSplit ¶ added in v0.2.43
NewTextSplit creates a new TextSplit with the given maximum width and height.
Parameters:
- maxWidth: The maximum length of a line.
- maxHeight: The maximum number of lines.
Returns:
- *TextSplit: A pointer to the newly created TextSplit.
- error: An error of type *uc.ErrInvalidParameter if the maxWidth or maxHeight is less than 0.
func SplitInEqualSizedLines ¶ added in v0.2.43
SplitInEqualSizedLines is a function that splits a given text into lines of equal width.
Errors:
- *uc.ErrInvalidParameter: If the input text is empty or the width is less than or equal to 0.
- *ErrLinesGreaterThanWords: If the number of lines needed to fit the text within the width is greater than the number of words in the text.
- *ErrNoCandidateFound: If no candidate is found during the optimization process.
Parameters:
- text: The slice of strings representing the text to split.
Returns:
- *TextSplit: A pointer to the created TextSplit instance.
- error: An error of type *ErrEmptyText if the input text is empty, or an error of type *ErrWidthTooSmall if the width is less than or equal to 0.
The function calculates the minimum number of lines needed to fit the text within the width using the CalculateNumberOfLines function. Furthermore, it uses the Sum of Squared Mean (SQM) to find the optimal solution for splitting the text into lines of equal width.
If maxHeight is not provided, the function calculates the number of lines needed to fit the text within the width using the CalculateNumberOfLines function.
func (*TextSplit) GetFirstLine ¶ added in v0.2.43
GetFirstLine is a method that returns the first line of the TextSplit.
Returns:
- []string: The first line of the TextSplit, or nil if the TextSplit is empty.
Behaviors:
- If the TextSplit is empty, the method returns nil.
func (*TextSplit) GetFurthestRightEdge ¶ added in v0.2.43
GetFurthestRightEdge is a method that returns the number of characters in the longest line of the TextSplit.
Returns:
- int: The number of characters in the longest line.
- bool: True if the TextSplit is not empty, and false otherwise.
func (*TextSplit) GetHeight ¶ added in v0.2.43
GetHeight is a method that returns the height of the TextSplit.
Returns:
- int: The height of the TextSplit.
func (*TextSplit) GetLines ¶ added in v0.2.43
GetLines is a method that returns the lines of the TextSplit.
Returns:
- []*SpltLine: The lines of the TextSplit.
func (*TextSplit) GetRunes ¶ added in v0.2.43
GetRunes is a method of TextSplit that returns the runes of the TextSplit.
Returns:
- [][]rune: A slice of runes representing the words in the TextSplit.
Behaviors:
- It is always a slice of runes with one line.
func (*TextSplit) InsertWord ¶ added in v0.2.43
InsertWord is a method that attempts to insert a given word into the TextSplit.
Parameters:
- word: The word to insert.
Returns:
- bool: True if the word was successfully inserted, and false if the word is too long to fit within the width of the TextSplit.
func (*TextSplit) InsertWords ¶ added in v0.2.43
InsertWords is a method that attempts to insert multiple words into the TextSplit.
Parameters:
- words: The words to insert.
Returns:
- int: The index of the first word that could not be inserted, or -1 if all words were inserted.