From 1e24d1d1b8d55005e54948fbd8bff6fb50d3bb8e Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Sat, 26 Mar 2016 13:00:10 +0100 Subject: degesch: add partial matching /buffer goto --- degesch.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/degesch.c b/degesch.c index 87f4680..01be6dc 100644 --- a/degesch.c +++ b/degesch.c @@ -9821,10 +9821,26 @@ try_decode_buffer (struct app_context *ctx, const char *word) struct buffer *buffer = NULL; if (xstrtoul (&n, word, 10) && n <= INT_MAX) buffer = buffer_at_index (ctx, n); - if (!buffer) - buffer = buffer_by_name (ctx, word); - // TODO: partial matches - return buffer; + if (buffer || (buffer = buffer_by_name (ctx, word))) + return buffer; + + // Basic case insensitive partial matching -- at most one buffer can match + int n_matches = 0; + LIST_FOR_EACH (struct buffer, iter, ctx->buffers) + { + char *string = xstrdup (iter->name); + char *pattern = xstrdup_printf ("*%s*", word); + for (char *p = string; *p; p++) *p = tolower_ascii (*p); + for (char *p = pattern; *p; p++) *p = tolower_ascii (*p); + if (!fnmatch (pattern, string, 0)) + { + n_matches++; + buffer = iter; + } + free (string); + free (pattern); + } + return n_matches == 1 ? buffer : NULL; } static void @@ -9860,6 +9876,21 @@ part_channel (struct server *s, const char *channel_name, const char *reason) channel->left_manually = true; } +static bool +handle_buffer_goto (struct app_context *ctx, struct handler_args *a) +{ + if (!*a->arguments) + return false; + + const char *which = cut_word (&a->arguments); + struct buffer *buffer = try_decode_buffer (ctx, which); + if (buffer) + buffer_activate (ctx, buffer); + else + log_global_error (ctx, "#s: #s", "No such buffer", which); + return true; +} + static void handle_buffer_close (struct app_context *ctx, struct handler_args *a) { @@ -9922,6 +9953,8 @@ handle_command_buffer (struct handler_args *a) } else if (!strcasecmp_ascii (action, "move")) result = handle_buffer_move (ctx, a); + else if (!strcasecmp_ascii (action, "goto")) + result = handle_buffer_goto (ctx, a); else if (!strcasecmp_ascii (action, "close")) handle_buffer_close (ctx, a); else @@ -10847,7 +10880,7 @@ g_command_handlers[] = "[]", handle_command_quit, 0 }, { "buffer", "Manage buffers", - " | list | clear | move | close [ | ]", + " | list | clear | move | goto | close []", handle_command_buffer, 0 }, { "set", "Manage configuration", "[