browser.c 7.1 KB

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