From 3410cb256d3b033c2ca61c78766eb58652dce49f Mon Sep 17 00:00:00 2001 From: s3rb31 Date: Tue, 21 Feb 2017 02:12:39 +0100 Subject: [PATCH] Implement mapping from string to layout as extra function --- include/util.h | 8 ++++++++ src/commands.c | 15 +-------------- src/util.c | 25 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/include/util.h b/include/util.h index e5ba3341..cbe9778c 100644 --- a/include/util.h +++ b/include/util.h @@ -69,6 +69,14 @@ Rect rect_sub(Rect a, Rect b); */ __attribute__((pure)) bool name_is_digits(const char *name); +/** + * Set 'out' to the layout_t value for the given layout. The function + * returns true on success or false if the passed string is not a valid + * layout name. + * + */ +bool layout_from_name(const char *layout_str, layout_t *out); + /** * Parses the workspace name as a number. Returns -1 if the workspace should be * interpreted as a "named workspace". diff --git a/src/commands.c b/src/commands.c index 2387ddd7..44a5d8a4 100644 --- a/src/commands.c +++ b/src/commands.c @@ -1489,21 +1489,8 @@ void cmd_move_direction(I3_CMD, const char *direction, long move_px) { void cmd_layout(I3_CMD, const char *layout_str) { HANDLE_EMPTY_MATCH; - if (strcmp(layout_str, "stacking") == 0) - layout_str = "stacked"; layout_t layout; - /* default is a special case which will be handled in con_set_layout(). */ - if (strcmp(layout_str, "default") == 0) - layout = L_DEFAULT; - else if (strcmp(layout_str, "stacked") == 0) - layout = L_STACKED; - else if (strcmp(layout_str, "tabbed") == 0) - layout = L_TABBED; - else if (strcmp(layout_str, "splitv") == 0) - layout = L_SPLITV; - else if (strcmp(layout_str, "splith") == 0) - layout = L_SPLITH; - else { + if (!layout_from_name(layout_str, &layout)) { ELOG("Unknown layout \"%s\", this is a mismatch between code and parser spec.\n", layout_str); return; } diff --git a/src/util.c b/src/util.c index cfe4c953..b6f45fde 100644 --- a/src/util.c +++ b/src/util.c @@ -66,6 +66,31 @@ __attribute__((pure)) bool name_is_digits(const char *name) { return true; } +/** + * Set 'out' to the layout_t value for the given layout. The function + * returns true on success or false if the passed string is not a valid + * layout name. + * + */ +bool layout_from_name(const char *layout_str, layout_t *out) { + if (strcmp(layout_str, "default") == 0) { + *out = L_DEFAULT; + } else if (strcasecmp(layout_str, "stacked") == 0 || + strcasecmp(layout_str, "stacking") == 0) { + *out = L_STACKED; + } else if (strcasecmp(layout_str, "tabbed") == 0) { + *out = L_TABBED; + } else if (strcasecmp(layout_str, "splitv") == 0) { + *out = L_SPLITV; + } else if (strcasecmp(layout_str, "splith") == 0) { + *out = L_SPLITH; + } else { + return false; + } + + return true; +} + /* * Parses the workspace name as a number. Returns -1 if the workspace should be * interpreted as a "named workspace". -- 2.39.2