I am trying to draw a black box on terminal at top left corner with stdin nonblocking mode turned on. The black box has size 10 cols and 5 rows. I am using the following code to draw it.
#include <sys/fcntl.h>
#include <unistd.h>
#include <wchar.h>
int main() {
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
char s[] = "\e[1;1H\e[1;1H\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[E\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[E\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[E\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[E\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀\e[38;5;0m\e[48;5;0m▀";
// char s1[] = "\e[1;1H\e[1;1H▀▀▀▀▀▀▀▀▀▀\e[E▀▀▀▀▀▀▀▀▀▀\e[E▀▀▀▀▀▀▀▀▀▀\e[E▀▀▀▀▀▀▀▀▀▀\e[E▀▀▀▀▀▀▀▀▀▀\e[E";
write(1, s, sizeof(s)-1);
fflush(stdout);
return 0;
}
However what I get is an incomplete box:
▀▀▀▀▀▀▀▀▀▀
▀▀▀▀▀▀▀▀▀▀
▀▀▀▀▀▀▀▀▀▀
▀▀▀▀▀▀▀▀▀▀
▀▀▀▀▀▀▀
The printed string is produced by inserting \e[38;5;0m\e[48;5;0m
before each half block character.
What I tried didn't work are
- using different shell
- using different terminal emulators (Alacritty, Terminal.app, iTerm2)
What I tried that worked but not what I wanted
- printing in blocking mode
- remove code that changes color (
\e[38;5;0m\e[48;5;0m
) - changing half block character to normal printable ASCII character
Why is this and how can I fix it?