1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2018 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 "wreadln.hxx"
21 #include "Completion.hxx"
22 #include "charset.hxx"
23 #include "screen_utils.hxx"
26 #include "util/LocaleString.hxx"
27 #include "util/StringUTF8.hxx"
35 #if (defined(HAVE_CURSES_ENHANCED) || defined(ENABLE_MULTIBYTE)) && !defined(_WIN32)
56 /** the ncurses window where this field is displayed */
59 /** the origin coordinates in the window */
62 /** the screen width of the input field */
65 /** is the input masked, i.e. characters displayed as '*'? */
68 /** the byte position of the cursor */
71 /** the byte position displayed at the origin (for horizontal
75 /** the current value */
78 wreadln(WINDOW *_w, bool _masked)
79 :w(_w), masked(_masked) {}
82 /** max items stored in the history list */
83 static constexpr std::size_t wrln_max_history_length = 32;
85 /** converts a byte position to a screen column */
88 byte_to_screen(const char *data, size_t x)
90 #if defined(HAVE_CURSES_ENHANCED) || defined(ENABLE_MULTIBYTE)
91 assert(x <= strlen(data));
93 return StringWidthMB(data, x);
101 /** finds the first character which doesn't fit on the screen */
104 screen_to_bytes(const char *data, unsigned width)
106 #if defined(HAVE_CURSES_ENHANCED) || defined(ENABLE_MULTIBYTE)
107 size_t length = strlen(data);
110 unsigned p_width = StringWidthMB(data, length);
111 if (p_width <= width)
119 return (size_t)width;
123 /** returns the screen column where the cursor is located */
126 cursor_column(const struct wreadln *wr)
128 return byte_to_screen(wr->value.data() + wr->start,
129 wr->cursor - wr->start);
132 /** returns the offset in the string to align it at the right border
136 right_align_bytes(const char *data, size_t right, unsigned width)
138 #if defined(HAVE_CURSES_ENHANCED) || defined(ENABLE_MULTIBYTE)
141 assert(right <= strlen(data));
143 while (start < right) {
144 if (StringWidthMB(data + start, right - start) < width)
147 start += CharSizeMB(data + start, right - start);
154 return right >= width ? right + 1 - width : 0;
158 /* move the cursor one step to the right */
159 static inline void cursor_move_right(struct wreadln *wr)
161 if (wr->cursor == wr->value.length())
164 size_t size = CharSizeMB(wr->value.data() + wr->cursor,
165 wr->value.length() - wr->cursor);
167 if (cursor_column(wr) >= wr->width)
168 wr->start = right_align_bytes(wr->value.c_str(),
169 wr->cursor, wr->width);
172 /* move the cursor one step to the left */
173 static inline void cursor_move_left(struct wreadln *wr)
175 const char *v = wr->value.c_str();
176 const char *new_cursor = PrevCharMB(v, v + wr->cursor);
177 wr->cursor = new_cursor - v;
178 if (wr->cursor < wr->start)
179 wr->start = wr->cursor;
182 /* move the cursor to the end of the line */
183 static inline void cursor_move_to_eol(struct wreadln *wr)
185 wr->cursor = wr->value.length();
186 if (cursor_column(wr) >= wr->width)
187 wr->start = right_align_bytes(wr->value.c_str(),
188 wr->cursor, wr->width);
191 /* draw line buffer and update cursor position */
192 static inline void drawline(const struct wreadln *wr)
194 wmove(wr->w, wr->point.y, wr->point.x);
195 /* clear input area */
196 whline(wr->w, ' ', wr->width);
197 /* print visible part of the line buffer */
199 whline(wr->w, '*', utf8_width(wr->value.c_str() + wr->start));
201 waddnstr(wr->w, wr->value.c_str() + wr->start,
202 screen_to_bytes(wr->value.c_str(), wr->width));
203 /* move the cursor to the correct position */
204 wmove(wr->w, wr->point.y, wr->point.x + cursor_column(wr));
205 /* tell ncurses to redraw the screen */
210 wreadln_insert_byte(struct wreadln *wr, int key)
213 #if (defined(HAVE_CURSES_ENHANCED) || defined(ENABLE_MULTIBYTE)) && !defined(_WIN32)
214 char buffer[32] = { (char)key };
215 struct pollfd pfd = {
221 /* wide version: try to complete the multibyte sequence */
223 while (length < sizeof(buffer)) {
224 if (!IsIncompleteCharMB(buffer, length))
225 /* sequence is complete */
228 /* poll for more bytes on stdin, without timeout */
230 if (poll(&pfd, 1, 0) <= 0)
231 /* no more input from keyboard */
234 buffer[length++] = wgetch(wr->w);
237 wr->value.insert(wr->cursor, buffer, length);
240 wr->value.insert(wr->cursor, key);
243 wr->cursor += length;
244 if (cursor_column(wr) >= wr->width)
245 wr->start = right_align_bytes(wr->value.c_str(),
246 wr->cursor, wr->width);
250 wreadln_delete_char(struct wreadln *wr, size_t x)
252 assert(x < wr->value.length());
254 size_t length = CharSizeMB(wr->value.data() + x,
255 wr->value.length() - x);
256 wr->value.erase(x, length);
259 /* libcurses version */
264 const char *initial_value,
267 Completion *completion,
270 struct wreadln wr(w, masked);
271 History::iterator hlist, hcurrent;
279 /* make sure the cursor is visible */
281 /* print prompt string */
286 /* retrieve y and x0 position */
287 getyx(w, wr.point.y, wr.point.x);
288 /* check the x1 value */
289 if (x1 <= (unsigned)wr.point.x || x1 > (unsigned)COLS)
291 wr.width = x1 - wr.point.x;
292 /* clear input area */
293 mvwhline(w, wr.point.y, wr.point.x, ' ', wr.width);
296 /* append the a new line to our history list */
297 history->emplace_back();
298 /* hlist points to the current item in the history list */
299 hcurrent = hlist = std::prev(history->end());
302 if (initial_value == (char *)-1) {
303 /* get previous history entry */
304 if (history && hlist != history->begin()) {
305 /* get previous line */
309 cursor_move_to_eol(&wr);
311 } else if (initial_value) {
312 /* copy the initial value to the line buffer */
313 wr.value = initial_value;
314 cursor_move_to_eol(&wr);
319 while (key != 13 && key != '\n') {
322 /* check if key is a function key */
323 for (size_t i = 0; i < 63; i++)
324 if (key == (int)KEY_F(i)) {
331 case KEY_MOUSE: /* ignore mouse events */
333 case ERR: /* ignore errors */
338 if (completion != nullptr) {
339 completion->Pre(wr.value.c_str());
340 auto r = completion->Complete(wr.value.c_str());
341 if (!r.new_prefix.empty()) {
342 wr.value = std::move(r.new_prefix);
343 cursor_move_to_eol(&wr);
347 completion->Post(wr.value.c_str(), r.range);
361 cursor_move_left(&wr);
365 cursor_move_right(&wr);
374 cursor_move_to_eol(&wr);
377 wr.value.erase(wr.cursor);
380 wr.value.erase(0, wr.cursor);
384 /* Firstly remove trailing spaces. */
385 for (; wr.cursor > 0 && wr.value[wr.cursor - 1] == ' ';)
387 cursor_move_left(&wr);
388 wreadln_delete_char(&wr, wr.cursor);
390 /* Then remove word until next space. */
391 for (; wr.cursor > 0 && wr.value[wr.cursor - 1] != ' ';)
393 cursor_move_left(&wr);
394 wreadln_delete_char(&wr, wr.cursor);
398 case KEY_BCKSPC: /* handle backspace: copy all */
399 case KEY_BACKSPACE: /* chars starting from curpos */
400 if (wr.cursor > 0) { /* - 1 from buf[n+1] to buf */
401 cursor_move_left(&wr);
402 wreadln_delete_char(&wr, wr.cursor);
405 case KEY_DC: /* handle delete key. As above */
407 if (wr.cursor < wr.value.length())
408 wreadln_delete_char(&wr, wr.cursor);
412 /* get previous history entry */
413 if (history && hlist != history->begin()) {
414 if (hlist == hcurrent)
415 /* save the current line */
418 /* get previous line */
422 cursor_move_to_eol(&wr);
426 /* get next history entry */
427 if (history && std::next(hlist) != history->end()) {
432 cursor_move_to_eol(&wr);
445 wreadln_insert_byte(&wr, key);
453 if (!wr.value.empty()) {
454 /* update the current history entry */
455 *hcurrent = wr.value;
457 /* the line was empty - remove the current history entry */
458 history->erase(hcurrent);
461 auto history_length = history->size();
462 while (history_length > wrln_max_history_length) {
463 history->pop_front();
468 return std::move(wr.value);
474 const char *initial_value,
477 Completion *completion)
479 return _wreadln(w, prompt, initial_value, x1,
480 history, completion, false);
484 wreadln_masked(WINDOW *w,
486 const char *initial_value,
489 return _wreadln(w, prompt, initial_value, x1, nullptr, nullptr, true);