degesch: add partial matching /buffer goto

This commit is contained in:
Přemysl Eric Janouch 2016-03-26 13:00:10 +01:00
parent 6292114c76
commit 1e24d1d1b8
1 changed files with 38 additions and 5 deletions

View File

@ -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[] =
"[<message>]",
handle_command_quit, 0 },
{ "buffer", "Manage buffers",
"<N> | list | clear | move <N> | close [<N> | <name>]",
"<N> | list | clear | move <N> | goto <N or name> | close [<N or name>]",
handle_command_buffer, 0 },
{ "set", "Manage configuration",
"[<option>]",