Skip to content

Commit d701643

Browse files
committed
Add colored output to zfs list
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 Signed-off-by: Ethan Coe-Renner <[email protected]> Signed-off-by: Tino Reichardt <[email protected]> Closes: #14350
1 parent 7bb7b1f commit d701643

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

cmd/zfs/zfs_main.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3440,6 +3440,8 @@ print_header(list_cbdata_t *cb)
34403440
boolean_t first = B_TRUE;
34413441
boolean_t right_justify;
34423442

3443+
color_start(ANSI_BOLD);
3444+
34433445
for (; pl != NULL; pl = pl->pl_next) {
34443446
if (!first) {
34453447
(void) printf(" ");
@@ -3466,9 +3468,31 @@ print_header(list_cbdata_t *cb)
34663468
(void) printf("%-*s", (int)pl->pl_width, header);
34673469
}
34683470

3471+
color_end();
3472+
34693473
(void) printf("\n");
34703474
}
34713475

3476+
/*
3477+
* Decides on the color that the avail value should be printed in.
3478+
* > 80% used = yellow
3479+
* > 90% used = red
3480+
*/
3481+
static const char *
3482+
zfs_list_avail_color(zfs_handle_t *zhp)
3483+
{
3484+
uint64_t used = zfs_prop_get_int(zhp, ZFS_PROP_USED);
3485+
uint64_t avail = zfs_prop_get_int(zhp, ZFS_PROP_AVAILABLE);
3486+
int percentage = (int)((double)avail / MAX(avail + used, 1) * 100);
3487+
3488+
if (percentage > 20)
3489+
return (NULL);
3490+
else if (percentage > 10)
3491+
return (ANSI_YELLOW);
3492+
else
3493+
return (ANSI_RED);
3494+
}
3495+
34723496
/*
34733497
* Given a dataset and a list of fields, print out all the properties according
34743498
* to the described layout.
@@ -3531,6 +3555,9 @@ print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
35313555
right_justify = B_FALSE;
35323556
}
35333557

3558+
if (pl->pl_prop == ZFS_PROP_AVAILABLE)
3559+
color_start(zfs_list_avail_color(zhp));
3560+
35343561
/*
35353562
* If this is being called in scripted mode, or if this is the
35363563
* 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)
35423569
(void) printf("%*s", (int)pl->pl_width, propstr);
35433570
else
35443571
(void) printf("%-*s", (int)pl->pl_width, propstr);
3572+
3573+
if (pl->pl_prop == ZFS_PROP_AVAILABLE)
3574+
color_end();
35453575
}
35463576

35473577
(void) putchar('\n');

man/man8/zfs.8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,8 @@ command will be undone if the share is ever unshared (like via a reboot).
740740
.It Sy ZFS_COLOR
741741
Use ANSI color in
742742
.Nm zfs Cm diff
743+
and
744+
.Nm zfs Cm list
743745
output.
744746
.It Sy ZFS_MOUNT_HELPER
745747
Cause

0 commit comments

Comments
 (0)