I’d like to write a wrapper function for the mvwprint/mvwchgat ncurses functions which prints the message in the specified window and then changes its attributes.
However, mvwchgat needs to know how many characters it should change – and I have no idea how to tell mvwchgat how long the formatted string is, since a strlen() on, for instance, "abc%d" obviously returns 5, because strlen doesn’t know what %d stands for …
In C99 or C11, you can use a line like this:
From the manual of
snprintf(emphasis mine):Since we are giving
snprintf0 as the size, then the output is always truncated and the output ofsnprintfwould be the number of characters that would have been written, which is basically the length of the string.In C89, you don’t have
snprintf. A workaround is to create a temporary file, or if you are in *nix open/dev/nulland write something like this: