pterm

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2020 License: MIT Imports: 3 Imported by: 2,510

README

:computer: PTerm | Pretty Terminal

A golang module to print pretty text

Latest Release Stars Forks Issues
Downloads
PTerm Documentation Dops


Examples

demo

Animation

package main

import (
	"time"

	"github.com/pterm/pterm"
)

func main() {

	pterm.PrintHeader("You can do many things with PTerm")

	time.Sleep(time.Second * 3)

	pterm.Println(pterm.Cyan("Like this header above!"))

	time.Sleep(time.Second * 2)

	clear()

	pterm.PrintSuccess("You can print success messages!")

	time.Sleep(time.Second * 2)

	pterm.PrintInfo("Or infos!")

	time.Sleep(time.Second)

	pterm.PrintError("Or errors!")

	time.Sleep(time.Second)

	pterm.PrintWarning("Or warnings!")

	time.Sleep(time.Second)

	pterm.PrintDescription("Even descriptions can be printed...")

	time.Sleep(time.Second * 2)

	customPrefixPrinter := pterm.PrefixPrinter{
		Prefix: pterm.Prefix{
			Text:  "CUSTOM",
			Style: []pterm.Color{pterm.FgLightRed, pterm.BgBlue},
		},
	}

	customPrefixPrinter.Println("Or a custom PrefixPrinter can be crafted :)")

	time.Sleep(time.Second * 2)

	pterm.WarningPrinter.WithScope("custom-scope").Println("PrefixPrinters also support scopes!")

	time.Sleep(time.Second * 4)

	clear()

	pterm.PrintHeader("Everything can be customized!")

	time.Sleep(time.Second * 2)

	headerStyles := []pterm.Style{
		{pterm.BgGreen},
		{pterm.BgWhite},
		{pterm.BgRed},
		{pterm.BgBlue},
		{pterm.BgYellow},
		{pterm.BgLightMagenta},
	}

	for _, style := range headerStyles {
		clear()
		pterm.HeaderPrinter{Header: pterm.Header{
			BackgroundStyle: style,
			TextStyle:       pterm.Style{pterm.FgLightWhite},
			Margin:          5,
		}}.Println("Everything can be customized")
		time.Sleep(time.Second / 2)
	}

	for i := 0; i < 10; i++ {
		clear()
		style := headerStyles[len(headerStyles)-1]
		pterm.HeaderPrinter{Header: pterm.Header{
			BackgroundStyle: style,
			TextStyle:       pterm.Style{pterm.FgLightWhite},
			Margin:          5 + i,
		}}.Println("Everything can be customized")
		time.Sleep(time.Millisecond * 100)
	}

	for i := 0; i < 15; i++ {
		clear()
		style := headerStyles[len(headerStyles)-1]
		pterm.HeaderPrinter{Header: pterm.Header{
			BackgroundStyle: style,
			TextStyle:       pterm.Style{pterm.FgLightWhite},
			Margin:          15 - i,
		}}.Println("Everything can be customized")
		time.Sleep(time.Millisecond * 100)
	}

}

func clear() {
	print("\033[H\033[2J")
}

override-default-printer

Animation

package main

import "github.com/pterm/pterm"

func main() {

	pterm.ErrorPrinter.Println("This is the default ErrorPrinter")

	pterm.ErrorPrinter.Prefix = pterm.Prefix{
		Text:  "OVERRIDE",
		Style: pterm.Style{pterm.BgCyan, pterm.FgRed},
	}

	pterm.ErrorPrinter.Println("This is the default ErrorPrinter after the prefix was overridden")
}

print-header

Animation

package main

import (
	"time"

	"github.com/pterm/pterm"
)

func main() {
	tick := time.Tick(time.Second * 2)

	// Print with the default HeaderPrinter
	pterm.PrintHeader("This is the default header style")

	<-tick // Wait

	// Create a custom HeaderPrinter
	customHeaderPrinter := pterm.HeaderPrinter{Header: pterm.Header{
		TextStyle:       pterm.Style{pterm.FgLightRed},
		BackgroundStyle: pterm.Style{pterm.BgGreen},
		Margin:          15,
	}}
	// Use custom Header printer
	customHeaderPrinter.Println("This is a custom header.")
}

print-with-color

Animation

package main

import "github.com/pterm/pterm"

func main() {
	// Simple Println with different colored words.
	pterm.Println(pterm.Red("Hello, ") + pterm.Green("World") + pterm.Cyan("!"))
}

Documentation

Index

Constants

View Source
const Version = "v0.0.1" // <---VERSION---> | This comment is for our CI System. Do not edit it.

Version returns the current version of PTerm in SemVer format.

Variables

