blob: f36d2ff509ed285baa5290d6780b3e9d8ca4ae9e [file] [log] [blame]
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -03001#define _GNU_SOURCE
2#include <stdio.h>
3#include <stdlib.h>
4#include <newt.h>
5
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -03006#include "../debug.h"
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -03007#include "helpline.h"
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -02008#include "ui.h"
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -03009
10void ui_helpline__pop(void)
11{
12 newtPopHelpLine();
13}
14
15void ui_helpline__push(const char *msg)
16{
17 newtPushHelpLine(msg);
18}
19
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -030020void ui_helpline__vpush(const char *fmt, va_list ap)
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -030021{
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
32void 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
41void ui_helpline__puts(const char *msg)
42{
43 ui_helpline__pop();
44 ui_helpline__push(msg);
45}
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030046
47void ui_helpline__init(void)
48{
49 ui_helpline__puts(" ");
50}
51
52char ui_helpline__last_msg[1024];
53
54int ui_helpline__show_help(const char *format, va_list ap)
55{
56 int ret;
57 static int backlog;
58
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -020059 pthread_mutex_lock(&ui__lock);
60 ret = vsnprintf(ui_helpline__last_msg + backlog,
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030061 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 Melo5c35d692011-02-09 11:38:43 -020069 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030070
71 return ret;
72}