Giter Club home page Giter Club logo

splash's Introduction

splash logo

pkg.go.dev documentation MIT license Go report card

Splash is a small package which allows styling terminal output. It provides a set of types and functions to facilitate coloring and styling of output text. It can be useful in CLI applications or logging libraries.

The core of the package is the Property type. A property represents either a color (foreground or background) or a text attribute (bold, underline, etc.). The package also defines the Style type which is a collection of properties. Styles provide the ability to store a group of properties and reuse them when needed. Both types come with the familiar String, Sprint, Sprintf and Sprintln methods which are used the same as the ones in the fmt package. Moreover, from a programming standpoint, there is no difference between using a property and a style.

Full documentation can be found at https://pkg.go.dev/github.com/adrg/splash.

Installation

go get github.com/adrg/splash

Usage

Properties

package main

import (
    "fmt"

    "github.com/adrg/splash"
)

func main() {
	reset := splash.Reset

	// Using text attributes
	fmt.Printf("%s%s%s ", splash.Bold, "To boldly go", reset)
	fmt.Printf("%s%s%s ", splash.Underline, "where no man", reset)
	fmt.Printf("%s%s%s\n", splash.Reverse, "has gone before.", reset)

	// Using foreground and background colors
	fmt.Printf("%s%s%s\n", splash.Red, "Roses are red", reset)
	fmt.Printf("%s%s%s\n", splash.BgGreen, "Here's something new:", reset)
	fmt.Printf("%s%s%s\n", splash.Magenta, "Violets are violet", reset)
	fmt.Printf("%s%s%s\n", splash.BgBlue, "Not freaking blue!", reset)

	// Combining colors with text attributes
	fmt.Printf("%s%s%s%s\n", splash.Bold, splash.Green, "Hint: lamp", reset)
	fmt.Printf("%s%s%s\n", splash.Red, splash.BgBlue, "Hint: famous plumbler")

	fmt.Println(reset)

	// Using property functions
	fmt.Println(splash.BgYellow.Sprint("Yellow there!"))
	fmt.Print(splash.Green.Sprintln("The Wicked Witch of The West"))
	fmt.Println(splash.Bold.Sprintf("%s%s", splash.Blue, "Don't feel blue!"))
}

properties output

Styles

package main

import (
    "fmt"

    "github.com/adrg/splash"
)

func main() {
	// Using styles
	info := splash.NewStyle(splash.Green, splash.Bold)
	warning := splash.NewStyle(splash.Yellow)
	err := splash.NewStyle(splash.Red, splash.Bold)
	critical := splash.NewStyle(splash.Bold, splash.Yellow, splash.BgRed)

	fmt.Printf("%s%s%s\n", info, "INFO: I'm so informative", splash.Reset)
	fmt.Println(warning.Sprint("WARNING: You should not ignore me"))
	fmt.Print(err.Sprintln("ERROR: You can't say I didn't warn you"))
	fmt.Println(critical.Sprintf("%s %s\n", "CRITICAL:", "This should be good"))

	// Parsing styles
	// Format: foreground:background+attributes
	attr := splash.ParseStyle("+b")
	fmt.Println(attr.Sprint("Bold"))

	attrs := splash.ParseStyle("+bu")
	fmt.Println(attrs.Sprint("Bold, underline"))

	fg := splash.ParseStyle("yellow")
	fmt.Println(fg.Sprint("Yellow foreground"))

	bg := splash.ParseStyle(":red")
	fmt.Println(bg.Sprint("Red background"))

	fgAttr := splash.ParseStyle("green+b")
	fmt.Println(fgAttr.Sprint("Green foreground, bold"))

	bgAttr := splash.ParseStyle(":magenta+u")
	fmt.Println(bgAttr.Sprint("Magenta background, underline"))

	fgBg := splash.ParseStyle("cyan:red")
	fmt.Println(fgBg.Sprint("Cyan foreground, red background"))

	fgBgAttr := splash.ParseStyle("yellow:blue+b")
	fmt.Println(fgBgAttr.Sprint("Yellow foreground, blue background, bold"))

	fgBgAttrs := splash.ParseStyle("red:green+br")
	fmt.Println(fgBgAttrs.Sprint("Red foreground, green background, bold, reverse"))
}

styles output

Property reference

property reference

Foreground colors

Black Red Green Yellow Blue Magenta Cyan White

Background colors

BgBlack BgRed BgGreen BgYellow BgBlue BgMagenta BgCyan BgWhite

Text attributes

Reset Bold Dim Italic Underline Blink FastBlink Reverse Hidden CrossedOut
  • Note: unfortunately not all text attributes are supported in all terminals.

Style parsing reference

Format

foreground:background+attributes

Colors

black red green yellow blue magenta cyan white

Text attributes

b     - Bold
d     - Dim
i     - Italic
u     - Underline
B     - Blink
f     - FastBlink
r     - Reverse
h     - Hidden
c     - CrossedOut
reset - Reset

Contributing

Contributions in the form of pull requests, issues or just general feedback, are always welcome.
See CONTRIBUTING.MD.

References

For more information see the ANSI escape sequences and Terminal colors and formatting.

License

Copyright (c) 2014 Adrian-George Bostan.

This project is licensed under the MIT license. See LICENSE for more details.

splash's People

Contributors

adrg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.