View Source
var (
	// Sprint formats using the default formats for its operands and returns the resulting string.
	// Spaces are added between operands when neither is a string.
	Sprint = color.Sprint

	// Sprintf formats according to a format specifier and returns the resulting string.
	Sprintf = color.Sprintf

	// Println formats using the default formats for its operands and writes to standard output.
	// Spaces are always added between operands and a newline is appended.
	// It returns the number of bytes written and any write error encountered.
	Println = color.Println

	// Printf formats according to a format specifier and writes to standard output.
	// It returns the number of bytes written and any write error encountered.
	Printf = color.Printf

	// Print formats using the default formats for its operands and writes to standard output.
	// Spaces are added between operands when neither is a string.
	// It returns the number of bytes written and any write error encountered.
	Print = color.Print
)
View Source
var (
	// Red is an alias for FgRed.Sprint
	Red = FgRed.Sprint
	// Cyan is an alias for FgCyan.Sprint
	Cyan = FgCyan.Sprint
	// Gray is an alias for FgGray.Sprint
	Gray = FgGray.Sprint
	// Blue is an alias for FgBlue.Sprint
	Blue = FgBlue.Sprint
	// Black is an alias for FgBlack.Sprint
	Black = FgBlack.Sprint
	// Green is an alias for FgGreen.Sprint
	Green = FgGreen.Sprint
	// White is an alias for FgWhite.Sprint
	White = FgWhite.Sprint
	// Yellow is an alias for FgYellow.Sprint
	Yellow = FgYellow.Sprint
	// Magenta is an alias for FgMagenta.Sprint
	Magenta = FgMagenta.Sprint

	// Normal is an alias for FgDefault.Sprint
	Normal = FgDefault.Sprint

	// LightRed is a shortcut for FgLightRed.Sprint
	LightRed = FgLightRed.Sprint
	// LightCyan is a shortcut for FgLightCyan.Sprint
	LightCyan = FgLightCyan.Sprint
	// LightBlue is a shortcut for FgLightBlue.Sprint
	LightBlue = FgLightBlue.Sprint
	// LightGreen is a shortcut for FgLightGreen.Sprint
	LightGreen = FgLightGreen.Sprint
	// LightWhite is a shortcut for FgLightWhite.Sprint
	LightWhite = FgLightWhite.Sprint
	// LightYellow is a shortcut for FgLightYellow.Sprint
	LightYellow = FgLightYellow.Sprint
	// LightMagenta is a shortcut for FgLightMagenta.Sprint
	LightMagenta = FgLightMagenta.Sprint
)
View Source
var (
	// DefaultHeaderPrinter returns the printer for a default header text.
	// Defaults to LightWhite, Bold Text and a Gray Header background.
	DefaultHeaderPrinter = HeaderPrinter{Header: Header{
		TextStyle:       Style{FgLightWhite, Bold},
		BackgroundStyle: Style{BgGray},
		Margin:          5,
	}}

	// PrintHeader is the short form of DefaultHeaderPrinter.Println
	PrintHeader = DefaultHeaderPrinter.Println
)
View Source
var (
	// InfoPrinter returns a PrefixPrinter, which can be used to print text with an "info" Prefix
	InfoPrinter = PrefixPrinter{
		Prefix: Prefix{
			Text:  "INFO",
			Style: NewStyle(FgLightWhite, BgCyan),
		},
		MessageStyle: NewStyle(FgLightCyan),
	}
	// PrintInfo is a shortcut to InfoPrinter.Println
	PrintInfo = InfoPrinter.Println

	// WarningPrinter returns a PrefixPrinter, which can be used to print text with a "warning" Prefix
	WarningPrinter = PrefixPrinter{
		Prefix: Prefix{
			Text:  "WARNING",
			Style: NewStyle(FgLightWhite, BgYellow),
		},
		MessageStyle: NewStyle(FgYellow),
	}
	// PrintWarning is a shortcut to WarningPrinter.Println
	PrintWarning = WarningPrinter.Println

	// SuccessPrinter returns a PrefixPrinter, which can be used to print text with a "success" Prefix
	SuccessPrinter = PrefixPrinter{
		Prefix: Prefix{
			Text:  "SUCCESS",
			Style: NewStyle(FgLightWhite, BgGreen),
		},
		MessageStyle: NewStyle(FgGreen),
	}
	// PrintSuccess is a shortcut to SuccessPrinter.Println
	PrintSuccess = SuccessPrinter.Println

	// ErrorPrinter returns a PrefixPrinter, which can be used to print text with an "error" Prefix
	ErrorPrinter = PrefixPrinter{
		Prefix: Prefix{
			Text:  "ERROR",
			Style: NewStyle(FgLightWhite, BgLightRed),
		},
		MessageStyle: NewStyle(FgLightRed),
	}
	// PrintError is a shortcut to ErrorPrinter.Println
	PrintError = ErrorPrinter.Println

	// DescriptionPrinter returns a PrefixPrinter, which can be used to print text with a "description" Prefix
	DescriptionPrinter = PrefixPrinter{
		Prefix: Prefix{
			Text:  "Description",
			Style: Style{BgDarkGray, FgLightWhite},
		},
		MessageStyle: Style{BgDarkGray, FgLightWhite},
	}
	// PrintDescription is a shortcut to DescriptionPrinter.Println
	PrintDescription = DescriptionPrinter.Println
)
View Source
var (
	// GrayBoxStyle wraps text in a gray box
	GrayBoxStyle = NewStyle(BgGray, FgLightWhite)
)

