I've been making tools for the command line for a little while now. However, one thing that stuck out to me when I was going over some old projects was this:
// I ABSOLUTELY NEED THIS BUT IDK WHY!!!!
system("Title Something");
So I decided to investigate. When I removed it and ran the tool again in a simulated environment (more specifically VSCodes simulated console) everything seemed fine. However when I tried running it in cmd I saw that all of the colors that I had previously used in my code where broken.
So I pulled up a temporary projects like this one:
#include <stdio.h>
int main(){
printf("\033[1;31mI am red!\033[0m");
}
And when I ran it I got:
C:\tests\ANSI-test>hello
←[1;31mI am red!←[0m
Which was similar to what I had previously gotten with my other tool. And so I added back the seemingly random system("Title Something");
line (making sure I included stdlib.h):
#include <stdio.h>
#include <stdlib.h>
int main(){
system("Title Something");
printf("\033[1;31mI am red!\033[0m");
}
And I got color working again!:
C:\tests\ANSI-test>hello
I am red! (pretend this is red)
I'm not sure exactly what causes this, I have a couple of speculations:
- the system function initializes something to do with the console, which triggers the working of ANSI colors
- system somehow calls to WinAPI and tells it to use colors inside the terminal
I did try searching for this but I didn't really find anything related to it other than this: