apps/top.c
/**
 * @brief Display a progress-bar-style usage meter.
 *
 * @p title Label to apply to the meter, shown on left.
 * @p label Label to show inside of the meter, show on the right.
 * @p width Available width to display the meter in, including title and frame.
 * @p count Number of values to display.
 * @p filled Values to stack in the meter.
 * @p maximum Maximum value of the meter.
 */
@brief Display a progress-bar-style usage meter.
@p title Label to apply to the meter, shown on left.
@p label Label to show inside of the meter, show on the right.
@p width Available width to display the meter in, including title and frame.
@p count Number of values to display.
@p filled Values to stack in the meter.
@p maximum Maximum value of the meter.
@brief Display a progress - bar - style usage meter . @p title Label to apply to the meter shown on left . @p label Label to show inside of the meter show on the right . @p width Available width to display the meter in including title and frame . @p count Number of values to display . @p filled Values to stack in the meter . @p maximum Maximum value of the meter .
static void print_meter(const char * title, const char * label, int width, int count, int filled[], int maximum) {
	int available = width - strlen(title) - 4;
	int remaining = available;
	int fillSlots = 0;
	for (int i = 0; i < count; ++i) {
		filled[i] = filled[i] * available / maximum;
		if (filled[i] < 0) filled[i] = 0;
		if (filled[i] > remaining) filled[i] = remaining;
		fillSlots += filled[i];
		remaining = available - fillSlots;
	}
	int emptSlots = available - fillSlots;
	printf("\033[1m%s [", title);
	char * fill = malloc(available + 1);
	size_t j = 0;
	for (int i = 0; i < fillSlots; ++i, j++) fill[j] = '|';
	for (int i = 0; i < emptSlots; ++i, j++) fill[j] = ' ';
	size_t l = strlen(label);
	if (available > (int)l) {
		sprintf(fill + available - l, "%s", label);
	}
	j = 0;
	for (int c = 0; c < count; ++c) {
		printf("\033[0;9%dm", fill_colors[c % (sizeof(fill_colors) / sizeof(int))]);
		for (int i = 0; i < filled[c]; ++i, j++) printf("%c",fill[j]);
	}
	printf("\033[90m");
	for (int i = 0; i < emptSlots; ++i, j++) printf("%c",fill[j]);
	printf("\033[0;1m]\033[0m ");
	free(fill);
}
static void print_meter ( const char * title , const char * label , int width , int count , int filled [ ] , int maximum ) { int available = width - strlen ( title ) - 4 ; int remaining = available ; int fillSlots = 0 ; for ( int i = 0 ; i < count ; ++ i ) { filled [ i ] = filled [ i ] * available / maximum ; if ( filled [ i ] < 0 ) filled [ i ] = 0 ; if ( filled [ i ] > remaining ) filled [ i ] = remaining ; fillSlots += filled [ i ] ; remaining = available - fillSlots ; } int emptSlots = available - fillSlots ; printf ( " \033 " , title ) ; char * fill = malloc ( available + 1 ) ; size_t j = 0 ; for ( int i = 0 ; i < fillSlots ; ++ i , j ++ ) fill [ j ] = ' ' ; for ( int i = 0 ; i < emptSlots ; ++ i , j ++ ) fill [ j ] = ' ' ; size_t l = strlen ( label ) ; if ( available > ( int ) l ) { sprintf ( fill + available - l , " " , label ) ; } j = 0 ; for ( int c = 0 ; c < count ; ++ c ) { printf ( " \033 " , fill_colors [ c % ( sizeof ( fill_colors ) / sizeof ( int ) ) ] ) ; for ( int i = 0 ; i < filled [ c ] ; ++ i , j ++ ) printf ( " " , fill [ j ] ) ; } printf ( " \033 " ) ; for ( int i = 0 ; i < emptSlots ; ++ i , j ++ ) printf ( " " , fill [ j ] ) ; printf ( " \033 \033 " ) ; free ( fill ) ; }
@brief Display a progress-bar-style usage meter.
@brief Display a progress - bar - style usage meter .
/* Count total fill slots */