pintor

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2020 License: Apache-2.0 Imports: 2 Imported by: 0

README

Pintor

Go Reference

Pintor is a small package to colorize text output in your Command Line applications. Pintor applies ANSI escape sequences to modify text.

Installation

  1. Install Pintor
$ go get -u github.com/jbaxx/pintor
  1. Import it in your code
import "github.com/jbaxx/pintor"

Quick start

Start by creating a formatter object passing the foreground and background colors, and modifiers as parameters. Then apply the format to a string, which will be properly ANSI escaped to print using the desired colors and modifiers.

package main

import (
	"fmt"

	"github.com/jbaxx/pintor"
)

func main() {
	red := pintor.NewFormatter(pintor.Red, 0, 0)
	textColoredRed := red.Format("This text is red")
	fmt.Println(textColoredRed)

	blue := pintor.NewFormatter(pintor.Blue, 0, 0)
	fmt.Println(blue.Format("This text is blue"))

	fmt.Println(red.Format("This text is also red"))

	whiteMagentaB := pintor.NewFormatter(pintor.White, pintor.Magenta, pintor.Bold)
	fmt.Println(
		whiteMagentaB.Format("This text is bold, colored white, magenta foreground"),
	)

	cyanBI := pintor.NewFormatter(pintor.Cyan, pintor.Default, pintor.Bold|pintor.Italic)
	fmt.Println(
		cyanBI.Format("This text is bold and italic, colored cyan, default background"),
	)

}

Will provide output similar to this

pintor terminal output

Demo

You can run a small demo that displays foreground, background and modifiers combinations, by executing at the root of the repository:

go run examples/main.go

Documentation

Overview

Package pintor colorizes text printed to a terminal. It applies format to the output using ANSI escape sequences.

Index

Constants

View Source
const (
	Default = 0
	Black   = 1 << iota
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
	Bold
	Italic
	Underline
)

Values that can be used to create a Formatter object.

Default leaves the current format as it is. Eight colors supported: Black, Red, Green, Yellow, Blue, Magenta, Cyan and White. Colors apply for foreground and background color. Modifiers supported: Bold, Italic and Underline.

Variables

This section is empty.

Functions

This section is empty.

Types

type Formatter

type Formatter struct {
	// contains filtered or unexported fields
}

Formatter formats the text output.

func NewFormatter

func NewFormatter(foreground, background, modifiers uint) *Formatter

NewFormatter returns a new Formatter that can apply the colors and modifiers specified as parameters. Predefined bits control the colors and modifiers. Look at the package constant to get the colors and modifiers names. Only accepts one color for foreground and one color for background. To apply multiple modifiers at once set the modifiers parameter to:

Bold|Italic

This for example will apply both Bold and Italic to the text.

func (*Formatter) Format

func (f *Formatter) Format(target string) string

Format applies the format defined by the Formatter. It receives the target string and returns the string with the formatting applied. Formatting is performed using ANSI escape sequences.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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