MBasith
MBasith

Reputation: 1499

Python Formatting ASCII Escape Sequences with Format Method

Hi I am trying to nicely format a table with color using ASCII Escape sequences but when I apply the color, the format method does not format the string causing the table to be out of alignment. Is there a reason why the the format method is not formatting the string after the color ASCII escape sequences are applied? Also, is there a better way to apply text coloring (Note, I cannot install Coloroma or any other modules on this system)?

Below is testable code that can be used to demonstrate the issue.

Code:

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

    def disable(self):
        self.HEADER = ''
        self.OKBLUE = ''
        self.OKGREEN = ''
        self.WARNING = ''
        self.FAIL = ''
        self.ENDC = ''


def pass_fail(color, string):
    if color == 'red':
        return bcolors.FAIL + string + bcolors.ENDC
    elif color == 'green':
        return bcolors.OKGREEN + string + bcolors.ENDC
    elif color == 'yellow':
        return bcolors.WARNING + string + bcolors.ENDC

print('\n')
line_sep = '\t+' + ('-' * 57) + '+'
format_table = line_sep + '\n' + '\t| {:<42} | {:^10} |'

Critical = 100
Major = 200
Minor = 10

print('')
print(format_table.format('Number of Critical Issues Are: ', str(Critical)))
print(format_table.format('Number of Major Issues Are: ',  str(Major)))
print(format_table.format('Number of Minor Issues Are: ', (Minor)))
print(line_sep)


print('')
print(format_table.format('Number of Critical Issues Are: ', pass_fail('green', str(Critical))))
print(format_table.format('Number of Major Issues Are: ',  pass_fail('red', str(Major))))
print(format_table.format('Number of Minor Issues Are: ', pass_fail('yellow', str(Minor))))
print(line_sep)

1st Output without color formatted correctly:

+---------------------------------------------------------+
| Number of Critical Issues Are:             |    100     |
+---------------------------------------------------------+
| Number of Major Issues Are:                |    200     |
+---------------------------------------------------------+
| Number of Minor Issues Are:                |     10     |
+---------------------------------------------------------+

2nd Output with color formatting is not being applied:

+---------------------------------------------------------+
| Number of Critical Issues Are:             | 100 |
+---------------------------------------------------------+
| Number of Major Issues Are:                | 200 |
+---------------------------------------------------------+
| Number of Minor Issues Are:                | 10 |
+---------------------------------------------------------+

Upvotes: 1

Views: 462

Answers (1)

Jordan McQueen
Jordan McQueen

Reputation: 787

The string format method is indeed formatting your string, but it is including all of the ANSI Color Escape characters in its count of your string length when determining the padding.

To check this: len(pass_fail('yellow', str(Minor)))

The solution to this problem is to rework the string formatting so that the ANSI Color Code characters don't trick the formatter. You might have to do this by manually adjusting the column widths.

Upvotes: 1

Related Questions