curseOutput.hh 5.8 KB

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