aec

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2017 License: MIT Imports: 2 Imported by: 410

README

aec

GoDoc

Go wrapper for ANSI escape code.

Install

go get github.com/morikuni/aec

Features

ANSI escape codes depend on terminal environment.
Some of these features may not work.
Check supported Font-Style/Font-Color features with checkansi.

Wikipedia for more detail.

Cursor
  • Up(n)
  • Down(n)
  • Right(n)
  • Left(n)
  • NextLine(n)
  • PreviousLine(n)
  • Column(col)
  • Position(row, col)
  • Save
  • Restore
  • Hide
  • Show
  • Report
Erase
  • EraseDisplay(mode)
  • EraseLine(mode)
Scroll
  • ScrollUp(n)
  • ScrollDown(n)
Font Style
  • Bold
  • Faint
  • Italic
  • Underline
  • BlinkSlow
  • BlinkRapid
  • Inverse
  • Conceal
  • CrossOut
  • Frame
  • Encircle
  • Overline
Font Color

Foreground color.

  • DefaultF
  • BlackF
  • RedF
  • GreenF
  • YellowF
  • BlueF
  • MagentaF
  • CyanF
  • WhiteF
  • LightBlackF
  • LightRedF
  • LightGreenF
  • LightYellowF
  • LightBlueF
  • LightMagentaF
  • LightCyanF
  • LightWhiteF
  • Color3BitF(color)
  • Color8BitF(color)
  • FullColorF(r, g, b)

Background color.

  • DefaultB
  • BlackB
  • RedB
  • GreenB
  • YellowB
  • BlueB
  • MagentaB
  • CyanB
  • WhiteB
  • LightBlackB
  • LightRedB
  • LightGreenB
  • LightYellowB
  • LightBlueB
  • LightMagentaB
  • LightCyanB
  • LightWhiteB
  • Color3BitB(color)
  • Color8BitB(color)
  • FullColorB(r, g, b)
Color Converter

24bit RGB color to ANSI color.

  • NewRGB3Bit(r, g, b)
  • NewRGB8Bit(r, g, b)
Builder

To mix these features.

custom := aec.EmptyBuilder.Right(2).RGB8BitF(128, 255, 64).RedB().ANSI
custom.Apply("Hello World")

Usage

  1. Create ANSI by aec.XXX().With(aec.YYY()) or aec.EmptyBuilder.XXX().YYY().ANSI
  2. Print ANSI by fmt.Print(ansi, "some string", aec.Reset) or fmt.Print(ansi.Apply("some string"))

aec.Reset should be added when using font style or font color features.

Example

Simple progressbar.

sample

package main

import (
	"fmt"
	"strings"
	"time"

	"github.com/morikuni/aec"
)

func main() {
	const n = 20
	builder := aec.EmptyBuilder

	up2 := aec.Up(2)
	col := aec.Column(n + 2)
	bar := aec.Color8BitF(aec.NewRGB8Bit(64, 255, 64))
	label := builder.LightRedF().Underline().With(col).Right(1).ANSI

	// for up2
	fmt.Println()
	fmt.Println()

	for i := 0; i <= n; i++ {
		fmt.Print(up2)
		fmt.Println(label.Apply(fmt.Sprint(i, "/", n)))
		fmt.Print("[")
		fmt.Print(bar.Apply(strings.Repeat("=", i)))
		fmt.Println(col.Apply("]"))
		time.Sleep(100 * time.Millisecond)
	}
}

License

MIT

Documentation

Index

Constants

View Source
const Reset string = "\x1b[0m"

Reset resets SGR effect.

Variables

This section is empty.

Functions

func Apply

func Apply(s string, ansi ...ANSI) string

Apply wraps given string in ANSIs.

Types

type ANSI

type ANSI interface {
	fmt.Stringer

	// With adapts given ANSIs.
	With(...ANSI) ANSI

	// Apply wraps given string in ANSI.
	Apply(string) string
}

ANSI represents ANSI escape code.

var (
	// EraseModes is a list of EraseMode.
	EraseModes struct {
		// All erase all.
		All EraseMode

		// Head erase to head.
		Head EraseMode

		// Tail erase to tail.
		Tail EraseMode
	}

	// Save saves the cursor position.
	Save ANSI

	// Restore restores the cursor position.
	Restore ANSI

	// Hide hides the cursor.
	Hide ANSI

	// Show shows the cursor.
	Show ANSI

	// Report reports the cursor position.
	Report ANSI
)
var (
	// Bold set the text style to bold or increased intensity.
	Bold ANSI

	// Faint set the text style to faint.
	Faint ANSI

	// Italic set the text style to italic.
	Italic ANSI

	// Underline set the text style to underline.
	Underline ANSI

	// BlinkSlow set the text style to slow blink.
	BlinkSlow ANSI

	// BlinkRapid set the text style to rapid blink.
	BlinkRapid ANSI

	// Inverse swap the foreground color and background color.
	Inverse ANSI

	// Conceal set the text style to conceal.
	Conceal ANSI

	// CrossOut set the text style to crossed out.
	CrossOut ANSI

	// Frame set the text style to framed.
	Frame ANSI

	// Encircle set the text style to encircled.
	Encircle ANSI

	// Overline set the text style to overlined.
	Overline ANSI
)

