localectl: use Table to show status

This commit is contained in:
Yu Watanabe 2022-06-14 09:09:00 +09:00
parent 3e5203b3eb
commit 2b1eb5f871

View file

@ -2,7 +2,6 @@
#include <getopt.h>
#include <stdbool.h>
#include <stdlib.h>
#include "sd-bus.h"
@ -11,6 +10,7 @@
#include "bus-map-properties.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-table.h"
#include "kbd-util.h"
#include "locale-setup.h"
#include "main-func.h"
@ -51,50 +51,102 @@ static void status_info_clear(StatusInfo *info) {
}
}
static void print_overridden_variables(void) {
_cleanup_(locale_context_clear) LocaleContext c = { .mtime = USEC_INFINITY };
_cleanup_strv_free_ char **env = NULL;
static int print_status_info(StatusInfo *i) {
_cleanup_strv_free_ char **kernel_locale = NULL;
_cleanup_(table_unrefp) Table *table = NULL;
TableCell *cell;
int r;
if (arg_transport != BUS_TRANSPORT_LOCAL)
return;
(void) locale_context_load(&c, LOCALE_LOAD_PROC_CMDLINE);
r = locale_context_build_env(&c, &env, NULL);
if (r < 0)
return (void) log_warning_errno(r, "Failed to build locale settings from kernel command line, ignoring: %m");
STRV_FOREACH(p, env)
if (p == env)
log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.\n"
" Command Line: %s", *p);
else
log_warning(" %s", *p);
}
static void print_status_info(StatusInfo *i) {
assert(i);
if (strv_isempty(i->locale))
puts(" System Locale: n/a");
else {
printf(" System Locale: %s\n", i->locale[0]);
STRV_FOREACH(j, i->locale + 1)
printf(" %s\n", *j);
if (arg_transport == BUS_TRANSPORT_LOCAL) {
_cleanup_(locale_context_clear) LocaleContext c = { .mtime = USEC_INFINITY };
r = locale_context_load(&c, LOCALE_LOAD_PROC_CMDLINE);
if (r < 0)
return log_error_errno(r, "Failed to read /proc/cmdline: %m");
r = locale_context_build_env(&c, &kernel_locale, NULL);
if (r < 0)
return log_error_errno(r, "Failed to build locale settings from kernel command line: %m");
}
printf(" VC Keymap: %s\n", strna(i->vconsole_keymap));
if (!isempty(i->vconsole_keymap_toggle))
printf("VC Toggle Keymap: %s\n", i->vconsole_keymap_toggle);
table = table_new("key", "value");
if (!table)
return log_oom();
printf(" X11 Layout: %s\n", strna(i->x11_layout));
if (!isempty(i->x11_model))
printf(" X11 Model: %s\n", i->x11_model);
if (!isempty(i->x11_variant))
printf(" X11 Variant: %s\n", i->x11_variant);
if (!isempty(i->x11_options))
printf(" X11 Options: %s\n", i->x11_options);
assert_se(cell = table_get_cell(table, 0, 0));
(void) table_set_ellipsize_percent(table, cell, 100);
(void) table_set_align_percent(table, cell, 100);
table_set_header(table, false);
r = table_set_empty_string(table, "n/a");
if (r < 0)
return log_oom();
if (!strv_isempty(kernel_locale)) {
log_warning("Warning: Settings on kernel command line override system locale settings in /etc/locale.conf.");
r = table_add_many(table,
TABLE_STRING, "Command Line:",
TABLE_SET_COLOR, ansi_highlight_yellow(),
TABLE_STRV, kernel_locale,
TABLE_SET_COLOR, ansi_highlight_yellow());
if (r < 0)
return table_log_add_error(r);
}
r = table_add_many(table,
TABLE_STRING, "System Locale:",
TABLE_STRV, i->locale,
TABLE_STRING, "VC Keymap:",
TABLE_STRING, i->vconsole_keymap);
if (r < 0)
return table_log_add_error(r);
if (!isempty(i->vconsole_keymap_toggle)) {
r = table_add_many(table,
TABLE_STRING, "VC Toggle Keymap:",
TABLE_STRING, i->vconsole_keymap_toggle);
if (r < 0)
return table_log_add_error(r);
}
r = table_add_many(table,
TABLE_STRING, "X11 Layout:",
TABLE_STRING, i->x11_layout);
if (r < 0)
return table_log_add_error(r);
if (!isempty(i->x11_model)) {
r = table_add_many(table,
TABLE_STRING, "X11 Model:",
TABLE_STRING, i->x11_model);
if (r < 0)
return table_log_add_error(r);
}
if (!isempty(i->x11_variant)) {
r = table_add_many(table,
TABLE_STRING, "X11 Variant:",
TABLE_STRING, i->x11_variant);
if (r < 0)
return table_log_add_error(r);
}
if (!isempty(i->x11_options)) {
r = table_add_many(table,
TABLE_STRING, "X11 Options:",
TABLE_STRING, i->x11_options);
if (r < 0)
return table_log_add_error(r);
}
r = table_print(table, NULL);
if (r < 0)
return table_log_print_error(r);
return 0;
}
static int show_status(int argc, char **argv, void *userdata) {
@ -128,10 +180,7 @@ static int show_status(int argc, char **argv, void *userdata) {
if (r < 0)
return log_error_errno(r, "Could not get properties: %s", bus_error_message(&error, r));
print_overridden_variables();
print_status_info(&info);
return r;
return print_status_info(&info);
}
static int set_locale(int argc, char **argv, void *userdata) {