Closed jimratliff closed 2 years ago
Problem solved, even though I couldn't apply str.format method to the output from chalk, I could apply the str.format method first and then apply chalk.
from yachalk import chalk
s1 = "e4"
s2 = "c5"
s3 = "Nf3"
s = '{:8}'.format(s1) + '{:8}'.format(s2) + '{:8}'.format(s3)
c1 = chalk.yellow('{:8}'.format(s1))
c2 = chalk.yellow('{:8}'.format(s2))
c3 = chalk.yellow('{:8}'.format(s3))
c = c1 + c2 + c3
print(s)
print(c)
Here's the output, just as I wanted.
Sorry for the late response. Yes exactly, as explained in https://github.com/bluenote10/yachalk/issues/6 as well:
That is a common problem with ANSI escape codes. They are actually part of the string, just not visible! So if you colour your string, it actually has increased its length, and so the width-based alignment has unexpected behavior. What you'll have to do is to either reverse the order of width-alignment and coloring [...] or subtract the size increment due to ANSI escape codes from your target width.
I want to both (a) use the str.format method to print strings with a fixed width and (b) use yachalk to color the output sent to the terminal.
When I use str.format without yachalk, I successfully achieve the fixed-width output. However, when I add yachalk to the mix, I get the colored output I seek, but the strings all run together, ignoring the fixed-width formatting.
Here's my test-case code, followed by screenshot of the terminal output:
Am I doing something wrong? Is yachalk incompatible with str.format?