emerald

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2022 License: MIT Imports: 9 Imported by: 6

README

Emerald

A basic color library for use in my Go projects, built on top of mgutz/ansi.

Package ansi is a small, fast library to create ANSI colored strings and codes.

Install

Get it

go get -u github.com/jcwillox/emerald

Example

import "github.com/jcwillox/emerald"

// colorize a string, SLOW
msg := emerald.Color("foo", "red+b:white")

// create a FAST closure function to avoid computation of ANSI code
phosphorize := emerald.ColorFunc("green+h:black")
msg = phosphorize("Bring back the 80s!")
msg2 := phospohorize("Look, I'm a CRT!")

// cache escape codes and build strings manually
lime := emerald.ColorCode("green+h:black")
reset := emerald.ColorCode("reset")

fmt.Println(lime, "Bring back the 80s!", reset)

Other examples

Color(s, "red")            // red
Color(s, "red+d")          // red dim
Color(s, "red+b")          // red bold
Color(s, "red+B")          // red blinking
Color(s, "red+u")          // red underline
Color(s, "red+bh")         // red bold bright
Color(s, "red:white")      // red on white
Color(s, "red+b:white+h")  // red bold on white bright
Color(s, "red+B:white+h")  // red blink on white bright
Color(s, "off")            // turn off ansi codes

To view color combinations, from project directory in terminal.

go test

Style format

"foregroundColor+attributes:backgroundColor+attributes"

Colors

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • 0...255 (256 colors)
  • #FFFFFF (hex color codes)

Foreground Attributes

  • B = Blink
  • b = bold
  • h = high intensity (bright)
  • d = dim
  • i = inverse
  • s = strikethrough
  • u = underline

Background Attributes

  • h = high intensity (bright)

Constants

  • emerald.Reset
  • emerald.DefaultBG
  • emerald.DefaultFG
  • emerald.Black
  • emerald.Red
  • emerald.Green
  • emerald.Yellow
  • emerald.Blue
  • emerald.Magenta
  • emerald.Cyan
  • emerald.White
  • emerald.LightBlack
  • emerald.LightRed
  • emerald.LightGreen
  • emerald.LightYellow
  • emerald.LightBlue
  • emerald.LightMagenta
  • emerald.LightCyan
  • emerald.LightWhite

Windows Support

Emerald has full support for Windows and has print functions that wrap mattn/go-colorable to handle ANSI support on older windows systems.

import (
    "github.com/jcwillox/emerald"
)

emerald.Print(emerald.Blue)
emerald.Printf("%s%s", emerald.Blue, "Hello World")
emerald.Println(emerald.Blue, "Hello World", Reset)

References

Wikipedia ANSI escape codes Colors

General tips and formatting

Documentation

Index

Constants

View Source
const (
	ShowCursorSeq      = "\x1b[?25h"
	HideCursorSeq      = "\x1b[?25l"
	EnableLineWrapSeq  = "\x1b[?7h"
	DisableLineWrapSeq = "\x1b[?7l"

	CursorUpSeq    = "\x1b[%dA"
	CursorRightSeq = "\x1b[%dG"
	CursorDownSeq  = "\x1b[%dB"
)

Variables

