parse-vis / shell_color.py

# -*- coding: utf-8 -*-
#
#  shell_color.py
#  parse_vis
#
#  Created by Lars Yencken on 2006-05-02.
#  Copyright 2006-2010 Lars Yencken. All rights reserved.
#

"""
Colored output for unix shells, which can be disabled to avoid problems on
non-supporting platforms.
"""

#----------------------------------------------------------------------------#

# whether color output is currently enabled or not
_enable_color = True

#----------------------------------------------------------------------------#

def enable_color():
    """Enable colorized output from this module's methods."""
    global _enable_color
    _enable_color = True
    return

#----------------------------------------------------------------------------#

def disable_color():
    """Disable colorized output from this module's methods."""
    global _enable_color
    _enable_color = False
    return

#----------------------------------------------------------------------------#

_col_string = '\x1b[01;%.2dm'
_reset_string = '\x1b[01;00m'

_colors = {
    'regular':      0,
    'darkgrey':     30,
    'red':          31,
    'lightgreen':   32,
    'yellow':       33,
    'blue':         34
}

#----------------------------------------------------------------------------#

def color(str_obj, color):
    """
    Changes the color of the given string as printed on a UNIX shell.

    >>> color('dog', 'blue') == '\x1b[01;34mdog\x1b[01;00m'
    True
    """ 
    code = _colors.get(color)
    if code is None:
        raise KeyError('no color matches name: %s' % color)

    global _enable_color
    if _enable_color:
        return (_col_string % _colors[color]) + str_obj + (_col_string % 0)
    else:
        return str_obj

#----------------------------------------------------------------------------#

def change_color(color):
    """ Change the color for the remaining text after this is printed to the
        given color.
    """
    global _enable_color
    if _enable_color:
        return _col_string % _colors[color]
    else:
        return ''

#----------------------------------------------------------------------------#

def real_len(str_obj):
    """
    Determine the real length of a string object.

    >>> real_len('dog gone')
    8
    >>> real_len(color('dog', 'blue') + ' gone')
    8
    >>> real_len('\x1b[01;34mdog\x1b[01;00m gone')
    8
    """
    final_len = 0
    start_index = 0

    color_str = '\x1b[01;'

    next_color = str_obj.find(color_str, start_index)
    while next_color != -1:
        final_len += next_color - start_index
        start_index = next_color + len(_reset_string)
        next_color = str_obj.find(color_str, start_index)

    final_len += len(str_obj) - start_index

    return final_len

#----------------------------------------------------------------------------#

def reset_color():
    """
    Return the string to print to reset the color to the default.
    """
    global _enable_color

    if _enable_color:
        return _reset_string

    return ''

#----------------------------------------------------------------------------#
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.