curseOutput.hh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * curseOutput.hh for jsonstroller
  3. *
  4. * Author: isundil <isundill@gmail.com>
  5. **/
  6. #pragma once
  7. #include <ncurses.h>
  8. #include <fstream>
  9. #include <list>
  10. #include <set>
  11. #include <map>
  12. #include "outputFlag.hh"
  13. #include "params.hh"
  14. class JSonElement;
  15. class JSonContainer;
  16. class JSonArray;
  17. class JSonObject;
  18. template<class T> class Optional;
  19. class SearchPattern;
  20. class CurseOutput
  21. {
  22. public:
  23. CurseOutput(JSonElement *rootData, const Params &);
  24. virtual ~CurseOutput();
  25. /**
  26. * Display data, and shutdown ncurses at the end
  27. **/
  28. void run();
  29. /**
  30. * Called on SIG* while displaying data
  31. * Do not use (private).
  32. **/
  33. bool onsig(int signo);
  34. private:
  35. /**
  36. * until kill-input, display data and read user inputs
  37. **/
  38. virtual void loop();
  39. protected:
  40. /**
  41. * Initialize ncurses
  42. **/
  43. void init();
  44. /**
  45. * Release ncurses
  46. **/
  47. void shutdown();
  48. /**
  49. * return false if bottom of screen is touched
  50. * redraw all data
  51. **/
  52. bool redraw();
  53. /**
  54. * Like redraw, but append a message on the last line
  55. **/
  56. bool redraw(const std::string &errorMsg);
  57. /**
  58. * redraw item and children
  59. **/
  60. bool redraw(std::pair<int, int> &, const std::pair<unsigned int, unsigned int> &maxWidth, const JSonElement *item);
  61. /**
  62. * Wait for input
  63. * @return false if ncurses should stop
  64. **/
  65. bool readInput();
  66. /**
  67. * get the screen size
  68. **/
  69. const std::pair<unsigned int, unsigned int> getScreenSize() const;
  70. /**
  71. * set the select_up and select_down pointers, scroll to selection if it is above view port
  72. **/
  73. void checkSelection(const JSonElement *item, const std::pair<int, int>&);
  74. /**
  75. * Return the number of lines written while writting nbChar bytes
  76. * @param nbChar col written
  77. * @param @maxWidth screen width
  78. * @return the number of line written
  79. **/
  80. static unsigned int getNbLines(float nbChar, unsigned int maxWidth);
  81. /**
  82. * get flags to be passed to write.
  83. * Contains indications on who to write item
  84. **/
  85. const OutputFlag getFlag(const JSonElement *item) const;
  86. /**
  87. * Write data
  88. **/
  89. /**
  90. * Warning: this one does not check line height, because he's not aware of cursor position
  91. **/
  92. void write(const std::string &str, const OutputFlag flags) const;
  93. unsigned int write(const int &x, const int &y, const JSonElement *item, unsigned int maxWidth, const OutputFlag);
  94. unsigned int write(const int &x, const int &y, const std::string &item, unsigned int maxWidth, const OutputFlag);
  95. unsigned int write(const int &x, const int &y, const char item, unsigned int maxWidth, const OutputFlag);
  96. bool writeKey(const std::string &key, std::pair<int, int> &cursor, const std::pair<unsigned int, unsigned int> &maxWidth, OutputFlag, unsigned int extraLen =0);
  97. bool writeKey(const std::string &key, const std::string &after, std::pair<int, int> &cursor, const std::pair<unsigned int, unsigned int> &maxWidth, OutputFlag);
  98. bool writeContainer(std::pair<int, int> &, const std::pair<unsigned int, unsigned int> &maxSize, const JSonContainer *);
  99. bool writeContent(std::pair<int, int> &cursor, const std::pair<unsigned int, unsigned int> &maxSize, const std::list<JSonElement *> * obj);
  100. /**
  101. * find next search occurence and select it (wich cause viewport to move, eventually)
  102. * Have it own redraw because may have to write message
  103. **/
  104. bool jumpToNextSearch(const JSonElement *initial_selection, bool &);
  105. bool jumpToNextSearch();
  106. /**
  107. * prompt for user input, and return it
  108. * @throws noInputException if user use a kill-key (to exit buffer)
  109. **/
  110. const SearchPattern *inputSearch();
  111. /**
  112. * find occurences of search result and fill this#search_result
  113. **/
  114. unsigned int search(const SearchPattern &, const JSonElement *);
  115. /**
  116. * Write a message on the last line, using color
  117. **/
  118. void writeBottomLine(const std::string &currentBuffer, short color) const;
  119. /**
  120. * unfold all item's parents
  121. **/
  122. void unfold(const JSonElement *);
  123. // Fields
  124. /**
  125. * collapsed items
  126. **/
  127. std::set<const JSonContainer *> collapsed;
  128. /**
  129. * Root item
  130. **/
  131. const JSonElement *data;
  132. /**
  133. * selected item
  134. **/
  135. const JSonElement *selection;
  136. /**
  137. * currently searching pattern and its results
  138. **/
  139. std::list<const JSonElement*> search_result;
  140. /**
  141. * prev/next items to be selected on up/down keys
  142. **/
  143. const JSonElement *select_up, *select_down;
  144. /**
  145. * Program params (ac/av)
  146. **/
  147. const Params &params;
  148. /**
  149. * ncurses stuff
  150. **/
  151. SCREEN *screen;
  152. FILE *screen_fd;
  153. /**
  154. * Used by signals to stop reading input and shutdown ncurses
  155. **/
  156. bool breakLoop;
  157. /**
  158. * Viewport start
  159. **/
  160. int scrollTop;
  161. /**
  162. * initialized colors
  163. **/
  164. std::set<char /* OutputFlag::TYPE_SOMETHING */> colors;
  165. /**
  166. * Selection helpers
  167. * Used for moving viewport
  168. **/
  169. bool selectFound, selectIsLast;
  170. };