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