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

Colorize zfs list output #14350

Closed
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)
{
int used = zfs_prop_get_int(zhp, ZFS_PROP_USED);
int available = zfs_prop_get_int(zhp, ZFS_PROP_AVAILABLE);
ethan-coe-renner marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int available = zfs_prop_get_int(zhp, ZFS_PROP_AVAILABLE);
uint64_t available = zfs_prop_get_int(zhp, ZFS_PROP_AVAILABLE);

zfs_prop_get_int() returns a uint64_t which we want to make sure to use here for both used and available.

int percentage = (int)((double)available / (available + used) * 100);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int percentage = (int)((double)available / (available + used) * 100);
int percentage = (int)((double)available / MAX(available + used, 1) * 100);

Even though it shouldn't happen, we should protect against a division by zero error here.


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
2 changes: 1 addition & 1 deletion lib/libzfs/libzfs_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -2010,7 +2010,7 @@ use_color(void)
void
color_start(const char *color)
{
if (use_color()) {
if (color && use_color()) {
fputs(color, stdout);
fflush(stdout);
}
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