Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio output via stdout and UDP #440

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
Allow color to be used when stdout is redirected with -O
  • Loading branch information
ars-ka0s committed Sep 25, 2023
commit 74d6cc6bc26ca8b609711d426c064989bf32d988
11 changes: 7 additions & 4 deletions src/direwolf.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,16 @@ int main (int argc, char *argv[])
* Default will be no colors if stdout is not a terminal (i.e. piped into
* something else such as "tee") but command line can override this.
*/
for (j=1; j<argc; j++) {
if (strcmp(argv[j], "-O") == 0) {
O_opt = 1;
}
}

#if __WIN32__
t_opt = _isatty(_fileno(stdout)) > 0;
t_opt = _isatty(_fileno(O_opt ? stderr : stdout)) > 0;
#else
t_opt = isatty(fileno(stdout));
t_opt = isatty(fileno(O_opt ? stderr : stdout));
#endif
/* 1 = normal, 0 = no text colors. */
/* 2, 3, ... alternate escape sequences for different terminals. */
Expand All @@ -292,8 +297,6 @@ int main (int argc, char *argv[])
if (strcmp(argv[j], "-t") == 0) {
t_opt = atoi (argv[j+1]);
//dw_printf ("DEBUG: text color option = %d.\n", t_opt);
} else if (strcmp(argv[j], "-O") == 0) {
O_opt = 1;
}
}

Expand Down
54 changes: 31 additions & 23 deletions src/textcolor.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ void text_color_init (int enable_color, int redirect_output)
{
if (redirect_output != 0) {
g_dw_printf_dest = stderr;
enable_color = 0;
} else {
g_dw_printf_dest = stdout;
}
Expand All @@ -195,7 +194,12 @@ void text_color_init (int enable_color, int redirect_output)
COORD coord;
DWORD nwritten;

h = GetStdHandle(STD_OUTPUT_HANDLE);
if (redirect_output != 0) {
h = GetStdHandle(STD_ERROR_HANDLE);
} else {
h = GetStdHandle(STD_OUTPUT_HANDLE);
}

if (h != NULL && h != INVALID_HANDLE_VALUE) {

GetConsoleScreenBufferInfo (h, &csbi);
Expand All @@ -215,17 +219,17 @@ void text_color_init (int enable_color, int redirect_output)
int t;
for (t = 0; t <= MAX_T; t++) {
text_color_init (t, redirect_output);
printf ("-t %d", t);
if (t) printf (" [white background] ");
printf ("\n");
printf ("%sBlack ", t_black[t]);
printf ("%sRed ", t_red[t]);
printf ("%sGreen ", t_green[t]);
printf ("%sDark-Green ", t_dark_green[t]);
printf ("%sYellow ", t_yellow[t]);
printf ("%sBlue ", t_blue[t]);
printf ("%sMagenta ", t_magenta[t]);
printf ("%sCyan \n", t_cyan[t]);
fprintf (g_dw_printf_dest,"-t %d", t);
if (t) fprintf (g_dw_printf_dest, " [white background] ");
fprintf (g_dw_printf_dest,"\n");
fprintf (g_dw_printf_dest,"%sBlack ", t_black[t]);
fprintf (g_dw_printf_dest,"%sRed ", t_red[t]);
fprintf (g_dw_printf_dest,"%sGreen ", t_green[t]);
fprintf (g_dw_printf_dest,"%sDark-Green ", t_dark_green[t]);
fprintf (g_dw_printf_dest,"%sYellow ", t_yellow[t]);
fprintf (g_dw_printf_dest,"%sBlue ", t_blue[t]);
fprintf (g_dw_printf_dest, "%sMagenta ", t_magenta[t]);
fprintf (g_dw_printf_dest, "%sCyan \n", t_cyan[t]);
}
exit (EXIT_SUCCESS);
}
Expand All @@ -238,9 +242,9 @@ void text_color_init (int enable_color, int redirect_output)
if (t < 0) t = 0;
if (t > MAX_T) t = MAX_T;

printf ("%s", t_background_white[t]);
printf ("%s", clear_eos);
printf ("%s", t_black[t]);
fprintf (g_dw_printf_dest, "%s", t_background_white[t]);
fprintf (g_dw_printf_dest, "%s", clear_eos);
fprintf (g_dw_printf_dest, "%s", t_black[t]);
}
#endif
}
Expand Down Expand Up @@ -291,7 +295,11 @@ void text_color_set ( enum dw_color_e c )
break;
}

h = GetStdHandle(STD_OUTPUT_HANDLE);
if (dw_printf_redirected()) {
h = GetStdHandle(STD_ERROR_HANDLE);
} else {
h = GetStdHandle(STD_OUTPUT_HANDLE);
}

if (h != NULL && h != INVALID_HANDLE_VALUE) {
SetConsoleTextAttribute (h, attr);
Expand All @@ -316,30 +324,30 @@ void text_color_set ( enum dw_color_e c )

default:
case DW_COLOR_INFO:
printf ("%s", t_black[t]);
fprintf (g_dw_printf_dest, "%s", t_black[t]);
break;

case DW_COLOR_ERROR:
printf ("%s", t_red[t]);
fprintf (g_dw_printf_dest, "%s", t_red[t]);
break;

case DW_COLOR_REC:
// Bright green is very difficult to read against a while background.
// Let's use dark green instead. release 1.6.
//printf ("%s", t_green[t]);
printf ("%s", t_dark_green[t]);
fprintf (g_dw_printf_dest, "%s", t_dark_green[t]);
break;

case DW_COLOR_DECODED:
printf ("%s", t_blue[t]);
fprintf (g_dw_printf_dest, "%s", t_blue[t]);
break;

case DW_COLOR_XMIT:
printf ("%s", t_magenta[t]);
fprintf (g_dw_printf_dest, "%s", t_magenta[t]);
break;

case DW_COLOR_DEBUG:
printf ("%s", t_dark_green[t]);
fprintf (g_dw_printf_dest, "%s", t_dark_green[t]);
break;
}
}
Expand Down