1
1

curseOutput.hh 5.6 KB

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