helpline.c 1.2 KB

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