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