Arnaldo Carvalho de Melo | 5575536 | 2010-08-08 19:48:31 -0300 | [diff] [blame] | 1 | #define _GNU_SOURCE |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <newt.h> |
| 5 | |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 6 | #include "../debug.h" |
Arnaldo Carvalho de Melo | 5575536 | 2010-08-08 19:48:31 -0300 | [diff] [blame] | 7 | #include "helpline.h" |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 8 | #include "ui.h" |
Arnaldo Carvalho de Melo | 5575536 | 2010-08-08 19:48:31 -0300 | [diff] [blame] | 9 | |
| 10 | void ui_helpline__pop(void) |
| 11 | { |
| 12 | newtPopHelpLine(); |
| 13 | } |
| 14 | |
| 15 | void ui_helpline__push(const char *msg) |
| 16 | { |
| 17 | newtPushHelpLine(msg); |
| 18 | } |
| 19 | |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 20 | void ui_helpline__vpush(const char *fmt, va_list ap) |
Arnaldo Carvalho de Melo | 5575536 | 2010-08-08 19:48:31 -0300 | [diff] [blame] | 21 | { |
| 22 | char *s; |
| 23 | |
| 24 | if (vasprintf(&s, fmt, ap) < 0) |
| 25 | vfprintf(stderr, fmt, ap); |
| 26 | else { |
| 27 | ui_helpline__push(s); |
| 28 | free(s); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void ui_helpline__fpush(const char *fmt, ...) |
| 33 | { |
| 34 | va_list ap; |
| 35 | |
| 36 | va_start(ap, fmt); |
| 37 | ui_helpline__vpush(fmt, ap); |
| 38 | va_end(ap); |
| 39 | } |
| 40 | |
| 41 | void ui_helpline__puts(const char *msg) |
| 42 | { |
| 43 | ui_helpline__pop(); |
| 44 | ui_helpline__push(msg); |
| 45 | } |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 46 | |
| 47 | void ui_helpline__init(void) |
| 48 | { |
| 49 | ui_helpline__puts(" "); |
| 50 | } |
| 51 | |
| 52 | char ui_helpline__last_msg[1024]; |
| 53 | |
| 54 | int ui_helpline__show_help(const char *format, va_list ap) |
| 55 | { |
| 56 | int ret; |
| 57 | static int backlog; |
| 58 | |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 59 | pthread_mutex_lock(&ui__lock); |
| 60 | ret = vsnprintf(ui_helpline__last_msg + backlog, |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 61 | sizeof(ui_helpline__last_msg) - backlog, format, ap); |
| 62 | backlog += ret; |
| 63 | |
| 64 | if (ui_helpline__last_msg[backlog - 1] == '\n') { |
| 65 | ui_helpline__puts(ui_helpline__last_msg); |
| 66 | newtRefresh(); |
| 67 | backlog = 0; |
| 68 | } |
Arnaldo Carvalho de Melo | 5c35d69 | 2011-02-09 11:38:43 -0200 | [diff] [blame] | 69 | pthread_mutex_unlock(&ui__lock); |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 70 | |
| 71 | return ret; |
| 72 | } |