Style

var (
	// DefaultF is the default color of foreground.
	DefaultF ANSI

	// Normal color
	BlackF   ANSI
	RedF     ANSI
	GreenF   ANSI
	YellowF  ANSI
	BlueF    ANSI
	MagentaF ANSI
	CyanF    ANSI
	WhiteF   ANSI

	// Light color
	LightBlackF   ANSI
	LightRedF     ANSI
	LightGreenF   ANSI
	LightYellowF  ANSI
	LightBlueF    ANSI
	LightMagentaF ANSI
	LightCyanF    ANSI
	LightWhiteF   ANSI
)

Foreground color of text.

var (
	// DefaultB is the default color of background.
	DefaultB ANSI

	// Normal color
	BlackB   ANSI
	RedB     ANSI
	GreenB   ANSI
	YellowB  ANSI
	BlueB    ANSI
	MagentaB ANSI
	CyanB    ANSI
	WhiteB   ANSI

	// Light color
	LightBlackB   ANSI
	LightRedB     ANSI
	LightGreenB   ANSI
	LightYellowB  ANSI
	LightBlueB    ANSI
	LightMagentaB ANSI
	LightCyanB    ANSI
	LightWhiteB   ANSI
)

Background color of text.

func Color3BitB

func Color3BitB(c RGB3Bit) ANSI

Color3BitB set the background color of text.

func Color3BitF

func Color3BitF(c RGB3Bit) ANSI

Color3BitF set the foreground color of text.

func Color8BitB

func Color8BitB(c RGB8Bit) ANSI

Color8BitB set the background color of text.

func Color8BitF

func Color8BitF(c RGB8Bit) ANSI

Color8BitF set the foreground color of text.

func Column

func Column(col uint) ANSI

Column set the cursor position to a given column.

func Down

func Down(n uint) ANSI

Down moves down the cursor.

func EraseDisplay

func EraseDisplay(m EraseMode) ANSI

EraseDisplay erases display by given EraseMode.

func EraseLine

func EraseLine(m EraseMode) ANSI

EraseLine erases lines by given EraseMode.

func FullColorB

func FullColorB(r, g, b uint8) ANSI

FullColorB set the foreground color of text.

func FullColorF

func FullColorF(r, g, b uint8) ANSI

FullColorF set the foreground color of text.

func Left

func Left(n uint) ANSI

Left moves left the cursor.

func NextLine

func NextLine(n uint) ANSI

NextLine moves down the cursor to head of a line.

func Position

func Position(row, col uint) ANSI

Position set the cursor position to a given absolute position.

func PreviousLine

func PreviousLine(n uint) ANSI

PreviousLine moves up the cursor to head of a line.

func Right(n uint) ANSI

Right moves right the cursor.

func ScrollDown

func ScrollDown(n int) ANSI

ScrollDown scrolls down the page.

func ScrollUp

func ScrollUp(n int) ANSI

ScrollUp scrolls up the page.

func Up

func Up(n uint) ANSI

Up moves up the cursor.

type Builder

type Builder struct {
	ANSI ANSI
}

Builder is a lightweight syntax to construct customized ANSI.

var EmptyBuilder *Builder

EmptyBuilder is an initialized Builder.

func NewBuilder

func NewBuilder(a ...ANSI) *Builder

NewBuilder creates a Builder from existing ANSI.

func (*Builder) BlackB

func (builder *Builder) BlackB() *Builder

BlackB is a syntax for BlackB.

func (*Builder) BlackF

func (builder *Builder) BlackF() *Builder

BlackF is a syntax for BlackF.

func (*Builder) BlinkRapid

func (builder *Builder) BlinkRapid() *Builder

BlinkRapid is a syntax for BlinkRapid.

func (*Builder) BlinkSlow

func (builder *Builder) BlinkSlow() *Builder

BlinkSlow is a syntax for BlinkSlow.

func (*Builder) BlueB

func (builder *Builder) BlueB() *Builder

BlueB is a syntax for BlueB.

func (*Builder) BlueF

func (builder *Builder) BlueF() *Builder

BlueF is a syntax for BlueF.

func (*Builder) Bold

func (builder *Builder) Bold() *Builder

Bold is a syntax for Bold.

