nakedmudv3.8.1/
nakedmudv3.8.1/html/
nakedmudv3.8.1/html/tutorials/
nakedmudv3.8.1/html/tutorials/building_extras/
nakedmudv3.8.1/html/tutorials/c/
nakedmudv3.8.1/html/tutorials/reference/
nakedmudv3.8.1/html/tutorials/scripting/
nakedmudv3.8.1/html/tutorials/scripting_extras/
nakedmudv3.8.1/lib/
nakedmudv3.8.1/lib/help/A/
nakedmudv3.8.1/lib/help/B/
nakedmudv3.8.1/lib/help/C/
nakedmudv3.8.1/lib/help/D/
nakedmudv3.8.1/lib/help/G/
nakedmudv3.8.1/lib/help/H/
nakedmudv3.8.1/lib/help/J/
nakedmudv3.8.1/lib/help/L/
nakedmudv3.8.1/lib/help/M/
nakedmudv3.8.1/lib/help/O/
nakedmudv3.8.1/lib/help/P/
nakedmudv3.8.1/lib/help/R/
nakedmudv3.8.1/lib/help/S/
nakedmudv3.8.1/lib/help/W/
nakedmudv3.8.1/lib/logs/
nakedmudv3.8.1/lib/misc/
nakedmudv3.8.1/lib/players/
nakedmudv3.8.1/lib/txt/
nakedmudv3.8.1/lib/world/
nakedmudv3.8.1/lib/world/zones/examples/
nakedmudv3.8.1/lib/world/zones/examples/mproto/
nakedmudv3.8.1/lib/world/zones/examples/oproto/
nakedmudv3.8.1/lib/world/zones/examples/reset/
nakedmudv3.8.1/lib/world/zones/examples/rproto/
nakedmudv3.8.1/lib/world/zones/examples/trigger/
nakedmudv3.8.1/lib/world/zones/limbo/
nakedmudv3.8.1/lib/world/zones/limbo/room/
nakedmudv3.8.1/lib/world/zones/limbo/rproto/
nakedmudv3.8.1/src/alias/
nakedmudv3.8.1/src/dyn_vars/
nakedmudv3.8.1/src/editor/
nakedmudv3.8.1/src/example_module/
nakedmudv3.8.1/src/help2/
nakedmudv3.8.1/src/set_val/
nakedmudv3.8.1/src/socials/
nakedmudv3.8.1/src/time/
"""
colour.py

NakedMud's base colour module. Contains functions for outbound text processing
to add ASCII colour codes to the text.
"""
import mud, mudsock, hooks



# symbols and values we need for processing colours
base_colour_marker   = '{'
colour_start         = '\x1b['
cDARK                = '0'
cLIGHT               = '1'

# colour symbols
c_none    = 'n'
c_dark    = 'd'
c_red     = 'r'
c_green   = 'g'
c_yellow  = 'y'
c_blue    = 'b'
c_magenta = 'p'
c_cyan    = 'c'
c_white   = 'w'

# maps of colour symbols to their ASCII number type thing
base_colours = { c_none    : '0',
                 c_dark    : '30',  
                 c_red     : '31',  
                 c_green   : '32',  
                 c_yellow  : '33',  
                 c_blue    : '34',  
                 c_magenta : '35',  
                 c_cyan    : '36',  
                 c_white   : '37' }



################################################################################
# colour processing hooks
################################################################################
def process_colour_hook(info):
    """When outbound text is being processed, find colour codes and replace them
       by the proper colour escape sequences."""
    sock,  = hooks.parse_info(info)
    buf    = sock.outbound_text
    newbuf = []

    # go through our outbound text and process all of the colour codes
    i = 0
    while i < len(buf):
        if buf[i] == base_colour_marker and i + 1 < len(buf):
            i = i + 1
            char  = buf[i]

            # upper case is bright, lower case is dark
            shade = cLIGHT
            if char == char.lower():
                shade = cDARK

            # if it's a valid colour code, build it
            if base_colours.has_key(char.lower()):
                newbuf.append(colour_start + shade + ';' + \
                              base_colours[char.lower()] + 'm')

            # if it was an invalid code, ignore it
            else:
                newbuf.append(base_colour_marker)
                if not char == base_colour_marker:
                    i = i - 1

        else:
            newbuf.append(buf[i])
        i = i + 1

    # replace our outbound buffer with the processed text
    sock.outbound_text = ''.join(newbuf)



################################################################################
# initializing and unloading our hooks
################################################################################
hooks.add("process_outbound_text",   process_colour_hook)
hooks.add("process_outbound_prompt", process_colour_hook)

def __unload__():
    '''detaches our colour module from the game'''
    hooks.remove("process_outbound_text",   process_colour_hook)
    hooks.remove("process_outbound_prompt", process_colour_hook)