browser.c 7.2 KB

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