How to fix position of message printed in reverse video at the bottom of terminal currently I am doing this
printf("\033[7m more?\033[m");
How to fix position of message printed in reverse video at the bottom of terminal currently I am doing this
printf("\033[7m more?\033[m");
The following ANSI escape sequences can be used to accomplish printing text in the bottom row:
If you know the number of rows on the screen you could instead use the Absolute Horizontal and Vertical Position command. (With Windows escape sequence emulation, ANSI.SYS, you may not have access to the Cursor Next Line Down or Cursor Absolute Horizontal movement commands)
The final code might look like:
fputs("\033[s\033[99E\033[2K\033[7m more?\033[m\033[u", stdout);
Constants do not tell the reader much. The C statement
printf("\033[7m more?\033[m");
can be done using the termcap interface, e.g., from ncurses by retrieving the mode reverse or "mr"
capability (the "\033[7m"
substring), and the mode default "md"
(the "\033[m"
substring). In fact, that is how the pagers "more" and "less" are written.
If you were using termcap, then you could retrieve using tgetstr
the cursor movement "cm"
capability and then use tgoto
with tputs
to send the cursor to some position on the screen.
That "cm"
would be something like "\E[%i%d;%dH"
, indicating that the zero-based coordinates are incremented and then converted to decimal. VT100-based terminals start counting rows/columns from 1
(one). Other terminals may not.
For reference, the termcap manual page from ncurses lists these calls, while the terminfo format manual page lists termcap mnemonics. terminfo is actually preferred (see manual page).
One feature lacking from the termcap interface is how to obtain the position of the last line on the screen. The OP did not give the context in which the printf
is used. It could be an application where clearing the screen is undesirable. In that case, one could use ioctl(0, TIOCGWINSZ, &ws);
(see How to get terminal window width?) to obtain the screen size and use the resulting height as a parameter in the tgoto
call. The alternative using hardcoded escape sequences to move "past" the bottom of the screen is less flexible because their availability varies across terminal types.
If clearing the screen is acceptable, using curses is simpler than termcap. Here is a short example:
#include <curses.h>
int main(void)
{
initscr(); cbreak(); noecho();
attron(A_REVERSE);
mvprintw(LINES - 1, 0, " more?");
getch();
endwin();
return 0;
}