Code

release v0.29
[ncmpc.git] / src / screen_text.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 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         assert(str != NULL);
44         const char *eol;
45         while ((eol = strchr(str, '\n')) != NULL) {
46                 char *line;
48                 const char *next = eol + 1;
50                 /* strip whitespace at end */
52                 while (eol > str && (unsigned char)eol[-1] <= 0x20)
53                         --eol;
55                 /* create copy and append it to text->lines */
57                 line = g_malloc(eol - str + 1);
58                 memcpy(line, str, eol - str);
59                 line[eol - str] = 0;
61                 g_ptr_array_add(text->lines, line);
63                 /* reset control characters */
65                 for (eol = line + (eol - str); line < eol; ++line)
66                         if ((unsigned char)*line < 0x20)
67                                 *line = ' ';
69                 str = next;
70         }
72         if (*str != 0)
73                 g_ptr_array_add(text->lines, g_strdup(str));
75         list_window_set_length(text->lw, text->lines->len);
76 }
78 const char *
79 screen_text_list_callback(unsigned idx, void *data)
80 {
81         const struct screen_text *text = data;
83         assert(idx < text->lines->len);
85         char *value = utf8_to_locale(g_ptr_array_index(text->lines, idx));
87         static char buffer[256];
88         g_strlcpy(buffer, value, sizeof(buffer));
89         g_free(value);
91         return buffer;
92 }
94 bool
95 screen_text_cmd(struct screen_text *text,
96                 gcc_unused struct mpdclient *c, command_t cmd)
97 {
98         if (list_window_scroll_cmd(text->lw, cmd)) {
99                 screen_text_repaint(text);
100                 return true;
101         }
103         list_window_set_cursor(text->lw, text->lw->start);
104         if (screen_find(text->lw, cmd, screen_text_list_callback, text)) {
105                 /* center the row */
106                 list_window_center(text->lw, text->lw->selected);
107                 screen_text_repaint(text);
108                 return true;
109         }
111         return false;