1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 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.
20 #include "screen_text.h"
21 #include "screen_find.h"
28 screen_text_clear(struct screen_text
*text
)
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);
40 screen_text_set(struct screen_text
*text
, const GString
*str
)
42 const char *p
, *eol
, *next
;
46 screen_text_clear(text
);
49 while ((eol
= strchr(p
, '\n')) != NULL
) {
54 /* strip whitespace at end */
56 while (eol
> p
&& (unsigned char)eol
[-1] <= 0x20)
59 /* create copy and append it to text->lines */
61 line
= g_malloc(eol
- p
+ 1);
62 memcpy(line
, p
, eol
- p
);
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)
77 g_ptr_array_add(text
->lines
, g_strdup(p
));
79 list_window_set_length(text
->lw
, text
->lines
->len
);
83 screen_text_list_callback(unsigned idx
, void *data
)
85 const struct screen_text
*text
= data
;
86 static char buffer
[256];
89 assert(idx
< text
->lines
->len
);
91 value
= utf8_to_locale(g_ptr_array_index(text
->lines
, idx
));
92 g_strlcpy(buffer
, value
, sizeof(buffer
));
99 screen_text_cmd(struct screen_text
*text
,
100 G_GNUC_UNUSED
struct mpdclient
*c
, command_t cmd
)
102 if (list_window_scroll_cmd(text
->lw
, cmd
)) {
103 screen_text_repaint(text
);
107 list_window_set_cursor(text
->lw
, text
->lw
->start
);
108 if (screen_find(text
->lw
, cmd
, screen_text_list_callback
, text
)) {
110 list_window_center(text
->lw
, text
->lw
->selected
);
111 screen_text_repaint(text
);