Functions

This section is empty.

Types

type Color

type Color uint8

Color is a number which will be used to color strings in the terminal

const (
	FgBlack Color = iota + 30
	FgRed
	FgGreen
	FgYellow
	FgBlue
	FgMagenta
	FgCyan
	FgWhite
	// FgDefault revert default FG
	FgDefault Color = 39
)

Foreground colors. basic foreground colors 30 - 37

const (
	FgDarkGray Color = iota + 90 // 亮黑(灰)
	FgLightRed
	FgLightGreen
	FgLightYellow
	FgLightBlue
	FgLightMagenta
	FgLightCyan
	FgLightWhite
	// FgGray is an alias of FgDarkGray
	FgGray Color = 90
)

Extra foreground color 90 - 97(非标准)

const (
	BgBlack Color = iota + 40
	BgRed
	BgGreen
	BgYellow // BgBrown like yellow
	BgBlue
	BgMagenta
	BgCyan
	BgWhite
	// BgDefault reverts to the default background
	BgDefault Color = 49
)

Background colors. basic background colors 40 - 47

const (
	BgDarkGray Color = iota + 100
	BgLightRed
	BgLightGreen
	BgLightYellow
	BgLightBlue
	BgLightMagenta
	BgLightCyan
	BgLightWhite
	// BgGray is an alias of BgDarkGray
	BgGray Color = 100
)

Extra background color 100 - 107(非标准)

const (
	Reset Color = iota
	Bold
	Fuzzy
	Italic
	Underscore
	Blink
	FastBlink
	Reverse
	Concealed
	Strikethrough
)

Option settings

func (Color) Print

func (c Color) Print(a ...interface{})

Print formats using the default formats for its operands and writes to standard output. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered. Input will be colored with the parent Color

func (Color) Printf

func (c Color) Printf(format string, a ...interface{})

Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered. Input will be colored with the parent Color

func (Color) Println

func (c Color) Println(a ...interface{})

Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered. Input will be colored with the parent Color

func (Color) Sprint

func (c Color) Sprint(a ...interface{}) string

Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string. Input will be colored with the parent Color

func (Color) Sprintf

func (c Color) Sprintf(format string, a ...interface{}) string

Sprintf formats according to a format specifier and returns the resulting string. Input will be colored with the parent Color

func (Color) Sprintln

func (c Color) Sprintln(a ...interface{}) string

Sprintln formats using the default formats for its operands and returns the resulting string. Spaces are always added between operands and a newline is appended. Input will be colored with the parent Color

func (Color) String

func (c Color) String() string

String converts the color to a string. eg "35"

type GenericPrinter

type GenericPrinter interface {
	// Sprint formats using the default formats for its operands and returns the resulting string.
	// Spaces are added between operands when neither is a string.
	Sprint(a ...interface{}) string

	// Sprintln formats using the default formats for its operands and returns the resulting string.
	// Spaces are always added between operands and a newline is appended.
	Sprintln(a ...interface{}) string

	// Sprintf formats according to a format specifier and returns the resulting string.
	Sprintf(format string, a ...interface{}) string

	// Print formats using the default formats for its operands and writes to standard output.
	// Spaces are added between operands when neither is a string.
	// It returns the number of bytes written and any write error encountered.
	Print(a ...interface{}) GenericPrinter

	// Println formats using the default formats for its operands and writes to standard output.
	// Spaces are always added between operands and a newline is appended.
	// It returns the number of bytes written and any write error encountered.
	Println(a ...interface{}) GenericPrinter

	// Printf formats according to a format specifier and writes to standard output.
	// It returns the number of bytes written and any write error encountered.
	Printf(format string, a ...interface{}) GenericPrinter
}

GenericPrinter contains methods to print formatted text to the console or return it as a string

type Header struct {
	TextStyle       Style
	BackgroundStyle Style
	Margin          int
}

Header contains the data used to craft a header. A Header is printed as a big box with text in it. Can be used as title screens or section separator.

