1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
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.
9 *
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.
14 *
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_append(struct screen_text *text, const char *str)
41 {
42 const char *eol, *next;
44 assert(str != NULL);
46 while ((eol = strchr(str, '\n')) != NULL) {
47 char *line;
49 next = eol + 1;
51 /* strip whitespace at end */
53 while (eol > str && (unsigned char)eol[-1] <= 0x20)
54 --eol;
56 /* create copy and append it to text->lines */
58 line = g_malloc(eol - str + 1);
59 memcpy(line, str, eol - str);
60 line[eol - str] = 0;
62 g_ptr_array_add(text->lines, line);
64 /* reset control characters */
66 for (eol = line + (eol - str); line < eol; ++line)
67 if ((unsigned char)*line < 0x20)
68 *line = ' ';
70 str = next;
71 }
73 if (*str != 0)
74 g_ptr_array_add(text->lines, g_strdup(str));
76 list_window_set_length(text->lw, text->lines->len);
77 }
79 const char *
80 screen_text_list_callback(unsigned idx, void *data)
81 {
82 const struct screen_text *text = data;
83 static char buffer[256];
84 char *value;
86 assert(idx < text->lines->len);
88 value = utf8_to_locale(g_ptr_array_index(text->lines, idx));
89 g_strlcpy(buffer, value, sizeof(buffer));
90 g_free(value);
92 return buffer;
93 }
95 bool
96 screen_text_cmd(struct screen_text *text,
97 G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
98 {
99 if (list_window_scroll_cmd(text->lw, cmd)) {
100 screen_text_repaint(text);
101 return true;
102 }
104 list_window_set_cursor(text->lw, text->lw->start);
105 if (screen_find(text->lw, cmd, screen_text_list_callback, text)) {
106 /* center the row */
107 list_window_center(text->lw, text->lw->selected);
108 screen_text_repaint(text);
109 return true;
110 }
112 return false;
113 }