-/*
- * function statusline_getnstr
- *
- * parameters:
- * (char *str)
- * if n >= 0 str is a pointer which points a place where to store
- * the string, else str is ingnored
- * (int n)
- * the maximum length of the string
- * If n < 0 function will allocate needed space for the string.
- * Value 0 is not allowed for n.
- * (int use_filesel)
- * if this value is nonzero the fileselector is enabled
- *
- * returns (char *)
- * If n < 0 a pointer to a newly allocated string is returned.
- * If n > 0 a nonzero value is returned if user has typed a valid
- * string. If not NULL value is returned. Never really use the
- * _pointer_ if n > 0.
- *
- */
+/* Same as statusline_addstr(), but hilight "<str>" sequences if the terminal
+ * supports it */
+static void
+statusline_addhlstr(const char *str)
+{
+#if defined(A_BOLD) && defined(A_NORMAL) && defined(A_DIM)
+ const char *p = str, *start = str;
+ char *tmp;
+ int pos = 0;
+
+ while(1) {
+ if(!*p || strchr("<>", *p)) {
+ if(p - start > 0) {
+ wattrset(bottom, (*p == '>') ? A_BOLD : A_NORMAL);
+ tmp = xstrndup(start, p - start);
+ mvwaddstr(bottom, 1, pos, tmp);
+ pos += strwidth(tmp);
+ free(tmp);
+ }
+ if(*p) {
+ start = p + 1;
+
+ /* show tag markers */
+ wattrset(bottom, A_DIM);
+ mvwaddch(bottom, 1, pos++, *p);
+ }
+ }
+
+ if(!*p) {
+ wattrset(bottom, A_NORMAL);
+ break;
+ }
+
+ p++;
+ }
+#else
+ mvwaddstr(bottom, 1, 0, str);
+#endif
+
+ refresh();
+ wrefresh(bottom);
+}
+
+int
+statusline_askchoice(const char *msg, const char *choices, short dflt)
+{
+ char *s;
+ int ch;
+
+ assert((dflt >= 0) && (dflt <= strlen(choices)));
+
+ if(dflt) {
+ s = strdup_printf("%s [%c]", msg, choices[dflt - 1]);
+ statusline_addhlstr(s);
+ free(s);
+ } else
+ statusline_addhlstr(msg);
+
+ while(1)
+ {
+ ch = tolower(getch());
+
+ if(ch == 7) /* ctrl+G */
+ return 0;
+
+ if(dflt && (ch == '\r')) /* default choice */
+ return dflt;
+
+ if((s = strchr(choices, ch)))
+ return (s - choices + 1);
+ }
+}