libtcod / python-tcod

A high-performance Python port of libtcod. Includes the libtcodpy module for backwards compatibility with older projects.
BSD 2-Clause "Simplified" License
410 stars 36 forks source link

Color-Coding not workely properly unless first '%c' is preceeded by any letter or symbol #71

Closed Wolfenswan closed 5 years ago

Wolfenswan commented 5 years ago

I'm running into an odd issue with color-coded strings, after updating to latest TCOD (was on 8.x.x). Issue persists with both old print_ex() and new con.print() method.

In a nutshell, color coding won't apply properly unless the first letters or symbols in the string are anything but '%c'. It will work fine if anything preceeds '%c%', including whitespace.

Examples:

tcod.console_set_color_control(tcod.COLCTRL_1, colors.pink, bgcolor)

# Will not work:
con.print(x, y, '%cTEST%c' % (tcod.COLCTRL_1, tcod.COLCTRL_STOP))

# Will work:
con.print(x, y, ' ABC %cTEST%c' % (tcod.COLCTRL_1, tcod.COLCTRL_STOP)) # Non-Formatted letters preceeding
con.print(x, y, '  %cTEST%c' % (tcod.COLCTRL_1, tcod.COLCTRL_STOP)) # Whitespace preceeding

# Multiple Colors #
tcod.console_set_color_control(tcod.COLCTRL_1, colors.pink, bgcolor)
tcod.console_set_color_control(tcod.COLCTRL_2, colors.purple, bgcolor)

# Only Test2 colored #
con.print(x, y, '%cTEST%c %cTEST2%c' % (tcod.COLCTRL_1, tcod.COLCTRL_STOP, tcod.COLCTRL_2,  tcod.COLCTRL_STOP))

# Both colored #
con.print(x, y, 'ABC %cTEST%c %cTEST2%c' % (tcod.COLCTRL_1, tcod.COLCTRL_STOP, tcod.COLCTRL_2,  tcod.COLCTRL_STOP))
HexDecimal commented 5 years ago

Thanks for pointing this out. I think I have a good idea where I went wrong in the current implementation.