Colorize expanded tabs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • qwweeeit

    #1

    Colorize expanded tabs

    Hi all,
    from a string embedding tabs I want to colorize them when expanded:

    # Starting from a string:
    a= '1234\t5678\t\t 90\nqwerty\nasd fg'
    # which embeds both tabs and lfs

    # printing it you obtain:
    print a
    # 1234 5678 90
    # qwerty
    # asdfg

    # print automatically expands tabs and interprets the NL.

    # to colorize the expanded tabs, I tried:
    print a.replace('\t', "\033[41m\t\033[0m") # 41 = red color
    # instead it works (in Linux) for the \n (substituted with a red
    space):
    print a.replace('\n', "\033[41m \033[0m\n")

    Can you help me?
    Bye.

  • Peter Otten

    #2
    Re: Colorize expanded tabs

    qwweeeit wrote:
    [color=blue]
    > Hi all,
    > from a string embedding tabs I want to colorize them when expanded:
    >
    > # Starting from a string:
    > a= '1234\t5678\t\t 90\nqwerty\nasd fg'
    > # which embeds both tabs and lfs
    >
    > # printing it you obtain:
    > print a
    > # 1234 5678 90
    > # qwerty
    > # asdfg
    >
    > # print automatically expands tabs and interprets the NL.
    >
    > # to colorize the expanded tabs, I tried:
    > print a.replace('\t', "\033[41m\t\033[0m") # 41 = red color
    > # instead it works (in Linux) for the \n (substituted with a red
    > space):
    > print a.replace('\n', "\033[41m \033[0m\n")
    >
    > Can you help me?
    > Bye.[/color]

    If all else fails you can colorize the tabs after converting them to
    spaces:

    def splititer(text, token):
    # A lazy iter(text.split (token)).
    # Probably not worth the effort.
    start = 0
    while True:
    try:
    end = text.index(toke n, start)
    except ValueError:
    break
    yield text[start:end]
    start = end + len(token)
    yield text[start:]

    def colortabs(text, tabcolor, normcolor, tabwidth=8):
    parts = splititer(text, "\t")
    part = parts.next()
    pos = len(part)
    yield part
    for part in parts:
    width = tabwidth - pos % tabwidth
    yield tabcolor
    yield " " * width
    yield normcolor
    yield part
    pos += width + len(part)

    print "\t1234\t5678\t \t90".replace(" \t", "\033[41m\t\033[0m")
    print "".join(colorta bs("\t1234\t567 8\t\t90", "\033[41m", "\033[0m"))

    Peter

    Comment

    • qwweeeit

      #3
      Re: Colorize expanded tabs

      Hi Peter,
      thank you for your replay, but I was looking for a very
      short routine. I even had in mind to use Linux & bash
      (only one command line).
      It seems that tab expansion, made by print, prevents
      the working of the escape sequences for colors.
      In fact, if you replace tab with a given number of spaces,
      all works well.
      That is :
      print "\t1234\t5678\t \t90".replace(" \t", "\033[41m \033[0m")
      correctly colorizes in red the spaces replacing the tabs...
      Unfortunately this is not tab expansion!
      Bye.

      Comment

      Working...