blob: 6ef3c56917626a38d835491d82461219b7dddf88 [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 Melo4610e412011-10-26 12:04:37 -02004#include <string.h>
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -03005
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 Melo2ba908e2011-10-25 13:52:05 -02009#include "libslang.h"
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -030010
11void ui_helpline__pop(void)
12{
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -030013}
14
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -020015char ui_helpline__current[512];
16
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -030017void ui_helpline__push(const char *msg)
18{
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -020019 const size_t sz = sizeof(ui_helpline__current);
20
Arnaldo Carvalho de Melo2ba908e2011-10-25 13:52:05 -020021 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 Melo4610e412011-10-26 12:04:37 -020025 strncpy(ui_helpline__current, msg, sz)[sz - 1] = '\0';
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -030026}
27
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -030028void ui_helpline__vpush(const char *fmt, va_list ap)
Arnaldo Carvalho de Melo55755362010-08-08 19:48:31 -030029{
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
40void 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
49void ui_helpline__puts(const char *msg)
50{
51 ui_helpline__pop();
52 ui_helpline__push(msg);
53}
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030054
55void ui_helpline__init(void)
56{
57 ui_helpline__puts(" ");
58}
59
60char ui_helpline__last_msg[1024];
61
62int ui_helpline__show_help(const char *format, va_list ap)
63{
64 int ret;
65 static int backlog;
66
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -020067 pthread_mutex_lock(&ui__lock);
68 ret = vsnprintf(ui_helpline__last_msg + backlog,
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030069 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 Melo2ba908e2011-10-25 13:52:05 -020074 SLsmg_refresh();
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030075 backlog = 0;
76 }
Arnaldo Carvalho de Melo5c35d692011-02-09 11:38:43 -020077 pthread_mutex_unlock(&ui__lock);
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -030078
79 return ret;
80}