Posix command that moves cursor to specific position in terminal window

cursorposixterminal

in school we have been assigned a homework in which we are suppose to print an ascii art into a terminal window. A input is data in format [x_coordinate, y_coordinate, char_ascii_value] (there is no data for coordinates where shouldn't be print any character). I don't have any trouble actually doing it but I guess I am simply too lazy to go into for cycle and print an empty space every time there is no data for character, then go to another line in terminal and do the same, etc.

So I was thinking that, there must be an easier way! Since we are allowed to work only with commands which are in POSIX, is there any command that allows you to move cursor to specific position in terminal?

I ran into the command named tput and tput cup does exactly what I need but I am not quite sure if tput cup is in POSIX.

P.S. Please don't take this like some kind of cheating. I am just trying to find a way to make my life easier instead of brainless writing code.

Best Answer

As mikeserv explains, POSIX doesn't specify tput cup. POSIX does specify tput but only minimally. That said, tput cup is widely supported!

The standardised way of positioning the cursor is using ANSI escape sequences. To position the cursor you'd use something like

printf "\33[%d;%dH%s" "$Y" "$X" "$CHAR"

which will print $CHAR at line $Y and column $X. A more complete solution would be

printf "\337\33[%d;%dH%s\338" "$Y" "$X" "$CHAR"

which will restore the cursor position.