golor

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: MIT Imports: 6 Imported by: 2

README

hygge/golor

workflow Go Report Card Go Reference

Turns github.com/fatih/color into HTML. Just kidding!

It is just a very convenient way of dealing with colors in the terminal to get some fancy output.

Just a tag based approach to write some nice text, supporting text/template.

Usage

From simple
golor.Println(`This is a #red[part of text with #bold[some] in bold] word.
Possible to #(blue,italic)[combine]`)

simple

To more interesting
	golor.Println(`#(blue)[Welcome to #(red)[hygge-io/golor]!]
#(green,italic)[{{.FromTemplate}} in italic]
#(cyan,bold)[<some brackets> in bold]
{{- range .Items}}
#(yellow,bold)[{{.}}]{{end}}
Notice the #(white,bold,italic)[trick] to not have lines between the range items!`, map[string]any{
		"FromTemplate": "Hello from template",
		"Items":        []string{"Item 1", "Item 2"},
	})
}

output

Changing the syntax for the style tags!

Depending on the text you want to format, it can be necessary to change how the parsing is done:

s := `In Markdown, @(bold)<a new paragraph> uses the # tag
while links are written as @green<[link](url)>`

renderer := golor.New().WithTagMarker('@').WithTextLimiter('<', '>')
fmt.Println(renderer.Render(s))

output

Documentation

Index

Constants

View Source
const (
	Black = iota
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
)
View Source
const (
	None = iota
	Bold
	Italic
)

Variables

This section is empty.

Functions

func Print

func Print(text string, template ...any)

Print prints the colored to fmt.Printf

func Println

func Println(text string, template ...any)

Println prints the colorized text to fmt.Println

func SameStyle

func SameStyle(a, b *Style) bool

func Sprintf

func Sprintf(text string, template ...any) string

Types

type Color

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

type Renderer

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

func New

func New() *Renderer

func (*Renderer) Println

func (renderer *Renderer) Println(s string)

func (*Renderer) Render

func (renderer *Renderer) Render(text string, template ...any) string

func (*Renderer) Scanner

func (renderer *Renderer) Scanner() *Scanner

func (*Renderer) Sprint

func (renderer *Renderer) Sprint(s string) string

func (*Renderer) WithTagMarker

func (renderer *Renderer) WithTagMarker(m int32) *Renderer

func (*Renderer) WithTextLimiter

func (renderer *Renderer) WithTextLimiter(start, end int32) *Renderer

type Scanner

type Scanner struct {
	TagMarker int32
	Start     int32
	End       int32
}

func NewScanner

func NewScanner() *Scanner

func (*Scanner) Scan

func (s *Scanner) Scan(text string) []Token

func (*Scanner) ScanWithTemplate

func (s *Scanner) ScanWithTemplate(text string, obj any) []Token

type Style

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

func NewStyle

func NewStyle() *Style

func (*Style) Add

func (style *Style) Add(ts ...string)

func (*Style) AddOne

func (style *Style) AddOne(t string)

func (*Style) Clone

func (style *Style) Clone() *Style

func (*Style) Color

func (style *Style) Color(color int) *Style

func (*Style) String

func (style *Style) String() string

func (*Style) Typography

func (style *Style) Typography(t int) *Style

type Theme

type Theme struct {
}

func (*Theme) Color

func (theme *Theme) Color(c *Color) (color.Attribute, bool)

func (*Theme) Convert

func (theme *Theme) Convert(token Token) string

func (*Theme) Produce

func (theme *Theme) Produce(tokens []Token) string

func (*Theme) Typographies

func (theme *Theme) Typographies(typographies []Typography) []color.Attribute

type Token

type Token struct {
	Text  string
	Style *Style
}

type Type

type Type interface {
}

type Typography

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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