项目作者: adrg

项目描述 :
Add a splash of color to your CLI apps
高级语言: Go
项目地址: git://github.com/adrg/splash.git
创建时间: 2014-08-04T08:38:03Z
项目社区:https://github.com/adrg/splash

开源协议:MIT License

下载



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

  1. go get github.com/adrg/splash

Usage

Properties

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/adrg/splash"
  5. )
  6. func main() {
  7. reset := splash.Reset
  8. // Using text attributes
  9. fmt.Printf("%s%s%s ", splash.Bold, "To boldly go", reset)
  10. fmt.Printf("%s%s%s ", splash.Underline, "where no man", reset)
  11. fmt.Printf("%s%s%s\n", splash.Reverse, "has gone before.", reset)
  12. // Using foreground and background colors
  13. fmt.Printf("%s%s%s\n", splash.Red, "Roses are red", reset)
  14. fmt.Printf("%s%s%s\n", splash.BgGreen, "Here's something new:", reset)
  15. fmt.Printf("%s%s%s\n", splash.Magenta, "Violets are violet", reset)
  16. fmt.Printf("%s%s%s\n", splash.BgBlue, "Not freaking blue!", reset)
  17. // Combining colors with text attributes
  18. fmt.Printf("%s%s%s%s\n", splash.Bold, splash.Green, "Hint: lamp", reset)
  19. fmt.Printf("%s%s%s\n", splash.Red, splash.BgBlue, "Hint: famous plumbler")
  20. fmt.Println(reset)
  21. // Using property functions
  22. fmt.Println(splash.BgYellow.Sprint("Yellow there!"))
  23. fmt.Print(splash.Green.Sprintln("The Wicked Witch of The West"))
  24. fmt.Println(splash.Bold.Sprintf("%s%s", splash.Blue, "Don't feel blue!"))
  25. }

properties output

Styles

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/adrg/splash"
  5. )
  6. func main() {
  7. // Using styles
  8. info := splash.NewStyle(splash.Green, splash.Bold)
  9. warning := splash.NewStyle(splash.Yellow)
  10. err := splash.NewStyle(splash.Red, splash.Bold)
  11. critical := splash.NewStyle(splash.Bold, splash.Yellow, splash.BgRed)
  12. fmt.Printf("%s%s%s\n", info, "INFO: I'm so informative", splash.Reset)
  13. fmt.Println(warning.Sprint("WARNING: You should not ignore me"))
  14. fmt.Print(err.Sprintln("ERROR: You can't say I didn't warn you"))
  15. fmt.Println(critical.Sprintf("%s %s\n", "CRITICAL:", "This should be good"))
  16. // Parsing styles
  17. // Format: foreground:background+attributes
  18. attr := splash.ParseStyle("+b")
  19. fmt.Println(attr.Sprint("Bold"))
  20. attrs := splash.ParseStyle("+bu")
  21. fmt.Println(attrs.Sprint("Bold, underline"))
  22. fg := splash.ParseStyle("yellow")
  23. fmt.Println(fg.Sprint("Yellow foreground"))
  24. bg := splash.ParseStyle(":red")
  25. fmt.Println(bg.Sprint("Red background"))
  26. fgAttr := splash.ParseStyle("green+b")
  27. fmt.Println(fgAttr.Sprint("Green foreground, bold"))
  28. bgAttr := splash.ParseStyle(":magenta+u")
  29. fmt.Println(bgAttr.Sprint("Magenta background, underline"))
  30. fgBg := splash.ParseStyle("cyan:red")
  31. fmt.Println(fgBg.Sprint("Cyan foreground, red background"))
  32. fgBgAttr := splash.ParseStyle("yellow:blue+b")
  33. fmt.Println(fgBgAttr.Sprint("Yellow foreground, blue background, bold"))
  34. fgBgAttrs := splash.ParseStyle("red:green+br")
  35. fmt.Println(fgBgAttrs.Sprint("Red foreground, green background, bold, reverse"))
  36. }

styles output

Property reference

property reference

Foreground colors

  1. Black Red Green Yellow Blue Magenta Cyan White

Background colors

  1. BgBlack BgRed BgGreen BgYellow BgBlue BgMagenta BgCyan BgWhite

Text attributes

  1. 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

  1. foreground:background+attributes

Colors

  1. black red green yellow blue magenta cyan white

Text attributes

  1. b - Bold
  2. d - Dim
  3. i - Italic
  4. u - Underline
  5. B - Blink
  6. f - FastBlink
  7. r - Reverse
  8. h - Hidden
  9. c - CrossedOut
  10. 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.