X-Git-Url: https://mudpy.org/gitweb?p=mudpy.git;a=blobdiff_plain;f=lib%2Fmuff%2Fmuffmisc.py;h=ba90981b75f1e2b537f2b61840627a7d8cf98559;hp=6bfbe62013ede82d3210977ddc8fb5973e96b4c9;hb=0f39af78818acbbee0b99145ff5ff303553027c6;hpb=d49d19d943672b3cea1b2e43802f4b5eca6c81b5 diff --git a/lib/muff/muffmisc.py b/lib/muff/muffmisc.py index 6bfbe62..ba90981 100644 --- a/lib/muff/muffmisc.py +++ b/lib/muff/muffmisc.py @@ -1,63 +1,84 @@ """Miscellaneous objects for the MUFF Engine""" -# Copyright (c) 2005 mudpy, Jeremy Stanley -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# -# - Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# - Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following -# disclaimer in the documentation and/or other materials provided -# with the distribution. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS -# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE -# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, -# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN -# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -# POSSIBILITY OF SUCH DAMAGE. - -import string +# Copyright (c) 2005 mudpy, Jeremy Stanley , all rights reserved. +# Licensed per terms in the LICENSE file distributed with this software. +# hack to load all modules in the muff package import muff for module in muff.__all__: exec("import " + module) def broadcast(output): + """Send a message to all connected users.""" for each_user in muffvars.userlist: each_user.send(output) def wrap_ansi_text(text, width): - relative_position = 0 + """Wrap text with arbitrary width while ignoring ANSI colors.""" + + # the current position in the entire text string, including all + # characters, printable or otherwise absolute_position = 0 - escape = 0 + + # the current text position relative to the begining of the line, + # ignoring color escape sequences + relative_position = 0 + + # whether the current character is part of a color escape sequence + escape = False + + # iterate over each character from the begining of the text for each_character in text: + + # the current character is the escape character if each_character == chr(27): - escape = 1 + escape = True + + # the current character is within an escape sequence elif escape: + + # the current character is m, which terminates the + # current escape sequence if each_character == "m": - escape = 0 + escape = False + + # the current character is a newline, so reset the relative + # position (start a new line) elif each_character == "\n": relative_position = 0 + + # the current character meets the requested maximum line width, + # so we need to backtrack and find a space at which to wrap elif relative_position == width: + + # distance of the current character examined from the + # relative position wrap_offset = 0 + + # count backwards until we find a space while text[absolute_position - wrap_offset] != " ": wrap_offset += 1 + + # insert an eol in place of the space text = text[:absolute_position - wrap_offset] + "\r\n" + text[absolute_position - wrap_offset + 1:] + + # increase the absolute position because an eol is two + # characters but the space it replaced was only one absolute_position += 1 + + # now we're at the begining of a new line, plus the + # number of characters wrapped from the previous line relative_position = wrap_offset + + # as long as the character is not a carriage return and the + # other above conditions haven't been met, count it as a + # printable character elif each_character != "\r": relative_position += 1 + + # increase the absolute position for every character absolute_position += 1 + + # return the newly-wrapped text return text