0

In Python, I want to both (a) use the str.format method to print strings with a fixed width and (b) use yachalk/chalk (see also this answer) 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?

from yachalk import chalk
s1 = "e4"
s2 = "c5"
s3 = "Nf3"
s = '{:8}'.format(s1) + '{:8}'.format(s2) + '{:8}'.format(s3)
c1 = chalk.yellow(s1)
c2 = chalk.yellow(s2)
c3 = chalk.yellow(s3)
c = '{:8}'.format(c1) + '{:8}'.format(c2) + '{:8}'.format(c3)
print(s)
print(c)

enter image description here

Jim Ratliff
  • 623
  • 7
  • 14

1 Answers1

0

Problem solved. Even though I couldn't apply str.format method to the output of yachalk, I could apply the str.format method first and then apply yachalk.

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)

See output as pasted graphic:

enter image description here

Jim Ratliff
  • 623
  • 7
  • 14