figlet4go

package module
v0.0.0-...-d6cef5b Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2019 License: MIT Imports: 12 Imported by: 0

README

FIGlet for Go

Go Report Card

figlet4go is a go library which is a port of FIGlet to Golang.
With figlet4go it's easy to create ascii text banners in the command-line or with the given api.

screenshot

This Repository used to be a fork of getwe/figlet4go, but I changed so much that it's not compatible anymore

Installation

$ go get -u github.com/mbndr/figlet4go/...

Usage

Command-line

You can use the figlet4go command in the command-line.
For example (generates the banner on top):

$ figlet4go -str "figlet4go" -font "larry3d" -colors "green;FF9900;cyan"

For a usage instruction read the commands usage with figlet4go -h.

Basic

You have to create a renderer (ascii) and let it render the desired string through the Render method. After that you can simply print the returned string.

import "github.com/mbndr/figlet4go"

// ...

ascii := figlet4go.NewAsciiRender()

// The underscore would be an error
renderStr, _ := ascii.Render("Hello World")
fmt.Print(renderStr)
Colored

The colors given in the []figlet4go.Color slice are repeating if the string is longer than the slice. You have to call the RenderOpts instead of the Render method to give the Renderer the Options.
If you use a TrueColor color, you have to ensure that your terminal supports it.
If you use a AnsiColor with an TrueColor only parser (f.e. ParserHTML), TrueColor objects are automatically generated.

import "github.com/mbndr/figlet4go"

// ...

ascii := figlet4go.NewAsciiRender()

// Adding the colors to RenderOptions
options := figlet4go.NewRenderOptions()
options.FontColor = []figlet4go.Color{
	// Colors can be given by default ansi color codes...
	figlet4go.ColorGreen,
	figlet4go.ColorYellow,
	figlet4go.ColorCyan,
	// ...or by an hex string...
	figlet4go.NewTrueColorFromHexString("885DBA"),
	// ...or by an TrueColor object with rgb values
	figlet4go.TrueColor{136, 93, 186},
}

renderStr, _ := ascii.RenderOpts("Hello Colors", options)
fmt.Print(renderStr)
Other font

If you want to use another font, you have to specify the name of the font as in this example.
Is the font you want to use not included you have to load the font manually with the LoadFont method. This method will walk the path recursively and load all .flf files.

import "github.com/mbndr/figlet4go"

// ...

ascii := figlet4go.NewAsciiRender()

options := figlet4go.NewRenderOptions()
options.FontName = "larry3d"

// If 'larry3d' wouldn't be included you would have to load your .flf files like that:
ascii.LoadFont("/path/to/fonts/")

renderStr, _ := ascii.RenderOpts("Hello Fonts", options)
fmt.Print(renderStr)
Other parser

A Parser can be set through the GetParser function with a valid key

import "github.com/mbndr/figlet4go"

// ...

ascii := figlet4go.NewAsciiRender()

options := figlet4go.NewRenderOptions()
p, _ := figlet4go.GetParser("html")
options.Parser = *p

renderStr, _ := ascii.RenderOpts("Hello Fonts", options)
fmt.Print(renderStr)

Parsers

There a currently these Parsers available:

Parser What does it do?
ParserTerminal Parses the result directly
ParserHTML Parses a pasteable <code> html block

Fonts

Builtin

The builtin fonts are built into the bindata.go file with the tool go-bindata.
The bash script for building the default font is stored in tools/ (go-bindata must be installed).

The default font is standard. These are the builtin fonts:

Font name Source
standard http://www.figlet.org/fontdb_example.cgi?font=standard.flf
larry3d http://www.figlet.org/fontdb_example.cgi?font=larry3d.flf
Other fonts

Other fonts can mainly be found on figlet. You have to load them as in this example.

Todo

  • Tests
  • automatically the perfect char margin
  • Linebreak possible?
  • Pointer-Value standarization
  • Parser as interface
  • Cli client
  • Colors in the cli client
  • No dependencies (fatih/color)
  • Truecolor support
  • More parsers (HTML)
  • Better parsers (maybe stored in a map)
  • Writer choosing for writing to file

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Asset

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames

func AssetNames() []string

AssetNames returns the names of the assets.

func MustAsset

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.

func RestoreAsset

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory

func RestoreAssets

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively

Types

type AnsiColor

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

AnsiColor representation

var (
	ColorBlack   AnsiColor = AnsiColor{30}
	ColorRed     AnsiColor = AnsiColor{31}
	ColorGreen   AnsiColor = AnsiColor{32}
	ColorYellow  AnsiColor = AnsiColor{33}
	ColorBlue    AnsiColor = AnsiColor{34}
	ColorMagenta AnsiColor = AnsiColor{35}
	ColorCyan    AnsiColor = AnsiColor{36}
	ColorWhite   AnsiColor = AnsiColor{37}
)

Terminal AnsiColors

type AsciiRender

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

AsciiRender is the wrapper to render a string

func NewAsciiRender

func NewAsciiRender() *AsciiRender

NewAsciiRender creates a new AsciiRender

func (*AsciiRender) LoadBindataFont

func (ar *AsciiRender) LoadBindataFont(fontBinary []byte, fontName string) error

LoadBinDataFont loads provided font binary

func (*AsciiRender) LoadFont

func (ar *AsciiRender) LoadFont(fontPath string) error

LoadFont loads all *.flf font files recursively in a path

func (*AsciiRender) Render

func (ar *AsciiRender) Render(str string) (string, error)

Render renders a string with the default options Calls the RenderOpts method with a new RenderOptions object

func (*AsciiRender) RenderOpts

func (ar *AsciiRender) RenderOpts(str string, opt *RenderOptions) (string, error)

RenderOpts renders a string with special RenderOptions Can be called from the user (if options wished) or the above Render method Contains the whole rendering logic

type Color

type Color interface {
	// contains filtered or unexported methods
}

Color has a pre- and a suffix

type Parser

type Parser struct {
	// Name used for switching in colors.go
	Name string
	// Parser prefix
	Prefix string
	// Parser suffix
	Suffix string
	// Newline representation
	NewLine string
	// Things to be replaced (f.e. \n to <br>)
	Replaces map[string]string
}

Parser stores some output specific stuff

func GetParser

func GetParser(key string) (*Parser, error)

GetParser returns a parser by its key

type RenderOptions

type RenderOptions struct {
	// Name of the used font
	FontName string
	// Colors of the font
	FontColor []Color
	// Parser
	Parser Parser
}

RenderOptions are used to set color or maybe future options to the AsciiRenderer

func NewRenderOptions

func NewRenderOptions() *RenderOptions

NewRenderOptions creates new RenderOptions Sets the default font name

type TrueColor

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

TrueColor with rgb Attributes

func NewTrueColorFromHexString

func NewTrueColorFromHexString(c string) (*TrueColor, error)

NewTrueColorFromHexString returns a Truecolor object based on a hexadezimal string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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