View Source
var (
	// Black FG
	Black = start + "30m"
	// Red FG
	Red = start + "31m"
	// Green FG
	Green = start + "32m"
	// Yellow FG
	Yellow = start + "33m"
	// Blue FG
	Blue = start + "34m"
	// Magenta FG
	Magenta = start + "35m"
	// Cyan FG
	Cyan = start + "36m"
	// White FG
	White = start + "37m"
	// LightBlack FG
	LightBlack = start + "90m"
	// LightRed FG
	LightRed = start + "91m"
	// LightGreen FG
	LightGreen = start + "92m"
	// LightYellow FG
	LightYellow = start + "93m"
	// LightBlue FG
	LightBlue = start + "94m"
	// LightMagenta FG
	LightMagenta = start + "95m"
	// LightCyan FG
	LightCyan = start + "96m"
	// LightWhite FG
	LightWhite = start + "97m"
	// Reset is the ANSI reset escape sequence
	Reset = "\033[0m"
	// DefaultBG is the default background
	DefaultBG = "\033[49m"
	// DefaultFG is the default foreground
	DefaultFG = "\033[39m"
	// Bold FG
	Bold = start + "1m"
)
View Source
var (
	ImmediateNames = []string{
		"Makefile", "Cargo.toml", "SConstruct", "CMakeLists.txt",
		"build.gradle", "pom.xml", "Rakefile", "package.json", "Gruntfile.js",
		"Gruntfile.coffee", "BUILD", "BUILD.bazel", "WORKSPACE", "build.xml", "Podfile",
		"webpack.config.js", "meson.build", "composer.json", "RoboFile.php", "PKGBUILD",
		"Justfile", "Procfile", "Dockerfile", "Containerfile", "Vagrantfile", "Brewfile",
		"Gemfile", "Pipfile", "build.sbt", "mix.exs", "bsconfig.json", "tsconfig.json",
	}
	ImageExt = []string{
		"png", "jfi", "jfif", "jif", "jpe", "jpeg", "jpg", "gif", "bmp",
		"tiff", "tif", "ppm", "pgm", "pbm", "pnm", "webp", "raw", "arw",
		"svg", "stl", "eps", "dvi", "ps", "cbr", "jpf", "cbz", "xpm",
		"ico", "cr2", "orf", "nef", "heif",
	}
	VideoExt = []string{
		"avi", "flv", "m2v", "m4v", "mkv", "mov", "mp4", "mpeg",
		"mpg", "ogm", "ogv", "vob", "wmv", "webm", "m2ts", "heic",
	}
	MusicExt = []string{
		"aac", "m4a", "mp3", "ogg", "wma", "mka", "opus",
	}
	LosslessExt = []string{
		"alac", "ape", "flac", "wav",
	}
	CryptoExt = []string{
		"asc", "enc", "gpg", "pgp", "sig", "signature", "pfx", "p12",
	}
	DocumentExt = []string{
		"djvu", "doc", "docx", "dvi", "eml", "eps", "fotd", "key",
		"keynote", "numbers", "odp", "odt", "pages", "pdf", "ppt",
		"pptx", "rtf", "xls", "xlsx",
	}
	CompressedExt = []string{
		"zip", "tar", "Z", "z", "gz", "bz2", "a", "ar", "7z",
		"iso", "dmg", "tc", "rar", "par", "tgz", "xz", "txz",
		"lz", "tlz", "lzma", "deb", "rpm", "zst",
	}
	TempExt = []string{
		"tmp", "swp", "swo", "swn", "bak", "bkp", "bk",
	}
	CompiledExt = []string{
		"class", "elc", "hi", "o", "pyc", "zwc", "ko",
	}
)
View Source
var (
	Stdout io.Writer
	Stderr io.Writer
)
View Source
var ColorEnabled bool
View Source
var (

	// Colors maps common color names to their ANSI color code.
	Colors = map[string]int{
		"black":   black,
		"red":     red,
		"green":   green,
		"yellow":  yellow,
		"blue":    blue,
		"magenta": magenta,
		"cyan":    cyan,
		"white":   white,
		"default": defaultt,
	}
)
View Source
var FileColors = FileColorsT{
	Base:        Cyan,
	NonExistent: ColorCode("red+u"),

	Temp:       ColorIndexFg(244),
	Immediate:  ColorCode("yellow+bu"),
	Image:      ColorIndexFg(133),
	Video:      ColorIndexFg(135),
	Music:      ColorIndexFg(92),
	Lossless:   ColorIndexFg(93),
	Crypto:     ColorIndexFg(109),
	Document:   ColorIndexFg(105),
	Compressed: Red,
	Compiled:   ColorIndexFg(137),

	Directory:  ColorCode("blue+b"),
	Symlink:    ColorCode("cyan+b"),
	Pipe:       Yellow,
	Device:     ColorCode("yellow+b"),
	Socket:     ColorCode("red+b"),
	Irregular:  Yellow,
	Executable: ColorCode("green+b"),
	File:       White,
}

Functions

func AutoSetColorState

func AutoSetColorState()

AutoSetColorState automatically SetColorState based on the whether we are outputting to a terminal or the environment variables

func Color

func Color(s, style string) string

Color colors a string based on the ANSI color code for style.

func ColorCode

func ColorCode(style string) string

ColorCode returns the ANSI color code for style.

func ColorIndexBg

func ColorIndexBg(index int) string

ColorIndexBg convert 256 color code to ANSI background color

func ColorIndexFg

func ColorIndexFg(index int) string

ColorIndexFg convert 256 color code to ANSI foreground color

func CursorDown

func CursorDown(lines int)

func CursorDownVar

func CursorDownVar(lines int) string

