xz: Automatically align the strings in --info-memory.

This makes it easier to translate the strings.

Also, the string for amount of RAM was shortened.
This commit is contained in:
Lasse Collin 2019-06-28 00:54:31 +03:00
parent 8ce679125d
commit de1f47b2b4
1 changed files with 34 additions and 11 deletions

View File

@ -98,15 +98,22 @@ hardware_memlimit_get(enum operation_mode mode)
/// Helper for hardware_memlimit_show() to print one human-readable info line. /// Helper for hardware_memlimit_show() to print one human-readable info line.
static void static void
memlimit_show(const char *str, uint64_t value) memlimit_show(const char *str, size_t str_columns, uint64_t value)
{ {
// Calculate the field width so that str will be padded to take
// str_columns on the terminal.
//
// NOTE: If the string is invalid, this will be -1. Using -1 as
// the field width is fine here so it's not handled specially.
const int fw = tuklib_mbstr_fw(str, (int)(str_columns));
// The memory usage limit is considered to be disabled if value // The memory usage limit is considered to be disabled if value
// is 0 or UINT64_MAX. This might get a bit more complex once there // is 0 or UINT64_MAX. This might get a bit more complex once there
// is threading support. See the comment in hardware_memlimit_get(). // is threading support. See the comment in hardware_memlimit_get().
if (value == 0 || value == UINT64_MAX) if (value == 0 || value == UINT64_MAX)
printf("%s %s\n", str, _("Disabled")); printf("%-*s %s\n", fw, str, _("Disabled"));
else else
printf("%s %s MiB (%s B)\n", str, printf("%-*s %s MiB (%s B)\n", fw, str,
uint64_to_str(round_up_to_mib(value), 0), uint64_to_str(round_up_to_mib(value), 0),
uint64_to_str(value, 1)); uint64_to_str(value, 1));
@ -121,14 +128,30 @@ hardware_memlimit_show(void)
printf("%" PRIu64 "\t%" PRIu64 "\t%" PRIu64 "\n", total_ram, printf("%" PRIu64 "\t%" PRIu64 "\t%" PRIu64 "\n", total_ram,
memlimit_compress, memlimit_decompress); memlimit_compress, memlimit_decompress);
} else { } else {
// TRANSLATORS: Test with "xz --info-memory" to see if const char *msgs[] = {
// the alignment looks nice. _("Amount of physical memory (RAM):"),
memlimit_show(_("Total amount of physical memory (RAM): "), _("Memory usage limit for compression:"),
total_ram); _("Memory usage limit for decompression:"),
memlimit_show(_("Memory usage limit for compression: "), };
memlimit_compress);
memlimit_show(_("Memory usage limit for decompression: "), size_t width_max = 1;
memlimit_decompress); for (unsigned i = 0; i < ARRAY_SIZE(msgs); ++i) {
size_t w = tuklib_mbstr_width(msgs[i], NULL);
// When debugging, catch invalid strings with
// an assertion. Otherwise fallback to 1 so
// that the columns just won't be aligned.
assert(w != (size_t)-1);
if (w == (size_t)-1)
w = 1;
if (width_max < w)
width_max = w;
}
memlimit_show(msgs[0], width_max, total_ram);
memlimit_show(msgs[1], width_max, memlimit_compress);
memlimit_show(msgs[2], width_max, memlimit_decompress);
} }
tuklib_exit(E_SUCCESS, E_ERROR, message_verbosity_get() != V_SILENT); tuklib_exit(E_SUCCESS, E_ERROR, message_verbosity_get() != V_SILENT);