diff --git a/cmd/zfs/zfs_main.c b/cmd/zfs/zfs_main.c index 44440dc3dd4d..e07e71f9c25d 100644 --- a/cmd/zfs/zfs_main.c +++ b/cmd/zfs/zfs_main.c @@ -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(" "); @@ -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); + int percentage = (int)((double)available / (available + used) * 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. @@ -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 @@ -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'); diff --git a/lib/libzfs/libzfs_util.c b/lib/libzfs/libzfs_util.c index c31f18009e0c..189cf02440fb 100644 --- a/lib/libzfs/libzfs_util.c +++ b/lib/libzfs/libzfs_util.c @@ -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); } diff --git a/man/man8/zfs.8 b/man/man8/zfs.8 index d12377f9b4f2..dd578cb74aac 100644 --- a/man/man8/zfs.8 +++ b/man/man8/zfs.8 @@ -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