Skip to content

Colorize patchset #14621

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions cmd/zfs/zfs_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3440,6 +3440,8 @@ print_header(list_cbdata_t *cb)
boolean_t first = B_TRUE;
boolean_t right_justify;

color_start(ANSI_BOLD);

for (; pl != NULL; pl = pl->pl_next) {
if (!first) {
(void) printf(" ");
Expand All @@ -3466,9 +3468,31 @@ print_header(list_cbdata_t *cb)
(void) printf("%-*s", (int)pl->pl_width, header);
}

color_end();

(void) printf("\n");
}

/*
* Decides on the color that the avail value should be printed in.
* > 80% used = yellow
* > 90% used = red
*/
static const char *
zfs_list_avail_color(zfs_handle_t *zhp)
{
uint64_t used = zfs_prop_get_int(zhp, ZFS_PROP_USED);
uint64_t avail = zfs_prop_get_int(zhp, ZFS_PROP_AVAILABLE);
int percentage = (int)((double)avail / MAX(avail + used, 1) * 100);

if (percentage > 20)
return (NULL);
else if (percentage > 10)
return (ANSI_YELLOW);
else
return (ANSI_RED);
}

/*
* Given a dataset and a list of fields, print out all the properties according
* to the described layout.
Expand Down Expand Up @@ -3531,6 +3555,9 @@ print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
right_justify = B_FALSE;
}

if (pl->pl_prop == ZFS_PROP_AVAILABLE)
color_start(zfs_list_avail_color(zhp));

/*
* If this is being called in scripted mode, or if this is the
* last column and it is left-justified, don't include a width
Expand All @@ -3542,6 +3569,9 @@ print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
(void) printf("%*s", (int)pl->pl_width, propstr);
else
(void) printf("%-*s", (int)pl->pl_width, propstr);

if (pl->pl_prop == ZFS_PROP_AVAILABLE)
color_end();
}

(void) putchar('\n');
Expand Down
35 changes: 34 additions & 1 deletion cmd/zpool/zpool_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4220,6 +4220,8 @@ print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
unsigned int namewidth;
const char *title;

color_start(ANSI_BOLD);

if (cb->cb_flags & IOS_ANYHISTO_M) {
title = histo_to_title[IOS_HISTO_IDX(cb->cb_flags)];
} else if (cb->cb_vdevs.cb_names_count) {
Expand Down Expand Up @@ -4253,6 +4255,8 @@ print_iostat_header_impl(iostat_cbdata_t *cb, unsigned int force_column_width,
if (cb->vcdl != NULL)
print_cmd_columns(cb->vcdl, 1);

color_end();

printf("\n");
}

Expand All @@ -4262,6 +4266,35 @@ print_iostat_header(iostat_cbdata_t *cb)
print_iostat_header_impl(cb, 0, NULL);
}

/*
* Prints a size string (i.e. 120M) with the suffix ("M") colored
* by order of magnitude. Uses column_size to add padding.
*/
static void
print_stat_color(char *statbuf, unsigned int column_size)
{
fputs(" ", stdout);
if (*statbuf == '0') {
color_start(ANSI_GRAY);
fputc('0', stdout);
column_size--;
} else {
for (; *statbuf; statbuf++) {
if (*statbuf == 'K') color_start(ANSI_GREEN);
else if (*statbuf == 'M') color_start(ANSI_YELLOW);
else if (*statbuf == 'G') color_start(ANSI_RED);
else if (*statbuf == 'T') color_start(ANSI_BOLD_BLUE);
else if (*statbuf == 'P') color_start(ANSI_MAGENTA);
else if (*statbuf == 'E') color_start(ANSI_CYAN);
fputc(*statbuf, stdout);
if (--column_size <= 0)
break;
}
}
color_end();
for (; column_size > 0; column_size--)
fputc(' ', stdout);
}

/*
* Display a single statistic.
Expand All @@ -4277,7 +4310,7 @@ print_one_stat(uint64_t value, enum zfs_nicenum_format format,
if (scripted)
printf("\t%s", buf);
else
printf(" %*s", column_size, buf);
print_stat_color(buf, column_size);
}

/*
Expand Down
6 changes: 6 additions & 0 deletions include/libzutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,16 @@ struct zfs_cmd;
/*
* List of colors to use
*/
#define ANSI_BLACK "\033[0;30m"
#define ANSI_RED "\033[0;31m"
#define ANSI_GREEN "\033[0;32m"
#define ANSI_YELLOW "\033[0;33m"
#define ANSI_BLUE "\033[0;34m"
#define ANSI_BOLD_BLUE "\033[1;34m" /* light blue */
#define ANSI_MAGENTA "\033[0;35m"
#define ANSI_CYAN "\033[0;36m"
#define ANSI_GRAY "\033[0;37m"

#define ANSI_RESET "\033[0m"
#define ANSI_BOLD "\033[1m"

Expand Down
6 changes: 3 additions & 3 deletions lib/libzfs/libzfs_diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@
#define ZDIFF_REMOVED '-'
#define ZDIFF_RENAMED "R"

#define ZDIFF_ADDED_COLOR ANSI_GREEN
#define ZDIFF_ADDED_COLOR ANSI_GREEN
#define ZDIFF_MODIFIED_COLOR ANSI_YELLOW
#define ZDIFF_REMOVED_COLOR ANSI_RED
#define ZDIFF_RENAMED_COLOR ANSI_BLUE
#define ZDIFF_REMOVED_COLOR ANSI_RED
#define ZDIFF_RENAMED_COLOR ANSI_BOLD_BLUE

/*
* Given a {dsname, object id}, get the object path
Expand Down
13 changes: 8 additions & 5 deletions lib/libzfs/libzfs_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -2011,18 +2011,19 @@ use_color(void)
}

/*
* color_start() and color_end() are used for when you want to colorize a block
* of text. For example:
* The functions color_start() and color_end() are used for when you want
* to colorize a block of text.
*
* color_start(ANSI_RED_FG)
* For example:
* color_start(ANSI_RED)
* printf("hello");
* printf("world");
* color_end();
*/
void
color_start(const char *color)
{
if (use_color()) {
if (color && use_color()) {
fputs(color, stdout);
fflush(stdout);
}
Expand All @@ -2038,7 +2039,9 @@ color_end(void)

}

/* printf() with a color. If color is NULL, then do a normal printf. */
/*
* printf() with a color. If color is NULL, then do a normal printf.
*/
int
printf_color(const char *color, const char *format, ...)
{
Expand Down
2 changes: 2 additions & 0 deletions man/man8/zfs.8
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,8 @@ command will be undone if the share is ever unshared (like via a reboot).
.It Sy ZFS_COLOR
Use ANSI color in
.Nm zfs Cm diff
and
.Nm zfs Cm list
output.
.It Sy ZFS_MOUNT_HELPER
Cause
Expand Down
2 changes: 2 additions & 0 deletions man/man8/zpool.8
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,8 @@ to dump core on exit for the purposes of running
.It Sy ZFS_COLOR
Use ANSI color in
.Nm zpool Cm status
and
.Nm zpool Cm iostat
output.
.It Sy ZPOOL_IMPORT_PATH
The search path for devices or files to use with the pool.
Expand Down