4660f2851c06b90cbafb079c3a02e52da6a99969
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "screen_text.h"
21 #include "screen_find.h"
22 #include "charset.h"
24 #include <assert.h>
25 #include <string.h>
27 void
28 screen_text_clear(struct screen_text *text)
29 {
30 list_window_reset(text->lw);
32 for (guint i = 0; i < text->lines->len; ++i)
33 g_free(g_ptr_array_index(text->lines, i));
35 g_ptr_array_set_size(text->lines, 0);
36 list_window_set_length(text->lw, 0);
37 }
39 void
40 screen_text_set(struct screen_text *text, const GString *str)
41 {
42 const char *p, *eol, *next;
44 assert(str != NULL);
46 screen_text_clear(text);
48 p = str->str;
49 while ((eol = strchr(p, '\n')) != NULL) {
50 char *line;
52 next = eol + 1;
54 /* strip whitespace at end */
56 while (eol > p && (unsigned char)eol[-1] <= 0x20)
57 --eol;
59 /* create copy and append it to text->lines */
61 line = g_malloc(eol - p + 1);
62 memcpy(line, p, eol - p);
63 line[eol - p] = 0;
65 g_ptr_array_add(text->lines, line);
67 /* reset control characters */
69 for (eol = line + (eol - p); line < eol; ++line)
70 if ((unsigned char)*line < 0x20)
71 *line = ' ';
73 p = next;
74 }
76 if (*p != 0)
77 g_ptr_array_add(text->lines, g_strdup(p));
79 list_window_set_length(text->lw, text->lines->len);
80 }
82 const char *
83 screen_text_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
84 G_GNUC_UNUSED char** sc, void *data)
85 {
86 const struct screen_text *text = data;
87 static char buffer[256];
88 char *value;
90 if (idx >= text->lines->len)
91 return NULL;
93 value = utf8_to_locale(g_ptr_array_index(text->lines, idx));
94 g_strlcpy(buffer, value, sizeof(buffer));
95 g_free(value);
97 return buffer;
98 }
100 bool
101 screen_text_cmd(struct screen_text *text,
102 G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
103 {
104 if (list_window_scroll_cmd(text->lw, cmd)) {
105 screen_text_repaint(text);
106 return true;
107 }
109 list_window_set_cursor(text->lw, text->lw->start);
110 if (screen_find(text->lw, cmd, screen_text_list_callback, text)) {
111 /* center the row */
112 list_window_center(text->lw, text->lw->selected);
113 screen_text_repaint(text);
114 return true;
115 }
117 return false;
118 }