Skip to content

Commit b978f78

Browse files
peffgitster
authored andcommitted
color: return bool from want_color()
The point of want_color() is to take in a git_colorbool enum value and collapse it down to a single true/false boolean, letting UNKNOWN fall back to the color.ui default and checking isatty() for AUTO. Let's make that more clear in the type system by returning a bool rather than an integer. This sadly still does not help us much with compiler warnings for using the two types interchangeably. But it helps make the intent more clear to a human reader. We still retain the idempotency of want_color(), because in C a bool true/false converts to 1/0 when converted to an integer, which corresponds to GIT_COLOR_ALWAYS and GIT_COLOR_NEVER. So you can store the bool in a git_colorbool and get the right result (something a few pieces of code still do, but which we'll clean up in further patches). Note that we rely on this same bool/int conversion for check_auto_color(). We cache its results in a tristate int with "-1" as "not yet set", but we can assign to it (and return it) with implicit conversions to/from bool. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e9330ae commit b978f78

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

color.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,20 +391,20 @@ enum git_colorbool git_config_colorbool(const char *var, const char *value)
391391
return GIT_COLOR_AUTO;
392392
}
393393

394-
static int check_auto_color(int fd)
394+
static bool check_auto_color(int fd)
395395
{
396396
static int color_stderr_is_tty = -1;
397397
int *is_tty_p = fd == 1 ? &color_stdout_is_tty : &color_stderr_is_tty;
398398
if (*is_tty_p < 0)
399399
*is_tty_p = isatty(fd);
400400
if (*is_tty_p || (fd == 1 && pager_in_use() && pager_use_color)) {
401401
if (!is_terminal_dumb())
402-
return 1;
402+
return true;
403403
}
404-
return 0;
404+
return false;
405405
}
406406

407-
int want_color_fd(int fd, enum git_colorbool var)
407+
bool want_color_fd(int fd, enum git_colorbool var)
408408
{
409409
/*
410410
* NEEDSWORK: This function is sometimes used from multiple threads, and

color.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ enum git_colorbool git_config_colorbool(const char *var, const char *value);
106106
* Return a boolean whether to use color, where the argument 'var' is
107107
* one of GIT_COLOR_UNKNOWN, GIT_COLOR_NEVER, GIT_COLOR_ALWAYS, GIT_COLOR_AUTO.
108108
*/
109-
int want_color_fd(int fd, enum git_colorbool var);
109+
bool want_color_fd(int fd, enum git_colorbool var);
110110
#define want_color(colorbool) want_color_fd(1, (colorbool))
111111
#define want_color_stderr(colorbool) want_color_fd(2, (colorbool))
112112

0 commit comments

Comments
 (0)