tout

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: MIT Imports: 8 Imported by: 0

README

tout

tout is a Go package that provides utilities for styled terminal output.

Features

  • Text Coloring: Apply ANSI color codes to text for enhanced readability and visual appeal.
  • Cursor Management: Manage terminal cursor settings and operations.
  • Font Styling: Set and modify font styles for terminal text.
  • Error Handling: Separate cursors and writers for standard and error output.

Installation

To install the tout package, use the following command:

go get github.com/pachecot/tout

Usage

Basic Example
package main

import (
    "github.com/pachecot/tout"
    "github.com/pachecot/tout/color16"
)

func main() {
    tout.Foreground(color16.Red)
    tout.SetFont(tout.BoldFont | tout.UnderlineFont)
    tout.Println("This is bold red underlined text")
    tout.ResetForeground()
    tout.ResetFont()
    tout.Println("This is default text")
}
Setting Colors
import "github.com/pachecot/tout/color24"

tout.Foreground(color24.Magenta)
tout.Println("This is magenta text")

tout.Foreground(color24.RGB(128,128,128))
tout.Println("This is grey text")

tout.Foreground(color24.Hex("#00ffd7"))
tout.Println("This is bright teal text")
Using a Cursor
import "github.com/pachecot/tout/color24"

    c := tout.NewCursor(os.Stdout)
    c.Foreground(color24.ColdCanada)
    c.Println("ColdCanada")
    c.Font |= tout.BoldFont | tout.UnderlineFont | tout.ReversedFont
    c.Background(color24.OrangeBrown)
    c.Println("Reversed ColdCanada & OrangeBrown")
Error Output
tout.Errorln("This is an error message")

Documentation

For detailed documentation, please refer to the GoDoc.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Overview

Package tout provides utilities for terminal output.

The tout package includes various functionalities to enhance terminal output, such as text coloring using ANSI color codes. It defines the Term type, which can be used to manage terminal-related operations and settings.

The package includes a default Cursor that is used by helper functions to set the text colors and other display attributes.

example:

package main

import (
	"github.com/pachecot/tout"
)

func main() {
	tout.Println("Hello, World!")
}

The example above prints "Hello, World!" to the terminal using the default Cursor settings.

The tout package also provides functions to set the text color, background color, and font style of the text. These functions can be used to customize the appearance of the text output.

example:

package main

import (
	"github.com/pachecot/tout"
	"github.com/pachecot/tout/color24"
)

func main() {
	tout.Foreground(color24.Blue)
	tout.Println("This is blue text")
}

The example above sets the text color to blue and prints "This is blue text" to the terminal using the default Cursor settings.

The tout package also provides functions to set the font style of the text. These functions can be used to apply bold, italic, underline, and other font styles to the text output.

example:

package main

import (
	"github.com/pachecot/tout"
)

func main() {
	tout.SetFont(tout.BoldFont | tout.UnderlineFont)
	tout.Println("This is bold underlined text")
}

The example above sets the font style to bold and underline and prints "This is bold underlined text" to the terminal using the default Cursor settings.

Index

Constants

View Source
const (
	ESC = "\x1b"    // Escape character
	CSI = ESC + "[" // Control Sequence Introducer

)

ANSI escape codes and sequences.

Variables

This section is empty.

Functions

func AddStyle

func AddStyle(f FontType)

AddStyle adds a style to the current font type of the default cursor.

func Background

func Background(c Color)

Background sets the background color of the default cursor.

func DefaultBackground

func DefaultBackground()

DefaultBackground sets the background color of the default cursor to the default value.

func DefaultForeground

func DefaultForeground()

DefaultForeground sets the foreground color of the default cursor to the default value.

func Error

func Error(a ...any)

Error prints the given arguments to the error cursor's writer.

func ErrorWriter

func ErrorWriter() io.Writer

ErrorWriter returns the writer associated with the error cursor.

func Errorf

func Errorf(format string, a ...any)

Errorf formats according to a format specifier and writes to the error cursor's writer.

func Errorln

func Errorln(a ...any)

Errorln prints the given arguments to the error cursor's writer with a newline.

func Foreground

func Foreground(c Color)

Foreground sets the foreground color of the default cursor.

func Print

func Print(a ...any)

Print prints the given arguments to the default cursor's writer.

func Printf

func Printf(format string, a ...any)

Printf formats according to a format specifier and writes to the default cursor's writer.

func Println

func Println(a ...any)

