browser.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. newtFormAddHotKey(self->form, ' ');
  150. newtFormAddComponent(self->form, self->sb);
  151. return 0;
  152. }
  153. int ui_browser__refresh(struct ui_browser *self)
  154. {
  155. int row;
  156. newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
  157. row = self->refresh(self);
  158. SLsmg_set_color(HE_COLORSET_NORMAL);
  159. SLsmg_fill_region(self->y + row, self->x,
  160. self->height - row, self->width, ' ');
  161. return 0;
  162. }
  163. int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
  164. {
  165. if (ui_browser__refresh(self) < 0)
  166. return -1;
  167. while (1) {
  168. off_t offset;
  169. newtFormRun(self->form, es);
  170. if (es->reason != NEWT_EXIT_HOTKEY)
  171. break;
  172. if (is_exit_key(es->u.key))
  173. return es->u.key;
  174. switch (es->u.key) {
  175. case NEWT_KEY_DOWN:
  176. if (self->index == self->nr_entries - 1)
  177. break;
  178. ++self->index;
  179. if (self->index == self->top_idx + self->height) {
  180. ++self->top_idx;
  181. self->seek(self, +1, SEEK_CUR);
  182. }
  183. break;
  184. case NEWT_KEY_UP:
  185. if (self->index == 0)
  186. break;
  187. --self->index;
  188. if (self->index < self->top_idx) {
  189. --self->top_idx;
  190. self->seek(self, -1, SEEK_CUR);
  191. }
  192. break;
  193. case NEWT_KEY_PGDN:
  194. case ' ':
  195. if (self->top_idx + self->height > self->nr_entries - 1)
  196. break;
  197. offset = self->height;
  198. if (self->index + offset > self->nr_entries - 1)
  199. offset = self->nr_entries - 1 - self->index;
  200. self->index += offset;
  201. self->top_idx += offset;
  202. self->seek(self, +offset, SEEK_CUR);
  203. break;
  204. case NEWT_KEY_PGUP:
  205. if (self->top_idx == 0)
  206. break;
  207. if (self->top_idx < self->height)
  208. offset = self->top_idx;
  209. else
  210. offset = self->height;
  211. self->index -= offset;
  212. self->top_idx -= offset;
  213. self->seek(self, -offset, SEEK_CUR);
  214. break;
  215. case NEWT_KEY_HOME:
  216. ui_browser__reset_index(self);
  217. break;
  218. case NEWT_KEY_END:
  219. offset = self->height - 1;
  220. if (offset >= self->nr_entries)
  221. offset = self->nr_entries - 1;
  222. self->index = self->nr_entries - 1;
  223. self->top_idx = self->index - offset;
  224. self->seek(self, -offset, SEEK_END);
  225. break;
  226. default:
  227. return es->u.key;
  228. }
  229. if (ui_browser__refresh(self) < 0)
  230. return -1;
  231. }
  232. return 0;
  233. }
  234. unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
  235. {
  236. struct list_head *pos;
  237. struct list_head *head = self->entries;
  238. int row = 0;
  239. if (self->top == NULL || self->top == self->entries)
  240. self->top = head->next;
  241. pos = self->top;
  242. list_for_each_from(pos, head) {
  243. SLsmg_gotorc(self->y + row, self->x);
  244. self->write(self, pos, row);
  245. if (++row == self->height)
  246. break;
  247. }
  248. return row;
  249. }
  250. static struct newtPercentTreeColors {
  251. const char *topColorFg, *topColorBg;
  252. const char *mediumColorFg, *mediumColorBg;
  253. const char *normalColorFg, *normalColorBg;
  254. const char *selColorFg, *selColorBg;
  255. const char *codeColorFg, *codeColorBg;
  256. } defaultPercentTreeColors = {
  257. "red", "lightgray",
  258. "green", "lightgray",
  259. "black", "lightgray",
  260. "lightgray", "magenta",
  261. "blue", "lightgray",
  262. };
  263. void ui_browser__init(void)
  264. {
  265. struct newtPercentTreeColors *c = &defaultPercentTreeColors;
  266. sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
  267. sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
  268. sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
  269. sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
  270. sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
  271. }