header.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "util/cache.h"
  2. #include "util/debug.h"
  3. #include "ui/browser.h"
  4. #include "ui/keysyms.h"
  5. #include "ui/ui.h"
  6. #include "ui/util.h"
  7. #include "ui/libslang.h"
  8. #include "util/header.h"
  9. #include "util/session.h"
  10. static void ui_browser__argv_write(struct ui_browser *browser,
  11. void *entry, int row)
  12. {
  13. char **arg = entry;
  14. char *str = *arg;
  15. char empty[] = " ";
  16. bool current_entry = ui_browser__is_current_entry(browser, row);
  17. unsigned long offset = (unsigned long)browser->priv;
  18. if (offset >= strlen(str))
  19. str = empty;
  20. else
  21. str = str + offset;
  22. ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
  23. HE_COLORSET_NORMAL);
  24. ui_browser__write_nstring(browser, str, browser->width);
  25. }
  26. static int list_menu__run(struct ui_browser *menu)
  27. {
  28. int key;
  29. unsigned long offset;
  30. const char help[] =
  31. "h/?/F1 Show this window\n"
  32. "UP/DOWN/PGUP\n"
  33. "PGDN/SPACE\n"
  34. "LEFT/RIGHT Navigate\n"
  35. "q/ESC/CTRL+C Exit browser";
  36. if (ui_browser__show(menu, "Header information", "Press 'q' to exit") < 0)
  37. return -1;
  38. while (1) {
  39. key = ui_browser__run(menu, 0);
  40. switch (key) {
  41. case K_RIGHT:
  42. offset = (unsigned long)menu->priv;
  43. offset += 10;
  44. menu->priv = (void *)offset;
  45. continue;
  46. case K_LEFT:
  47. offset = (unsigned long)menu->priv;
  48. if (offset >= 10)
  49. offset -= 10;
  50. menu->priv = (void *)offset;
  51. continue;
  52. case K_F1:
  53. case 'h':
  54. case '?':
  55. ui_browser__help_window(menu, help);
  56. continue;
  57. case K_ESC:
  58. case 'q':
  59. case CTRL('c'):
  60. key = -1;
  61. break;
  62. default:
  63. continue;
  64. }
  65. break;
  66. }
  67. ui_browser__hide(menu);
  68. return key;
  69. }
  70. static int ui__list_menu(int argc, char * const argv[])
  71. {
  72. struct ui_browser menu = {
  73. .entries = (void *)argv,
  74. .refresh = ui_browser__argv_refresh,
  75. .seek = ui_browser__argv_seek,
  76. .write = ui_browser__argv_write,
  77. .nr_entries = argc,
  78. };
  79. return list_menu__run(&menu);
  80. }
  81. int tui__header_window(struct perf_env *env)
  82. {
  83. int i, argc = 0;
  84. char **argv;
  85. struct perf_session *session;
  86. char *ptr, *pos;
  87. size_t size;
  88. FILE *fp = open_memstream(&ptr, &size);
  89. session = container_of(env, struct perf_session, header.env);
  90. perf_header__fprintf_info(session, fp, true);
  91. fclose(fp);
  92. for (pos = ptr, argc = 0; (pos = strchr(pos, '\n')) != NULL; pos++)
  93. argc++;
  94. argv = calloc(argc + 1, sizeof(*argv));
  95. if (argv == NULL)
  96. goto out;
  97. argv[0] = pos = ptr;
  98. for (i = 1; (pos = strchr(pos, '\n')) != NULL; i++) {
  99. *pos++ = '\0';
  100. argv[i] = pos;
  101. }
  102. BUG_ON(i != argc + 1);
  103. ui__list_menu(argc, argv);
  104. out:
  105. free(argv);
  106. free(ptr);
  107. return 0;
  108. }