Println prints the given arguments to the default cursor's writer with a newline.

func RemoveStyle

func RemoveStyle(f FontType)

RemoveStyle removes a style from the current font type of the default cursor.

func ResetBackground

func ResetBackground()

ResetBackground resets the background color of the default cursor to the default value.

func ResetFont

func ResetFont()

ResetFont resets the font type of the default cursor to the default value.

func ResetForeground

func ResetForeground()

ResetForeground resets the foreground color of the default cursor to the default value.

func SetDefaultCursor

func SetDefaultCursor(c *Cursor)

SetDefaultCursor sets the default cursor for terminal output.

func SetErrorCursor

func SetErrorCursor(c *Cursor)

SetErrorCursor sets the cursor for error output.

func SetFont

func SetFont(f FontType)

SetFont sets the font type for the default cursor.

func Write

func Write[T string | []byte](t T)

Write writes the given string or byte slice to the default cursor's writer.

func Writer

func Writer() io.Writer

Writer returns the writer associated with the default cursor.

Types

type Color

type Color interface {
	Foreground() string
	Background() string
}

Color interface represents a color that can provide ANSI escape codes for foreground and background colors.

type Cursor

type Cursor struct {
	Font  FontType
	Level supportscolor.ColorLevel
	// contains filtered or unexported fields
}

Cursor represents a terminal cursor with associated writer, colors, and font styles.

func DefaultCursor

func DefaultCursor() *Cursor

DefaultCursor returns sets the default cursor for terminal output.

func ErrorCursor

func ErrorCursor() *Cursor

ErrorCursor returns the cursor used for error output.

func NewCursor

func NewCursor(w io.Writer) *Cursor

NewCursor creates a new Cursor with the specified writer. It detects the color support level of the writer if it is a file.

If the writer is nil, it uses os.Stdout as the default writer.

func (*Cursor) Background

func (c *Cursor) Background(k Color)

Background sets the background color of the Cursor. If the color level is not supported, it resets the background color.

func (*Cursor) DefaultBackground

func (c *Cursor) DefaultBackground()

DefaultBackground sets the background color of the Cursor to the default value.

func (*Cursor) DefaultForeground

func (c *Cursor) DefaultForeground()

DefaultForeground sets the foreground color of the Cursor to the default value.

func (*Cursor) Foreground

func (c *Cursor) Foreground(k Color)

Foreground sets the foreground color of the Cursor. If the color level is not supported, it resets the foreground color.

func (*Cursor) Print

func (c *Cursor) Print(a ...any)

Print prints the given arguments to the Cursor's writer.

func (*Cursor) Printf

func (c *Cursor) Printf(format string, a ...any)

Printf formats according to a format specifier and writes to the Cursor's writer.

func (*Cursor) Println

func (c *Cursor) Println(a ...any)

Println prints the given arguments to the Cursor's writer with a newline. The newline is not styled.

func (*Cursor) ResetBackground

func (c *Cursor) ResetBackground()

ResetBackground resets the background color of the Cursor.

func (*Cursor) ResetForeground

func (c *Cursor) ResetForeground()

ResetForeground resets the foreground color of the Cursor.

func (*Cursor) SetWriter

func (c *Cursor) SetWriter(w io.Writer)

SetWriter sets the writer for the Cursor and updates the color support level.

func (*Cursor) Writer

func (c *Cursor) Writer() io.Writer

Writer returns the writer associated with the Cursor.

type FontType

type FontType int

FontType represents different font styles that can be applied to terminal text.

const (
	BoldFont      FontType = 1 << iota // Bold font style
	LightFont                          // Light font style
	ItalicFont                         // Italic font style
	UnderlineFont                      // Underline font style
	BlinkFont                          // Blink font style
	ReversedFont                       // Reversed font style
	CrossOutFont                       // Crossed-out font style
)

Font style constants.

func Font

func Font() FontType

Font returns the current font type of the default cursor.

func (FontType) Has

func (f FontType) Has(s FontType) bool

Has checks if the FontType includes a specific style.

Directories

Path Synopsis
Package color16 provides a set of basic 16-color ANSI color definitions.
Package color16 provides a set of basic 16-color ANSI color definitions.
Package color24 provides a comprehensive set of 24-bit color definitions.
Package color24 provides a comprehensive set of 24-bit color definitions.
Package color256 provides a set of 256-color ANSI color definitions.
Package color256 provides a set of 256-color ANSI color definitions.

Jump to

Keyboard shortcuts

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