figlet

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 11 Imported by: 0

README

FIGlet.go

___________.___  ________.__          __
\_   _____/|   |/  _____/|  |   _____/  |_      ____   ____
 |    __)  |   /   \  ___|  | _/ __ \   __\    / ___\ /  _ \
 |     \   |   \    \_\  \  |_\  ___/|  |     / /_/  >  <_> )
 \___  /   |___|\______  /____/\___  >__| /\  \___  / \____/
     \/                \/          \/     \/ /_____/

CI Go Report Card Go Reference GitHub release License

A Go port of figlet.js that implements the FIGfont spec to generate ASCII art from text. Provides both an importable Go library and a command-line tool.

Quick Start

Install
go get github.com/damienbutt/figlet@latest
Simple Usage
package main

import (
    "fmt"
    "github.com/damienbutt/figlet"
)

func main() {
    text, err := figlet.Text("Hello World!")
    if err != nil {
        panic(err)
    }

    fmt.Println(text)
}

This will print out:

  _   _      _ _        __        __         _     _ _
 | | | | ___| | | ___   \ \      / /__  _ __| | __| | |
 | |_| |/ _ \ | |/ _ \   \ \ /\ / / _ \| '__| |/ _` | |
 |  _  |  __/ | | (_) |   \ V  V / (_) | |  | | (_| |_|
 |_| |_|\___|_|_|\___/     \_/\_/ \___/|_|  |_|\__,_(_)

Basic Usage

Text

Generates ASCII art from the given text. Takes two parameters:

  • text — the string to render as ASCII art.
  • opts — an optional *FigletOptions to control rendering (font, layout, width, etc.). Omit or pass nil to use the package defaults.

Returns the generated ASCII art as a string and an error.

package main

import (
    "fmt"
    "github.com/damienbutt/figlet"
)

func main() {
    text, err := figlet.Text("Boo!", &figlet.FigletOptions{
        Font:             "Ghost",
        HorizontalLayout: "default",
        VerticalLayout:   "default",
        Width:            80,
        WhitespaceBreak:  true,
    })
    if err != nil {
        fmt.Println("Something went wrong:", err)
        return
    }

    fmt.Println(text)
}

That will print out:

.-. .-')                            ,---.
\  ( OO )                           |   |
 ;-----.\  .-'),-----.  .-'),-----. |   |
 | .-.  | ( OO'  .-.  '( OO'  .-.  '|   |
 | '-' /_)/   |  | |  |/   |  | |  ||   |
 | .-. `. \_) |  |\|  |\_) |  |\|  ||  .'
 | |  \  |  \ |  | |  |  \ |  | |  |`--'
 | '--'  /   `'  '-'  '   `'  '-'  '.--.
 `------'      `-----'      `-----' '--'
Options

The FigletOptions struct has the following fields:

Font

Type: FontName (string alias) Default value: "Standard"

The FIGlet font to use for rendering.

HorizontalLayout

Type: KerningMethods (string alias) Default value: "default"

Controls horizontal kerning/smushing. Accepted values:

Value Description
"default" Kerning as intended by the font designer
"full" Full letter spacing, no overlap
"fitted" Letters moved together until they almost touch
"controlled smushing" Standard FIGlet controlled smushing rules
"universal smushing" Universal smushing — overlapping characters override one another
VerticalLayout

Type: KerningMethods (string alias) Default value: "default"

Controls vertical kerning/smushing. Accepts the same values as HorizontalLayout.

Width

Type: int Default value: 0 (no limit)

Limits the output to this many characters wide. For example, if you want your output to be a max of 80 characters wide, you would set this option to 80. Set to 0 to disable.

WhitespaceBreak

Type: bool Default value: false

When used with Width, attempts to break text on whitespace boundaries rather than mid-word.

PrintDirection

Type: PrintDirection Default value: DefaultDirection (uses the font's own setting)

Controls the rendering direction. Use LeftToRight or RightToLeft to override the font default.

ShowHardBlanks

Type: bool Default value: false

When true, hardblank characters (used internally for spacing) are preserved in the output rather than replaced with spaces.

Understanding Kerning

The 2 layout options allow you to override a font's default "kerning". Below you can see how this effects the text. The string "Kerning" was printed using the "Standard" font with horizontal layouts of "default", "fitted" and then "full".

  _  __               _
 | |/ /___ _ __ _ __ (_)_ __   __ _
 | ' // _ \ '__| '_ \| | '_ \ / _` |
 | . \  __/ |  | | | | | | | | (_| |
 |_|\_\___|_|  |_| |_|_|_| |_|\__, |
                              |___/
  _  __                   _
 | |/ / ___  _ __  _ __  (_) _ __    __ _
 | ' / / _ \| '__|| '_ \ | || '_ \  / _` |
 | . \|  __/| |   | | | || || | | || (_| |
 |_|\_\\___||_|   |_| |_||_||_| |_| \__, |
                                    |___/
  _  __                        _
 | |/ /   ___   _ __   _ __   (_)  _ __     __ _
 | ' /   / _ \ | '__| | '_ \  | | | '_ \   / _` |
 | . \  |  __/ | |    | | | | | | | | | | | (_| |
 |_|\_\  \___| |_|    |_| |_| |_| |_| |_|  \__, |
                                           |___/

In most cases you'll either use the default setting or the "fitted" setting. Most fonts don't support vertical kerning, but a hand full of them do (like the "Standard" font).

Metadata

Metadata retrieves a font's parsed header options and comment string.

package main

import (
    "fmt"
    "github.com/damienbutt/figlet"
)

func main() {
    meta, comment, err := figlet.Metadata("Standard")
    if err != nil {
        fmt.Println("something went wrong:", err)
        return
    }

    fmt.Printf("%+v\n", meta)
    fmt.Println(comment)
}
Fonts

Fonts returns a sorted list of all available font names from the embedded font library.

package main

import (
    "fmt"
    "github.com/damienbutt/figlet"
)

func main() {
    fonts, err := figlet.Fonts()
    if err != nil {
        fmt.Println("something went wrong:", err)
        return
    }

    fmt.Println(fonts)
}
LoadedFonts

LoadedFonts returns the names of all fonts currently loaded in the in-memory cache (i.e. already parsed and ready to use).

package main

import (
    "fmt"
    "github.com/damienbutt/figlet"
)

func main() {
    loaded := figlet.LoadedFonts()
    fmt.Println(loaded)
}
ClearLoadedFonts

ClearLoadedFonts resets the in-memory font cache so that no fonts are loaded.

package main

import (
    "fmt"
    "github.com/damienbutt/figlet"
)

func main() {
    figlet.ClearLoadedFonts()
}
ParseFont

ParseFont allows you to load a font from any source — for example, from a file on disk — and register it under a given name for use with Text.

package main

import (
    "fmt"
    "github.com/damienbutt/figlet"
)

func main() {
    data, err := os.ReadFile("myfont.flf")
    if err != nil {
        fmt.Println("something went wrong:", err)
        return
    }

    if _, err := figlet.ParseFont("myfont", string(data)); err != nil {
        fmt.Println("something went wrong:", err)
        return
    }

    text, err := figlet.Text("myfont!", &figlet.FigletOptions{Font: "myfont"})
    if err != nil {
        fmt.Println("something went wrong:", err)
        return
    }

    fmt.Println(text)
}
Defaults

Defaults allows you to read or update the package-level defaults. Pass nil to read without modifying.

package main

import (
    "fmt"
    "github.com/damienbutt/figlet"
)

func main() {
    // Update defaults
    figlet.Defaults(&figlet.FigletDefaults{
        Font:     "Standard",
        FontPath: "some-random-place/fonts",
    })

    // Read current defaults
    d := figlet.Defaults(nil)
    fmt.Println(d.Font)
}

Getting Started - Command Line

Installation
Homebrew (macOS)
brew install damienbutt/tap/figlet
Scoop (Windows)
scoop bucket add damienbutt https://github.com/damienbutt/scoop-bucket
scoop install figlet
Arch Linux (AUR)

Binary package:

paru -S figlet-bin

Or build from source:

paru -S figlet
Go Install
go install github.com/damienbutt/figlet/cmd/figlet@latest
Pre-built Binaries

Download pre-built binaries for your platform from the releases page.

Build from Source
git clone https://github.com/damienbutt/figlet.git
cd figlet
make install
Usage

Once installed, you can run figlet from the command line:

figlet "Hello World!"

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FontFS embed.FS

FontFS is the embedded filesystem containing all bundled .flf font files.

Functions

func ClearLoadedFonts

func ClearLoadedFonts()

ClearLoadedFonts removes all fonts from the cache.

func Fonts

func Fonts() ([]string, error)

Fonts returns a sorted list of all available font names (from embedded FS).

func LoadedFonts

func LoadedFonts() []string

LoadedFonts returns the names of all currently cached fonts.

func PreloadFonts

func PreloadFonts(names []FontName) error

PreloadFonts loads multiple fonts into the cache.

func Text

func Text(text string, opts ...*FigletOptions) (string, error)

Text generates ASCII art from the given text using the provided options. opts is optional; omit it or pass nothing to use the defaults.

Types

type BreakWordResult

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

type FigCharWithOverlap

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

type FigCharsWithOverlap

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

type FigletDefaults

type FigletDefaults struct {
	Font               FontName
	FontPath           string
	FetchFontIfMissing bool
}

FigletDefaults holds the package-level defaults used when no FigletOptions are provided to Text. Modify via Defaults().

func Defaults

func Defaults(opts *FigletDefaults) FigletDefaults

Defaults gets or sets the default options. Pass nil to read without modifying.

type FigletFont

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

FigletFont is the internal representation of a parsed .flf font file.

func NewFigletFont

func NewFigletFont() *FigletFont

NewFigletFont returns an empty FigletFont ready to be populated by ParseFont.

type FigletOptions

type FigletOptions struct {
	Font             FontName
	HorizontalLayout KerningMethods
	VerticalLayout   KerningMethods
	Width            int
	WhitespaceBreak  bool
	PrintDirection   PrintDirection
	ShowHardBlanks   bool
}

FigletOptions controls how a piece of text is rendered by Text. All fields are optional; zero values fall back to font/package defaults.

type FittingProperties

type FittingProperties string

FontName is a string alias for a FIGlet font name. KerningMethods is a string alias for a horizontal kerning/smushing mode. FittingProperties is a string alias for a field name within FittingRules.

const (
	FitHLayout FittingProperties = "hLayout"
	FitHRule1  FittingProperties = "hRule1"
	FitHRule2  FittingProperties = "hRule2"
	FitHRule3  FittingProperties = "hRule3"
	FitHRule4  FittingProperties = "hRule4"
	FitHRule5  FittingProperties = "hRule5"
	FitHRule6  FittingProperties = "hRule6"
	FitVLayout FittingProperties = "vLayout"
	FitVRule1  FittingProperties = "vRule1"
	FitVRule2  FittingProperties = "vRule2"
	FitVRule3  FittingProperties = "vRule3"
	FitVRule4  FittingProperties = "vRule4"
	FitVRule5  FittingProperties = "vRule5"
)

FittingProperties constants name the individual fields of FittingRules.

type FittingRules

type FittingRules struct {
	HLayout int
	HRule1  bool
	HRule2  bool
	HRule3  bool
	HRule4  bool
	HRule5  bool
	HRule6  bool
	VLayout int
	VRule1  bool
	VRule2  bool
	VRule3  bool
	VRule4  bool
	VRule5  bool
}

FittingRules holds the horizontal and vertical layout mode and the individual smushing rule flags decoded from a font's header.

type FontMetadata

type FontMetadata struct {
	Baseline        int
	CodeTagCount    *int
	FittingRules    FittingRules
	FullLayout      *int
	HardBlank       string
	Height          int
	MaxLength       int
	NumCommentLines int
	OldLayout       int
	PrintDirection  PrintDirection
}

FontMetadata contains the parsed header fields of a FIGlet font file (.flf).

func LoadFont

func LoadFont(name string) (*FontMetadata, error)

LoadFont loads a font by name. It first resolves any known aliases via getFontName, then checks the in-memory cache, then tries the embedded FontFS, and finally falls back to the filesystem at figDefaults.FontPath.

func Metadata

func Metadata(name string) (*FontMetadata, string, error)

Metadata returns the metadata and comment string for a named font.

func ParseFont

func ParseFont(name string, data string) (*FontMetadata, error)

ParseFont parses raw .flf font data and registers it into the cache under the given name. If a font with that name is already cached it is overwritten.

type FontName

type FontName string

FontName is a string alias for a FIGlet font name. KerningMethods is a string alias for a horizontal kerning/smushing mode. FittingProperties is a string alias for a field name within FittingRules.

type InternalOptions

type InternalOptions struct {
	FontMetadata
	Width           int
	WhitespaceBreak bool
	ShowHardBlanks  bool
}

type KerningMethods

type KerningMethods string

FontName is a string alias for a FIGlet font name. KerningMethods is a string alias for a horizontal kerning/smushing mode. FittingProperties is a string alias for a field name within FittingRules.

const (
	KerningDefault            KerningMethods = "default"
	KerningFull               KerningMethods = "full"
	KerningFitted             KerningMethods = "fitted"
	KerningControlledSmushing KerningMethods = "controlled smushing"
	KerningUniversalSmushing  KerningMethods = "universal smushing"
)

KerningMethods constants define the supported horizontal kerning/smushing modes.

type PrintDirection

type PrintDirection int

PrintDirection controls the direction in which FIGlet characters are rendered.

const (
	DefaultDirection PrintDirection = iota - 1 // -1: use font header's value
	LeftToRight                                //  0: left to right (FIGlet default)
	RightToLeft                                //  1: right to left
)

Directories

Path Synopsis
cmd
figlet command
internal
version
Package version holds build-time metadata injected via ldflags.
Package version holds build-time metadata injected via ldflags.

Jump to

Keyboard shortcuts

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