der0keks
der0keks

Reputation: 78

strange ansi colour bevahiour in terminal

I have been playing around with the ansi colours in OSX terminal (bash v.3.2.57, Yosmite).

I have a problem with background colour behaviour once I fill up a terminal-window (as in, when it scrolls down).

The background colour will fill the right hand side white space, whilst also "skipping" a line (see picture). It works as I want it to until the output makes the window scroll. If I use the "clear" command the output will look fine until the output fills up a terminal window again.

The code below was simply getting the different combinations of colours (I truncated it a bit for this problem).

I have a feeling terminal is to blame rather than python, because the output works initially. Can anyone explain this behaviour? Cheers.

#coloured text in terminal
#ANSI escape sequences

std_txt = '\033[0m'

print('colour test' +'\n')
print('         X in 033[0;Xm')
for x in range(30,35):

    print ''.join(["\033[0;",str(x), 'm']) + 'test' +'\t'  + str(x)

print std_txt +'\n' + ('end')

print('colour test 2' +'\n')
print('         X in 033[0;30;Xm')
for x in range(40,45):

    print ''.join(["\033[0;30;",str(x), 'm']) + 'test' +'\t'  + str(x)

print std_txt +'\n' + ('end')

enter image description here

ps: What I mean by filling up a terminal window or scrolling. If your terminal-window is 80x24, filling it up will be using 24 lines, and >25 would make it scroll. Sorry, I found it hard to explain this in the problem.

Upvotes: 0

Views: 173

Answers (1)

Eric
Eric

Reputation: 1521

The problem is that you're not resetting the color before the newline, so the terminal tries to be helpful.

Change

print ''.join(["\033[0;30;",str(x), 'm']) + 'test' +'\t'  + str(x)

To:

print ''.join(["\033[0;30;",str(x), 'm']) + 'test' +'\t'  + str(x) + std_txt

Upvotes: 1

Related Questions