tools: fixed colors if -v is specified

This commit is contained in:
Frank Morgner 2019-01-04 20:22:46 +01:00
parent fbb4d1f843
commit 3611b5c9f2
2 changed files with 21 additions and 21 deletions

View File

@ -362,24 +362,6 @@ set_drivers(struct _sc_ctx_options *opts, const scconf_list *list)
}
}
static int is_a_tty(FILE *fp)
{
if (fp != NULL) {
int fd = fileno(fp);
if (fd >= 0) {
#ifdef _WIN32
HANDLE h = (HANDLE)_get_osfhandle(fd);
if (h != INVALID_HANDLE_VALUE) {
return GetFileType(h) == FILE_TYPE_CHAR;
}
#else
return isatty(fd);
#endif
}
}
return 0;
}
static int
load_parameters(sc_context_t *ctx, scconf_block *block, struct _sc_ctx_options *opts)
{
@ -749,9 +731,6 @@ static void process_config_file(sc_context_t *ctx, struct _sc_ctx_options *opts)
* so at least one is NULL */
for (i = 0; ctx->conf_blocks[i]; i++)
load_parameters(ctx, ctx->conf_blocks[i], opts);
if (ctx->debug_file && !is_a_tty(ctx->debug_file))
ctx->flags |= SC_CTX_FLAG_DISABLE_COLORS;
}
int sc_ctx_detect_readers(sc_context_t *ctx)

View File

@ -174,6 +174,24 @@ void _sc_log(struct sc_context *ctx, const char *format, ...)
va_end(ap);
}
static int is_a_tty(FILE *fp)
{
if (fp != NULL) {
int fd = fileno(fp);
if (fd >= 0) {
#ifdef _WIN32
HANDLE h = (HANDLE)_get_osfhandle(fd);
if (h != INVALID_HANDLE_VALUE) {
return GetFileType(h) == FILE_TYPE_CHAR;
}
#else
return isatty(fd);
#endif
}
}
return 0;
}
#ifdef _WIN32
#define set_color(sc_color, win_color, vt100_color) \
do { if (colors & sc_color) { attr |= win_color; } } while (0)
@ -192,6 +210,9 @@ int sc_color_fprintf(int colors, struct sc_context *ctx, FILE * stream, const ch
HANDLE handle = fd >= 0 ? (HANDLE) _get_osfhandle(fd) : INVALID_HANDLE_VALUE;
#endif
if (!is_a_tty(stream))
colors = 0;
if (colors && (!ctx || (!(ctx->flags & SC_CTX_FLAG_DISABLE_COLORS)))) {
#ifdef _WIN32
WORD attr = 0;