Python’s `str.format()`, fill characters, and ANSI colors

前端 未结 2 1320
终归单人心
终归单人心 2020-12-16 01:11

In Python 2, I’m using str.format() to align a bunch of columns of text I’m printing to a terminal. Basically, it’s a table, but I’m not printing any borders o

相关标签:
2条回答
  • 2020-12-16 01:46

    This is a very late answer, left as bread crumbs for anyone who finds this page while struggling to format text with built-in ANSI color codes.

    byoungb's comment about making padding decisions on the length of pre-colorized text is exactly right. But if you already have colored text, here's a work-around:

    See my ansiwrap module on PyPI. Its primary purpose is providing textwrap for ANSI-colored text, but it also exports ansilen() which tells you "how long would this string be if it didn't contain ANSI control codes?" It's quite useful in making formatting, column-width, and wrapping decisions on pre-colored text. Add width - ansilen(s) spaces to the end or beginning of s to left (or respectively, right) justify s in a column of your desired width. E.g.:

    def ansi_ljust(s, width):
        needed = width - ansilen(s)
        if needed > 0:
            return s + ' ' * needed
        else:
            return s
    

    Also, if you need to split, truncate, or combine colored text at some point, you will find that ANSI's stateful nature makes that a chore. You may find ansi_terminate_lines() helpful; it "patch up" a list of sub-strings so that each has independent, self-standing ANSI codes with equivalent effect as the original string.

    The latest versions of ansicolors also contain an equivalent implementation of ansilen().

    0 讨论(0)
  • 2020-12-16 01:52

    Python doesn't distinguish between 'normal' characters and ANSI colour codes, which are also characters that the terminal interprets.

    In other words, printing '\x1b[92m' to a terminal may change the terminal text colour, Python doesn't see that as anything but a set of 5 characters. If you use print repr(line) instead, python will print the string literal form instead, including using escape codes for non-ASCII printable characters (so the ESC ASCII code, 27, is displayed as \x1b) to see how many have been added.

    You'll need to adjust your column alignments manually to allow for those extra characters.

    Without your actual code, that's hard for us to help you with though.

    0 讨论(0)
提交回复
热议问题