def highlight(self, textlist, place, memlength, hl): """ Takes a bunch of stuff and applies the highlight involved. It's messy. @param textlist: the list of strings representing the incoming text--this is usually text interspersed with ansi color tokens. @type textlist: list of strings @param place: the point in the text (skipping over ansi color stuff) that marks the beginning of the highlight @type place: int @param memlength: the length of the string to be highlighted @type memlength: int @param hl: the highlight to apply @type hl: string @returns: the newly adjusted textlist @rtype: list of strings """ # first we find the place to stick the highlight thingy. i = 0 for i in range(0, len(textlist)): if not ansi.is_color_token(textlist[i]): if place > len(textlist[i]): place -= len(textlist[i]) else: break newlist = textlist[:i] newlist.append(textlist[i][:place]) newcolor = ansi.figure_color(newlist, self._currcolor)[0] newlist.append(hl) # if the string to highlight begins and ends in the # same token we deal with that and eject if len(textlist[i][place:]) >= memlength: newlist.append(textlist[i][place:place + memlength]) newlist.append(chr(27) + "[0m") color = ansi.convert_tuple_to_ansi(newcolor) if color: newlist.append(color) newlist.append(textlist[i][place + memlength:]) for mem in textlist[i+1:]: newlist.append(mem) return newlist newlist.append(textlist[i][place:]) # now we have to find the end of the highlight memlength -= len(textlist[i][place:]) j = i+1 for j in range(i+1, len(textlist)): if not ansi.is_color_token(textlist[j]): if memlength > len(textlist[j]): memlength -= len(textlist[j]) newlist.append(textlist[j]) else: break else: newcolor = ansi.figure_color([textlist[j]], newcolor, '')[0] newlist.append(textlist[j][:memlength]) newlist.append(chr(27) + "[0m") color = ansi.convert_tuple_to_ansi(newcolor) if color: newlist.append(color) newlist.append(textlist[j][memlength:]) for mem in textlist[j+1:]: newlist.append(mem) return newlist
def write(self, args): """ Handles writing information from the mud and/or Lyntin to the user. """ msg = args["message"] if type(msg) == types.StringType: msg = message.Message(msg, message.LTDATA) line = msg.data ses = msg.session if line == '' or self.showTextForSession(ses) == 0: return # we prepend the session name to the text if this is not the # current session sending text. pretext = "" if ses != None and ses != exported.get_current_session(): pretext = "[" + ses.getName() + "] " if msg.type == message.ERROR or msg.type == message.LTDATA: if msg.type == message.ERROR: pretext = "error: " + pretext else: pretext = "lyntin: " + pretext line = pretext + utils.chomp(line).replace("\n", "\n" + pretext) if exported.get_config("ansicolor") == 1: line = DEFAULT_ANSI + line sys.stdout.write(line + "\n") return elif msg.type == message.USERDATA: # we don't print user data in the textui return if exported.get_config("ansicolor") == 0: if pretext: if line.endswith("\n"): line = (pretext + line[:-1].replace("\n", "\n" + pretext) + "\n") else: line = pretext + line.replace("\n", "\n" + pretext) sys.stdout.write(line) sys.stdout.flush() return # each session has a saved current color for mud data. we grab # that current color--or user our default if we don't have one # for the session yet. if self._currcolors.has_key(ses): color = self._currcolors[ses] else: # need a copy of the list and not a reference to the list itself. color = list(DEFAULT_COLOR) # some sessions have an unfinished color as well--in case we # got a part of an ansi color code in a mud message, and the other # part is in another message. if self._unfinishedcolor.has_key(ses): leftover = self._unfinishedcolor[ses] else: leftover = "" lines = line.splitlines(1) if lines: for i in range(0, len(lines)): mem = lines[i] acolor = ansi.convert_tuple_to_ansi(color) color, leftover = ansi.figure_color(mem, color, leftover) if pretext: lines[i] = DEFAULT_ANSI + pretext + acolor + mem else: lines[i] = DEFAULT_ANSI + acolor + mem sys.stdout.write("".join(lines) + DEFAULT_ANSI) sys.stdout.flush() self._currcolors[ses] = color self._unfinishedcolor[ses] = leftover