Skip to content

Commit

Permalink
Add colored output to zfs list
Browse files Browse the repository at this point in the history
- bold header row
- color AVAIL column based on percentage of volume available
	- < 20%: Yellow
	- < 10%: Red

Signed-off-by: Ethan Coe-Renner <ecoerenner@llnl.gov>
  • Loading branch information
ethan-coe-renner committed Jan 10, 2023
1 parent fb11b15 commit 9cb07df
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 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,40 @@ 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 *
color_available(zfs_handle_t *zhp)
{
char used_prop[ZFS_MAXPROPLEN];
char available_prop[ZFS_MAXPROPLEN];

zfs_prop_get(zhp, ZFS_PROP_USED, used_prop,
sizeof (used_prop), NULL, NULL, 0, 1);

zfs_prop_get(zhp, ZFS_PROP_AVAILABLE, available_prop,
sizeof (available_prop), NULL, NULL, 0, 1);

int used = atoi(used_prop);
int available = atoi(available_prop);
int percentage = (int)((double)available / (available + used) * 100);

if (percentage > 20)
return ("");
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 +3564,9 @@ print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
right_justify = B_FALSE;
}

if (pl->pl_prop == ZFS_PROP_AVAILABLE)
color_start(color_available(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 +3578,8 @@ 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);

color_end();
}

(void) putchar('\n');
Expand Down

0 comments on commit 9cb07df

Please sign in to comment.