Documentation ¶
Index ¶
Constants ¶
const ( // CodeStart is an escape sequence for starting color coding CodeStart = "\x1b" // CodeEnd is an escape sequence fo ending color coding CodeEnd = `m` // AttrForeground is an escape sequence part for setting foreground AttrForeground = `3` // AttrBackground is an escape sequence part for setting background AttrBackground = `4` // AttrDefault is an escape sequence part for resetting foreground // or background AttrDefault = `9` // AttrReset is an esscape sequence part for resetting all attributes AttrReset = `0` // AttrReverse is an escape sequence part for setting reverse display AttrReverse = `7` // AttrNoReverse is an escape sequence part for setting reverse display off AttrNoReverse = `27` // AttrUnderline is an escape sequence part for setting underline display AttrUnderline = `4` // AttrNoUnderline is an escape sequence part for setting underline display off AttrNoUnderline = `24` // AttrBold is an escape sequence part for setting bold mode on AttrBold = `1` // AttrNoBold is an escape sequence part for setting bold mode off AttrNoBold = `22` // AttrForeground256 is an escape sequence part for setting foreground // color in 256 color mode AttrForeground256 = `38;5` // AttrBackground256 is an escape sequence part for setting background // color in 256 color mode AttrBackground256 = `48;5` // StyleReset is a placeholder for resetting all attributes. StyleReset = `{reset}` // StyleForeground is a Sprintf template for setting foreground color. StyleForeground = `{fg %d}` // StyleBackground is a Sprintf template for setting background color. StyleBackground = `{bg %d}` // StyleNoFg is a placeholder for resetting foreground color to the default // state. StyleNoFg = `{nofg}` // StyleNoBg is a placeholder for resetting background color to the default // state. StyleNoBg = `{nobg}` // StyleBold is a placeholder for setting bold display mode on. StyleBold = `{bold}` // StyleNoBold is a placeholder for setting bold display mode off. StyleNoBold = `{nobold}` // StyleReverse is a placeholder for setting reverse display mode on. StyleReverse = `{reverse}` // StyleNoReverse is a placeholder for setting reverse display mode off. StyleNoReverse = `{noreverse}` // StyleUnderline is a placeholder for setting reverse display mode on. StyleUnderline = `{underline}` // StyleNoUnderline is a placeholder for setting underline display mode off. StyleNoUnderline = `{nounderline}` )
const ( UnknownFlag Flag = -1 UnsetFlag = 0 SetFlag = 1 )
Variables ¶
var ( // CodeRegexp is an regular expression for matching escape codes. CodeRegexp = regexp.MustCompile(CodeStart + `[^` + CodeEnd + `]+` + CodeEnd) // DelimLeft is used for match template syntax (see Go-lang templates). DelimLeft = `{` // DelimRight is used for match template syntax (see Go-lang templates). DelimRight = `}` // Colorize controls whether loreley will output color codes. By default, // loreley will try to detect TTY and print colorized output only if // TTY is present. Colorize = ColorizeOnTTY // TTYStream is a stream FD (os.Stderr or os.Stdout), which // will be checked by loreley when Colorize = ColorizeOnTTY. TTYStream = int(os.Stderr.Fd()) )
Functions ¶
func CompileAndExecuteToString ¶
func CompileAndExecuteToString( text string, extensions map[string]interface{}, data map[string]interface{}, ) (string, error)
CompileAndExecuteToString compiles Compile specified template and immediately execute it with given `data`.
func TrimStyles ¶
TrimStyles removes all escape codes from the given string.
Types ¶
type Color ¶
type Color int
Color represents type for foreground or background color, 256-color based.
type ColorizeMode ¶
type ColorizeMode int
ColorizeMode represents default behaviour for colorizing or not colorizing output/
const ( // ColorizeAlways will tell loreley to colorize output no matter of what. ColorizeAlways ColorizeMode = iota // ColorizeOnTTY will tell loreley to colorize output only on TTY. ColorizeOnTTY // ColorizeNever turns of output colorizing. ColorizeNever )
type State ¶
type State struct {
// contains filtered or unexported fields
}
State represents foreground and background color, bold and reversed modes in between of Style template execution, which may be usefull for various extension functions.
type Style ¶
type Style struct { *template.Template // NoColors can be set to true to disable escape sequence output, // but still render underlying template. NoColors bool // contains filtered or unexported fields }
Style is a compiled style. Can be used as text/template.
func Compile ¶
Compile compiles style which is specified by string into Style, optionally adding extended formatting functions.
Avaiable functions to extend Go-lang template syntax:
- {bg <color>} - changes background color to the specified <color>.
- {fg <color>} - changes foreground color to the specified <color>.
- {nobg} - resets background color to the default.
- {nofg} - resets background color to the default.
- {bold} - set display mode to bold.
- {nobold} - disable bold display mode.
- {reverse} - set display mode to reverse.
- {noreverse} - disable reverse display mode.
- {reset} - resets all previously set styles.
- {from <text> <bg>} - renders <text> which foreground color equals current background color, and background color with specified <bg> color. Applies current foreground color and specified <bg> color to the following statements.
- {to <fg> <text>} - renders <text> with specified <fg> color as foreground color, and then use it as background color for the following statements.
`extensions` is a additional functions, which will be available in the template.
func CompileWithReset ¶
CompileWithReset is same as Compile, but appends {reset} to the end of given style string.
func (*Style) ExecuteToString ¶
ExecuteToString is same, as text/template Execute, but return string as a result.