helpline.c 1.4 KB

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