mirror of
				https://git.proxmox.com/git/mirror_zfs.git
				synced 2025-10-26 18:05:04 +03:00 
			
		
		
		
	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 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 #14621 Closes #14350
This commit is contained in:
		
							parent
							
								
									433b9a89c4
								
							
						
					
					
						commit
						3da577280a
					
				| @ -3474,6 +3474,8 @@ print_header(list_cbdata_t *cb) | |||||||
| 	boolean_t first = B_TRUE; | 	boolean_t first = B_TRUE; | ||||||
| 	boolean_t right_justify; | 	boolean_t right_justify; | ||||||
| 
 | 
 | ||||||
|  | 	color_start(ANSI_BOLD); | ||||||
|  | 
 | ||||||
| 	for (; pl != NULL; pl = pl->pl_next) { | 	for (; pl != NULL; pl = pl->pl_next) { | ||||||
| 		if (!first) { | 		if (!first) { | ||||||
| 			(void) printf("  "); | 			(void) printf("  "); | ||||||
| @ -3500,9 +3502,31 @@ print_header(list_cbdata_t *cb) | |||||||
| 			(void) printf("%-*s", (int)pl->pl_width, header); | 			(void) printf("%-*s", (int)pl->pl_width, header); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	color_end(); | ||||||
|  | 
 | ||||||
| 	(void) printf("\n"); | 	(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 |  * Given a dataset and a list of fields, print out all the properties according | ||||||
|  * to the described layout. |  * to the described layout. | ||||||
| @ -3565,6 +3589,9 @@ print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb) | |||||||
| 			right_justify = B_FALSE; | 			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 | 		 * 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 | 		 * last column and it is left-justified, don't include a width | ||||||
| @ -3576,6 +3603,9 @@ print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb) | |||||||
| 			(void) printf("%*s", (int)pl->pl_width, propstr); | 			(void) printf("%*s", (int)pl->pl_width, propstr); | ||||||
| 		else | 		else | ||||||
| 			(void) printf("%-*s", (int)pl->pl_width, propstr); | 			(void) printf("%-*s", (int)pl->pl_width, propstr); | ||||||
|  | 
 | ||||||
|  | 		if (pl->pl_prop == ZFS_PROP_AVAILABLE) | ||||||
|  | 			color_end(); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	(void) printf("\n"); | 	(void) printf("\n"); | ||||||
|  | |||||||
| @ -708,6 +708,8 @@ command will be undone if the share is ever unshared (like via a reboot). | |||||||
| .It Sy ZFS_COLOR | .It Sy ZFS_COLOR | ||||||
| Use ANSI color in | Use ANSI color in | ||||||
| .Nm zfs Cm diff | .Nm zfs Cm diff | ||||||
|  | and | ||||||
|  | .Nm zfs Cm list | ||||||
| output. | output. | ||||||
| .El | .El | ||||||
| .Bl -tag -width "ZFS_MOUNT_HELPER" | .Bl -tag -width "ZFS_MOUNT_HELPER" | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Tino Reichardt
						Tino Reichardt