browser.c 6.9 KB

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