helpline.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <newt.h>
  5. #include "../debug.h"
  6. #include "helpline.h"
  7. void ui_helpline__pop(void)
  8. {
  9. newtPopHelpLine();
  10. }
  11. void ui_helpline__push(const char *msg)
  12. {
  13. newtPushHelpLine(msg);
  14. }
  15. void ui_helpline__vpush(const char *fmt, va_list ap)
  16. {
  17. char *s;
  18. if (vasprintf(&s, fmt, ap) < 0)
  19. vfprintf(stderr, fmt, ap);
  20. else {
  21. ui_helpline__push(s);
  22. free(s);
  23. }
  24. }
  25. void ui_helpline__fpush(const char *fmt, ...)
  26. {
  27. va_list ap;
  28. va_start(ap, fmt);
  29. ui_helpline__vpush(fmt, ap);
  30. va_end(ap);
  31. }
  32. void ui_helpline__puts(const char *msg)
  33. {
  34. ui_helpline__pop();
  35. ui_helpline__push(msg);
  36. }
  37. void ui_helpline__init(void)
  38. {
  39. ui_helpline__puts(" ");
  40. }
  41. char ui_helpline__last_msg[1024];
  42. int ui_helpline__show_help(const char *format, va_list ap)
  43. {
  44. int ret;
  45. static int backlog;
  46. ret = vsnprintf(ui_helpline__last_msg + backlog,
  47. sizeof(ui_helpline__last_msg) - backlog, format, ap);
  48. backlog += ret;
  49. if (ui_helpline__last_msg[backlog - 1] == '\n') {
  50. ui_helpline__puts(ui_helpline__last_msg);
  51. newtRefresh();
  52. backlog = 0;
  53. }
  54. return ret;
  55. }