helpline.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "../debug.h"
  6. #include "helpline.h"
  7. #include "ui.h"
  8. #include "libslang.h"
  9. void ui_helpline__pop(void)
  10. {
  11. }
  12. char ui_helpline__current[512];
  13. void ui_helpline__push(const char *msg)
  14. {
  15. const size_t sz = sizeof(ui_helpline__current);
  16. SLsmg_gotorc(SLtt_Screen_Rows - 1, 0);
  17. SLsmg_set_color(0);
  18. SLsmg_write_nstring((char *)msg, SLtt_Screen_Cols);
  19. SLsmg_refresh();
  20. strncpy(ui_helpline__current, msg, sz)[sz - 1] = '\0';
  21. }
  22. void ui_helpline__vpush(const char *fmt, va_list ap)
  23. {
  24. char *s;
  25. if (vasprintf(&s, fmt, ap) < 0)
  26. vfprintf(stderr, fmt, ap);
  27. else {
  28. ui_helpline__push(s);
  29. free(s);
  30. }
  31. }
  32. void ui_helpline__fpush(const char *fmt, ...)
  33. {
  34. va_list ap;
  35. va_start(ap, fmt);
  36. ui_helpline__vpush(fmt, ap);
  37. va_end(ap);
  38. }
  39. void ui_helpline__puts(const char *msg)
  40. {
  41. ui_helpline__pop();
  42. ui_helpline__push(msg);
  43. }
  44. void ui_helpline__init(void)
  45. {
  46. ui_helpline__puts(" ");
  47. }
  48. char ui_helpline__last_msg[1024];
  49. int ui_helpline__show_help(const char *format, va_list ap)
  50. {
  51. int ret;
  52. static int backlog;
  53. pthread_mutex_lock(&ui__lock);
  54. ret = vsnprintf(ui_helpline__last_msg + backlog,
  55. sizeof(ui_helpline__last_msg) - backlog, format, ap);
  56. backlog += ret;
  57. if (ui_helpline__last_msg[backlog - 1] == '\n') {
  58. ui_helpline__puts(ui_helpline__last_msg);
  59. SLsmg_refresh();
  60. backlog = 0;
  61. }
  62. pthread_mutex_unlock(&ui__lock);
  63. return ret;
  64. }