loreley

package module
v0.0.0-...-29b1d7b Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2021 License: MIT Imports: 7 Imported by: 45

README

loreley

Easy and extensible colorizer for the programs' output.

Basically, loreley turns this:

{bold}{fg 15}{bg 27} hello {from "" 29} there {to 16 ""},

Into this:

2016-06-27-13t53t45

Usage

package main

import "fmt"
import "github.com/reconquest/loreley"

func main() {
	text, err := loreley.CompileAndExecuteToString(
		`{bold}{fg 15}{bg 27} hello {from "" 29} {.where} {to 16 ""}{reset}`,
		nil,
		map[string]interface{}{"where": "there"},
	)
	if err != nil {
		fmt.Errorf(`can't compile loreley template: %s`, err)
	}

	fmt.Println(text)
}

Colors in text/tabwriter

Unfortunately, stdlib tabwriter does not implement proper column width calculation if you use escape sequences in your data to highlight some output.

So, probably, You will see something like this trying tabwriter:

tabwriter-before

Using loreley you can achieve exactly what you're expecting to see:

tabwriter-after

package main

import (
	"bytes"
	"fmt"
	"strings"
	"text/tabwriter"

	"github.com/reconquest/loreley"
)

const ()

func main() {
	buffer := &bytes.Buffer{}

	writer := tabwriter.NewWriter(buffer, 2, 4, 2, ' ', tabwriter.FilterHTML)

	writer.Write([]byte(strings.Join(
		[]string{
			"<underline>CORES<reset>",
			"<underline>DESCRIPTION<reset>\n",
		}, "\t",
	)))

	writer.Write([]byte(strings.Join(
		[]string{
			"<fg 15><bg 1><bold> 1 <reset> <fg 15><bg 243><bold> 3 <reset>",
			"test\n",
		}, "\t",
	)))

	writer.Flush()

	loreley.DelimLeft = "<"
	loreley.DelimRight = ">"

	result, err := loreley.CompileAndExecuteToString(
		buffer.String(),
		nil,
		nil,
	)
	if err != nil {
		panic(err)
	}

	fmt.Print(result)
}

Reference

loreley extends Go-lang template system. So, fully syntax is supported with exception, that { and } will be used as delimiters.

All <color>, accepted by loreley, should be the 256-color code.

Available template functions:

  • {bg <color>} sets background color for the next text;
  • {fg <color>} sets foreground color for the next text;
  • {nobg} resets background color to the default;
  • {nofg} resets foreground color to the default;
  • {bold} set bold mode on for the next text;
  • {nobold} set bold mode to off;
  • {reverse} set reverse mode on for the next text;
  • {noreverse} set reverse mode off;
  • {underline} set underline mode on for the next text;
  • {nounderline} set underline mode off;
  • {reset} resets all styles to default;
  • {from <text> <bg>} reuse current fg as specified <text>'s bg color, specified <bg> will be used as fg color and as bg color for the following text;
  • {to <bg> <text>} reuse current bg as specified <text>'s bg color, specified <bg> will be used as fg color for the following text;

License

This project is licensed under the terms of the MIT license.

Documentation

Index

Constants

View Source
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}`
)
View Source
const (
	UnknownFlag Flag = -1
	UnsetFlag        = 0
	SetFlag          = 1
)

Variables

View Source
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 HasTTY

func HasTTY(fd int) bool

HasTTY will return true if specified file descriptor is bound to a TTY.

func TrimStyles

func TrimStyles(input string) string

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.

const (
	UnknownColor Color = -1

	// DefaultColor represents default foreground and background color.
	DefaultColor Color = 0
)

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 Flag

type Flag int

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.

func (State) String

func (state State) String() string

String returns current state representation as loreley template.

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

func Compile(
	text string,
	extensions map[string]interface{},
) (*Style, error)

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

func CompileWithReset(
	text string,
	extensions map[string]interface{},
) (*Style, error)

CompileWithReset is same as Compile, but appends {reset} to the end of given style string.

func (*Style) ExecuteToString

func (style *Style) ExecuteToString(
	data map[string]interface{},
) (string, error)

ExecuteToString is same, as text/template Execute, but return string as a result.

func (*Style) GetState

func (style *Style) GetState() State

GetState returns current style state in the middle of template execution.

func (*Style) SetState

func (style *Style) SetState(state State)

SetState sets current style state in the middle of template execution.

Jump to

Keyboard shortcuts

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