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) elif msg.type == message.USERDATA: return line = msg.data ses = msg.session if line == '' or self.showTextForSession(ses) == 0: return line = line.replace("\t", " ") pretext = [] if ses != None and ses != exported.get_current_session(): attr = self.attr_session_ pretext = [("[", attr), (ses.getName(), attr | curses.A_BOLD), ("] ", attr)] default_attr = curses.A_NORMAL if msg.type == message.ERROR: pretext[:0] = [("! ", self.attr_error_)] elif msg.type == message.LTDATA: pretext[:0] = [("@ ", self.attr_lyntin_)] self._decode_colors(ses, default_attr, line, pretext)
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) elif msg.type == message.USERDATA: return line = msg.data ses = msg.session if line == '' or self.showTextForSession(ses) == 0: return line = line.replace("\t", " ") pretext = [] if ses != None and ses != exported.get_current_session(): attr = self.attr_session_ pretext = [ ("[", attr), (ses.getName(), attr | curses.A_BOLD), ("] ", attr ) ] default_attr = curses.A_NORMAL if msg.type == message.ERROR: pretext[:0] = [ ("! ", self.attr_error_) ] elif msg.type == message.LTDATA: pretext[:0] = [ ("@ ", self.attr_lyntin_) ] self._decode_colors(ses, default_attr, line, pretext)
def _decode_colors(self, ses, default_attr, line, pretext=[]): if self.unfinished_.has_key(ses): (currentcolor, leftover) = self.unfinished_[ses] else: currentcolor = list(ansi.DEFAULT_COLOR) leftover = '' for single in line.splitlines(1): current = [] tokens = ansi.split_ansi_from_text(leftover + single) leftover = '' lasttok = '' for tok in tokens: if ansi.is_color_token(tok): currentcolor, leftover = ansi.figure_color([tok], currentcolor, leftover) elif tok: attr = default_attr if currentcolor[ansi.PLACE_BOLD]: attr |= curses.A_BOLD if currentcolor[ansi.PLACE_UNDERLINE]: attr |= curses.A_UNDERLINE if currentcolor[ansi.PLACE_BLINK]: attr |= curses.A_BLINK if currentcolor[ansi.PLACE_REVERSE]: attr |= curses.A_REVERSE foreground = currentcolor[ansi.PLACE_FG] - 30 if 0 <= foreground and foreground <= 7: attr += curses_fore(foreground) background = currentcolor[ansi.PLACE_BG] - 40 if 0 <= background and background <= 7: attr += curses_back(background) lasttok = tok current.append((tok, attr)) if current: lines = self.lines_ current[:0] = pretext if not lasttok.endswith("\n"): # it is a prompt # # Append newline to prompts coming from another sessions if ses != exported.get_current_session(): current.append(("\n", curses.A_NORMAL)) else: if self.cfg_compact_ and self.prompt_ == current: # # Remove the identical prompt from previous output buffer: # lines[self.prompt_index_:self.prompt_index_ + 1] = [] else: self.prompt_ = current self.prompt_index_ = len(lines) self._append(current) # eliminating empty lines: elif current[0][0] != "\n" or not self.cfg_compact_: self._append(current) self.unfinished_[ses] = (currentcolor, leftover)
def _decode_colors(self, ses, default_attr, line, pretext=[]): if self.unfinished_.has_key(ses): (currentcolor, leftover) = self.unfinished_[ses] else: currentcolor = list(ansi.DEFAULT_COLOR) leftover = '' for single in line.splitlines(1): current = [] tokens = ansi.split_ansi_from_text(leftover + single) leftover = '' lasttok = '' for tok in tokens: if ansi.is_color_token(tok): currentcolor, leftover = ansi.figure_color([tok], currentcolor, leftover) elif tok: attr = default_attr if currentcolor[ansi.PLACE_BOLD]: attr |= curses.A_BOLD if currentcolor[ansi.PLACE_UNDERLINE]: attr |= curses.A_UNDERLINE if currentcolor[ansi.PLACE_BLINK]: attr |= curses.A_BLINK if currentcolor[ansi.PLACE_REVERSE]: attr |= curses.A_REVERSE foreground = currentcolor[ansi.PLACE_FG] - 30 if 0 <= foreground and foreground <= 7: attr += curses_fore(foreground) background = currentcolor[ansi.PLACE_BG] - 40 if 0 <= background and background <= 7: attr += curses_back(background) lasttok = tok current.append( (tok, attr) ) if current: lines = self.lines_ current[:0] = pretext if not lasttok.endswith("\n"): # it is a prompt # # Append newline to prompts coming from another sessions if ses != exported.get_current_session(): current.append( ("\n", curses.A_NORMAL) ) else: if self.cfg_compact_ and self.prompt_ == current: # # Remove the identical prompt from previous output buffer: # lines[self.prompt_index_:self.prompt_index_+1] = [] else: self.prompt_ = current self.prompt_index_ = len(lines) self._append(current) # eliminating empty lines: elif current[0][0] != "\n" or not self.cfg_compact_: self._append(current) self.unfinished_[ses] = (currentcolor, leftover)
def _executeBinding(self, binding): """ Returns the alias for this keybinding.""" ses = exported.get_current_session() action = exported.get_manager("alias").getAlias(ses, binding) if action: self._partk.handleinput(action) return 1 else: # we're commenting this out since it seems to be more annoying # than useful. it's not a good substitute for good documentation # for the tkui. # exported.write_error("%s is currently not bound to anything." % binding) return 0
def _executeBinding(self, binding): """ Returns the alias for this keybinding.""" ses = exported.get_current_session() action = exported.get_manager("alias").getAlias(ses, binding) if action: self._partk.handleinput(action) return 1 else: # we're commenting this out since it seems to be more annoying # than useful. it's not a good substitute for good documentation # for the tkui. # exported.write_error("%s is currently not bound to anything." % binding) return 0
def showTextForSession(self, ses): """ Returns whether or not we should show text for this session--it's a convenience method. We return a 1 if the session is None, it doesn't have a _snoop attribute, it's the current session, or get_config("snoop", ses) == 1. @param ses: the session we're looking at--if it's None we return a 1 @type ses: Session @returns: 1 if we should show text, 0 if not @rtype: boolean """ if ses == None or getattr(ses, "_snoop", None) == None \ or exported.get_current_session() == ses \ or exported.get_config("snoop", ses, 1) == 1: return 1 return 0
def showTextForSession(self, ses): """ Returns whether or not we should show text for this session--it's a convenience method. We return a 1 if the session is None, it doesn't have a _snoop attribute, it's the current session, or get_config("snoop", ses) == 1. @param ses: the session we're looking at--if it's None we return a 1 @type ses: Session @returns: 1 if we should show text, 0 if not @rtype: boolean """ if ses == None or getattr(ses, "_snoop", None) == None \ or exported.get_current_session() == ses \ or exported.get_config("snoop", ses, 1) == 1: return 1 return 0
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) if not hasattr(msg, "data"): return 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 line = pretext + utils.chomp(line).replace("\n", "\n" + pretext) self.window.write(line+"\n") return elif msg.type == message.MUDDATA: if "\n" not in msg.data: msg.data = msg.data + "\n" self.window.write(msg.data) 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) self.window.write(line) return
def startAutotyper(self, tkevent): """ This will start the autotyper. It will be called if you type <Ctrl>+<t>. There can be only one autotyper at a time. The autotyper cannot be started for the common session. """ if self._autotyper != None: exported.write_error("cannot start autotyper: already started.") return session = exported.get_current_session() if session.getName() == "common": exported.write_error("autotyper cannot be applied to common session.") return self._autotyper = Autotyper(self._partk._tk, self.autotyperDone) self._autotyper_ses = session exported.write_message("autotyper: started.")
def startAutotyper(self, tkevent): """ This will start the autotyper. It will be called if you type <Ctrl>+<t>. There can be only one autotyper at a time. The autotyper cannot be started for the common session. """ if self._autotyper != None: exported.write_error("cannot start autotyper: already started.") return session = exported.get_current_session() if session.getName() == "common": exported.write_error("autotyper cannot be applied to common session.") return self._autotyper = Autotyper(self._partk._tk, self.autotyperDone) self._autotyper_ses = session exported.write_message("autotyper: started.")
def buffer_write(msg, txtbuffer, currentcolor, unfinishedcolor): """ Handles writing messages to a Tk Text widget taking into accound ANSI colors, message types, session scoping, and a variety of other things. @param msg: the ui.message.Message to write to the buffer @type msg: ui.message.Message @param txtbuffer: the Tk Text buffer to write to @type txtbuffer: Text @param currentcolor: the current color that we should start with @type currentcolor: color (list of ints) @param unfinishedcolor: the string of unfinished ANSI color stuff that we'll prepend to the string we're printing @type unfinishedcolor: string @returns: the new color and unfinished color @rtype: list of ints, string """ global myui line = msg.data ses = msg.session if msg.type == message.ERROR: if line.endswith("\n"): line = "%s%s%s\n" % (ansi.get_color("b blue"), line[:-1], ansi.get_color("default")) else: line = "%s%s%s" % (ansi.get_color("b blue"), line[:-1], ansi.get_color("default")) elif msg.type == message.USERDATA: if myui._do_i_echo == 1: if line.endswith("\n"): line = "%s%s%s\n" % (ansi.get_color("b blue"), line[:-1], ansi.get_color("default")) else: line = "%s%s%s" % (ansi.get_color("b blue"), line[:-1], ansi.get_color("default")) else: # if echo is not on--we don't print this return currentcolor, unfinishedcolor elif msg.type == message.LTDATA: if line.endswith("\n"): line = "# %s\n" % line[:-1].replace("\n", "\n# ") else: line = "# %s" % line.replace("\n", "\n# ") # now we go through and handle writing all the data index = 0 start = 0 # we prepend the session name to the text if this is not the # current session sending text and if the Message is session # scoped. if ses != None and ses != exported.get_current_session(): pretext = "[%s]" % ses.getName() if line.endswith("\n"): line = pretext + line[:-1].replace("\n", "\n" + pretext) + "\n" else: line = pretext + line.replace("\n", "\n" + pretext) + "\n" # we remove all \\r stuff because it's icky line = line.replace("\r", "") tokens = ansi.split_ansi_from_text(line) # each session has a saved current color for MUDDATA. we grab # that current color--or use our default if we don't have one # for the session yet. additionally, 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 msg.type == message.MUDDATA: color = currentcolor.get(ses, list(DEFAULT_COLOR)) leftover = unfinishedcolor.get(ses, "") else: color = list(DEFAULT_COLOR) leftover = "" for mem in tokens: if ansi.is_color_token(mem): color, leftover = ansi.figure_color([mem], color, leftover) else: format = [] fg = "" bg = "" # handle reverse if color[ansi.PLACE_REVERSE] == 0: if color[ansi.PLACE_FG] == -1: fg = "37" else: fg = str(color[ansi.PLACE_FG]) if color[ansi.PLACE_BG] != -1: bg = str(color[ansi.PLACE_BG]) else: if color[ansi.PLACE_BG] == -1: fg = "30" else: fg = str(color[ansi.PLACE_BG] - 10) if color[ansi.PLACE_FG] == -1: bg = "47" else: bg = str(color[ansi.PLACE_FG] + 10) # handle bold if color[ansi.PLACE_BOLD] == 1: fg = "b" + fg # handle underline if color[ansi.PLACE_UNDERLINE] == 1: format.append("u") format.append(fg) if bg: format.append(bg) # mem = add_special_characters(mem) # insert the text using the formatting tuple we just generated txtbuffer.insert("end", mem.decode(UNICODE_ENCODING), tuple(format)) return color, leftover
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
def buffer_write(msg, txtbuffer, currentcolor, unfinishedcolor): """ Handles writing messages to a Tk Text widget taking into accound ANSI colors, message types, session scoping, and a variety of other things. @param msg: the ui.message.Message to write to the buffer @type msg: ui.message.Message @param txtbuffer: the Tk Text buffer to write to @type txtbuffer: Text @param currentcolor: the current color that we should start with @type currentcolor: color (list of ints) @param unfinishedcolor: the string of unfinished ANSI color stuff that we'll prepend to the string we're printing @type unfinishedcolor: string @returns: the new color and unfinished color @rtype: list of ints, string """ global myui line = msg.data ses = msg.session if msg.type == message.ERROR: if line.endswith("\n"): line = "%s%s%s\n" % (ansi.get_color("b blue"), line[:-1], ansi.get_color("default")) else: line = "%s%s%s" % (ansi.get_color("b blue"), line[:-1], ansi.get_color("default")) elif msg.type == message.USERDATA: if myui._do_i_echo == 1: if line.endswith("\n"): line = "%s%s%s\n" % (ansi.get_color("b blue"), line[:-1], ansi.get_color("default")) else: line = "%s%s%s" % (ansi.get_color("b blue"), line[:-1], ansi.get_color("default")) else: # if echo is not on--we don't print this return currentcolor, unfinishedcolor elif msg.type == message.LTDATA: if line.endswith("\n"): line = "# %s\n" % line[:-1].replace("\n", "\n# ") else: line = "# %s" % line.replace("\n", "\n# ") # now we go through and handle writing all the data index = 0 start = 0 # we prepend the session name to the text if this is not the # current session sending text and if the Message is session # scoped. if (ses != None and ses != exported.get_current_session()): pretext = "[%s]" % ses.getName() if line.endswith("\n"): line = (pretext + line[:-1].replace("\n", "\n" + pretext) + "\n") else: line = pretext + line.replace("\n", "\n" + pretext) + "\n" # we remove all \\r stuff because it's icky line = line.replace("\r", "") tokens = ansi.split_ansi_from_text(line) # each session has a saved current color for MUDDATA. we grab # that current color--or use our default if we don't have one # for the session yet. additionally, 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 msg.type == message.MUDDATA: color = currentcolor.get(ses, list(DEFAULT_COLOR)) leftover = unfinishedcolor.get(ses, "") else: color = list(DEFAULT_COLOR) leftover = "" for mem in tokens: if ansi.is_color_token(mem): color, leftover = ansi.figure_color([mem], color, leftover) else: format = [] fg = "" bg = "" # handle reverse if color[ansi.PLACE_REVERSE] == 0: if color[ansi.PLACE_FG] == -1: fg = "37" else: fg = str(color[ansi.PLACE_FG]) if color[ansi.PLACE_BG] != -1: bg = str(color[ansi.PLACE_BG]) else: if color[ansi.PLACE_BG] == -1: fg = "30" else: fg = str(color[ansi.PLACE_BG] - 10) if color[ansi.PLACE_FG] == -1: bg = "47" else: bg = str(color[ansi.PLACE_FG] + 10) # handle bold if color[ansi.PLACE_BOLD] == 1: fg = "b" + fg # handle underline if color[ansi.PLACE_UNDERLINE] == 1: format.append("u") format.append(fg) if bg: format.append(bg) # insert the text using the formatting tuple we just generated txtbuffer.insert('end', _decode(mem), tuple(format)) return color, leftover
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