Another Text Attribute Manupulator

Gopher Ataman

go-ataman

Another Text Attribute Manipulator

GoDoc Build Status Coverage Status Go Report Card License

The goal of the project is to help render colored text in terminal applications with text attribute manipulation contained in text template. The idea is similar to HTML, e.g. <span style="color:green">Hello!</span>

ataman is not a full-featured template processor. It aims only on simple terminal text attribute, graphic mode in ANSI terms, manipulations using ANSI sequences.

Features

  • Parsing and rendering template with single iteration.
  • Minimal memory allocations with pooling.
  • Thread-safe parsing and rendering.
  • Fully customizable decoration of tags.

Installation

To install the package use go get gopkg.in/workanator/go-ataman.v1

Quick Example

The next example shows basic usage of the package. The renderer here uses basic decoration style.

rndr := ataman.NewRenderer(ataman.BasicStyle())
tpl := "<light+green>%s<->, <bg+light+yellow,black,bold> <<%s>> <-><red>!"
fmt.Println(rndr.MustRenderf(tpl, "Hello", "Terminal World"))

This example produces colored text like this.

Example Output

Renderer is able to pre-render templates so in further output operations they can be reused without parsing and rendering. In this example the renderer uses curly brackets decoration style.

rndr := ataman.NewRenderer(ataman.CurlyStyle())
prep := rndr.MustPrepare("{light_green}%s{-}, {bg_light_yellow+black+bold} <%s> {-}{red}!")
fmt.Println(rndr.MustRenderf(tpl, "Hi", "Everybody"))

The pre-rendered template implements fmt.Stringer interface so it can be used in output operations without additional code, e.g.

prep := rndr.MustPrepare("{red}Red {green}Green {blue}Blue{-}")
fmt.Println(prep)

Writing Templates

Templates follow the simple rules.

  • Tag can contain one or more attributes, e.g. {white,bold}, {red}.
  • If template should render open or close tag sequence as regular text then the sequence should be doubled. For example, if tag is enclosed in { and } then in template it should be This rendered as {{regular text}}.
  • Renderer adds reset graphic mode ANSI sequence to the each template if it contains any other ANSI sequences. So visually those templates are equivalent {bold}Bold{-} and {bold}Bold.

Decoration styles use the follows dictionary.

  • - or reset stand for reset graphic mode.
  • b or bold make font bold.
  • u or underscore or underline make font underline.
  • blink makes font blink.
  • reverse swaps text and background colors.
  • conceal makes font concealed (whatever that means).
  • black color.
  • red color.
  • green color.
  • yellow color.
  • blue color.
  • magenta color.
  • cyan color.
  • white color.
  • default reverts to the default color.
  • bg or background should be used in conjunction with color to set background color.
  • intensive or light should be used in conjunction with color to make the color intensive. Could be used with background as well.

Some template examples with curly decoration style.

  • {light_green} - makes text light (intensive) green.
  • {bg_yellow} - makes background yellow.
  • {bold} - makes font bold.
  • {red,bg_blue} - makes text red on blue background.
  • {u,black,bg_intensive_white} - makes text black with underline font on intensive white background.
  • {-} - reset the current graphic mode.

Customizing Renderer

The package allows to customize tag decorations what can be achieved through decorate.Style struct. The struct should be initialized with preferred values. For example with the code below we can define a decoration style like [[bold,yellow]]Warning![[-]] [[intensive_white]]This package is awesome![[-]] :).

style := decorate.Style{
  TagOpen:            decorate.NewMarker("[["),
  TagClose:           decorate.NewMarker("]]"),
  AttributeDelimiter: decorate.NewMarker(","),
  ModDelimiter:       decorate.NewMarker("-"),
  Attributes:         ansi.DefaultDict,
}

rndr := ataman.NewRenderer(style)

The rules of decoration styles are the follows.

  • TagOpen is the sequence of runes which open tag.
  • TagClose is the sequence of runes which close tag.
  • AttributeDelimiter is the sequence of runes which delimits attributes inside tag.
  • ModDelimiter is the sequence of runes which delimits modifiers in attribute.
  • Attributes is the map of attributes, where key is the name of ANSI code user uses in templates and value is the ANSI code used in ANSI sequence.

It's recommended to use attribute codes defined in ansi package with the default Renderer provided by ataman.

Similar Resources

Yet Another CLi Spinner; providing over 70 easy to use and customizable terminal spinners for multiple OSes

Yet Another CLi Spinner; providing over 70 easy to use and customizable terminal spinners for multiple OSes

Yet Another CLi Spinner (for Go) Package yacspin provides yet another CLi spinner for Go, taking inspiration (and some utility code) from the https://

Dec 25, 2022

An yet-another red-black tree implementation, with a C++ STL-like API.

A red-black tree with an API similar to C++ STL's. INSTALLATION go get github.com/yasushi-saito/rbtree EXAMPLE More examples can be fou

Apr 25, 2022

Yet another Go REPL that works nicely. Featured with line editing, code completion, and more.

  Yet another Go REPL that works nicely. Featured with line editing, code completion, and more.

gore Yet another Go REPL that works nicely. Featured with line editing, code completion, and more. (Screencast taken with cho45/KeyCast) Usage gore Af

Jan 2, 2023

🍱 yet another collection of go utilities & tools

gut 🍱 Yet another collection of Go utilities & tools. A simple one. Just go with your gut feeling. Shortcuts Symbol 🍱 弁当 Document License Build Stat

