Code

screen_utils: automatically append "[y/n]"
[ncmpc.git] / src / screen_utils.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_utils.h"
21 #include "screen.h"
22 #include "mpdclient.h"
23 #include "config.h"
24 #include "i18n.h"
25 #include "options.h"
26 #include "colors.h"
27 #include "wreadln.h"
29 #include <mpd/client.h>
30 #include <ctype.h>
32 void
33 screen_bell(void)
34 {
35         if (options.audible_bell)
36                 beep();
37         if (options.visible_bell)
38                 flash();
39 }
41 static bool
42 ignore_key(int key)
43 {
44         return
45 #ifdef HAVE_GETMOUSE
46                 /* ignore mouse events */
47                 key == KEY_MOUSE ||
48 #endif
49                 key == ERR;
50 }
52 int
53 screen_getch(const char *prompt)
54 {
55         WINDOW *w = screen.status_bar.window.w;
57         colors_use(w, COLOR_STATUS_ALERT);
58         werase(w);
59         wmove(w, 0, 0);
60         waddstr(w, prompt);
62         echo();
63         curs_set(1);
65         int key;
66         while (ignore_key(key = wgetch(w))) {}
68         noecho();
69         curs_set(0);
71         return key;
72 }
74 bool
75 screen_get_yesno(const char *_prompt, bool def)
76 {
77         /* NOTE: if one day a translator decides to use a multi-byte character
78            for one of the yes/no keys, we'll have to parse it properly */
80         char *prompt = g_strdup_printf(_("%s [%s/%s] "), _prompt, YES, NO);
81         int key = tolower(screen_getch(prompt));
82         g_free(prompt);
84         if (key == YES[0])
85                 return true;
86         else if (key == NO[0])
87                 return false;
88         else
89                 return def;
90 }
92 char *
93 screen_readln(const char *prompt,
94               const char *value,
95               GList **history,
96               GCompletion *gcmp)
97 {
98         struct window *window = &screen.status_bar.window;
99         WINDOW *w = window->w;
100         char *line = NULL;
102         wmove(w, 0,0);
103         curs_set(1);
104         colors_use(w, COLOR_STATUS_ALERT);
105         line = wreadln(w, prompt, value, window->cols, history, gcmp);
106         curs_set(0);
107         return line;
110 char *
111 screen_read_password(const char *prompt)
113         struct window *window = &screen.status_bar.window;
114         WINDOW *w = window->w;
116         wmove(w, 0,0);
117         curs_set(1);
118         colors_use(w, COLOR_STATUS_ALERT);
120         if (prompt == NULL)
121                 prompt = _("Password");
122         char *ret = wreadln_masked(w, prompt, NULL, window->cols, NULL, NULL);
124         curs_set(0);
125         return ret;
128 void
129 screen_display_completion_list(GList *list)
131         static GList *prev_list = NULL;
132         static guint prev_length = 0;
133         static guint offset = 0;
134         WINDOW *w = screen.main_window.w;
136         unsigned length = g_list_length(list);
137         if (list == prev_list && length == prev_length) {
138                 offset += screen.main_window.rows;
139                 if (offset >= length)
140                         offset = 0;
141         } else {
142                 prev_list = list;
143                 prev_length = length;
144                 offset = 0;
145         }
147         colors_use(w, COLOR_STATUS_ALERT);
149         unsigned y = 0;
150         while (y < screen.main_window.rows) {
151                 GList *item = g_list_nth(list, y+offset);
153                 wmove(w, y++, 0);
154                 wclrtoeol(w);
155                 if (item) {
156                         gchar *tmp = g_strdup(item->data);
157                         waddstr(w, g_basename(tmp));
158                         g_free(tmp);
159                 }
160         }
162         wrefresh(w);
163         colors_use(w, COLOR_LIST);