HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/format-table.c
Git blob: 518ef5bd25f38caccb277111a5696205a33034a4
Lines 1001–2000 of 3,252
case TABLE_INT:
case TABLE_SIGNAL:
buffer.int_val = va_arg(ap, int);
data = &buffer.int_val;
break;
case TABLE_INT8: {
int x = va_arg(ap, int);
assert(x >= INT8_MIN && x <= INT8_MAX);
buffer.int8 = x;
data = &buffer.int8;
break;
}
case TABLE_INT16: {
int x = va_arg(ap, int);
assert(x >= INT16_MIN && x <= INT16_MAX);
buffer.int16 = x;
data = &buffer.int16;
break;
}
case TABLE_INT32:
buffer.int32 = va_arg(ap, int32_t);
data = &buffer.int32;
break;
case TABLE_INT64:
buffer.int64 = va_arg(ap, int64_t);
data = &buffer.int64;
break;
case TABLE_UINT:
buffer.uint_val = va_arg(ap, unsigned);
data = &buffer.uint_val;
break;
case TABLE_UINT8: {
unsigned x = va_arg(ap, unsigned);
assert(x <= UINT8_MAX);
buffer.uint8 = x;
data = &buffer.uint8;
break;
}
case TABLE_UINT16: {
unsigned x = va_arg(ap, unsigned);
assert(x <= UINT16_MAX);
buffer.uint16 = x;
data = &buffer.uint16;
break;
}
case TABLE_UINT32:
case TABLE_UINT32_HEX:
case TABLE_UINT32_HEX_0x:
buffer.uint32 = va_arg(ap, uint32_t);
data = &buffer.uint32;
break;
case TABLE_UINT64:
case TABLE_UINT64_HEX:
case TABLE_UINT64_HEX_0x:
buffer.uint64 = va_arg(ap, uint64_t);
data = &buffer.uint64;
break;
case TABLE_PERCENT:
buffer.percent = va_arg(ap, int);
data = &buffer.percent;
break;
case TABLE_IFINDEX:
buffer.ifindex = va_arg(ap, int);
data = &buffer.ifindex;
break;
case TABLE_IN_ADDR:
buffer.address.in = *va_arg(ap, struct in_addr *);
data = &buffer.address.in;
break;
case TABLE_IN6_ADDR:
buffer.address.in6 = *va_arg(ap, struct in6_addr *);
data = &buffer.address.in6;
break;
case TABLE_UUID:
case TABLE_ID128:
buffer.id128 = va_arg(ap, sd_id128_t);
data = &buffer.id128;
break;
case TABLE_UID:
buffer.uid = va_arg(ap, uid_t);
data = &buffer.uid;
break;
case TABLE_GID:
buffer.gid = va_arg(ap, gid_t);
data = &buffer.gid;
break;
case TABLE_PID:
buffer.pid = va_arg(ap, pid_t);
data = &buffer.pid;
break;
case TABLE_MODE:
case TABLE_MODE_INODE_TYPE:
buffer.mode = va_arg(ap, mode_t);
data = &buffer.mode;
break;
case TABLE_DEVNUM:
buffer.devnum = va_arg(ap, dev_t);
data = &buffer.devnum;
break;
case TABLE_JSON:
data = va_arg(ap, sd_json_variant*);
break;
case TABLE_SET_MINIMUM_WIDTH: {
size_t w = va_arg(ap, size_t);
r = table_set_minimum_width(t, last_cell, w);
goto check;
}
case TABLE_SET_MAXIMUM_WIDTH: {
size_t w = va_arg(ap, size_t);
r = table_set_maximum_width(t, last_cell, w);
goto check;
}
case TABLE_SET_WEIGHT: {
unsigned w = va_arg(ap, unsigned);
r = table_set_weight(t, last_cell, w);
goto check;
}
case TABLE_SET_ALIGN_PERCENT: {
unsigned p = va_arg(ap, unsigned);
r = table_set_align_percent(t, last_cell, p);
goto check;
}
case TABLE_SET_ELLIPSIZE_PERCENT: {
unsigned p = va_arg(ap, unsigned);
r = table_set_ellipsize_percent(t, last_cell, p);
goto check;
}
case TABLE_SET_COLOR: {
const char *c = va_arg(ap, const char*);
r = table_set_color(t, last_cell, c);
goto check;
}
case TABLE_SET_RGAP_COLOR: {
const char *c = va_arg(ap, const char*);
r = table_set_rgap_color(t, last_cell, c);
goto check;
}
case TABLE_SET_BOTH_COLORS: {
const char *c = va_arg(ap, const char*);
r = table_set_color(t, last_cell, c);
if (r < 0) {
va_end(ap);
return r;
}
r = table_set_rgap_color(t, last_cell, c);
goto check;
}
case TABLE_SET_UNDERLINE: {
int u = va_arg(ap, int);
r = table_set_underline(t, last_cell, u);
goto check;
}
case TABLE_SET_RGAP_UNDERLINE: {
int u = va_arg(ap, int);
r = table_set_rgap_underline(t, last_cell, u);
goto check;
}
case TABLE_SET_BOTH_UNDERLINES: {
int u = va_arg(ap, int);
r = table_set_underline(t, last_cell, u);
if (r < 0) {
va_end(ap);
return r;
}
r = table_set_rgap_underline(t, last_cell, u);
goto check;
}
case TABLE_SET_URL: {
const char *u = va_arg(ap, const char*);
r = table_set_url(t, last_cell, u);
goto check;
}
case TABLE_SET_UPPERCASE: {
int u = va_arg(ap, int);
r = table_set_uppercase(t, last_cell, u);
goto check;
}
case TABLE_SET_JSON_FIELD_NAME: {
const char *n = va_arg(ap, const char*);
size_t idx;
if (t->vertical) {
assert(TABLE_CELL_TO_INDEX(last_cell) >= t->n_columns);
idx = TABLE_CELL_TO_INDEX(last_cell) / t->n_columns - 1;
} else {
idx = TABLE_CELL_TO_INDEX(last_cell);
assert(idx < t->n_columns);
}
r = table_set_json_field_name(t, idx, n);
goto check;
}
case _TABLE_DATA_TYPE_MAX:
/* Used as end marker */
va_end(ap);
return 0;
default:
assert_not_reached();
}
r = table_add_cell(t, &last_cell, type, data);
check:
if (r < 0) {
va_end(ap);
return r;
}
}
}
void table_set_header(Table *t, bool b) {
assert(t);
t->header = b;
}
void table_set_width(Table *t, size_t width) {
assert(t);
t->width = width;
}
void table_set_cell_height_max(Table *t, size_t height) {
assert(t);
assert(height >= 1 || height == SIZE_MAX);
t->cell_height_max = height;
}
void table_set_ersatz_string(Table *t, TableErsatz ersatz) {
assert(t);
assert(ersatz >= 0 && ersatz < _TABLE_ERSATZ_MAX);
t->ersatz = ersatz;
}
static const char* table_ersatz_string(const Table *t) {
switch (t->ersatz) {
case TABLE_ERSATZ_EMPTY:
return "";
case TABLE_ERSATZ_DASH:
return "-";
case TABLE_ERSATZ_UNSET:
return "(unset)";
case TABLE_ERSATZ_NA:
return "n/a";
default:
assert_not_reached();
}
}
static int table_set_display_all(Table *t) {
size_t *d;
assert(t);
/* Initialize the display map to the identity */
d = reallocarray(t->display_map, t->n_columns, sizeof(size_t));
if (!d)
return -ENOMEM;
for (size_t i = 0; i < t->n_columns; i++)
d[i] = i;
t->display_map = d;
t->n_display_map = t->n_columns;
return 0;
}
int table_set_display_internal(Table *t, size_t first_column, ...) {
size_t column;
va_list ap;
assert(t);
column = first_column;
va_start(ap, first_column);
for (;;) {
assert(column < t->n_columns);
if (!GREEDY_REALLOC(t->display_map, MAX(t->n_columns, t->n_display_map+1))) {
va_end(ap);
return -ENOMEM;
}
t->display_map[t->n_display_map++] = column;
column = va_arg(ap, size_t);
if (column == SIZE_MAX)
break;
}
va_end(ap);
return 0;
}
int table_set_sort_internal(Table *t, size_t first_column, ...) {
size_t column;
va_list ap;
assert(t);
column = first_column;
va_start(ap, first_column);
for (;;) {
assert(column < t->n_columns);
if (!GREEDY_REALLOC(t->sort_map, MAX(t->n_columns, t->n_sort_map+1))) {
va_end(ap);
return -ENOMEM;
}
t->sort_map[t->n_sort_map++] = column;
column = va_arg(ap, size_t);
if (column == SIZE_MAX)
break;
}
va_end(ap);
return 0;
}
int table_hide_column_from_display_internal(Table *t, ...) {
size_t cur = 0;
int r;
assert(t);
/* If the display map is empty, initialize it with all available columns */
if (!t->display_map) {
r = table_set_display_all(t);
if (r < 0)
return r;
}
FOREACH_ARRAY(i, t->display_map, t->n_display_map) {
bool listed = false;
va_list ap;
va_start(ap, t);
for (;;) {
size_t column;
column = va_arg(ap, size_t);
if (column == SIZE_MAX)
break;
if (column == *i) {
listed = true;
break;
}
}
va_end(ap);
if (listed)
continue;
t->display_map[cur++] = *i;
}
t->n_display_map = cur;
return 0;
}
static int cell_data_compare(TableData *a, TableData *b) {
int r;
assert(a);
assert(b);
r = CMP(a->type == TABLE_EMPTY, b->type == TABLE_EMPTY);
if (r != 0)
return r;
r = CMP(a->type, b->type);
if (r != 0)
return r;
switch (a->type) {
case TABLE_STRING:
case TABLE_STRING_WITH_ANSI:
case TABLE_FIELD:
case TABLE_HEADER:
return strcmp(a->string, b->string);
case TABLE_PATH:
case TABLE_PATH_BASENAME:
return path_compare(a->string, b->string);
case TABLE_VERSION:
return strverscmp_improved(a->string, b->string);
case TABLE_STRV:
case TABLE_STRV_WRAPPED:
return strv_compare(a->strv, b->strv);
case TABLE_BOOLEAN:
case TABLE_BOOLEAN_CHECKMARK:
if (!a->boolean && b->boolean)
return -1;
if (a->boolean && !b->boolean)
return 1;
return 0;
case TABLE_TRISTATE:
/* NB: we do not use CMP() here, since we want to collapse all negative and all
* positive into one bucket each. */
if ((a->tristate < 0 && b->tristate >= 0) ||
(a->tristate == 0 && b->tristate > 0))
return -1;
if ((b->tristate < 0 && a->tristate >= 0) ||
(b->tristate == 0 && a->tristate > 0))
return 1;
return 0;
case TABLE_TIMESTAMP:
case TABLE_TIMESTAMP_UTC:
case TABLE_TIMESTAMP_RELATIVE:
case TABLE_TIMESTAMP_RELATIVE_MONOTONIC:
case TABLE_TIMESTAMP_LEFT:
case TABLE_TIMESTAMP_DATE:
return CMP(a->timestamp, b->timestamp);
case TABLE_TIMESPAN:
case TABLE_TIMESPAN_MSEC:
case TABLE_TIMESPAN_DAY:
return CMP(a->timespan, b->timespan);
case TABLE_SIZE:
case TABLE_BPS:
return CMP(a->size, b->size);
case TABLE_INT:
case TABLE_SIGNAL:
return CMP(a->int_val, b->int_val);
case TABLE_INT8:
return CMP(a->int8, b->int8);
case TABLE_INT16:
return CMP(a->int16, b->int16);
case TABLE_INT32:
return CMP(a->int32, b->int32);
case TABLE_INT64:
return CMP(a->int64, b->int64);
case TABLE_UINT:
return CMP(a->uint_val, b->uint_val);
case TABLE_UINT8:
return CMP(a->uint8, b->uint8);
case TABLE_UINT16:
return CMP(a->uint16, b->uint16);
case TABLE_UINT32:
case TABLE_UINT32_HEX:
case TABLE_UINT32_HEX_0x:
return CMP(a->uint32, b->uint32);
case TABLE_UINT64:
case TABLE_UINT64_HEX:
case TABLE_UINT64_HEX_0x:
return CMP(a->uint64, b->uint64);
case TABLE_PERCENT:
return CMP(a->percent, b->percent);
case TABLE_IFINDEX:
return CMP(a->ifindex, b->ifindex);
case TABLE_IN_ADDR:
return CMP(a->address.in.s_addr, b->address.in.s_addr);
case TABLE_IN6_ADDR:
return memcmp(&a->address.in6, &b->address.in6, FAMILY_ADDRESS_SIZE(AF_INET6));
case TABLE_UUID:
case TABLE_ID128:
return memcmp(&a->id128, &b->id128, sizeof(sd_id128_t));
case TABLE_UID:
return CMP(a->uid, b->uid);
case TABLE_GID:
return CMP(a->gid, b->gid);
case TABLE_PID:
return CMP(a->pid, b->pid);
case TABLE_MODE:
case TABLE_MODE_INODE_TYPE:
return CMP(a->mode, b->mode);
case TABLE_DEVNUM:
r = CMP(major(a->devnum), major(b->devnum));
if (r != 0)
return r;
return CMP(minor(a->devnum), minor(b->devnum));
case TABLE_JSON:
return json_variant_compare(a->json, b->json);
case TABLE_EMPTY:
return 0;
default:
assert_not_reached();
}
}
static int table_data_compare(const size_t *a, const size_t *b, Table *t) {
int r;
/* This is called from qsort()s inner loops. Correctly implemented qsort will never pass NULL so we
just suppress the check via POINTER_MAY_BE_NULL instead of assert() to avoid the runtime cost. */
POINTER_MAY_BE_NULL(a);
POINTER_MAY_BE_NULL(b);
assert(t);
assert(t->sort_map);
/* Make sure the header stays at the beginning */
if (*a < t->n_columns && *b < t->n_columns)
return 0;
if (*a < t->n_columns)
return -1;
if (*b < t->n_columns)
return 1;
/* Order other lines by the sorting map */
for (size_t i = 0; i < t->n_sort_map; i++) {
TableData *d, *dd;
d = t->data[*a + t->sort_map[i]];
dd = t->data[*b + t->sort_map[i]];
r = cell_data_compare(d, dd);
if (r != 0)
return t->reverse_map && t->reverse_map[t->sort_map[i]] ? -r : r;
}
/* Order identical lines by the order there were originally added in */
return CMP(*a, *b);
}
static char* format_strv_width(char **strv, size_t column_width) {
_cleanup_(memstream_done) MemStream m = {};
FILE *f;
f = memstream_init(&m);
if (!f)
return NULL;
size_t position = 0;
STRV_FOREACH(p, strv) {
size_t our_len = utf8_console_width(*p); /* This returns -1 on invalid utf-8 (which shouldn't happen).
* If that happens, we'll just print one item per line. */
if (position == 0) {
fputs(*p, f);
position = our_len;
} else if (size_add(size_add(position, 1), our_len) <= column_width) {
fprintf(f, " %s", *p);
position = size_add(size_add(position, 1), our_len);
} else {
fprintf(f, "\n%s", *p);
position = our_len;
}
}
char *buf;
if (memstream_finalize(&m, &buf, NULL) < 0)
return NULL;
return buf;
}
static const char* table_data_format(
Table *t,
TableData *d,
bool avoid_uppercasing,
size_t column_width,
bool *have_soft) {
assert(d);
if (d->formatted &&
/* Only TABLE_STRV_WRAPPED adjust based on column_width so far… */
(d->type != TABLE_STRV_WRAPPED || d->formatted_for_width == column_width))
return d->formatted;
d->formatted = mfree(d->formatted);
switch (d->type) {
case TABLE_EMPTY:
return table_ersatz_string(t);
case TABLE_STRING:
case TABLE_STRING_WITH_ANSI:
case TABLE_PATH:
case TABLE_PATH_BASENAME:
case TABLE_FIELD:
case TABLE_HEADER:
case TABLE_VERSION: {
_cleanup_free_ char *bn = NULL;
const char *s;
if (d->type == TABLE_PATH_BASENAME)
s = path_extract_filename(d->string, &bn) < 0 ? d->string : bn;
else
s = d->string;
if (d->uppercase && !avoid_uppercasing) {
d->formatted = new(char, strlen(s) + (d->type == TABLE_FIELD) + 1);
if (!d->formatted)
return NULL;
char *q = d->formatted;
for (const char *p = s; *p; p++)
*(q++) = (char) toupper((unsigned char) *p);
if (d->type == TABLE_FIELD)
*(q++) = ':';
*q = 0;
return d->formatted;
}
if (d->type == TABLE_FIELD)
return (d->formatted = strjoin(s, ":"));
if (bn)
return (d->formatted = TAKE_PTR(bn));
return d->string;
}
case TABLE_STRV:
if (strv_isempty(d->strv))
return table_ersatz_string(t);
return (d->formatted = strv_join(d->strv, "\n"));
case TABLE_STRV_WRAPPED:
if (strv_isempty(d->strv))
return table_ersatz_string(t);
d->formatted = format_strv_width(d->strv, column_width);
if (!d->formatted)
return NULL;
d->formatted_for_width = column_width;
if (have_soft)
*have_soft = true;
return d->formatted;
case TABLE_BOOLEAN:
return yes_no(d->boolean);
case TABLE_BOOLEAN_CHECKMARK:
return glyph(d->boolean ? GLYPH_CHECK_MARK : GLYPH_CROSS_MARK);
case TABLE_TRISTATE:
if (d->tristate < 0)
return table_ersatz_string(t);
return yes_no(d->tristate);
case TABLE_TIMESTAMP:
case TABLE_TIMESTAMP_UTC:
case TABLE_TIMESTAMP_RELATIVE:
case TABLE_TIMESTAMP_RELATIVE_MONOTONIC:
case TABLE_TIMESTAMP_LEFT:
case TABLE_TIMESTAMP_DATE: {
char *ret;
_cleanup_free_ char *p = new(
char,
IN_SET(d->type, TABLE_TIMESTAMP_RELATIVE, TABLE_TIMESTAMP_RELATIVE_MONOTONIC, TABLE_TIMESTAMP_LEFT) ?
FORMAT_TIMESTAMP_RELATIVE_MAX : FORMAT_TIMESTAMP_MAX);
if (!p)
return NULL;
if (d->type == TABLE_TIMESTAMP)
ret = format_timestamp(p, FORMAT_TIMESTAMP_MAX, d->timestamp);
else if (d->type == TABLE_TIMESTAMP_UTC)
ret = format_timestamp_style(p, FORMAT_TIMESTAMP_MAX, d->timestamp, TIMESTAMP_UTC);
else if (d->type == TABLE_TIMESTAMP_DATE)
ret = format_timestamp_style(p, FORMAT_TIMESTAMP_MAX, d->timestamp, TIMESTAMP_DATE);
else if (d->type == TABLE_TIMESTAMP_RELATIVE_MONOTONIC)
ret = format_timestamp_relative_monotonic(p, FORMAT_TIMESTAMP_RELATIVE_MAX, d->timestamp);
else
ret = format_timestamp_relative_full(p, FORMAT_TIMESTAMP_RELATIVE_MAX,
d->timestamp, CLOCK_REALTIME,
/* implicit_left= */ d->type == TABLE_TIMESTAMP_LEFT);
if (!ret)
return "-";
return (d->formatted = TAKE_PTR(p));
}
case TABLE_TIMESPAN:
case TABLE_TIMESPAN_MSEC:
case TABLE_TIMESPAN_DAY: {
_cleanup_free_ char *p = new(char, FORMAT_TIMESPAN_MAX);
if (!p)
return NULL;
if (!format_timespan(p, FORMAT_TIMESPAN_MAX, d->timespan,
d->type == TABLE_TIMESPAN ? 0 :
d->type == TABLE_TIMESPAN_MSEC ? USEC_PER_MSEC : USEC_PER_DAY))
return "-";
return (d->formatted = TAKE_PTR(p));
}
case TABLE_SIZE: {
_cleanup_free_ char *p = new(char, FORMAT_BYTES_MAX);
if (!p)
return NULL;
if (!format_bytes(p, FORMAT_BYTES_MAX, d->size))
return table_ersatz_string(t);
return (d->formatted = TAKE_PTR(p));
}
case TABLE_BPS: {
_cleanup_free_ char *p = new(char, FORMAT_BYTES_MAX+2);
if (!p)
return NULL;
if (!format_bytes_full(p, FORMAT_BYTES_MAX, d->size, FORMAT_BYTES_BELOW_POINT))
return table_ersatz_string(t);
size_t n = strlen(p);
strscpy(p + n, FORMAT_BYTES_MAX + 2 - n, "bps");
return (d->formatted = TAKE_PTR(p));
}
case TABLE_INT:
return (d->formatted = asprintf_safe("%i", d->int_val));
case TABLE_INT8:
return (d->formatted = asprintf_safe("%" PRIi8, d->int8));
case TABLE_INT16:
return (d->formatted = asprintf_safe("%" PRIi16, d->int16));
case TABLE_INT32:
return (d->formatted = asprintf_safe("%" PRIi32, d->int32));
case TABLE_INT64:
return (d->formatted = asprintf_safe("%" PRIi64, d->int64));
case TABLE_UINT:
return (d->formatted = asprintf_safe("%u", d->uint_val));
case TABLE_UINT8:
return (d->formatted = asprintf_safe("%" PRIu8, d->uint8));
case TABLE_UINT16:
return (d->formatted = asprintf_safe("%" PRIu16, d->uint16));
case TABLE_UINT32:
return (d->formatted = asprintf_safe("%" PRIu32, d->uint32));
case TABLE_UINT32_HEX:
return (d->formatted = asprintf_safe("%" PRIx32, d->uint32));
case TABLE_UINT32_HEX_0x:
return (d->formatted = asprintf_safe("0x%" PRIx32, d->uint32));
case TABLE_UINT64:
return (d->formatted = asprintf_safe("%" PRIu64, d->uint64));
case TABLE_UINT64_HEX:
return (d->formatted = asprintf_safe("%" PRIx64, d->uint64));
case TABLE_UINT64_HEX_0x:
return (d->formatted = asprintf_safe("0x%" PRIx64, d->uint64));
case TABLE_PERCENT:
return (d->formatted = asprintf_safe("%i%%" , d->percent));
case TABLE_IFINDEX:
(void) format_ifname_full_alloc(d->ifindex, FORMAT_IFNAME_IFINDEX, &d->formatted);
return d->formatted;
case TABLE_IN_ADDR:
case TABLE_IN6_ADDR:
(void) in_addr_to_string(d->type == TABLE_IN_ADDR ? AF_INET : AF_INET6,
&d->address,
&d->formatted);
return d->formatted;
case TABLE_ID128:
d->formatted = new(char, SD_ID128_STRING_MAX);
if (!d->formatted)
return NULL;
return sd_id128_to_string(d->id128, d->formatted);
case TABLE_UUID:
d->formatted = new(char, SD_ID128_UUID_STRING_MAX);
if (!d->formatted)
return NULL;
return sd_id128_to_uuid_string(d->id128, d->formatted);
case TABLE_UID:
if (!uid_is_valid(d->uid))
return table_ersatz_string(t);
return (d->formatted = asprintf_safe(UID_FMT, d->uid));
case TABLE_GID:
if (!gid_is_valid(d->gid))
return table_ersatz_string(t);
return (d->formatted = asprintf_safe(GID_FMT, d->gid));
case TABLE_PID:
if (!pid_is_valid(d->pid))
return table_ersatz_string(t);
return (d->formatted = asprintf_safe(PID_FMT, d->pid));
case TABLE_SIGNAL: {
const char *suffix = signal_to_string(d->int_val);
if (!suffix)
return table_ersatz_string(t);
return (d->formatted = strjoin("SIG", suffix));
}
case TABLE_MODE:
if (d->mode == MODE_INVALID)
return table_ersatz_string(t);
return (d->formatted = asprintf_safe("%04o", d->mode & 07777));
case TABLE_MODE_INODE_TYPE:
if (d->mode == MODE_INVALID)
return table_ersatz_string(t);
return inode_type_to_string(d->mode) ?: table_ersatz_string(t);
case TABLE_DEVNUM:
if (devnum_is_zero(d->devnum))
return table_ersatz_string(t);
return (d->formatted = asprintf_safe(DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(d->devnum)));
case TABLE_JSON:
if (!d->json)
return table_ersatz_string(t);
(void) sd_json_variant_format(d->json, /* flags= */ 0, &d->formatted);
return d->formatted;
default:
assert_not_reached();
}
}
static const char* table_data_format_strip_ansi(
Table *t,
TableData *d,
bool avoid_uppercasing,
size_t column_width,
bool *have_soft,
char **ret_buffer) {
/* Just like table_data_format() but strips ANSI sequences for ANSI fields. */
assert(ret_buffer);
const char *c;
c = table_data_format(t, d, avoid_uppercasing, column_width, have_soft);
if (!c)
return NULL;
if (d->type != TABLE_STRING_WITH_ANSI) {
/* Shortcut: we do not consider ANSI sequences for all other column types, hence return the
* original string as-is */
*ret_buffer = NULL;
return c;
}
_cleanup_free_ char *s = strdup(c);
if (!s)
return NULL;
if (!strip_tab_ansi(&s, /* isz= */ NULL, /* highlight= */ NULL))
return NULL;
*ret_buffer = TAKE_PTR(s);
return *ret_buffer;
}
static int console_width_height(
const char *s,
size_t *ret_width,
size_t *ret_height) {
size_t max_width = 0, height = 0;
const char *p;
assert(s);
/* Determine the width and height in console character cells the specified string needs. */
do {
size_t k;
p = strchr(s, '\n');
if (p) {
_cleanup_free_ char *c = NULL;
c = strndup(s, p - s);
if (!c)
return -ENOMEM;
k = utf8_console_width(c);
s = p + 1;
} else {
k = utf8_console_width(s);
s = NULL;
}
if (k == SIZE_MAX)
return -EINVAL;
if (k > max_width)
max_width = k;
height++;
} while (!isempty(s));
if (ret_width)
*ret_width = max_width;
if (ret_height)
*ret_height = height;