browser.c 7.6 KB

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