func (*Builder) Color3BitB

func (builder *Builder) Color3BitB(c RGB3Bit) *Builder

Color3BitB is a syntax for Color3BitB.

func (*Builder) Color3BitF

func (builder *Builder) Color3BitF(c RGB3Bit) *Builder

Color3BitF is a syntax for Color3BitF.

func (*Builder) Color8BitB

func (builder *Builder) Color8BitB(c RGB8Bit) *Builder

Color8BitB is a syntax for Color8BitB.

func (*Builder) Color8BitF

func (builder *Builder) Color8BitF(c RGB8Bit) *Builder

Color8BitF is a syntax for Color8BitF.

func (*Builder) Column

func (builder *Builder) Column(col uint) *Builder

Column is a syntax for Column.

func (*Builder) Conceal

func (builder *Builder) Conceal() *Builder

Conceal is a syntax for Conceal.

func (*Builder) CrossOut

func (builder *Builder) CrossOut() *Builder

CrossOut is a syntax for CrossOut.

func (*Builder) CyanB

func (builder *Builder) CyanB() *Builder

CyanB is a syntax for CyanB.

func (*Builder) CyanF

func (builder *Builder) CyanF() *Builder

CyanF is a syntax for CyanF.

func (*Builder) DefaultB

func (builder *Builder) DefaultB() *Builder

DefaultB is a syntax for DefaultB.

func (*Builder) DefaultF

func (builder *Builder) DefaultF() *Builder

DefaultF is a syntax for DefaultF.

func (*Builder) Down

func (builder *Builder) Down(n uint) *Builder

Down is a syntax for Down.

func (*Builder) Encircle

func (builder *Builder) Encircle() *Builder

Encircle is a syntax for Encircle.

func (*Builder) EraseDisplay

func (builder *Builder) EraseDisplay(m EraseMode) *Builder

EraseDisplay is a syntax for EraseDisplay.

func (*Builder) EraseLine

func (builder *Builder) EraseLine(m EraseMode) *Builder

EraseLine is a syntax for EraseLine.

func (*Builder) Faint

func (builder *Builder) Faint() *Builder

Faint is a syntax for Faint.

func (*Builder) Frame

func (builder *Builder) Frame() *Builder

Frame is a syntax for Frame.

func (*Builder) FullColorB

func (builder *Builder) FullColorB(r, g, b uint8) *Builder

FullColorB is a syntax for FullColorB.

func (*Builder) FullColorF

func (builder *Builder) FullColorF(r, g, b uint8) *Builder

FullColorF is a syntax for FullColorF.

func (*Builder) GreenB

func (builder *Builder) GreenB() *Builder

GreenB is a syntax for GreenB.

func (*Builder) GreenF

func (builder *Builder) GreenF() *Builder

GreenF is a syntax for GreenF.

func (*Builder) Hide

func (builder *Builder) Hide() *Builder

Hide is a syntax for Hide.

func (*Builder) Inverse

func (builder *Builder) Inverse() *Builder

Inverse is a syntax for Inverse.

func (*Builder) Italic

func (builder *Builder) Italic() *Builder

Italic is a syntax for Italic.

func (*Builder) Left

func (builder *Builder) Left(n uint) *Builder

Left is a syntax for Left.

func (*Builder) LightBlackB

func (builder *Builder) LightBlackB() *Builder

LightBlackB is a syntax for LightBlackB.

func (*Builder) LightBlackF

func (builder *Builder) LightBlackF() *Builder

LightBlackF is a syntax for LightBlueF.

func (*Builder) LightBlueB

func (builder *Builder) LightBlueB() *Builder

LightBlueB is a syntax for LightBlueB.

func (*Builder) LightBlueF

func (builder *Builder) LightBlueF() *Builder

LightBlueF is a syntax for LightBlueF.

func (*Builder) LightCyanB

func (builder *Builder) LightCyanB() *Builder

LightCyanB is a syntax for LightCyanB.

func (*Builder) LightCyanF

func (builder *Builder) LightCyanF() *Builder

LightCyanF is a syntax for LightCyanF.

func (*Builder) LightGreenB

func (builder *Builder) LightGreenB() *Builder

LightGreenB is a syntax for LightGreenB.

func (*Builder) LightGreenF

func (builder *Builder) LightGreenF() *Builder

LightGreenF is a syntax for LightGreenF.

func (*Builder) LightMagentaB

func (builder *Builder) LightMagentaB() *Builder

LightMagentaB is a syntax for LightMagentaB.

func (*Builder) LightMagentaF

func (builder *Builder) LightMagentaF() *Builder

LightMagentaF is a syntax for LightMagentaF.

func (*Builder) LightRedB

func (builder *Builder) LightRedB() *Builder

