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