Table of Contents

Module: formatter mudlib/formatter.py

This module encapsulates all the formatting markup and the code involved in manipulating the formatting markup to allow coders to embed color and formatting information in outgoing text strings to be seen by users.

It also holds a series of helper functions for framing text and columnizing lists of strings in pretty columns.

Lastly, it has a FormattedTextIter iterator that allows you to easily iterate over formatted text string without worrying about text elements vs. markup tokens.

Colors are defined in the mudlib.constants module.

Imported modules   
import constants
import re
import string
import types
Functions   
color
columnize
discard_formatting
fixedcolumnize
format_text
get_ansi_cheat_sheet
indent
insert_cr
is_color_token
join_formatting_and_text
paperframe
postcolor
precolor
signframe
split_formatting_from_text
unescape
  color 
color ( c,  text )

Takes a piece of text and returns the string formatted with the color specified.

arguments:

c
(string) the color string
text
(string) the text to color

returns:

(string) text enclosed in the color formatting markup

  columnize 
columnize (
        textlist,
        screenwidth=72,
        indent=0,
        sort=0,
        )

Takes a list of data and converts it into a series of columns and rows that are evenly spaced and pretty and stuff. Also will sort the list if you say sort=1. It ignores formatting markup when figuring out the columni widths.

arguments:

textlist
(list) list of strings to columnize
screenwidth=72
(int) the width of the screen to columnize to
indent=0
(int) the number of spaces to indent lines
sort=0
(0 or 1) whether or not to sort the list before columnizing

returns:

(string) the completely columnized and pretty string

  discard_formatting 
discard_formatting ( text )

Returns a string with no formatting in it. Useful for figuring out the length of things without the formatting.

arguments:

text
(string) the string in question

returns:

(string) the incoming text with all formatting removed

  fixedcolumnize 
fixedcolumnize ( text )

This will take in text with a bunch of rows delimited by \n and with columns delimited by tabs and output a series of rows with those columns in fixed positions separated by spaces.

example text could look like:

text.py\t23\tMon Sep 29th\n
text.py~\t22\tMon Sep 29th\n

arguments:

text
(string) the text with tabs and such to columnize

returns:

(string) the columnized text

  format_text 
format_text (
        textlist,
        ansicolor=1,
        wraplength=72,
        replace_dict={},
        )

This is the master function itself. It handles all conversion of formatting markup to ansi escape code and spacing and all that. It wordwraps the outgoing text to the user's terminal width. It's the central function around which all other functions in this module revole around.

arguments:

textlist
(string) or (list of strings) either a string of text needing to be formatted and wrapped or a textlist--preferably the former.
ansicolor
(int) 1 says we support ansicolor, 0 says we take it all out
wraplength
(int) how many characters to wrap at
replace_dict
(hash) the dict of name tags -> colors

returns:

(string) the text with all the formatting converted to ansi codes and all that stuff

  get_ansi_cheat_sheet 
get_ansi_cheat_sheet ()

Returns a list of color keys that we support. This function is useful for helping players and coders know what colors are available.

returns:

(list of strings) all the color keys in the ANSI_COLOR_MAP

  indent 
indent (
        i,
        text=None,
        indentfirstline=constants.TRUE,
        )

Returns text with the formatting for indenting in there. This allows you to easily indent an entire block of text x number of spaces in without having to do it manually.

arguments:

i
how much to indent
text=None
text to indent
indentfirstline=mudlib.constants.TRUE
whether or not to indent the first line

returns:

(string) the indent markup and the text to indent

  insert_cr 
insert_cr (
        text,
        index,
        indent=0,
        )

Inserts a carriage return into the line and deals with indenting the next line (if need be).

arguments:

text
(string) the text in question
index
(int) the place to stick the cr
indent=0
(int) how much to indent the next line

returns:

(string) the text with the cr at the index and the next line indented so many spaces

  is_color_token 
is_color_token ( text )

Returns whether this token is a formatting markup token. We use this pretty frequently in this module, and you can use it too if you're into that sort of thing.

arguments:

text
(string) the text in question

returns:

(0 or 1) whether or not this text is a formatting markup token

  join_formatting_and_text 
join_formatting_and_text ( textlist )

Returns split text back to its original state. This is the opposite of the split_formatting_from_text. The two functions complement each other.

arguments:

textlist
(list of strings) the textlist to join together

returns:

(string) the complete text string

  paperframe 
paperframe ( text )

Takes a string and frames it with ascii characters. This one caters to scrolls, parchment, and other paper media.

Something like this (yellow border):

@=========)
| blah   |
@_________)

arguments:

text
(string) the text to frame. can have cr's in it if you like.

returns:

(string) the text with additional markup and all sorts of stuff for the frame in it

  postcolor 
postcolor ()

Returns the format that we use internally to end a section of colored text.

returns:

(string) the end tag of our color formatting markup

  precolor 
precolor ( c )

Takes in a color constant and returns it in the format that we use internally to represent the beginning of colored text.

arguments:

c
(string) the color string

returns:

(string) the color string enclosed in our magic markup

  signframe 
signframe ( text )

Takes a string and frames it with ascii characters. This one caters to signs, plaque and placards.

Something like this (white border):

#---------#
| blah    |
#---------#

arguments:

text
(string) the text to frame. can have cr's in it if you like.

returns:

(string) the text with additional markup and all sorts of stuff for the frame in it

  split_formatting_from_text 
split_formatting_from_text ( text )

Takes a string and separates out the formatting elements from the text itself. It uses regular expressions and should be pretty speedy. It returns a list of elements and text in the order they existed in the original string. The complementary function is join_formatting_and_text .

example:

"<tag>text</tag>"

gets converted to:

["<tag>", "text", "</tag>"]

arguments:

text
(string) the text to split

returns:

(list) list of formatting tokens and text elements

  unescape 
unescape ( text )

Unescapes escaped formatting codes. I'm not wholly sure this works--it's kind of untested.

arguments:

text
(string) the text to unescape

returns:

(string) the text with &lt; -> &lt;

Classes   
FormattedTextIter

Iterator for text strings which allows you to iterate


Table of Contents

This document was automatically generated on Thu Jan 24 08:57:43 2002 by HappyDoc version 2.0