console

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2025 License: ISC Imports: 2 Imported by: 3

README

Colorize Console

A token based (@Style{msg}) printer library for styling and formatting console output with ANSI escape codes. It supports rich formatting options such as text styles, colors, and backgrounds to make your terminal output more expressive and readable.

Features

  • Text Styling: Bold, Underline, Italic, and Strike-through.
  • Text Colors: Red, Green, Yellow, Blue, Purple, Cyan, White.
  • Background Colors: Red, Green, Yellow, Blue, Purple, Cyan, White.
  • Chainable Message API for complex message composition.

Installation

Add the library to your Go project using:

go get github.com/go-universal/console

Usage

PrintF

The PrintF function allows you to apply inline styles and colors directly in your formatted strings. Use \@ to escape tokens in your patterns.

PrintF(format string, args ...any)

// Usage
PrintF("@Br{Bold Red Text} and @gb{Green Background %s}\n", "message")
Parameters
  • format: A string containing golang standart print pattern with text and styled tokens.
  • args: Additional arguments to replace format specifiers in the string.
Supported Tokens
  • Styles (Uppercase):
    • B: Bold
    • U: Underline
    • I: Italic
    • S: Strike-through
  • Text Colors (Lowercase):
    • r: Red
    • g: Green
    • y: Yellow
    • b: Blue
    • p: Purple
    • c: Cyan
    • w: White
  • Background Colors (Lowercase):
    • rb: Red
    • gb: Green
    • yb: Yellow
    • bb: Blue
    • pb: Purple
    • cb: Cyan
    • wb: White
Message

The Message simplifies creating and styling complex messages with a title, tags, and formatted content.

Methods
  • Indent(): Indents the message. For make long indent you can call this method multiple times.
  • Red(title string): Makes the message title red.
  • Green(title string): Makes the message title green.
  • Yellow(title string): Makes the message title yellow.
  • Blue(title string): Makes the message title blue.
  • Purple(title string): Makes the message title purple.
  • Cyan(title string): Makes the message title cyan.
  • Underline(): Adds underline style to the message body.
  • Strike(): Adds a strike-through style to the message body.
  • Italic(): Adds italic style to the message body.
  • Tags(tags ...string): Adds tags to the message.
  • Print(pattern string, args ...any): Prints the formatted message.
Message().
    Red("Error").
    Italic().
    Tags("CRITICAL", "SYSTEM").
    Print("System encountered an unexpected error: %s", "Disk full")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrintF

func PrintF(format string, args ...any)

Printf applies ANSI escape codes to a given pattern string and prints the formatted string with the provided arguments. The pattern string can contain tags followed by content token for various styles and colors, which will be replaced by the corresponding ANSI codes. You can scape token with \@.

Supported styles: B: BOLD. U: UNDERLINE. S: STRIKE. I: ITALIC.

Supported backgrounds: rb: RED. gb: GREEN. yb: YELLOW. bb: BLUE. pb: PURPLE. cb: CYAN. wb: WHITE.

Supported colors: r: RED. g: GREEN. y: YELLOW. b: BLUE. p: PURPLE. c: CYAN. w: WHITE.

Arguments: - format: The string containing the standard go fmt format with styled tokens. - args: The arguments to be passed into the format string.

code block:

PrintF("@Bg{Bold Green Text} and @rb{Red %s}\n", "message")

Types

type MessagePrinter

type MessagePrinter interface {
	// Indent add indent to message.
	Indent() MessagePrinter

	// Red make message title red.
	Red(title string) MessagePrinter

	// Green make message title green.
	Green(title string) MessagePrinter

	// Yellow make message title yellow.
	Yellow(title string) MessagePrinter

	// Blue make message title blue.
	Blue(title string) MessagePrinter

	// Purple make message title purple.
	Purple(title string) MessagePrinter

	// Cyan make message title cyan.
	Cyan(title string) MessagePrinter

	// Underline add underline style to message details.
	Underline() MessagePrinter

	// Strike add strike style to message details.
	Strike() MessagePrinter

	// Italic add italic style to message details.
	Italic() MessagePrinter

	// Tags add tags to message.
	Tags(tags ...string) MessagePrinter

	// Print print message
	Print(message string)

	// Printf print message with fmt style
	Printf(pattern string, args ...any)
}

MessagePrinter print a colored message (Title: [tags] message).

func Message

func Message() MessagePrinter

Jump to

Keyboard shortcuts

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