ansifmt

package module
v0.0.0-...-86f016c Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2015 License: Apache-2.0 Imports: 10 Imported by: 0

README

Build Status GoDoc

ansifmt library

ansifmt is a library to play around with ansi escape codes.

It provides, mainly, a Format object describing most of the ansi escape codes capability (color, bold, etc.), and the ability to generate a "Coder" from it.

var f Format
f.SetStrike(true)
striker := f.Coder()
//striker is a function that will, decorate any word with strike code 
fmt.Printf("%q", striker("Hello"))
//Output: "\x1b[9mHello\x1b[29m"

It also provides a Writer, where you can change the output format.

ansifmt/renderer

it's a renderer for the excellent blackfriday mardown generator.

Used in 'blackfriday', you can pretty print Markdown to the terminal console.

License

ansifmt is available under the Apache License, Version 2.0.

Branches

master: Build Status against go versions:

  • 1.2
  • 1.3
  • tip

dev: Build Status against go versions:

  • 1.2
  • 1.3
  • tip

Documentation

Overview

Package ansifmt deals with ansi escape code and there associated format.

Index

Examples

Constants

View Source
const (
	//caveat: we need to start with Color because iota value matters for "colors"
	BlackColor = Color(iota)
	RedColor
	GreenColor
	YellowColor
	BlueColor
	MagentaColor
	CyanColor
	WhiteColor
	//the reset code
	Reset = "\x1b[0m"

	NormalWeight = Weight(iota)
	BoldWeight
	FaintWeight
)

Variables

This section is empty.

Functions

func AnsiCode

func AnsiCode(from, to Format) (code string)

AnsiCode compute the ansi escape string to change from one Format to another

func AnsiSize

func AnsiSize() (lines, cols int, err error)

AnsiSize return the number of Lines and Columns in the current "terminal" (if available)

func LineWrap

func LineWrap(txt string, length int) (lines []string)

LineWrap executes the linewrap algorithm escaping ansi escape code. it returns a slice of lines.

'length' is the max numbers in a column. It is respected unless a single word is too big to fit in

Example
lines := LineWrap("toto \x1b[1mis\x1b[0m a weird person", 6)
for i, line := range lines {
	fmt.Printf("%v: %q\n", i, line)
}
Output:

0: "toto "
1: "\x1b[1mis\x1b[0m a "
2: "weird "
3: "person"

func ToTitle

func ToTitle(txt string) (title string)

ToTitle maps the strings.ToTitle but support text containing ansi escape code

strings.ToTitle("\x1b[1mHello")
> "\x1b[1MHELLO"

This is not a valid ansi escaped code.

Example
title := "this \x1b[1mis\x1b[0m a title"
Title := ToTitle(title)
fmt.Printf("%q", Title)
Output:

"THIS \x1b[1mIS\x1b[0m A TITLE"

Types

type Coder

type Coder func(string) string

Coder is a function that will decorate the input string with ansi escape code.

type Color

type Color int

Color one of the ansi color

type Format

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

Format describe a terminal Format. Limited capacity due to limited ansi escape capacity works nicely with the following pattern

defer f.SetWeight( f.SetWeight(BoldWeight) )

Because, `f.SetWeight(BoldWeight)` is evaluated immediatly, the format is now "Bold" It will be reset to previous value when the deferred call will happen

func (Format) Background

func (f Format) Background() Color

func (Format) Coder

func (f Format) Coder() Coder
Example
var f Format
f.SetStrike(true)
striker := f.Coder()

fmt.Printf("%q", striker("toto"))
Output:

"\x1b[9mtoto\x1b[29m"

func (Format) Foreground

func (f Format) Foreground() Color

func (Format) Reverse

func (f Format) Reverse() bool

func (*Format) SetBackground

func (f *Format) SetBackground(color Color) (c Color)

SetBackground and return previous value

func (*Format) SetForeground

func (f *Format) SetForeground(color Color) (c Color)

SetForeground and return previous value

func (*Format) SetReverse

func (f *Format) SetReverse(r bool) (c bool)

SetReverse and return previous value

func (*Format) SetStrike

func (f *Format) SetStrike(s bool) (c bool)

SetStrike and return previous value

func (*Format) SetUnder

func (f *Format) SetUnder(u bool) (c bool)

SetUnder and return previous value

func (*Format) SetWeight

func (f *Format) SetWeight(w Weight) (c Weight)

SetWeight and return previous value

func (Format) Strike

func (f Format) Strike() bool

func (Format) Under

func (f Format) Under() bool

func (Format) Weight

func (f Format) Weight() Weight

type Weight

type Weight int

Weight one of NormalWeight, BoldWeight or FaintWeight

type Writer

type Writer struct {
	Format
	// contains filtered or unexported fields
}

Writer implements Writer but it can change the output Format using ansi escape codes.

It also implements Formatter interface, it is easy to specify a Format.

Changes to the Format are buffered until the next Write. So it is not inefficient to toggle Format without writing anyting.

Only changes in Format are written down.

It has a method Reset that writedown immediatly a reset ansi code.

func NewWriter

func NewWriter(w io.Writer) *Writer

func (*Writer) Reset

func (p *Writer) Reset() error

Reset send the ansi code \033[0m immediatly to the stream.

It does not change the current in memory Format. So the next call to Write() the ansi code will be recomputed.

For instance

w:= NewWriter(os.Stdout)
w.SetWeight(BoldWeight)
fmt.Fprintf("Bolded Text")
fmt.Fprintf("Bolded Text")

func (*Writer) Write

func (p *Writer) Write(b []byte) (n int, err error)

Directories

Path Synopsis
renderer for russross's Blackfriday markdown processor.
renderer for russross's Blackfriday markdown processor.

Jump to

Keyboard shortcuts

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