/* 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); } }
void columns_format_line(columns_t *cols, const void *data, size_t max_line_width) { char prev_col_buf[1024 + 1]; size_t prev_col_start = 0UL; prev_col_buf[0] = '\0'; size_t i; size_t prev_col_end = 0; recalculate_if_needed(cols, max_line_width); for(i = 0U; i < cols->count; ++i) { /* Use big buffer to hold whole item so there will be no issues with right * aligned fields. */ char col_buffer[sizeof(prev_col_buf)]; char full_column[sizeof(prev_col_buf)]; size_t cur_col_start; AlignType align; const column_t *const col = &cols->list[i]; col->func(col->info.column_id, data, sizeof(col_buffer), col_buffer); strcpy(full_column, col_buffer); align = decorate_output(col, col_buffer, max_line_width); cur_col_start = calculate_start_pos(col, col_buffer, align); /* Ensure that we are not trying to draw current column in the middle of a * character inside previous column. */ if(prev_col_end > cur_col_start) { const size_t prev_col_max_width = (cur_col_start > prev_col_start) ? (cur_col_start - prev_col_start) : 0UL; const size_t break_point = utf8_strsnlen(prev_col_buf, prev_col_max_width); prev_col_buf[break_point] = '\0'; fill_gap_pos(data, prev_col_start + get_width_on_screen(prev_col_buf), cur_col_start); } else { fill_gap_pos(data, prev_col_end, cur_col_start); } print_func(data, col->info.column_id, col_buffer, cur_col_start, align, full_column); prev_col_end = cur_col_start + get_width_on_screen(col_buffer); /* Store information about the current column for usage on the next * iteration. */ strcpy(prev_col_buf, col_buffer); prev_col_start = cur_col_start; } fill_gap_pos(data, prev_col_end, max_line_width); }
/* 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; }
void columns_format_line(const columns_t cols, const void *data, size_t max_line_width) { size_t i; size_t prev_col_end = 0; recalculate_if_needed(cols, max_line_width); for(i = 0; i < cols->count; i++) { /* Use big buffer to hold whole item so there will be no issues with right * aligned fields. */ char col_buffer[1024 + 1]; size_t cur_col_start; const column_t *const col = &cols->list[i]; col->func(col->info.column_id, data, ARRAY_LEN(col_buffer), col_buffer); decorate_output(col, col_buffer, max_line_width); cur_col_start = calculate_start_pos(col, col_buffer); fill_gap_pos(data, prev_col_end, cur_col_start); print_func(data, col->info.column_id, col_buffer, cur_col_start); prev_col_end = cur_col_start + get_width_on_screen(col_buffer); } fill_gap_pos(data, prev_col_end, max_line_width); }
/* Adds ellipsis to the string in buf not changing enlarging its length (at most * three first or last characters are replaced). */ static void add_ellipsis(AlignType align, char buf[]) { const size_t len = get_width_on_screen(buf); const size_t dot_count = MIN(len, MAX_ELLIPSIS_DOT_COUNT); if(align == AT_LEFT) { const size_t width_limit = len - dot_count; const size_t pos = utf8_strsnlen(buf, width_limit); memset(buf + pos, '.', dot_count); buf[pos + dot_count] = '\0'; } else { const char *new_beginning = buf; size_t skipped = 0; while(skipped < dot_count) { skipped += utf8_chrsw(new_beginning); new_beginning += utf8_chrw(new_beginning); } memmove(buf + dot_count, new_beginning, strlen(new_beginning) + 1); memset(buf, '.', dot_count); } }
/* Calculates start position for outputting content of the col. */ static size_t calculate_start_pos(const column_t *col, const char buf[]) { if(col->info.align == AT_LEFT) { return col->start; } else { const size_t end = col->start + col->width; const size_t len = get_width_on_screen(buf); return (end > len) ? (end - len) : 0; } }
/* Adds ellipsis to the string in buf not changing its length (at most three * first or last characters are replaced). */ static void add_ellipsis(AlignType align, char buf[]) { const size_t len = get_width_on_screen(buf); const size_t dot_count = MIN(len, MAX_ELLIPSIS_DOT_COUNT); if(align == AT_LEFT) { const size_t width_limit = len - dot_count; const size_t pos = get_real_string_width(buf, width_limit); memset(buf + pos, '.', dot_count); buf[pos + dot_count] = '\0'; } else { const size_t beginning_shift = get_real_string_width(buf, dot_count); const char *const new_beginning = buf + beginning_shift; memmove(buf + dot_count, new_beginning, strlen(new_beginning) + 1); memset(buf, '.', dot_count); } }