0

I have a class to print "TextBoxes" that look similar to

#========================================#
|                                        |
|                                        |
|                                        |
|                                        |
|                                        |
#========================================#

When given dimensions. You can then assign lines text by calling the method setLineText. Each line is stored in a list and printed with the method render with a simple for loop.

Everything works fine and dandy until I introduce Colorama to the mix, when I use my setLineText method with any Colorama formatting the line that I changed will always be short 5 spaces per call I used for formatting

For example,

MainBox = views.TextBox(35, 6)
MainBox.setLineText(1, "Hello! " + Fore.RED + User.name + Fore.RESET)
MainBox.Render()

will output

#=================================#
| Hello! User           |
|                                 |
|                                 |
|                                 |
#=================================#

My question is, is there a way to avoid prevent this from happening?

EDIT: As an extra note, I'm using windows.

ANSWER: After tons of testing, any regex I could come up with didn't work. My solution is to just use sys.stdout.write() to print the color changes and resets. It works well and hardly even need to change my current code

THUNDERGROOVE
  • 197
  • 1
  • 10

1 Answers1

2

This is because the setLineText method (probably) considers the coloration characters (Fore.RED, Fore.RESET) as normal characters, and so it removes 5 spaces that are not compensated by any character (as the chars used for coloration are not shown in the output).

The solution is to reimplement your setLineText method to only take into account printed chracters. (provide that method's implementation if you need specific help)

EDIT:

You just have to do this at the beginning of the setLineText method: (add import string at the beginning of the file too)

# Extracting printable characters only
textSize = len([c for c in text if c in string.printable])

And use this textSize when aligning your text (instead of len(text)).

You may also have to strip the text from any 'color' related characters before doing this. Regex is the way to go, and I'd suggest you check this SO question (about a similar problem, with mIRC color codes).

Community
  • 1
  • 1
halflings
  • 1,540
  • 1
  • 13
  • 34
  • [Here](http://pastebin.com/aMbXuTW6) is a paste of the method. It's part of a class but you should be able to make out what it's doing. I'm not exactly sure of how Colorama does it's magic, maybe I should look into it. – THUNDERGROOVE May 02 '13 at 01:38
  • Post the code as a part of your question, it'll make it easier for others to answer you :-) (and for those who have the same problem to get the answers) – halflings May 02 '13 at 01:40
  • Added a solution that should fix your problem :-) Read this SO question for more information about how to strip non-printable characters: http://stackoverflow.com/questions/92438/stripping-non-printable-characters-from-a-string-in-python – halflings May 02 '13 at 01:46
  • That doesn't seem to work. You can't get the length of a generator and and after a quick test, whatever calls Colorama uses are considered "printable" I used >>>print all(c for c in Fore.RED + "Test" + Fore.RESET if c in string.printable) – THUNDERGROOVE May 02 '13 at 01:59
  • Oupsie, I should've used list comprehension instead of generator comprehension, I'll fix that right away. And I'll try to see how can these characters be removed :). – halflings May 02 '13 at 02:02
  • Can you provide an example of the color codes used by Colorama ? – halflings May 02 '13 at 02:03
  • I tried using filter(lambda x: x in string.printable, temp) but it just printed the ANSI escape sequences without color like Hello! [31mTHUNDERGROOVE[39m – THUNDERGROOVE May 02 '13 at 02:04
  • Check my edit. (stripping the text from the color codes require using regular expressions, and there's a question that have already been asked about that particular problem) – halflings May 02 '13 at 02:08