Sep 26, 2022

Yet Another REST Framework

YARF: Yet Another REST Framework YARF is a fast micro-framework designed to build REST APIs and web services in a fast and simple way. Designed after

Sep 27, 2022

Run a real Postgres database locally on Linux, OSX or Windows as part of another Go application or test

Run a real Postgres database locally on Linux, OSX or Windows as part of another Go application or test

embedded-postgres Run a real Postgres database locally on Linux, OSX or Windows as part of another Go application or test. When testing this provides

Dec 27, 2022

safe and easy casting from one type to another in Go

cast Easy and safe casting from one type to another in Go Don’t Panic! ... Cast What is Cast? Cast is a library to convert between different go types

Jan 7, 2023

Yaegi is Another Elegant Go Interpreter

Yaegi is Another Elegant Go Interpreter

Yaegi is Another Elegant Go Interpreter. It powers executable Go scripts and plugins, in embedded interpreters or interactive shells, on top of the Go

Dec 30, 2022

🚥 Yet another pinger: A high-performance ICMP ping implementation build on top of BPF technology.

yap Yet-Another-Pinger: A high-performance ICMP ping implementation build on top of BPF technology. yap uses the gopacket library to receive and handl

Nov 9, 2022

Yet another SIP003 plugin for shadowsocks, based on Xray-core

Yet another SIP003 plugin for shadowsocks, based on Xray-core Build go build Usage See command line args for advanced usages.

Jan 8, 2023

yet another rss reader

yet another rss reader

yarr yarr (yet another rss reader) is a web-based feed aggregator which can be used both as a desktop application and a personal self-hosted server. I

Dec 29, 2022

Yaegi is Another Elegant Go Interpreter

Yaegi is Another Elegant Go Interpreter

Yaegi is Another Elegant Go Interpreter. It powers executable Go scripts and plugins, in embedded interpreters or interactive shells, on top of the Go

Jan 5, 2023

Yet another Go REPL that works nicely. Featured with line editing, code completion, and more.

  Yet another Go REPL that works nicely. Featured with line editing, code completion, and more.

gore Yet another Go REPL that works nicely. Featured with line editing, code completion, and more. (Screencast taken with cho45/KeyCast) Usage gore Af

Jan 7, 2023

Are you forwarding DNS traffic to another server for some reason, but want to make sure only queries for certain names are passed? Say no more.

DNSFWD Redirect DNS traffic to an upstream. Get Latest: wget https://github.com/C-Sto/dnsfwd/releases/latest/download/dnsfwd_linux (replace linux with

Dec 16, 2022

Easily and securely send things from one computer to another :crocodile: :package:

Easily and securely send things from one computer to another :crocodile: :package:

This project is supported by: croc is a tool that allows any two computers to simply and securely transfer files and folders. AFAIK, croc is the only

Jan 3, 2023

safe and easy casting from one type to another in Go

cast Easy and safe casting from one type to another in Go Don’t Panic! ... Cast What is Cast? Cast is a library to convert between different go types

Jan 1, 2023

Not Yet Another Password Manager written in Go using libsodium

secrets Secure and simple passwords manager written in Go. It aims to be NYAPM (Not Yet Another Password Manager), but tries to be different from othe

May 30, 2022
Simple text-line analog clock

anaclock anaclock prints a simple analog clock as a line of text. Demo $ anaclock 23 . : .| 00 anaclock is easy to use in CLI prompts or anywhere

Dec 31, 2021
This is a simplification of the attribute-based-encryption padlock concept

A Cryptographic ABE Padlock This is a simplification of the attribute-based-encryption padlock concept. This implementation is just focusing on gettin

Mar 27, 2022
converts text-formats from one to another, it is very useful if you want to re-format a json file to yaml, toml to yaml, csv to yaml, ... etc

re-txt reformates a text file from a structure to another, i.e: convert from json to yaml, toml to json, ... etc Supported Source Formats json yaml hc

Sep 23, 2022
A Go package for n-gram based text categorization, with support for utf-8 and raw text

A Go package for n-gram based text categorization, with support for utf-8 and raw text. To do: write documentation make it faster Keywords: text categ

Nov 28, 2022
A Go package for n-gram based text categorization, with support for utf-8 and raw text

A Go package for n-gram based text categorization, with support for utf-8 and raw text. To do: write documentation make it faster Keywords: text categ

Nov 28, 2022
Chalk is a Go Package which can be used for making terminal output more vibrant with text colors, text styles and background colors.
Chalk is a Go Package which can be used for making terminal output more vibrant with text colors, text styles and background colors.

Chalk Chalk is a Go Package which can be used for making terminal output more vibrant with text colors, text styles and background colors. Documentati

Oct 29, 2022
Jan 27, 2022
Generates random text based on trigrams generated from input text
Generates random text based on trigrams generated from input text

Trigrams Generates random text based on trigrams generated from input text Contents Building Running Using Implementation notes NGram size Maximum wor

Feb 9, 2022
Read the text of memes, then inject that text into the image as searchable metadata.
Read the text of memes, then inject that text into the image as searchable metadata.

Make Meme Text Searchable I have an extensive set of memes I've been collecting since the early days of Flickr. #icanhascheeseburger It's a pain in th

May 2, 2022
Another CLI framework for Go. It works on my machine.
Another CLI framework for Go. It works on my machine.

Command line interface framework Go framework for rapid command line application development

Dec 30, 2022