]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
localectl: use Table to show status
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 14 Jun 2022 00:09:00 +0000 (09:09 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 14 Jun 2022 11:56:23 +0000 (20:56 +0900)
src/locale/localectl.c

index 6bfb564f9708e577eda917935092a1136dd4a975..55db3c9ed851e842d8981c291898b222b5c7a6cc 100644 (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;
+        assert(i);
+
+        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");
+        }
+
+        table = table_new("key", "value");
+        if (!table)
+                return log_oom();
 
-        (void) locale_context_load(&c, LOCALE_LOAD_PROC_CMDLINE);
+        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);
 
-        r = locale_context_build_env(&c, &env, NULL);
+        table_set_header(table, false);
+
+        r = table_set_empty_string(table, "n/a");
         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);
-}
+                return log_oom();
 
-static void print_status_info(StatusInfo *i) {
-        assert(i);
+        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 (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 (!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);
         }
 
-        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);
-
-        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);
+        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) {