helpline.c revision e6817ec1d8ab31fc7b01906e305f848542df6413
1#define _GNU_SOURCE
2#include <stdio.h>
3#include <stdlib.h>
4#include <newt.h>
5
6#include "../debug.h"
7#include "helpline.h"
8#include "ui.h"
9
10void ui_helpline__pop(void)
11{
12	newtPopHelpLine();
13}
14
15void ui_helpline__push(const char *msg)
16{
17	newtPushHelpLine(msg);
18}
19
20void ui_helpline__vpush(const char *fmt, va_list ap)
21{
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}
46
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
59	pthread_mutex_lock(&ui__lock);
60	ret = vsnprintf(ui_helpline__last_msg + backlog,
61			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	}
69	pthread_mutex_unlock(&ui__lock);
70
71	return ret;
72}
73