blob: 600243d766c027517e401ee2ac57a1505b66d544 [file] [log] [blame]
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -03001#define _GNU_SOURCE
2#include <stdio.h>
3#include <stdlib.h>
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -03004
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -03005#include "../debug.h"
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -03006#include "helpline.h"
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -02007#include "ui.h"
Arnaldo Carvalho de Melo2ba908e2011-10-25 13:52:05 -02008#include "libslang.h"
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -03009
10void ui_helpline__pop(void)
11{
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -030012}
13
14void ui_helpline__push(const char *msg)
15{
Arnaldo Carvalho de Melo2ba908e2011-10-25 13:52:05 -020016 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 Melo55755362010-08-08 19:48:31 -030020}
21
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -030022void ui_helpline__vpush(const char *fmt, va_list ap)
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -030023{
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
34void 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
43void ui_helpline__puts(const char *msg)
44{
45 ui_helpline__pop();
46 ui_helpline__push(msg);
47}
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030048
49void ui_helpline__init(void)
50{
51 ui_helpline__puts(" ");
52}
53
54char ui_helpline__last_msg[1024];
55
56int ui_helpline__show_help(const char *format, va_list ap)
57{
58 int ret;
59 static int backlog;
60
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -020061 pthread_mutex_lock(&ui__lock);
62 ret = vsnprintf(ui_helpline__last_msg + backlog,
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030063 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 Melo2ba908e2011-10-25 13:52:05 -020068 SLsmg_refresh();
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030069 backlog = 0;
70 }
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -020071 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030072
73 return ret;
74}