tempera

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2018 License: MIT Imports: 6 Imported by: 0

README

tempera

Release GoDoc Go Report Card Build Status License

Template based terminal coloring made really easy.

http://sw.cowtech.it/tempera

Usage

Tempera allows to add coloring to terminal in a really easy way.

Colorize strings

To colorize strings, simply use the Colorize function, passing a list of styles you want to apply. The list of supported color names correspondes to the keys of ANSICodes variable.


import "github.com/ShogunPanda/tempera"

inRed := tempera.Colorize("Colorized", "red")
inRedWithBlueBackground := tempera.Colorize("Colorized", "red bgBlue")
Colorize templates

To colorize a template using a tagged template syntax, simply use the ColorizeTemplate function.


import "github.com/ShogunPanda/tempera"

colored := tempera.ColorizeTemplate("{red}This is in red, {green underline}this in green underlined{-}, this in red again.")

The template recognizes styles between curly braces (use a double opening brace to escape them) and the token {-} as universal closing tag (which also restores the previous style).

The closing tag at the end of the string can be omitted, since tempera will append the global reset style (\x1b[0m) if any style was set.

If you want to discard styles to be restored, use the {reset} token.

Setting custom styles

If you want to define custom styles, use the AddCustomStyle function.

import "github.com/ShogunPanda/tempera"

tempera.AddCustomStyle("important", "red underline")
colored := tempera.ColorizeTemplate("This is in red, underlined.", "important")
256 and 16 millions colors support

tempera supports 256 ANSI codes and 16m RGB colors. Just give it a try:

import "github.com/ShogunPanda/tempera"

fgAnsi256 := tempera.Colorize("color me", "ansi:100")
bgAnsi256 := tempera.Colorize("color me", "bgANSI:3,4,5")

fgRgb := tempera.Colorize("color me", "rgb:255,0,0")
bgRgb := tempera.Colorize("color me", "bgRGB:0,255,0")

fgHex := tempera.Colorize("color me", "hex:#FF0000")
bgHex := tempera.Colorize("color me", "bgHEX:00FF00")

ANSI, RGB, and HEX can be used in style definitions and templates as well.

API Documentation

The API documentation can be found here.

Contributing to tempera

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

Copyright (C) 2018 and above Shogun (shogun@cowtech.it).

Licensed under the MIT license, which can be found at https://choosealicense.com/licenses/mit.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ANSIBackgroundCodes = ANSICode{48, 49}

ANSIBackgroundCodes holds general code for foreground color management

View Source
var ANSICodes = map[string]ANSICode{
	"reset": {0, 0},

	"bold":          {1, 22},
	"dim":           {2, 22},
	"italic":        {3, 23},
	"underline":     {4, 24},
	"inverse":       {7, 27},
	"hidden":        {8, 28},
	"strikethrough": {9, 29},

	"black":   {30, 39},
	"red":     {31, 39},
	"green":   {32, 39},
	"yellow":  {33, 39},
	"blue":    {34, 39},
	"magenta": {35, 39},
	"cyan":    {36, 39},
	"white":   {37, 39},
	"gray":    {90, 39},

	"bgBlack":   {40, 49},
	"bgRed":     {41, 49},
	"bgGreen":   {42, 49},
	"bgYellow":  {43, 49},
	"bgBlue":    {44, 49},
	"bgMagenta": {45, 49},
	"bgCyan":    {46, 49},
	"bgWhite":   {47, 49},

	"redBright":     {91, 39},
	"greenBright":   {92, 39},
	"yellowBright":  {93, 39},
	"blueBright":    {94, 39},
	"magentaBright": {95, 39},
	"cyanBright":    {96, 39},
	"whiteBright":   {97, 39},

	"bgBlackBright":   {100, 49},
	"bgRedBright":     {101, 49},
	"bgGreenBright":   {102, 49},
	"bgYellowBright":  {103, 49},
	"bgBlueBright":    {104, 49},
	"bgMagentaBright": {105, 49},
	"bgCyanBright":    {106, 49},
	"bgWhiteBright":   {107, 49},
}

ANSICodes is the list of ANSI Escape codes, taken from https://github.com/chalk/ansi-styles

View Source
var ANSIForegroundCodes = ANSICode{38, 39}

ANSIForegroundCodes holds general code for foreground color management

Functions

func AddCustomStyle

func AddCustomStyle(name string, styles ...string) error

AddCustomStyle add a new custom style

func CleanTemplate added in v1.1.0

func CleanTemplate(template string) string

CleanTemplate removes all style tag from a template

func Colorize

func Colorize(content string, styles ...string) string

Colorize add colors to a string using a specific list of styles, in order

func ColorizeTemplate

func ColorizeTemplate(template string) string

ColorizeTemplate add colors to a template string

func DeleteCustomStyles

func DeleteCustomStyles(names ...string)

DeleteCustomStyles removes one or more custom styles

Types

type ANSICode

type ANSICode struct {
	Open  int
	Close int
}

ANSICode represents a ANSI code with opening and closing (resetting) code

Jump to

Keyboard shortcuts

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