nakedmudv3.6/
nakedmudv3.6/lib/
nakedmudv3.6/lib/help/A/
nakedmudv3.6/lib/help/B/
nakedmudv3.6/lib/help/C/
nakedmudv3.6/lib/help/D/
nakedmudv3.6/lib/help/G/
nakedmudv3.6/lib/help/H/
nakedmudv3.6/lib/help/J/
nakedmudv3.6/lib/help/L/
nakedmudv3.6/lib/help/M/
nakedmudv3.6/lib/help/O/
nakedmudv3.6/lib/help/P/
nakedmudv3.6/lib/help/R/
nakedmudv3.6/lib/help/S/
nakedmudv3.6/lib/help/W/
nakedmudv3.6/lib/logs/
nakedmudv3.6/lib/misc/
nakedmudv3.6/lib/players/
nakedmudv3.6/lib/txt/
nakedmudv3.6/lib/world/
nakedmudv3.6/lib/world/examples/
nakedmudv3.6/lib/world/examples/mproto/
nakedmudv3.6/lib/world/examples/oproto/
nakedmudv3.6/lib/world/examples/reset/
nakedmudv3.6/lib/world/examples/rproto/
nakedmudv3.6/lib/world/examples/trigger/
nakedmudv3.6/lib/world/limbo/
nakedmudv3.6/lib/world/limbo/room/
nakedmudv3.6/lib/world/limbo/rproto/
nakedmudv3.6/src/alias/
nakedmudv3.6/src/dyn_vars/
nakedmudv3.6/src/editor/
nakedmudv3.6/src/example_module/
nakedmudv3.6/src/help2/
nakedmudv3.6/src/set_val/
nakedmudv3.6/src/socials/
nakedmudv3.6/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):
    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)