I have a python string:
my_string = 'thisisjustadummystringfortestingpurposes'
I want to print some regions of the string with color red, so I use ANSI escape codes:
- red: '\x1b[31m'
- black: '\x1b[0m'
Then what I do is, I create a new string with the ANSI codes inserted.
Example:
my_color_seq = seq[:5] # 0:5 will be black
my_color_seq += '\x1b[31m' # start red
my_color_seq += seq[5:10] # 5:10 will be red
my_color_seq += '\x1b[0m' # start black
my_color_seq += seq[10:] # 10: will be black
print(my_color_seq)
This code works and correctly prints the region 5:10 red. I have even created a loop where I can use a list of indexes and lengths for the regions that I want colored with red.
Now the problem is that in my colored string contain non visible characters (the ANSI codes), so the index of the characters is no longer what it seems to be.
Imagine that I want to now process again the colored string by painting some other fragments in blue. I can no longer trust the indexes since my string is now larger than it seems.
Of course I can update the indexes to point the correct locations in the new string, but I was just wondering if there is an easier way of doing this.