Skip to content

A dependency free package to help with "styling" stdout text with ANSI escape sequences.

License

Notifications You must be signed in to change notification settings

leo-alvarenga/go-easy-style

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Easy Style

Integration Tests Go Report Card License License GitHub release pkg.go.dev docs

A dependency free package to help with "styling" stdout text with ANSI escape sequences.

Installation

    go get github.com/leo-alvarenga/go-easy-style@v1

Usage

TextStyle

TextStyle is a struct as (seen bellow) that acts as the "entrypoint" to use the package.

type TextStyle struct {
	Text       string
	Background string
	Format     []string
	ANSI       string
}

func (t *TextStyle) New(txt, background string, styles ...string) {...}
func (t *TextStyle) Style(s string) string {...}
func (t *TextStyle) ShowWithStyle(s string){...}

It also exposes the three (3) methods:

  • New: Initializes a TextStyle instance using defined values. Every time you call New for the same instance, the style is overwritten. Invalid values are simply ignored
  • Style: Returns the styled version of the string passed as argument, ready to be printed to stdout without interfering with the rest of the content
  • ShowWithStyle: A shorthand to style and print the string passed as argument to stdout

Other

Other exposed functions:

func ShowAsError(title, msg string) {...}
  • ShowAsError: A shorthand to style the string passed as argument with a black background, red text and bold letters and print it to stdout

Example

package main

import (
    "fmt"
    "github.com/leo-alvarenga/go-easy-style"
)

func main() {
    var num int

    style := new(gostyle.TextStyle)

    style.New("black", "white", "bold", "italic", "undescore")
    style.ShowWithStyle("Hello, colorfull world!")
    
    style.New("white", "blue", "blink")

    println(style.Style("Type in a number:"))
    fmt.Scanf("%d", &num)

    if num < 0 {
        gostyle.ShowAsError("Too low!", "The value you typed is negative")
    }
}

About

A dependency free package to help with "styling" stdout text with ANSI escape sequences.

Topics

Resources

License

Stars

Watchers

Forks

Languages