func CursorRight

func CursorRight(columns int)

func CursorRightVar

func CursorRightVar(columns int) string

func CursorUp

func CursorUp(lines int)

func CursorUpVar

func CursorUpVar(lines int) string

func DisableLineWrap

func DisableLineWrap()

func DisableResetStyle added in v0.3.3

func DisableResetStyle(disabled bool)

func EnableLineWrap

func EnableLineWrap()

func ExtensionIsOneOf added in v0.3.0

func ExtensionIsOneOf(ext string, extensions []string) bool

func GetExtOnly added in v0.3.0

func GetExtOnly(filename string) string

func GetFileColor added in v0.3.0

func GetFileColor(filename string, mode ...os.FileMode) string

func GetFileColorStat added in v0.3.0

func GetFileColorStat(stat os.FileInfo) string

func GetFileModeColor added in v0.3.0

func GetFileModeColor(mode os.FileMode) string

func GetFileTypeColor added in v0.3.0

func GetFileTypeColor(filename string) string

func HideCursor

func HideCursor()

func HighlightFile added in v0.3.0

func HighlightFile(filename string, mode ...os.FileMode) string

func HighlightFileMode added in v0.3.0

func HighlightFileMode(mode os.FileMode) string

func HighlightFileStat added in v0.3.0

func HighlightFileStat(filename string, stat ...os.FileInfo) string

func HighlightPath added in v0.3.0

func HighlightPath(path string, mode ...os.FileMode) string

func HighlightPathStat added in v0.3.0

func HighlightPathStat(path string, stat ...os.FileInfo) string

func IsColorTerm

func IsColorTerm() bool

IsColorTerm combines IsColor and IsTerminal

func IsCompiled added in v0.3.0

func IsCompiled(ext string) bool

func IsCompressed added in v0.3.0

func IsCompressed(ext string) bool

func IsCrypto added in v0.3.0

func IsCrypto(ext string) bool

func IsDocument added in v0.3.0

func IsDocument(ext string) bool

func IsImage added in v0.3.0

func IsImage(ext string) bool

func IsImmediate added in v0.3.0

func IsImmediate(filename string) bool

func IsLossless added in v0.3.0

func IsLossless(ext string) bool

func IsMusic added in v0.3.0

func IsMusic(ext string) bool

func IsTemp added in v0.3.0

func IsTemp(filename string) bool

func IsTerminal

func IsTerminal() bool

IsTerminal returns true if stdout is attached to a terminal

func IsVideo added in v0.3.0

func IsVideo(ext string) bool

func Print

func Print(a ...interface{}) (n int)

func Printf

func Printf(format string, a ...interface{}) (n int)

func Println

func Println(a ...interface{}) (n int)

func SetColorState

func SetColorState(enabled bool)

SetColorState enables or disables ANSI support in the library using this function is preferred to directly setting `ColorEnabled` as it allows us to perform other actions.

func ShowCursor

func ShowCursor()

Types

type ColorPrinter

type ColorPrinter struct {
	ANSI string
}

func ColorInterface

func ColorInterface(style string) ColorPrinter

func (ColorPrinter) Print

func (c ColorPrinter) Print(a ...interface{})

func (ColorPrinter) Printf

func (c ColorPrinter) Printf(format string, a ...interface{})

func (ColorPrinter) Println

func (c ColorPrinter) Println(a ...interface{})

type ColorState

type ColorState int
const (
	ColorDisabled ColorState = iota
	ColorForced
	ColorUnset
)

func ColorPreference

func ColorPreference() ColorState

ColorPreference returns the color preference based on the environment variables

type Colorizer added in v0.2.0

type Colorizer func(s string) string

Colorizer takes an input string and colorizes it based on the style it was created with.

func ColorFunc

func ColorFunc(style string) Colorizer

ColorFunc creates a closure to avoid computation ANSI color code.

func ColorFuncVar

func ColorFuncVar(style string) (Colorizer, string)

ColorFuncVar creates a closure to avoid computation ANSI color code and returns ANSI sequence.

type FileColorsT added in v0.3.0

type FileColorsT struct {
	Base        string
	NonExistent string

	Temp       string
	Immediate  string
	Image      string
	Video      string
	Music      string
	Lossless   string
	Crypto     string
	Document   string
	Compressed string
	Compiled   string

	Directory  string
	Symlink    string
	Pipe       string
	Device     string
	Socket     string
	Irregular  string
	Executable string
	File       string
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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