curseOutput.hh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "optional.hpp"
  13. #include "outputFlag.hh"
  14. #include "params.hh"
  15. #include "inputSequence.hh"
  16. class JSonElement;
  17. class JSonContainer;
  18. class JSonArray;
  19. class JSonObject;
  20. class SearchPattern;
  21. enum inputResult: char
  22. {
  23. redraw =0
  24. ,redrawAll =1
  25. ,quit =2
  26. ,nextInput =3
  27. };
  28. typedef std::pair<unsigned int, unsigned int> t_Cursor;
  29. class CurseOutput
  30. {
  31. public:
  32. CurseOutput(const Params &);
  33. virtual ~CurseOutput();
  34. /**
  35. * Called on SIG* while displaying data
  36. * Do not use (private).
  37. **/
  38. bool onsig(int signo);
  39. protected:
  40. /**
  41. * until kill-input, display data and read user inputs
  42. **/
  43. virtual void loop(WINDOW *w);
  44. /**
  45. * Initialize ncurses
  46. **/
  47. virtual void init();
  48. /**
  49. * Release ncurses
  50. **/
  51. virtual void shutdown() =0;
  52. /**
  53. * Called before redraw after window got SIGWINCH'd
  54. **/
  55. virtual void onResizeHandler();
  56. /**
  57. * return false if bottom of screen is touched
  58. * redraw all data
  59. **/
  60. virtual bool redraw() =0;
  61. /**
  62. * Like redraw, but append a message on the last line
  63. **/
  64. bool redraw(const std::string &errorMsg);
  65. /**
  66. * redraw item and children
  67. **
  68. virtual bool redraw(t_Cursor &, const t_Cursor &maxWidth, JSonElement *item) =0;
  69. */
  70. /**
  71. * Wait for input
  72. * @return false if ncurses should stop
  73. **/
  74. inputResult readInput(WINDOW *w);
  75. inputResult evalKey(const InputSequence &k);
  76. virtual inputResult selectUp() =0;
  77. virtual inputResult selectDown() =0;
  78. virtual inputResult selectPUp() =0;
  79. virtual inputResult selectPDown() =0;
  80. virtual inputResult expandSelection() =0;
  81. virtual inputResult collapseSelection() =0;
  82. virtual inputResult initSearch() =0;
  83. virtual inputResult nextResult() =0;
  84. virtual inputResult changeWindow(char direction, bool cycle) =0;
  85. virtual bool hasReachedBottom(unsigned int pos, unsigned int scrollTop, unsigned int maxHeight) const;
  86. /**
  87. * get the screen size
  88. **/
  89. virtual const t_Cursor getScreenSize() const =0;
  90. const t_Cursor getScreenSizeUnsafe() const;
  91. /**
  92. * set the select_up and select_down pointers, scroll to selection if it is above view port
  93. **
  94. virtual void checkSelection(const JSonElement *item, const t_Cursor &) =0;
  95. */
  96. /**
  97. * Return the number of lines written while writting nbChar bytes
  98. * @param nbChar col written
  99. * @param @maxWidth screen width
  100. * @return the number of line written
  101. **/
  102. static unsigned int getNbLines(const size_t nbChar, unsigned int maxWidth);
  103. /**
  104. * get flags to be passed to write.
  105. * Contains indications on who to write item
  106. **/
  107. virtual const OutputFlag getFlag(const JSonElement *item) const =0;
  108. /**
  109. * Write data
  110. **/
  111. /**
  112. * Warning: this one does not check line height, because he's not aware of cursor position
  113. **/
  114. virtual void write(const std::string &str, const OutputFlag flags) const =0;
  115. unsigned int write(const int &x, const int &y, JSonElement *item, unsigned int maxWidth, const OutputFlag);
  116. virtual unsigned int write(const unsigned int &x, const unsigned int &y, const std::string &item, const size_t len, unsigned int maxWidth, const OutputFlag) =0;
  117. virtual unsigned int write(const unsigned int &x, const unsigned int &y, const char item, unsigned int maxWidth, const OutputFlag) =0;
  118. /**
  119. * prompt for user input, and return it
  120. * @throws noInputException if user use a kill-key (to exit buffer)
  121. **/
  122. const SearchPattern *inputSearch(WINDOW *w =nullptr);
  123. /**
  124. * find occurences of search result and fill this#search_result
  125. **/
  126. virtual unsigned int search(const SearchPattern &, const JSonElement *) =0;
  127. /**
  128. * Write a message on the last line, using color
  129. **/
  130. virtual void writeTopLine(const std::string &currentBuffer, short color) const;
  131. virtual void writeBottomLine(const std::string &currentBuffer, short color) const;
  132. virtual void writeBottomLine(const std::wstring &currentBuffer, short color) const;
  133. /**
  134. * unfold all item's parents
  135. **/
  136. void unfold(const JSonElement *);
  137. // Fields
  138. /**
  139. * collapsed items
  140. **/
  141. std::set<const JSonContainer *> collapsed;
  142. /**
  143. * Program params (ac/av)
  144. **/
  145. const Params &params;
  146. /**
  147. * ncurses stuff
  148. **/
  149. SCREEN *screen;
  150. FILE *screen_fd;
  151. /**
  152. * Used by signals to stop reading input and shutdown ncurses
  153. **/
  154. bool breakLoop;
  155. /**
  156. * initialized colors
  157. **/
  158. std::set<char /* OutputFlag::TYPE_SOMETHING */> colors;
  159. t_Cursor screenSize;
  160. class SelectionOutOfRange { };
  161. };
  162. void _resizeFnc(int signo);