Skip to content

Commit

Permalink
Allow use of the fg_ prefix for colors
Browse files Browse the repository at this point in the history
* Use esc(0) for the reset escape code
* Fix bold background colors
* Simpler escape_code generation
* Added TestRainbow class :)
  • Loading branch information
borntyping committed Aug 6, 2014
1 parent 02903d4 commit 40100a3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
5 changes: 2 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Codes

The following values are made availible for use in the format string:

- ``fg_{colorname}``, ``bg_{colorname}``: Foreground and background colors. The colors names are ``black``, ``red``, ``green``, ``yellow``, ``blue``, ``purple``, ``cyan`` and ``white``.
- ``bold``: Bold output.
- ``{color}``, ``fg_{color}``, ``bg_{color}``: Foreground and background colors. The color names are ``black``, ``red``, ``green``, ``yellow``, ``blue``, ``purple``, ``cyan`` and ``white``.
- ``bold``, ``bold_{color}``, ``fg_bold_{color}``, ``bg_bold_{color}``: Bold/bright colors.
- ``reset``: Clear all formatting (both foreground and background colors).
- ``log_color``: Return the color associated with the records level (from ``color_levels``).

Expand Down Expand Up @@ -108,7 +108,6 @@ Tests
=====

Tests similar to the above examples are found in ``tests/test_colorlog.py``.
They require colorlog to be installed or otherwise available to Python.

`tox`_ will run the tests under all compatible python versions.

Expand Down
26 changes: 16 additions & 10 deletions colorlog/escape_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

# The initial list of escape codes
escape_codes = {
'reset': esc('39', '49', '0'),
'reset': esc('0'),
'bold': esc('01'),
}

# The color names
colors = [
COLORS = [
'black',
'red',
'green',
Expand All @@ -36,11 +36,17 @@
'white'
]

# Create foreground and background colors...
for lcode, lname in [('3', ''), ('4', 'bg_')]:
# ...with the list of colors...
for code, name in enumerate(colors):
code = str(code)
# ...and both normal and bold versions of each color
escape_codes[lname + name] = esc(lcode + code)
escape_codes[lname + "bold_" + name] = esc(lcode + code, "01")
PREFIXES = [
# Foreground without prefix
('3', ''), ('01;3', 'bold_'),

# Foreground with fg_ prefix
('3', 'fg_'), ('01;3', 'fg_bold_'),

# Background with bg_ prefix - bold/light works differently
('4', 'bg_'), ('10', 'bg_bold_'),
]

for prefix, prefix_name in PREFIXES:
for code, name in enumerate(COLORS):
escape_codes[prefix_name + name] = esc(prefix + str(code))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name = 'colorlog',
version = '2.3.1',
version = '2.4.0',

description = 'Log formatting with colors!',
long_description = open("README.rst").read(),
Expand Down
35 changes: 31 additions & 4 deletions tests/test_colorlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from os.path import join, dirname, realpath
from sys import version_info
from unittest import TestCase, TestLoader, TextTestRunner
from unittest import TestCase, TextTestRunner, main

from logging import StreamHandler, DEBUG, getLogger, root
from logging.config import fileConfig
Expand Down Expand Up @@ -61,6 +61,35 @@ def test_file(self):
fileConfig(f.name)
self.example_log_messages(getLogger('fileConfig'))


class TestRainbow(TestCase):
RAINBOW = (
"%(log_color)s%(levelname)s%(reset)s:%(bold_black)s%(name)s:%(reset)s"

"%(bold_red)sr%(red)sa%(yellow)si%(green)sn%(bold_blue)sb"
"%(blue)so%(purple)sw%(reset)s "

"%(fg_bold_red)sr%(fg_red)sa%(fg_yellow)si%(fg_green)sn"
"%(fg_bold_blue)sb%(fg_blue)so%(fg_purple)sw%(reset)s "

"%(bg_red)sr%(bg_bold_red)sa%(bg_yellow)si%(bg_green)sn"
"%(bg_bold_blue)sb%(bg_blue)so%(bg_purple)sw%(reset)s "
)

def test_rainbow(self):
formatter = ColoredFormatter(self.RAINBOW)

stream = StreamHandler()
stream.setLevel(DEBUG)
stream.setFormatter(formatter)

logger = getLogger('rainbow')
logger.setLevel(DEBUG)
logger.addHandler(stream)

logger.critical(None)


if version_info > (2, 7):
from unittest import skipUnless
from logging.config import dictConfig
Expand Down Expand Up @@ -117,6 +146,4 @@ def test_py3(self):
self.example_log_messages(logger)

if __name__ == '__main__':
tests = TestLoader().loadTestsFromTestCase(TestColoredFormatter)
result = TextTestRunner(verbosity=0).run(tests)
exit(len(result.errors) + len(result.failures))
main(testRunner=TextTestRunner(verbosity=0))

0 comments on commit 40100a3

Please sign in to comment.