type HeaderPrinter added in v0.0.1

type HeaderPrinter struct {
	Header Header
}

HeaderPrinter is the printer used to print a Header.

func (HeaderPrinter) Print added in v0.0.1

func (p HeaderPrinter) Print(a ...interface{}) GenericPrinter

Print formats using the default formats for its operands and writes to standard output. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.

func (HeaderPrinter) Printf added in v0.0.1

func (p HeaderPrinter) Printf(format string, a ...interface{}) GenericPrinter

Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered.

func (HeaderPrinter) Println added in v0.0.1

func (p HeaderPrinter) Println(a ...interface{}) GenericPrinter

Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.

func (HeaderPrinter) Sprint added in v0.0.1

func (p HeaderPrinter) Sprint(a ...interface{}) string

Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.

func (HeaderPrinter) Sprintf added in v0.0.1

func (p HeaderPrinter) Sprintf(format string, a ...interface{}) string

Sprintf formats according to a format specifier and returns the resulting string.

func (HeaderPrinter) Sprintln added in v0.0.1

func (p HeaderPrinter) Sprintln(a ...interface{}) string

Sprintln formats using the default formats for its operands and returns the resulting string. Spaces are always added between operands and a newline is appended.

type Prefix

type Prefix struct {
	Text  string
	Style Style
}

Prefix contains the data used as the beginning of a printed text via a PrefixPrinter

type PrefixPrinter

type PrefixPrinter struct {
	Prefix       Prefix
	Scope        Scope
	MessageStyle Style
}

PrefixPrinter is the printer used to print a Prefix

func (PrefixPrinter) GetFormattedMessage

func (p PrefixPrinter) GetFormattedMessage(a ...interface{}) string

GetFormattedMessage returns the message as a styled text string

func (PrefixPrinter) GetFormattedPrefix

func (p PrefixPrinter) GetFormattedPrefix() string

GetFormattedPrefix returns the Prefix as a styled text string

func (PrefixPrinter) Print

func (p PrefixPrinter) Print(a ...interface{}) GenericPrinter

Print formats using the default formats for its operands and writes to standard output. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.

func (PrefixPrinter) Printf

func (p PrefixPrinter) Printf(format string, a ...interface{}) GenericPrinter

Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered.

func (PrefixPrinter) Println

func (p PrefixPrinter) Println(a ...interface{}) GenericPrinter

Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.

func (PrefixPrinter) Sprint

func (p PrefixPrinter) Sprint(a ...interface{}) string

Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.

func (PrefixPrinter) Sprintf

func (p PrefixPrinter) Sprintf(format string, a ...interface{}) string

Sprintf formats according to a format specifier and returns the resulting string.

func (PrefixPrinter) Sprintln

func (p PrefixPrinter) Sprintln(a ...interface{}) string

Sprintln formats using the default formats for its operands and returns the resulting string. Spaces are always added between operands and a newline is appended.

func (PrefixPrinter) WithScope

func (p PrefixPrinter) WithScope(scope string, style ...Style) *PrefixPrinter

WithScope adds a scope to the Prefix

type Scope

type Scope struct {
	Text  string
	Style Style
}

Scope contains the data of the optional scope of a prefix. If it has a text, it will be printed after the Prefix in brackets.

type Style

type Style []Color

Style is a collection of colors. Can include foreground, background and styling (eg. Bold, Underscore, etc.) colors.

func NewStyle added in v0.0.1

func NewStyle(colors ...Color) Style

NewStyle returns a new Style. Accepts multiple colors.

func (Style) Code

func (s Style) Code() string

Code convert to code string. returns like "32;45;3"

func (Style) Print

func (s Style) Print(a ...interface{})

Print formats using the default formats for its operands and writes to standard output. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered. Input will be colored with the parent Style

func (Style) Printf

func (s Style) Printf(format string, a ...interface{})

Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered. Input will be colored with the parent Style

func (Style) Println

func (s Style) Println(a ...interface{})

Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered. Input will be colored with the parent Style

func (Style) Sprint

func (s Style) Sprint(a ...interface{}) string

Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string. Input will be colored with the parent Style

func (Style) Sprintf

func (s Style) Sprintf(format string, a ...interface{}) string

Sprintf formats according to a format specifier and returns the resulting string. Input will be colored with the parent Style

func (Style) Sprintln

func (s Style) Sprintln(a ...interface{}) string

Sprintln formats using the default formats for its operands and returns the resulting string. Spaces are always added between operands and a newline is appended. Input will be colored with the parent Style

func (Style) String

func (s Style) String() string

String convert to code string. returns like "32;45;3"

Directories

Path Synopsis
_examples
demo command
print-header command

Jump to

Keyboard shortcuts

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