LightRedB is a syntax for LightRedB.

func (*Builder) LightRedF

func (builder *Builder) LightRedF() *Builder

LightRedF is a syntax for LightRedF.

func (*Builder) LightWhiteB

func (builder *Builder) LightWhiteB() *Builder

LightWhiteB is a syntax for LightWhiteB.

func (*Builder) LightWhiteF

func (builder *Builder) LightWhiteF() *Builder

LightWhiteF is a syntax for LightWhiteF.

func (*Builder) LightYellowB

func (builder *Builder) LightYellowB() *Builder

LightYellowB is a syntax for LightYellowB.

func (*Builder) LightYellowF

func (builder *Builder) LightYellowF() *Builder

LightYellowF is a syntax for LightYellowF.

func (*Builder) MagentaB

func (builder *Builder) MagentaB() *Builder

MagentaB is a syntax for MagentaB.

func (*Builder) MagentaF

func (builder *Builder) MagentaF() *Builder

MagentaF is a syntax for MagentaF.

func (*Builder) NextLine

func (builder *Builder) NextLine(n uint) *Builder

NextLine is a syntax for NextLine.

func (*Builder) Overline

func (builder *Builder) Overline() *Builder

Overline is a syntax for Overline.

func (*Builder) Position

func (builder *Builder) Position(row, col uint) *Builder

Position is a syntax for Position.

func (*Builder) PreviousLine

func (builder *Builder) PreviousLine(n uint) *Builder

PreviousLine is a syntax for PreviousLine.

func (*Builder) RGB3BitB

func (builder *Builder) RGB3BitB(r, g, b uint8) *Builder

RGB3BitB is a syntax for Color3BitB with NewRGB3Bit.

func (*Builder) RGB3BitF

func (builder *Builder) RGB3BitF(r, g, b uint8) *Builder

RGB3BitF is a syntax for Color3BitF with NewRGB3Bit.

func (*Builder) RGB8BitB

func (builder *Builder) RGB8BitB(r, g, b uint8) *Builder

RGB8BitB is a syntax for Color8BitB with NewRGB8Bit.

func (*Builder) RGB8BitF

func (builder *Builder) RGB8BitF(r, g, b uint8) *Builder

RGB8BitF is a syntax for Color8BitF with NewRGB8Bit.

func (*Builder) RedB

func (builder *Builder) RedB() *Builder

RedB is a syntax for RedB.

func (*Builder) RedF

func (builder *Builder) RedF() *Builder

RedF is a syntax for RedF.

func (*Builder) Report

func (builder *Builder) Report() *Builder

Report is a syntax for Report.

func (*Builder) Restore

func (builder *Builder) Restore() *Builder

Restore is a syntax for Restore.

func (*Builder) Right

func (builder *Builder) Right(n uint) *Builder

Right is a syntax for Right.

func (*Builder) Save

func (builder *Builder) Save() *Builder

Save is a syntax for Save.

func (*Builder) ScrollDown

func (builder *Builder) ScrollDown(n int) *Builder

ScrollDown is a syntax for ScrollDown.

func (*Builder) ScrollUp

func (builder *Builder) ScrollUp(n int) *Builder

ScrollUp is a syntax for ScrollUp.

func (*Builder) Show

func (builder *Builder) Show() *Builder

Show is a syntax for Show.

func (*Builder) Underline

func (builder *Builder) Underline() *Builder

Underline is a syntax for Underline.

func (*Builder) Up

func (builder *Builder) Up(n uint) *Builder

Up is a syntax for Up.

func (*Builder) WhiteB

func (builder *Builder) WhiteB() *Builder

WhiteB is a syntax for WhiteB.

func (*Builder) WhiteF

func (builder *Builder) WhiteF() *Builder

WhiteF is a syntax for WhiteF.

func (*Builder) With

func (builder *Builder) With(a ...ANSI) *Builder

With is a syntax for With.

func (*Builder) YellowB

func (builder *Builder) YellowB() *Builder

YellowB is a syntax for YellowB.

func (*Builder) YellowF

func (builder *Builder) YellowF() *Builder

YellowF is a syntax for YellowF.

type EraseMode

type EraseMode uint

EraseMode is listed in a variable EraseModes.

type RGB3Bit

type RGB3Bit uint8

RGB3Bit is a 3bit RGB color.

func NewRGB3Bit

func NewRGB3Bit(r, g, b uint8) RGB3Bit

NewRGB3Bit create a RGB3Bit from given RGB.

type RGB8Bit

type RGB8Bit uint8

RGB8Bit is a 8bit RGB color.

func NewRGB8Bit

func NewRGB8Bit(r, g, b uint8) RGB8Bit

NewRGB8Bit create a RGB8Bit from given RGB.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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