Code

screen_utils: Added new function which jumps to an entry.
authorPatrick Hallen <patrick.hallen@rwth-aachen.de>
Wed, 11 Feb 2009 19:09:33 +0000 (20:09 +0100)
committerPatrick Hallen <patrick.hallen@rwth-aachen.de>
Wed, 11 Feb 2009 19:09:33 +0000 (20:09 +0100)
Added a new function which opens a prompt. While typing it jumps to the
entry on the screen which starts with the sequence of character you just
typed. Typing any non-printable ncmpc hotkey will exit the prompt.

src/screen_utils.c
src/screen_utils.h

index 9dde20c03aa8a2fd3e0ac0abd6ecbb3aeafabea4..e9534cc8a37927fd0718bd97f885773118b89e81 100644 (file)
 #include "options.h"
 #include "colors.h"
 #include "wreadln.h"
+#ifndef NCMPC_H
+#include "ncmpc.h"
+#endif /* NCMPC_H */
 
 #include <stdlib.h>
 #include <unistd.h>
 
 #define FIND_PROMPT  _("Find")
 #define RFIND_PROMPT _("Find backward")
+#define JUMP_PROMPT _("Jump")
 
 void
 screen_bell(void)
@@ -213,6 +217,57 @@ screen_find(list_window_t *lw,
        return 0;
 }
 
+/* query user for a string and jump to the entry
+ * which begins with this string while the users types */
+void
+screen_jump(struct list_window *lw,
+               list_window_callback_fn_t callback_fn,
+               void *callback_data)
+{
+       char *search_str, *iter;
+       const int WRLN_MAX_LINE_SIZE = 1024;
+       int key = 65;
+       command_t cmd;
+
+       if (screen.findbuf) {
+               g_free(screen.findbuf);
+               screen.findbuf = NULL;
+       }
+       screen.findbuf = g_malloc0(WRLN_MAX_LINE_SIZE);
+       /* In screen.findbuf is the whole string which is displayed in the status_window
+        * and search_str is the string the user entered (without the prompt) */
+       search_str = screen.findbuf + g_snprintf(screen.findbuf, WRLN_MAX_LINE_SIZE, "%s: ", JUMP_PROMPT);
+       iter = search_str;
+
+       /* unfortunately wgetch returns "next/previous-page" not as an ascii-char */
+       while(!g_ascii_iscntrl(key) && key != KEY_NPAGE && key != KEY_PPAGE) {
+               key = screen_getch(screen.status_window.w, screen.findbuf);
+               /* if backspace was pressed */
+               if (key == KEY_BACKSPACE) {
+                       /* don't end the loop */
+                       key = 65;
+                       if (search_str <= g_utf8_find_prev_char(screen.findbuf, iter))
+                               iter = g_utf8_find_prev_char(screen.findbuf, iter);
+                       *iter = '\0';
+                       continue;
+               }
+               else {
+                       *iter = key;
+                       if (iter < screen.findbuf + WRLN_MAX_LINE_SIZE - 3)
+                               ++iter;
+               }
+               list_window_jump(lw, callback_fn, callback_data, search_str);
+               /* repaint the list_window */
+               list_window_paint(lw, callback_fn, callback_data);
+               wrefresh(lw->w);
+       }
+
+       /* ncmpc should get the command */
+       ungetch(key);
+       if ((cmd=get_keyboard_command()) != CMD_NONE)
+               do_input_event(cmd);
+}
+
 void
 screen_display_completion_list(GList *list)
 {
index 46895e3441cc0c8d913d059b9f4f1f9c40096abb..bfaf7c239c2b5e70d9d5db31a9bc5d78e89a4ee8 100644 (file)
@@ -51,6 +51,12 @@ int screen_find(struct list_window *lw,
                list_window_callback_fn_t callback_fn,
                void *callback_data);
 
+/* query user for a string and jump to the entry
+ * which begins with this string while the users types */
+void screen_jump(struct list_window *lw,
+               list_window_callback_fn_t callback_fn,
+               void *callback_data);
+
 gint screen_auth(struct mpdclient *c);
 
 void screen_display_completion_list(GList *list);