browser.c 7.3 KB

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