Example #1
0
/* Adds decorations like ellipsis to the output. */
static void
decorate_output(const column_t *col, char buf[], size_t max_line_width)
{
	const size_t len = get_width_on_screen(buf);
	const size_t max_col_width = calculate_max_width(col, len, max_line_width);
	const int too_long = len > max_col_width;

	if(!too_long)
	{
		return;
	}

	if(col->info.align == AT_LEFT)
	{
		const size_t truncate_pos = get_real_string_width(buf, max_col_width);
		buf[truncate_pos] = '\0';
	}
	else
	{
		const size_t truncate_pos = get_real_string_width(buf, len - max_col_width);
		const char *const new_beginning = buf + truncate_pos;
		memmove(buf, new_beginning, strlen(new_beginning) + 1);
		assert(get_width_on_screen(buf) == max_col_width && "Column isn't filled.");
	}

	if(col->info.cropping == CT_ELLIPSIS)
	{
		add_ellipsis(col->info.align, buf);
	}
}
Example #2
0
/* Adds decorations like ellipsis to the output.  Returns actual align type used
 * for the column (might not match col->info.align). */
static AlignType
decorate_output(const column_t *col, char buf[], size_t max_line_width)
{
	const size_t len = get_width_on_screen(buf);
	const size_t max_col_width = calculate_max_width(col, len, max_line_width);
	const int too_long = len > max_col_width;
	AlignType result;

	if(!too_long)
	{
		return (col->info.align == AT_RIGHT ? AT_RIGHT : AT_LEFT);
	}

	if(col->info.align == AT_LEFT ||
			(col->info.align == AT_DYN && len <= max_col_width))
	{
		const size_t truncate_pos = utf8_strsnlen(buf, max_col_width);
		buf[truncate_pos] = '\0';
		result = AT_LEFT;
	}
	else
	{
		int extra_spaces;

		const size_t truncate_pos = utf8_strsnlen(buf, len - max_col_width);
		const char *new_beginning = buf + truncate_pos;

		extra_spaces = 0;
		while(get_width_on_screen(new_beginning) > max_col_width)
		{
			++extra_spaces;
			new_beginning += utf8_chrw(new_beginning);
		}

		memmove(buf + extra_spaces, new_beginning, strlen(new_beginning) + 1);
		if(extra_spaces != 0)
		{
			memset(buf, ' ', extra_spaces);
		}

		assert(get_width_on_screen(buf) == max_col_width && "Column isn't filled.");
		result = AT_RIGHT;
	}

	if(col->info.cropping == CT_ELLIPSIS)
	{
		add_ellipsis(result, buf);
	}

	return result;
}