项目作者: adrg

项目描述 :
Add a splash of color to your CLI apps
高级语言: Python
项目地址: git://github.com/adrg/splash-py.git
创建时间: 2014-11-03T07:42:07Z
项目社区:https://github.com/adrg/splash-py

开源协议:MIT License

下载


" class="reference-link">logo

License: MIT

Splash is a small Python 3 package which gives you the ability to style
terminal output. It provides a set of types and functions to allow coloring
and styling of output text. It can be useful in CLI applications or logging
libraries.

The core of the package is the Property class. A property represents either
a color (foreground or background) or a text attribute (bold, underline, etc.).
The package also defines the Style class which is just a collection of
properties. Styles provide the ability to store a group of properties and
reuse them when needed. From a coding standpoint, there is no difference
between using a property and a style.

This package is a port of the Splash
library, originally written for the Go language.

Installation

  1. git clone https://github.com/adrg/splash-py.git
  2. # With distutils
  3. cd splash-py
  4. python3 setup.py install
  5. # With pip3
  6. pip3 install splash-py

Usage

Properties

  1. #!/usr/bin/env python3
  2. import splash
  3. reset = splash.reset
  4. # Using text attributes
  5. print(splash.bold, 'To boldly go ', reset, sep='', end='')
  6. print(splash.underline, 'where no man ', reset, ' ', sep='')
  7. print(splash.reverse, 'has gone before\n', reset, sep='')
  8. # Using foreground and background colors
  9. print(splash.red, 'Roses are red', reset, sep='')
  10. print(splash.bg_green, "Here's something new:", reset, sep='')
  11. print(splash.magenta, 'Violets are violet', reset, sep='')
  12. print(splash.bg_blue, 'Not freaking blue!\n', reset, sep='')
  13. # Combining colors with text attributes
  14. print(splash.bold, splash.green, 'Hint: lamp', reset, sep='')
  15. print(splash.red, splash.bg_blue, 'Hint: famous plumbler\n', reset, sep='')
  16. # Using formatting
  17. print(splash.bg_yellow.format('Yellow there!'))
  18. print('{0}The Wicked Witch of The West{1}'.format(splash.green, reset))
  19. print(splash.bold.format('{0}{1}', splash.blue, "Don't feel blue!"))

Output:
properties output

Styles

  1. #!/usr/bin/env python3
  2. import splash
  3. # Using styles
  4. info = splash.Style(splash.green, splash.bold)
  5. warning = splash.Style(splash.yellow)
  6. err = splash.Style(splash.red, splash.bold)
  7. critical = splash.Style(splash.bold, splash.yellow, splash.bg_red)
  8. print(info, "INFO: I'm so informative", splash.reset, sep='')
  9. print(warning, 'WARNING: You should not ignore me', splash.reset, sep='')
  10. print(err.format("ERROR: You can't say I didn't warn you"))
  11. print(critical.format('{0} {1}\n', 'CRITICAL:', 'This should be good'))
  12. # Parsing styles
  13. # Format: foreground:background+attributes
  14. attr = splash.Style.parse('+b')
  15. print(attr.format('Bold'))
  16. attrs = splash.Style.parse('+bu')
  17. print(attrs.format('Bold, underline'))
  18. fg = splash.Style.parse('yellow')
  19. print(fg.format('Yellow foreground'))
  20. bg = splash.Style.parse(':red')
  21. print(bg.format('Red background'))
  22. fgAttr = splash.Style.parse('green+b')
  23. print(fgAttr.format('Green foreground, bold'))
  24. bgAttr = splash.Style.parse(':magenta+u')
  25. print(bgAttr.format('Magenta background, underline'))
  26. fgBg = splash.Style.parse('cyan:red')
  27. print(fgBg.format('Cyan foreground, red background'))
  28. fgBgAttr = splash.Style.parse('yellow:blue+b')
  29. print(fgBgAttr.format('Yellow foreground, blue background, bold'))
  30. fgBgAttrs = splash.Style.parse('red:green+br')
  31. print(fgBgAttrs.format('Red foreground, green background, bold, reverse'))

Output:
styles output

Property reference

property reference

Foreground colors

  1. black red green yellow blue magenta cyan white

Background colors

  1. bg_black bg_red bg_green bg_yellow bg_blue bg_magenta bg_cyan bg_white

Text attributes

  1. reset bold dim italic underline blink fast_blink reverse hidden crossed_out
  • 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 - fast_blink
  7. r - reverse
  8. h - hidden
  9. c - crossed_out
  10. reset - reset

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.