✨The Standard Library
Corgi also comes with a standard library, providing a handful of useful mixins:
Implementation Note: The version of the standard library is tied directly to the installed corgi compiler.
If the standard library gets updated, all you need to do is update your corgi compiler.
fmt
mixin Grouped
Grouped formats the stringified version of val
in groups of size
runes.
Each group is separated by sep
.
If trailRem
is set, the remainder group, i.e. the group that has less than size
elements, will be the last group. Otherwise, the first group will be that group.
If minSize
is set and val
is shorter than minSize
, Grouped will print it as is.
mixin Num
Num is used for grouping numbers in groups of 3, if they are greater or equal to 10 000, or smaller or equal to -10 000.
Num handles three special cases that Grouped doesn't.
The sign isn't counted towards any group. Where Grouped would print
- 123 456
, Num will print-123 456
.Num correctly handles decimal points:
123456.7891
will be printed as123 456.7891
and12345.67891
will be printed as12 345.67 890
.If
useWords
is set totrue
, Num will write out the number if it is in the range of 0-9. This means instead of printing7
, Num will printseven
. That range and those numbers can be changed using SetNumWords.
By default, Num will separate groups with spaces, however, you may change this using groupSep
or by setting a global default using SetDigitGroupSep.
Num does not validate if val is actually a number. Instead, it operates on val's stringified version, scanning for .
as a decimal separator. When printing, Num uses that same separator, unless decimalSep
is set, or the global default was changed by calling SetDecimalSep.
mixin SetDigitGroupSep
SetDigitGroupSep sets the global default for the digit group separator used by Num.
SetDigitGroupSep may be called at any time, only affecting the calls to Num made after updating the separator.
If SetDigitGroupSep's _
block is set, the group separator will only be changed inside the block. Further calls to SetDigitGroupSep inside the block, even if without a _
block, will also be reverted.
mixin SetDecimalSep
SetDecimalSep sets the global default for the decimal separator used by Num.
SetDecimalSep may be called at any time, only affecting the calls to Num made after updating the separator.
If SetDecimalSep's _
block is set, the decimal separator will only be changed inside the block. Further calls to SetDecimalSep inside the block, even if without a _
block, will also be reverted.
mixin SetNumWords
SetNumWords sets the global default for the spelled-out numbers used by Num, if useWords
is set to true.
SetNumWords may be called at any time, only affecting the calls to Num made after updating the words.
If SetNumWords's _
block is set, the spelled-out words will only be changed inside the block. Further calls to SetNumWords inside the block, even if without a _
block, will also be reverted.
By default, words[0]
will be assumed to be zero. This can be changed by setting the start
param.
mixin List
List formats the stringified elements of val
, which must be a slice, as a list.
If val
is not a slice, List will return from the generated function with an error.
The list will be constructed as follows:
If the slice is empty or nil, nothing will be printed.
If the slice contains a single element, only that will be printed.
If the slice contains two elements they will both be printed, separated by lastSep
.
If the slice contains more than two elements, all but the last will be printed, separated by sep
. Then, the last element will be printed, separated from the rest by lastSep
.
You can change the defaults of sep
and lastSep
by calling SetListSep.
By default sep is ,
, and lastSep is , and
.
mixin SetListSep
SetListSep updates the global default for the sep
and lastSep
values used by List. If you don't specify sep
or lastSep
, or assign them an empty value, they won't get updated.
SetListSep may be called at any time, only affecting the calls to List made after updating the separator.
If SetListSeps's _
block is set, the spelled-out words will only be changed inside the block. Further calls to SetNumWords inside the block, even if without a _
block, will also be reverted.
mixin SetLocation
SetLocation updates the default location used for Time, Date, and DateTime.
mixin Time
Time formats t
according to format
, which defaults to the global time format, changeable through SetTimeFormat.
By default, the time is printed in the global default location, which by default is time.Local
, changeable trough SetLocation.
If asIs
is true, the time won't be converted to another timezone.
mixin SetTimeFormat
SetTimeFormat sets the default global time format.
If left unchanged, it will be 15:04
.
mixin Date
Date formats t
according to format, which defaults to the global time format, changeable through SetDateFormat.
Unlike Time and DateTime, the date will be printed in the timezone that t
is already in.
This can be changed by setting asIs
to true, after which, the date is printed in the global default location, which by default is time.Local
, changeable trough SetLocation. This is usually only desired if t
is a datetime and you only want to print the date.
mixin SetDateFormat
SetDateFormat sets the default global date format.
If left unchanged, it will be 2006-01-02
.
mixin DateTime
DateTime formats t
according to format, which defaults to the global time format, changeable through SetDateTimeFormat.
By default, the datetime is printed in the global default location, which by default is time.Local
, changeable trough SetLocation.
If asIs
is true, the time won't be converted to another timezone.
mixin SetDateTimeFormat
SetDateTimeFormat sets the default global date time format.
If left unchanged, it will be 2006-01-02 15:04
.
html
mixin Breaked
Breaked replaces each newline in the stringified version of val
with a <br>
and prints it.
The optional maxAdjacent
arg allows to limit the number of adjacent <br>
s, to an arbitrary number. This means groups of more than maxAdjacent
consecutive newlines are replaced with only maxAdjacent
* <br>
.
Setting maxAdjacent
to 0
effectively removes all newlines from val
.
A negative value is the default and causes all <br>
s to be printed.
strings
mixin Trunc
Trunc truncates the stringified version of val after the at
-th rune.
If at
is negative, Trunc will truncate the at
-th rune starting from the last rune.
Examples
mixin TruncWords
TruncWords truncates the stringified version of val
after max
words.
A word is a sequence of one or more characters that is separated from another such sequence by one or more of [ \t\r\n]
.
If max
is negative, TruncWords will truncate from the end of val
instead of from the beginning.
mixin Abbrev
Abbrev truncates the stringified version of val
according to the rules of Trunc and then appends replacement
, if it truncated anything.
If at
is negative, Abbrev prepends replacement
instead of appending it.
By default, replacement
is U+2026
Horizontal Ellipsis.
mixin AbbrevWords
AbbrevWords is the TruncWords version of Abbrev.
mixin Initials
Initials prints the first letter of each word in the stringified version of val
.
A word is a sequence of one or more characters that is separated from another such sequence by one or more of [ \t\r\n]
.
If max
is set to a number higher than 0
, Initials will print at most max
initials.
If upper
is set to true
, the initials will be uppercased before printing.
Similarly, if lower
is set to true
, the initials will be lowercase.
If both are false
, the initials will be printed as is, and if both are true
, upper
takes precedence.
mixin Wrap
Wrap wraps the stringified version of val
by printing with
unescaped every after
runes.
mixin Nbsp
Nbsp replaces all spaces in the stringified version of val
with non-breaking spaces.
mixin RandText
RandText produces a string of length
runes generated from the passed charset
.
The charset
may consist of character ranges such as a-z
, or literal characters.
If charset is set to an empty or invalid string, RandText returns with an error from the generated function.
If length
is negative or zero, RandText prints nothing. The generated text is cryptographically secure, utilizing crypto/rand
.
Last updated