util.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "../util.h"
  2. #include <signal.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include <sys/ttydefaults.h>
  6. #include "../cache.h"
  7. #include "../debug.h"
  8. #include "browser.h"
  9. #include "keysyms.h"
  10. #include "helpline.h"
  11. #include "ui.h"
  12. #include "util.h"
  13. #include "libslang.h"
  14. static void ui_browser__argv_write(struct ui_browser *browser,
  15. void *entry, int row)
  16. {
  17. char **arg = entry;
  18. bool current_entry = ui_browser__is_current_entry(browser, row);
  19. ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
  20. HE_COLORSET_NORMAL);
  21. slsmg_write_nstring(*arg, browser->width);
  22. }
  23. static int popup_menu__run(struct ui_browser *menu)
  24. {
  25. int key;
  26. if (ui_browser__show(menu, " ", "ESC: exit, ENTER|->: Select option") < 0)
  27. return -1;
  28. while (1) {
  29. key = ui_browser__run(menu, 0);
  30. switch (key) {
  31. case K_RIGHT:
  32. case K_ENTER:
  33. key = menu->index;
  34. break;
  35. case K_LEFT:
  36. case K_ESC:
  37. case 'q':
  38. case CTRL('c'):
  39. key = -1;
  40. break;
  41. default:
  42. continue;
  43. }
  44. break;
  45. }
  46. ui_browser__hide(menu);
  47. return key;
  48. }
  49. int ui__popup_menu(int argc, char * const argv[])
  50. {
  51. struct ui_browser menu = {
  52. .entries = (void *)argv,
  53. .refresh = ui_browser__argv_refresh,
  54. .seek = ui_browser__argv_seek,
  55. .write = ui_browser__argv_write,
  56. .nr_entries = argc,
  57. };
  58. return popup_menu__run(&menu);
  59. }
  60. int ui__question_window(const char *title, const char *text,
  61. const char *exit_msg, int delay_secs)
  62. {
  63. int x, y;
  64. int max_len = 0, nr_lines = 0;
  65. const char *t;
  66. t = text;
  67. while (1) {
  68. const char *sep = strchr(t, '\n');
  69. int len;
  70. if (sep == NULL)
  71. sep = strchr(t, '\0');
  72. len = sep - t;
  73. if (max_len < len)
  74. max_len = len;
  75. ++nr_lines;
  76. if (*sep == '\0')
  77. break;
  78. t = sep + 1;
  79. }
  80. max_len += 2;
  81. nr_lines += 4;
  82. y = SLtt_Screen_Rows / 2 - nr_lines / 2,
  83. x = SLtt_Screen_Cols / 2 - max_len / 2;
  84. SLsmg_set_color(0);
  85. SLsmg_draw_box(y, x++, nr_lines, max_len);
  86. if (title) {
  87. SLsmg_gotorc(y, x + 1);
  88. SLsmg_write_string((char *)title);
  89. }
  90. SLsmg_gotorc(++y, x);
  91. nr_lines -= 2;
  92. max_len -= 2;
  93. SLsmg_write_wrapped_string((unsigned char *)text, y, x,
  94. nr_lines, max_len, 1);
  95. SLsmg_gotorc(y + nr_lines - 2, x);
  96. SLsmg_write_nstring((char *)" ", max_len);
  97. SLsmg_gotorc(y + nr_lines - 1, x);
  98. SLsmg_write_nstring((char *)exit_msg, max_len);
  99. SLsmg_refresh();
  100. return ui__getch(delay_secs);
  101. }
  102. int ui__help_window(const char *text)
  103. {
  104. return ui__question_window("Help", text, "Press any key...", 0);
  105. }
  106. int ui__dialog_yesno(const char *msg)
  107. {
  108. return ui__question_window(NULL, msg, "Enter: Yes, ESC: No", 0);
  109. }
  110. int __ui__warning(const char *title, const char *format, va_list args)
  111. {
  112. char *s;
  113. if (use_browser > 0 && vasprintf(&s, format, args) > 0) {
  114. int key;
  115. pthread_mutex_lock(&ui__lock);
  116. key = ui__question_window(title, s, "Press any key...", 0);
  117. pthread_mutex_unlock(&ui__lock);
  118. free(s);
  119. return key;
  120. }
  121. fprintf(stderr, "%s:\n", title);
  122. vfprintf(stderr, format, args);
  123. return K_ESC;
  124. }
  125. int ui__warning(const char *format, ...)
  126. {
  127. int key;
  128. va_list args;
  129. va_start(args, format);
  130. key = __ui__warning("Warning", format, args);
  131. va_end(args);
  132. return key;
  133. }
  134. int ui__error(const char *format, ...)
  135. {
  136. int key;
  137. va_list args;
  138. va_start(args, format);
  139. key = __ui__warning("Error", format, args);
  140. va_end(args);
  141. return key;
  142. }