inStyle is a small library for quickly decorating strings with ANSI escape codes.
Run it once:
go run github.com/zphia/instyle/cmd/...@latest '[~italic]you can [~cyan]style[/] text with [~bold+magenta]inStyle[/]!!!'Or install it as a tool:
go install github.com/zphia/instyle/cmd/...@latestThis can be used as a library for any Go project:
package main
import (
"fmt"
"github.com/zphia/instyle"
)
func main() {
fmt.Println(instyle.Apply("[~italic]you can [~cyan]style[/] text with [~bold+magenta]inStyle[/]!!!"))
}The tags follow the following format:
[~style]text to be styled[/]
Style can be a named style, or a raw value to be used in an ANSI escape code. For example, both of these will turn the text red:
[~red]this text will show up as red[/]
[~31]this text will show up as red[/]
The ending sequence of [/] can be fully omitted for minor performance gains like so:
[~italic]ending tags need not be included
Hex colour codes are supported in standard formats:
[~#FF4137]this text will be red[/]
[~#2e6]and this will be green[/]
RGB colours can also be provided.
Spaces and values above 255 will not be understood as an RGB colour.
[~rgb(17,99,240)]a very blue message[/]
Multiple styles can be added by using the + character between each style desired.
[~magenta+bold]this text has two styles[/]
Up to 5 tags can be nested. All unclosed tags are terminated at the end of a string.
[~cyan]i never said [~bold]you[/] did that[/]... [~italic]somebody else[/] did
Complete list of default styles.
plainresetboldfaintitalicunderlineblinkstrike
blackredgreenyellowbluemagentacyanwhitedefault
bg-blackbg-redbg-greenbg-yellowbg-bluebg-magentabg-cyanbg-whitebg-default
light-blacklight-redlight-greenlight-yellowlight-bluelight-magentalight-cyanlight-white
bg-light-blackbg-light-redbg-light-greenbg-light-yellowbg-light-bluebg-light-magentabg-light-cyanbg-light-white
Aside from the named styles, additional styles can be added to a Styler instance by using the Register method.
This can be used to associated more than one ANSI escape code to a name.
s := instyle.NewStyler()
s.Register("error", "1;31") // Bold and redA style name can only be a maximum of 15 characters long.
The goal for this library was to be the fastest way to apply styles to strings. When using the functions that take an array of runes, this will only be about 3-5x slower than a plain copy of a string (even when applying styles).
And when compared a regex solution or using Lip Gloss directly, this will perform about 5-10x faster.
