Example #1
0
    def getInfo(self, text="", colorize=0):
        """
    Returns information about the highlights in here.

    This is used by #highlight to tell all the highlights involved
    as well as #write which takes this information and dumps
    it to the file.

    @param text: we return info on highlights that match this text
    @type  text: string

    @param colorize: whether (1) or not (0) to colorize the style
        text in the style
    @type  colorize: int

    @return: list of strings where each string represents a highlight
    @rtype: list of strings
    """
        listing = list(self._highlights.keys())

        if text:
            listing = utils.expand_text(text, listing)

        data = []
        for mem in listing:
            if colorize == 1:
                data.append("highlight {%s%s%s} {%s}" %
                            (ansi.get_color(self._highlights[mem][0]),
                             self._highlights[mem][0],
                             ansi.get_color("default"), utils.escape(mem)))
            else:
                data.append("highlight {%s} {%s}" %
                            (self._highlights[mem][0], utils.escape(mem)))

        return data
Example #2
0
  def addHighlight(self, style, text):
    """
    Adds a highlight to the dict.

    @param style: the style to highlight the text with
    @type  style: string

    @param text: the text to highlight
    @type  text: string
    """
    style = style.lower()
    markup, compiled = ansi.get_color(style), utils.compile_regexp(text, 0, 1)
    self._highlights[text] = (style, markup, compiled)
Example #3
0
  def getInfo(self, text="", colorize=0):
    """
    Returns information about the highlights in here.

    This is used by #highlight to tell all the highlights involved
    as well as #write which takes this information and dumps
    it to the file.

    @param text: we return info on highlights that match this text
    @type  text: string

    @param colorize: whether (1) or not (0) to colorize the style
        text in the style
    @type  colorize: int

    @return: list of strings where each string represents a highlight
    @rtype: list of strings
    """
    listing = self._highlights.keys()

    if text:
      listing = utils.expand_text(text, listing)

    data = []
    for mem in listing:
      if colorize == 1:
        data.append("highlight {%s%s%s} {%s}" % 
                    (ansi.get_color(self._highlights[mem][0]),
                     self._highlights[mem][0], 
                     ansi.get_color("default"),
                     utils.escape(mem)))
      else:
        data.append("highlight {%s} {%s}" % 
                    (self._highlights[mem][0], utils.escape(mem)))

    return data
Example #4
0
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
Example #5
0
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