browser.c 7.2 KB

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