3 >>> This code is released to the public domain. No warranty
4 whatsoever is provided, use it at your own risk. <<<
12 An "echo" replacement that can do color text.
16 myecho.exe [-fg color] [-bg color] "string to echo"
18 color can be: blue, red, green, yellow, cyan, violet, pink, black, white
19 brightblue, brightred, brightgreen, brightyellow,
20 brightviolet, brightpink, brightblack, gray, grey, or
36 MakeColor(BOOL fBg, char *sz)
41 Based on a string, return a color WORD.
59 if (strstr(sz, "bright"))
61 w = FOREGROUND_INTENSITY;
67 if (strstr(sz, "blue"))
71 if (strstr(sz, "red"))
75 if (strstr(sz, "green"))
77 w |= FOREGROUND_GREEN;
79 if (strstr(sz, "yellow"))
81 w |= (FOREGROUND_RED | FOREGROUND_GREEN);
83 if (strstr(sz, "cyan"))
85 w |= (FOREGROUND_BLUE | FOREGROUND_GREEN);
87 if ((strstr(sz, "violet")) ||
90 w |= (FOREGROUND_BLUE | FOREGROUND_RED);
92 if (strstr(sz, "white"))
94 w |= (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
96 if (strstr(sz, "black"))
98 w &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
100 if (strstr(sz, "gray") || strstr(sz, "grey"))
102 w &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
103 w |= FOREGROUND_INTENSITY;
115 main(int argc, char *argv[])
120 Program entry point; see above for usage.
133 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
137 BOOL fNewline = TRUE;
142 hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
143 if (INVALID_HANDLE_VALUE == hStdOut)
145 fprintf(stderr, "Failed to open handle to stdout, error=%u.\n",
151 // Save the current text colors.
153 if (0 == GetConsoleScreenBufferInfo(hStdOut, &csbiInfo))
155 fprintf(stderr, "Failed to save current text attr, error=%u\n",
161 // echo the string(s)
166 if (!_strcmpi(argv[x], "-fg"))
171 SetConsoleTextAttribute(hStdOut, MakeColor(0, argv[x]));
175 else if (!_strcmpi(argv[x], "-bg"))
180 SetConsoleTextAttribute(hStdOut, MakeColor(1, argv[x]));
184 else if (!_strcmpi(argv[x], "-n"))
192 if (TRUE == fNewline)
201 // restore the old text colors
203 (void)SetConsoleTextAttribute(hStdOut, csbiInfo.wAttributes);