]> git.mdlowis.com Git - proto/labwc.git/commitdiff
action: simplify action_prompt_command()
authortokyo4j <hrak1529@gmail.com>
Wed, 1 Oct 2025 02:54:26 +0000 (11:54 +0900)
committerHiroaki Yamamoto <hrak1529@gmail.com>
Sat, 11 Oct 2025 14:53:02 +0000 (23:53 +0900)
include/action-prompt-command.h [deleted file]
src/action-prompt-command.c [deleted file]
src/action.c
src/meson.build

diff --git a/include/action-prompt-command.h b/include/action-prompt-command.h
deleted file mode 100644 (file)
index 219896b..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-#ifndef LABWC_ACTION_PROMPT_COMMAND_H
-#define LABWC_ACTION_PROMPT_COMMAND_H
-
-struct buf;
-struct action;
-struct theme;
-
-void action_prompt_command(struct buf *buf, const char *format,
-       struct action *action, struct theme *theme);
-
-#endif /* LABWC_ACTION_PROMPT_COMMAND_H */
diff --git a/src/action-prompt-command.c b/src/action-prompt-command.c
deleted file mode 100644 (file)
index faad41c..0000000
+++ /dev/null
@@ -1,108 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-#define _POSIX_C_SOURCE 200809L
-#include "action-prompt-command.h"
-#include <stdio.h>
-#include <wlr/util/log.h>
-#include "action.h"
-#include "common/buf.h"
-#include "theme.h"
-#include "translate.h"
-
-enum {
-       LAB_PROMPT_NONE = 0,
-       LAB_PROMPT_MESSAGE,
-       LAB_PROMPT_NO,
-       LAB_PROMPT_YES,
-       LAB_PROMPT_BG_COL,
-       LAB_PROMPT_TEXT_COL,
-
-       LAB_PROMPT_COUNT
-};
-
-typedef void field_conversion_type(struct buf *buf, struct action *action,
-               struct theme *theme);
-
-struct field_converter {
-       const char fmt_char;
-       field_conversion_type *fn;
-};
-
-/* %m */
-static void
-set_message(struct buf *buf, struct action *action, struct theme *theme)
-{
-       buf_add(buf, action_get_str(action, "message.prompt", "Choose wisely"));
-}
-
-/* %n */
-static void
-set_no(struct buf *buf, struct action *action, struct theme *theme)
-{
-       buf_add(buf, _("No"));
-}
-
-/* %y */
-static void
-set_yes(struct buf *buf, struct action *action, struct theme *theme)
-{
-       buf_add(buf, _("Yes"));
-}
-
-/* %b */
-static void
-set_bg_col(struct buf *buf, struct action *action, struct theme *theme)
-{
-       buf_add_hex_color(buf, theme->osd_bg_color);
-}
-
-/* %t */
-static void
-set_text_col(struct buf *buf, struct action *action, struct theme *theme)
-{
-       buf_add_hex_color(buf, theme->osd_label_text_color);
-}
-
-static const struct field_converter field_converter[LAB_PROMPT_COUNT] = {
-       [LAB_PROMPT_MESSAGE]           = { 'm', set_message },
-       [LAB_PROMPT_NO]                = { 'n', set_no },
-       [LAB_PROMPT_YES]               = { 'y', set_yes },
-       [LAB_PROMPT_BG_COL]            = { 'b', set_bg_col },
-       [LAB_PROMPT_TEXT_COL]          = { 't', set_text_col },
-};
-
-void
-action_prompt_command(struct buf *buf, const char *format,
-               struct action *action, struct theme *theme)
-{
-       if (!format) {
-               wlr_log(WLR_ERROR, "missing format");
-               return;
-       }
-
-       for (const char *p = format; *p; p++) {
-               /*
-                * If we're not on a conversion specifier (like %m) then just
-                * keep adding it to the buffer
-                */
-               if (*p != '%') {
-                       buf_add_char(buf, *p);
-                       continue;
-               }
-
-               /* Process the %* conversion specifier */
-               ++p;
-
-               bool found = false;
-               for (unsigned char i = 0; i < LAB_PROMPT_COUNT; i++) {
-                       if (*p == field_converter[i].fmt_char) {
-                               field_converter[i].fn(buf, action, theme);
-                               found = true;
-                               break;
-                       }
-               }
-               if (!found) {
-                       wlr_log(WLR_ERROR,
-                               "invalid prompt command conversion specifier '%c'", *p);
-               }
-       }
-}
index 9a5623ff60aa72a60b4f4e4201544e7f31742abe..a78b01e81ed2141b71ca7f7edc973adf61ca2831 100644 (file)
@@ -10,7 +10,6 @@
 #include <wlr/types/wlr_scene.h>
 #include <wlr/util/log.h>
 #include "action-prompt-codes.h"
-#include "action-prompt-command.h"
 #include "common/buf.h"
 #include "common/macros.h"
 #include "common/list.h"
@@ -30,6 +29,7 @@
 #include "regions.h"
 #include "ssd.h"
 #include "theme.h"
+#include "translate.h"
 #include "view.h"
 #include "workspaces.h"
 
@@ -831,11 +831,55 @@ handle_view_destroy(struct wl_listener *listener, void *data)
        prompt->view = NULL;
 }
 
+static void
+print_prompt_command(struct buf *buf, const char *format,
+               struct action *action, struct theme *theme)
+{
+       assert(format);
+
+       for (const char *p = format; *p; p++) {
+               /*
+                * If we're not on a conversion specifier (like %m) then just
+                * keep adding it to the buffer
+                */
+               if (*p != '%') {
+                       buf_add_char(buf, *p);
+                       continue;
+               }
+
+               /* Process the %* conversion specifier */
+               ++p;
+
+               switch (*p) {
+               case 'm':
+                       buf_add(buf, action_get_str(action,
+                                       "message.prompt", "Choose wisely"));
+                       break;
+               case 'n':
+                       buf_add(buf, _("No"));
+                       break;
+               case 'y':
+                       buf_add(buf, _("Yes"));
+                       break;
+               case 'b':
+                       buf_add_hex_color(buf, theme->osd_bg_color);
+                       break;
+               case 't':
+                       buf_add_hex_color(buf, theme->osd_label_text_color);
+                       break;
+               default:
+                       wlr_log(WLR_ERROR,
+                               "invalid prompt command conversion specifier '%c'", *p);
+                       break;
+               }
+       }
+}
+
 static void
 action_prompt_create(struct view *view, struct server *server, struct action *action)
 {
        struct buf command = BUF_INIT;
-       action_prompt_command(&command, rc.prompt_command, action, rc.theme);
+       print_prompt_command(&command, rc.prompt_command, action, rc.theme);
 
        wlr_log(WLR_INFO, "prompt command: '%s'", command.data);
 
index dc760f0ccac08deed86ac3dc664aa08a9af9f43d..330b5daf5f41d7b5a7b31f390df4596976994f80 100644 (file)
@@ -1,6 +1,5 @@
 labwc_sources = files(
   'action.c',
-  'action-prompt-command.c',
   'buffer.c',
   'debug.c',
   'desktop.c',