color

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: MIT Imports: 6 Imported by: 4

README

color

This package provides support for coloring text with ANSI escape sequences.

Examples

package main

import (
	"encoding/json"
	"fmt"
	
	"github.com/mithrandie/go-text/color"
)

const (
	BlueColor   = "blue"
	YellowColor = "yellow"
)

type Config struct {
	Palette              color.PaletteConfig `json:"palette"`
}

var jsonConfig = `
{
  "palette": {
    "effectors": {
      "color1": {
        "effects": [
          "Bold"
        ],
        "foreground": "Blue",
        "background": null
      },
      "color2": {
        "effects": [],
        "foreground": "Magenta",
        "background": null
      }
    }
  }
}
`

func main() {
	message := "message"
	
	// Use JSON Configuration 
	conf := &Config{} 
	if err := json.Unmarshal([]byte(jsonConfig), conf); err != nil {
		panic(err)
	}
	
	palette, err := color.GeneratePalette(conf.Palette)
	if err != nil {
		panic(err)
	}
	
	fmt.Println(palette.Render("color1", message))

	// Use Effector
	e := color.NewEffector()
	e.SetFGColor(color.Red)
	e.SetEffect(color.Bold, color.Italic)
	
	fmt.Println(e.Render(message))
	
	// Use Palette that bundles multiple effectors.
	blue := color.NewEffector()
	blue.SetFGColor(color.Blue)
	yellow := color.NewEffector()
	yellow.SetFGColor(color.Blue)
	
	palette.SetEffector(BlueColor, blue)
	palette.SetEffector(YellowColor, yellow)
	
	fmt.Println(palette.Render(BlueColor, message))
}

Documentation

Overview

Package color is a Go library to render ANSI escape color sequences.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error

func Error(message string) string

func Info

func Info(message string) string

func Notice

func Notice(message string) string

func Warn

func Warn(message string) string

Types

type Code

type Code int
const (
	Black Code = iota + 30
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
)
const (
	BrightBlack Code = iota + 90
	BrightRed
	BrightGreen
	BrightYellow
	BrightBlue
	BrightMagenta
	BrightCyan
	BrightWhite
)
const DefaultColor Code = 39

func ParseColorCode

func ParseColorCode(s string) (Code, error)

func (Code) String

func (c Code) String() string

type EffectCode

type EffectCode int
const (
	Reset EffectCode = iota
	Bold
	Faint
	Italic
	Underline
	SlowBlink
	RapidBlink
	ReverseVideo
	Conceal
	CrossedOut
)

func ParseEffectCode

func ParseEffectCode(s string) (EffectCode, error)

func (EffectCode) String

func (c EffectCode) String() string

type Effector

type Effector struct {
	Enclose bool
	// contains filtered or unexported fields
}

func GenerateEffector

func GenerateEffector(config EffectorConfig) (*Effector, error)

func NewEffector

func NewEffector() *Effector

func (*Effector) ExportConfig

func (e *Effector) ExportConfig() EffectorConfig

func (*Effector) Render

func (e *Effector) Render(s string) string

func (*Effector) SetBG256Color

func (e *Effector) SetBG256Color(color int)

func (*Effector) SetBGColor

func (e *Effector) SetBGColor(color Code)

func (*Effector) SetBGRGBColor

func (e *Effector) SetBGRGBColor(red int, green int, blue int)

func (*Effector) SetEffect

func (e *Effector) SetEffect(code ...EffectCode)

func (*Effector) SetFG256Color

func (e *Effector) SetFG256Color(color int)

func (*Effector) SetFGColor

func (e *Effector) SetFGColor(color Code)

func (*Effector) SetFGRGBColor

func (e *Effector) SetFGRGBColor(red int, green int, blue int)

type EffectorConfig

type EffectorConfig struct {
	Effects    []string    `json:"effects"`
	Foreground interface{} `json:"foreground"`
	Background interface{} `json:"background"`
}

type Palette

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

func GeneratePalette

func GeneratePalette(config PaletteConfig) (*Palette, error)

func NewPalette

func NewPalette() *Palette

func (*Palette) Disable

func (p *Palette) Disable()

func (*Palette) Enable

func (p *Palette) Enable()

func (*Palette) ExportConfig

func (p *Palette) ExportConfig() PaletteConfig

func (*Palette) Merge

func (p *Palette) Merge(p2 *Palette)

func (*Palette) Render

func (p *Palette) Render(key string, text string) string

func (*Palette) SetEffector

func (p *Palette) SetEffector(key string, effector *Effector)

type PaletteConfig

type PaletteConfig struct {
	Effectors map[string]EffectorConfig `json:"effectors"`
}

Jump to

Keyboard shortcuts

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