diff --git a/common.c b/common.c index fab0205..3d40816 100644 --- a/common.c +++ b/common.c @@ -49,6 +49,15 @@ // --- To be moved to liberty -------------------------------------------------- +#define LIST_INSERT_WITH_TAIL(head, tail, link, following) \ + BLOCK_START \ + if (following) \ + LIST_APPEND_WITH_TAIL ((head), (following)->prev, (link)); \ + else \ + LIST_APPEND_WITH_TAIL ((head), (tail), (link)); \ + (link)->next = (following); \ + BLOCK_END + static void split_str (const char *s, char delimiter, struct str_vector *out) { diff --git a/degesch.c b/degesch.c index 7e8b8b4..b103d6b 100644 --- a/degesch.c +++ b/degesch.c @@ -5936,6 +5936,37 @@ handle_buffer_close (struct app_context *ctx, struct handler_args *a) } } +static bool +handle_buffer_move (struct app_context *ctx, struct handler_args *a) +{ + unsigned long request; + if (!xstrtoul (&request, a->arguments, 10)) + return false; + + unsigned long total = 0; + LIST_FOR_EACH (struct buffer, iter, ctx->buffers) + total++; + + if (request == 0 || request > total) + { + buffer_send_error (ctx, NULL, "%s: %s", + "Can't move buffer", "requested position is out of range"); + return true; + } + + struct buffer *buffer = a->buffer; + LIST_UNLINK_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer); + + struct buffer *following = ctx->buffers; + while (--request && following) + following = following->next; + + LIST_INSERT_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer, following); + + refresh_prompt (ctx); + return true; +} + static bool handle_command_buffer (struct app_context *ctx, struct handler_args *a) { @@ -5954,10 +5985,7 @@ handle_command_buffer (struct app_context *ctx, struct handler_args *a) buffer_print_backlog (ctx, a->buffer); } else if (!strcasecmp_ascii (action, "move")) - { - // TODO: unlink the buffer and link it back at index; - // we will probably need to extend liberty for this - } + handle_buffer_move (ctx, a); else if (!strcasecmp_ascii (action, "close")) handle_buffer_close (ctx, a); else @@ -6724,7 +6752,7 @@ g_command_handlers[] = "[]", handle_command_quit, 0 }, { "buffer", "Manage buffers", - "list | clear | move | { close [ | ] } | ", + " | list | clear | move | close [ | ]", handle_command_buffer, 0 }, { "set", "Manage configuration", "[