formatter

package module
v0.0.0-...-c657492 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2021 License: Apache-2.0 Imports: 6 Imported by: 1

README

formatter

Simple ANSI text formatter for Go

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ContextColors = map[string]colors.Color{
	"hl":      colors.BrightWhite,
	"em":      colors.Cyan,
	"warn":    colors.Yellow,
	"fail":    colors.Red,
	"success": colors.Green,
}

Functions

This section is empty.

Types

type Formatter

type Formatter func(input string) (output string, err error)

func ColorFormatter

func ColorFormatter(tagMap map[string]colors.Color) Formatter

ColorFormatter returns a formatter that will map ANSI color codes into the output based on matching tags in the tag map. For instance:

colors := map[string]colors.Color{
  "hl":      colors.BrightWhite,
  "em":      colors.Cyan,
  "warn":    colors.Yellow,
  "fail":    colors.Red,
  "success": colors.Green,
}
formatter := ColorFormatter(colors)

func ContextFormatter

func ContextFormatter() Formatter

ContextColorFormatter will format text based on some simple context tags. The colors are looked up from ContextColors

<hl>highlight</hl> - colors.BrightWhite
 <em>emphasis</em> - colors.Cyan
     <warn></warn> - colors.Yellow
     <fail></fail> - colors.Red

<success></success> - colors.Green

type LogOption

type LogOption func(*logger)

func LogFormatter

func LogFormatter(format Formatter) LogOption

func LogWriter

func LogWriter(writer io.Writer) LogOption

type Logger

type Logger interface {
	Log(v ...interface{})
	Logf(format string, v ...interface{})
}

func ColorLogger

func ColorLogger(options ...LogOption) Logger
Example
package main

import (
	"os"

	"github.com/abates/formatter"
)

func main() {
	logger := formatter.ColorLogger(formatter.LogWriter(os.Stdout))
	// Only logf parses and evaluates the tagged string and then only
	// on the format string.  This allows angle brackets to appear in
	// the variadic arguments
	logger.Logf("This is some text with <em>emphasis</em> in the middle")
	logger.Logf("This is some text with a <warn>warning</warn> in the middle")
	logger.Logf("This is some text with a <fail>failure</fail> in the middle")
	logger.Logf("This is something <success>good</success> that happened")
}
Output:

This is some text with �[36memphasis�[0m in the middle
This is some text with a �[33mwarning�[0m in the middle
This is some text with a �[31mfailure�[0m in the middle
This is something �[32mgood�[0m that happened

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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