browser.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. self->width = cols - 1;
  114. self->height = rows - 2;
  115. self->y = 1;
  116. self->x = 0;
  117. }
  118. void ui_browser__reset_index(struct ui_browser *self)
  119. {
  120. self->index = self->top_idx = 0;
  121. self->seek(self, 0, SEEK_SET);
  122. }
  123. void ui_browser__add_exit_key(struct ui_browser *self, int key)
  124. {
  125. newtFormAddHotKey(self->form, key);
  126. }
  127. void ui_browser__add_exit_keys(struct ui_browser *self, int keys[])
  128. {
  129. int i = 0;
  130. while (keys[i] && i < 64) {
  131. ui_browser__add_exit_key(self, keys[i]);
  132. ++i;
  133. }
  134. }
  135. int ui_browser__show(struct ui_browser *self, const char *title,
  136. const char *helpline, ...)
  137. {
  138. va_list ap;
  139. int keys[] = { NEWT_KEY_UP, NEWT_KEY_DOWN, NEWT_KEY_PGUP,
  140. NEWT_KEY_PGDN, NEWT_KEY_HOME, NEWT_KEY_END, ' ',
  141. NEWT_KEY_LEFT, NEWT_KEY_ESCAPE, 'q', CTRL('c'), 0 };
  142. if (self->form != NULL)
  143. newtFormDestroy(self->form);
  144. ui_browser__refresh_dimensions(self);
  145. self->form = newtForm(NULL, NULL, 0);
  146. if (self->form == NULL)
  147. return -1;
  148. self->sb = newtVerticalScrollbar(self->width, 1, self->height,
  149. HE_COLORSET_NORMAL,
  150. HE_COLORSET_SELECTED);
  151. if (self->sb == NULL)
  152. return -1;
  153. SLsmg_gotorc(0, 0);
  154. ui_browser__set_color(self, NEWT_COLORSET_ROOT);
  155. slsmg_write_nstring(title, self->width);
  156. ui_browser__add_exit_keys(self, keys);
  157. newtFormAddComponent(self->form, self->sb);
  158. va_start(ap, helpline);
  159. ui_helpline__vpush(helpline, ap);
  160. va_end(ap);
  161. return 0;
  162. }
  163. void ui_browser__hide(struct ui_browser *self)
  164. {
  165. newtFormDestroy(self->form);
  166. self->form = NULL;
  167. ui_helpline__pop();
  168. }
  169. int ui_browser__refresh(struct ui_browser *self)
  170. {
  171. int row;
  172. newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
  173. row = self->refresh(self);
  174. ui_browser__set_color(self, HE_COLORSET_NORMAL);
  175. SLsmg_fill_region(self->y + row, self->x,
  176. self->height - row, self->width, ' ');
  177. return 0;
  178. }
  179. int ui_browser__run(struct ui_browser *self)
  180. {
  181. struct newtExitStruct es;
  182. if (ui_browser__refresh(self) < 0)
  183. return -1;
  184. while (1) {
  185. off_t offset;
  186. newtFormRun(self->form, &es);
  187. if (es.reason != NEWT_EXIT_HOTKEY)
  188. break;
  189. switch (es.u.key) {
  190. case NEWT_KEY_DOWN:
  191. if (self->index == self->nr_entries - 1)
  192. break;
  193. ++self->index;
  194. if (self->index == self->top_idx + self->height) {
  195. ++self->top_idx;
  196. self->seek(self, +1, SEEK_CUR);
  197. }
  198. break;
  199. case NEWT_KEY_UP:
  200. if (self->index == 0)
  201. break;
  202. --self->index;
  203. if (self->index < self->top_idx) {
  204. --self->top_idx;
  205. self->seek(self, -1, SEEK_CUR);
  206. }
  207. break;
  208. case NEWT_KEY_PGDN:
  209. case ' ':
  210. if (self->top_idx + self->height > self->nr_entries - 1)
  211. break;
  212. offset = self->height;
  213. if (self->index + offset > self->nr_entries - 1)
  214. offset = self->nr_entries - 1 - self->index;
  215. self->index += offset;
  216. self->top_idx += offset;
  217. self->seek(self, +offset, SEEK_CUR);
  218. break;
  219. case NEWT_KEY_PGUP:
  220. if (self->top_idx == 0)
  221. break;
  222. if (self->top_idx < self->height)
  223. offset = self->top_idx;
  224. else
  225. offset = self->height;
  226. self->index -= offset;
  227. self->top_idx -= offset;
  228. self->seek(self, -offset, SEEK_CUR);
  229. break;
  230. case NEWT_KEY_HOME:
  231. ui_browser__reset_index(self);
  232. break;
  233. case NEWT_KEY_END:
  234. offset = self->height - 1;
  235. if (offset >= self->nr_entries)
  236. offset = self->nr_entries - 1;
  237. self->index = self->nr_entries - 1;
  238. self->top_idx = self->index - offset;
  239. self->seek(self, -offset, SEEK_END);
  240. break;
  241. default:
  242. return es.u.key;
  243. }
  244. if (ui_browser__refresh(self) < 0)
  245. return -1;
  246. }
  247. return -1;
  248. }
  249. unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
  250. {
  251. struct list_head *pos;
  252. struct list_head *head = self->entries;
  253. int row = 0;
  254. if (self->top == NULL || self->top == self->entries)
  255. self->top = head->next;
  256. pos = self->top;
  257. list_for_each_from(pos, head) {
  258. ui_browser__gotorc(self, row, 0);
  259. self->write(self, pos, row);
  260. if (++row == self->height)
  261. break;
  262. }
  263. return row;
  264. }
  265. static struct newtPercentTreeColors {
  266. const char *topColorFg, *topColorBg;
  267. const char *mediumColorFg, *mediumColorBg;
  268. const char *normalColorFg, *normalColorBg;
  269. const char *selColorFg, *selColorBg;
  270. const char *codeColorFg, *codeColorBg;
  271. } defaultPercentTreeColors = {
  272. "red", "lightgray",
  273. "green", "lightgray",
  274. "black", "lightgray",
  275. "lightgray", "magenta",
  276. "blue", "lightgray",
  277. };
  278. void ui_browser__init(void)
  279. {
  280. struct newtPercentTreeColors *c = &defaultPercentTreeColors;
  281. sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
  282. sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
  283. sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
  284. sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
  285. sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
  286. }