browser.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #undef _GNU_SOURCE
  4. /*
  5. * slang versions <= 2.0.6 have a "#if HAVE_LONG_LONG" that breaks
  6. * the build if it isn't defined. Use the equivalent one that glibc
  7. * has on features.h.
  8. */
  9. #include <features.h>
  10. #ifndef HAVE_LONG_LONG
  11. #define HAVE_LONG_LONG __GLIBC_HAVE_LONG_LONG
  12. #endif
  13. #include <slang.h>
  14. #include <linux/list.h>
  15. #include <linux/rbtree.h>
  16. #include <stdlib.h>
  17. #include <sys/ttydefaults.h>
  18. #include "browser.h"
  19. #include "helpline.h"
  20. #include "../color.h"
  21. #include "../util.h"
  22. #if SLANG_VERSION < 20104
  23. #define sltt_set_color(obj, name, fg, bg) \
  24. SLtt_set_color(obj,(char *)name, (char *)fg, (char *)bg)
  25. #else
  26. #define sltt_set_color SLtt_set_color
  27. #endif
  28. newtComponent newt_form__new(void);
  29. int ui_browser__percent_color(double percent, bool current)
  30. {
  31. if (current)
  32. return HE_COLORSET_SELECTED;
  33. if (percent >= MIN_RED)
  34. return HE_COLORSET_TOP;
  35. if (percent >= MIN_GREEN)
  36. return HE_COLORSET_MEDIUM;
  37. return HE_COLORSET_NORMAL;
  38. }
  39. void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whence)
  40. {
  41. struct list_head *head = self->entries;
  42. struct list_head *pos;
  43. switch (whence) {
  44. case SEEK_SET:
  45. pos = head->next;
  46. break;
  47. case SEEK_CUR:
  48. pos = self->top;
  49. break;
  50. case SEEK_END:
  51. pos = head->prev;
  52. break;
  53. default:
  54. return;
  55. }
  56. if (offset > 0) {
  57. while (offset-- != 0)
  58. pos = pos->next;
  59. } else {
  60. while (offset++ != 0)
  61. pos = pos->prev;
  62. }
  63. self->top = pos;
  64. }
  65. void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence)
  66. {
  67. struct rb_root *root = self->entries;
  68. struct rb_node *nd;
  69. switch (whence) {
  70. case SEEK_SET:
  71. nd = rb_first(root);
  72. break;
  73. case SEEK_CUR:
  74. nd = self->top;
  75. break;
  76. case SEEK_END:
  77. nd = rb_last(root);
  78. break;
  79. default:
  80. return;
  81. }
  82. if (offset > 0) {
  83. while (offset-- != 0)
  84. nd = rb_next(nd);
  85. } else {
  86. while (offset++ != 0)
  87. nd = rb_prev(nd);
  88. }
  89. self->top = nd;
  90. }
  91. unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
  92. {
  93. struct rb_node *nd;
  94. int row = 0;
  95. if (self->top == NULL)
  96. self->top = rb_first(self->entries);
  97. nd = self->top;
  98. while (nd != NULL) {
  99. SLsmg_gotorc(self->y + row, self->x);
  100. self->write(self, nd, row);
  101. if (++row == self->height)
  102. break;
  103. nd = rb_next(nd);
  104. }
  105. return row;
  106. }
  107. bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
  108. {
  109. return self->top_idx + row == self->index;
  110. }
  111. void ui_browser__refresh_dimensions(struct ui_browser *self)
  112. {
  113. int cols, rows;
  114. newtGetScreenSize(&cols, &rows);
  115. if (self->width > cols - 4)
  116. self->width = cols - 4;
  117. self->height = rows - 5;
  118. if (self->height > self->nr_entries)
  119. self->height = self->nr_entries;
  120. self->y = (rows - self->height) / 2;
  121. self->x = (cols - self->width) / 2;
  122. }
  123. void ui_browser__reset_index(struct ui_browser *self)
  124. {
  125. self->index = self->top_idx = 0;
  126. self->seek(self, 0, SEEK_SET);
  127. }
  128. int ui_browser__show(struct ui_browser *self, const char *title,
  129. const char *helpline, ...)
  130. {
  131. va_list ap;
  132. if (self->form != NULL) {
  133. newtFormDestroy(self->form);
  134. newtPopWindow();
  135. }
  136. ui_browser__refresh_dimensions(self);
  137. newtCenteredWindow(self->width, self->height, title);
  138. self->form = newt_form__new();
  139. if (self->form == NULL)
  140. return -1;
  141. self->sb = newtVerticalScrollbar(self->width, 0, self->height,
  142. HE_COLORSET_NORMAL,
  143. HE_COLORSET_SELECTED);
  144. if (self->sb == NULL)
  145. return -1;
  146. newtFormAddHotKey(self->form, NEWT_KEY_UP);
  147. newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
  148. newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
  149. newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
  150. newtFormAddHotKey(self->form, NEWT_KEY_HOME);
  151. newtFormAddHotKey(self->form, NEWT_KEY_END);
  152. newtFormAddHotKey(self->form, ' ');
  153. newtFormAddComponent(self->form, self->sb);
  154. va_start(ap, helpline);
  155. ui_helpline__vpush(helpline, ap);
  156. va_end(ap);
  157. return 0;
  158. }
  159. void ui_browser__hide(struct ui_browser *self)
  160. {
  161. newtFormDestroy(self->form);
  162. newtPopWindow();
  163. self->form = NULL;
  164. ui_helpline__pop();
  165. }
  166. int ui_browser__refresh(struct ui_browser *self)
  167. {
  168. int row;
  169. newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
  170. row = self->refresh(self);
  171. SLsmg_set_color(HE_COLORSET_NORMAL);
  172. SLsmg_fill_region(self->y + row, self->x,
  173. self->height - row, self->width, ' ');
  174. return 0;
  175. }
  176. int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
  177. {
  178. if (ui_browser__refresh(self) < 0)
  179. return -1;
  180. while (1) {
  181. off_t offset;
  182. newtFormRun(self->form, es);
  183. if (es->reason != NEWT_EXIT_HOTKEY)
  184. break;
  185. if (is_exit_key(es->u.key))
  186. return es->u.key;
  187. switch (es->u.key) {
  188. case NEWT_KEY_DOWN:
  189. if (self->index == self->nr_entries - 1)
  190. break;
  191. ++self->index;
  192. if (self->index == self->top_idx + self->height) {
  193. ++self->top_idx;
  194. self->seek(self, +1, SEEK_CUR);
  195. }
  196. break;
  197. case NEWT_KEY_UP:
  198. if (self->index == 0)
  199. break;
  200. --self->index;
  201. if (self->index < self->top_idx) {
  202. --self->top_idx;
  203. self->seek(self, -1, SEEK_CUR);
  204. }
  205. break;
  206. case NEWT_KEY_PGDN:
  207. case ' ':
  208. if (self->top_idx + self->height > self->nr_entries - 1)
  209. break;
  210. offset = self->height;
  211. if (self->index + offset > self->nr_entries - 1)
  212. offset = self->nr_entries - 1 - self->index;
  213. self->index += offset;
  214. self->top_idx += offset;
  215. self->seek(self, +offset, SEEK_CUR);
  216. break;
  217. case NEWT_KEY_PGUP:
  218. if (self->top_idx == 0)
  219. break;
  220. if (self->top_idx < self->height)
  221. offset = self->top_idx;
  222. else
  223. offset = self->height;
  224. self->index -= offset;
  225. self->top_idx -= offset;
  226. self->seek(self, -offset, SEEK_CUR);
  227. break;
  228. case NEWT_KEY_HOME:
  229. ui_browser__reset_index(self);
  230. break;
  231. case NEWT_KEY_END:
  232. offset = self->height - 1;
  233. if (offset >= self->nr_entries)
  234. offset = self->nr_entries - 1;
  235. self->index = self->nr_entries - 1;
  236. self->top_idx = self->index - offset;
  237. self->seek(self, -offset, SEEK_END);
  238. break;
  239. default:
  240. return es->u.key;
  241. }
  242. if (ui_browser__refresh(self) < 0)
  243. return -1;
  244. }
  245. return 0;
  246. }
  247. unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
  248. {
  249. struct list_head *pos;
  250. struct list_head *head = self->entries;
  251. int row = 0;
  252. if (self->top == NULL || self->top == self->entries)
  253. self->top = head->next;
  254. pos = self->top;
  255. list_for_each_from(pos, head) {
  256. SLsmg_gotorc(self->y + row, self->x);
  257. self->write(self, pos, row);
  258. if (++row == self->height)
  259. break;
  260. }
  261. return row;
  262. }
  263. static struct newtPercentTreeColors {
  264. const char *topColorFg, *topColorBg;
  265. const char *mediumColorFg, *mediumColorBg;
  266. const char *normalColorFg, *normalColorBg;
  267. const char *selColorFg, *selColorBg;
  268. const char *codeColorFg, *codeColorBg;
  269. } defaultPercentTreeColors = {
  270. "red", "lightgray",
  271. "green", "lightgray",
  272. "black", "lightgray",
  273. "lightgray", "magenta",
  274. "blue", "lightgray",
  275. };
  276. void ui_browser__init(void)
  277. {
  278. struct newtPercentTreeColors *c = &defaultPercentTreeColors;
  279. sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
  280. sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
  281. sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
  282. sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
  283. sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
  284. }