diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/xz/hardware.c | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/src/xz/hardware.c b/src/xz/hardware.c index ff32f6d3..dca9f5dd 100644 --- a/src/xz/hardware.c +++ b/src/xz/hardware.c @@ -98,15 +98,22 @@ hardware_memlimit_get(enum operation_mode mode) /// Helper for hardware_memlimit_show() to print one human-readable info line. 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 // is 0 or UINT64_MAX. This might get a bit more complex once there // is threading support. See the comment in hardware_memlimit_get(). if (value == 0 || value == UINT64_MAX) - printf("%s %s\n", str, _("Disabled")); + printf("%-*s %s\n", fw, str, _("Disabled")); 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(value, 1)); @@ -121,14 +128,30 @@ hardware_memlimit_show(void) printf("%" PRIu64 "\t%" PRIu64 "\t%" PRIu64 "\n", total_ram, memlimit_compress, memlimit_decompress); } else { - // TRANSLATORS: Test with "xz --info-memory" to see if - // the alignment looks nice. - memlimit_show(_("Total amount of physical memory (RAM): "), - total_ram); - memlimit_show(_("Memory usage limit for compression: "), - memlimit_compress); - memlimit_show(_("Memory usage limit for decompression: "), - memlimit_decompress); + const char *msgs[] = { + _("Amount of physical memory (RAM):"), + _("Memory usage limit for compression:"), + _("Memory usage limit for decompression:"), + }; + + size_t width_max = 1; + 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); |