Is there an escape code to pause the terminal screen refresh?

125 Views Asked by At

I have an application that displays images using 8-bit ANSI-art. This is handy for viewing images on a remote machine via SSH. When the viewer starts, it replaces all the 8-bit palette values from 16-255 with my standard values. This runs fine on the Mac terminal, but the xterm display flickers because it is trying to refresh while it is still executing these 240 commands.

I get the same thing when the program exits, and I have to reset the colours. Here is the code for resetting the palette when the program exits.

for (int N=16; N<256; ++N) printf("\e]104;%d\a", N);

I can see the colours changing in the terminal as it runs.

I have not found an escape code that resets the whole palette. Sending Ctrl-C resets some things but not the palette. All the examples I have found use a loop like this. It would be nice to find a reset escape sequence if there is one, but I will also need a way to pause the screen refresh until the commands have finished.

I tried running all the escape sequences into one big string and submitting all of them at once with puts(). That did not do the trick.

I hoped to find an escape sequence that pauses the screen refresh, and another that un-pauses it. It seems like something that ought to be there, and I am not seeing it. Or, if we know for sure that no such escape sequences exist, then I can stop looking. There may be other ways of fixing this other than escape codes. However, the application is handiest when working remotely, so I want it to work on whatever terminal is running at the time, not just xterm. I can ignore the flickering if I have to.

PS: I have a workaround. Xterm supports 24-bit Truecolor. I can use that and not change the 8-bit palette.

2

There are 2 best solutions below

0
Richard Kirk On BEST ANSWER

Not what the title asks for, but enabling the alternative screen buffer is a fix.

Print "\e[?1049h to enable when the program starts and and "\e[?1049l to disable.

The Wikipedia entry says little more than that, so it is easy to ignore. Enabling the alternative screen buffer means the application works with a terminal-sized window of characters. This is probably what an interactive terminal program needs rather than add all the screen refreshes to the current scrolling terminal buffer. Disabling restores the terminal to its previous state.

1
Thomas Dickey On

xterm patch #334 (August 2018) supports escape sequences which let you switch back/forth to/from previously initialized "ANSI" color palettes in a single escape sequence.

It is summarized in XTerm Control Sequences:

CSI # P
CSI Pm # P
          Push current dynamic- and ANSI-palette colors onto stack
          (XTPUSHCOLORS), xterm.  Parameters (integers in the range 1
          through 10, since the default 0 will push) may be used to
          store the palette into the stack without pushing.

CSI # Q
CSI Pm # Q
          Pop stack to set dynamic- and ANSI-palette colors
          (XTPOPCOLORS), xterm.  Parameters (integers in the range 1
          through 10, since the default 0 will pop) may be used to
          restore the palette from the stack without popping.

CSI # R   Report the current entry on the palette stack, and the number
          of palettes stored on the stack, using the same form as
          XTPOPCOLOR (default = 0) (XTREPORTCOLORS), xterm.

The sources have a demo script for the (simpler) Solarized color schemes, but the saved/restored palette works for 256-colors.