Skip to content

Commit

Permalink
Add colored output to zfs list
Browse files Browse the repository at this point in the history
Use a bold header row and colorize the AVAIL column based on
the used space percentage of volume.

We define these colors:
- when > 80%, use yellow
- when > 90%, use red

Reviewed-by: WHR <msl0000023508@gmail.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ethan Coe-Renner <coerenner1@llnl.gov>
Signed-off-by: Tino Reichardt <milky-zfs@mcmilk.de>
Closes openzfs#14621
Closes openzfs#14350
  • Loading branch information
mcmilk authored and manoj-joseph committed Apr 5, 2023
1 parent 1828248 commit 242503b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cmd/zfs/zfs_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3441,6 +3441,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 @@ -3467,9 +3469,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 @@ -3532,6 +3556,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 @@ -3543,6 +3570,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: 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

0 comments on commit 242503b

Please sign in to comment.