curseOutput.hh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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(const t_Cursor &);
  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. /**
  86. * get the screen size
  87. **/
  88. virtual const t_Cursor getScreenSize() const;
  89. const t_Cursor getScreenSizeUnsafe() const;
  90. /**
  91. * set the select_up and select_down pointers, scroll to selection if it is above view port
  92. **
  93. virtual void checkSelection(const JSonElement *item, const t_Cursor &) =0;
  94. */
  95. /**
  96. * Return the number of lines written while writting nbChar bytes
  97. * @param nbChar col written
  98. * @param @maxWidth screen width
  99. * @return the number of line written
  100. **/
  101. static unsigned int getNbLines(const size_t nbChar, unsigned int maxWidth);
  102. /**
  103. * get flags to be passed to write.
  104. * Contains indications on who to write item
  105. **/
  106. virtual const OutputFlag getFlag(const JSonElement *item) const =0;
  107. /**
  108. * Write data
  109. **/
  110. /**
  111. * Warning: this one does not check line height, because he's not aware of cursor position
  112. **/
  113. virtual void write(const std::string &str, const OutputFlag flags) const =0;
  114. unsigned int write(const int &x, const int &y, JSonElement *item, unsigned int maxWidth, const OutputFlag);
  115. virtual unsigned int write(const int &x, const int &y, const std::string &item, const size_t len, unsigned int maxWidth, const OutputFlag) =0;
  116. virtual unsigned int write(const int &x, const int &y, const char item, unsigned int maxWidth, const OutputFlag) =0;
  117. /**
  118. * prompt for user input, and return it
  119. * @throws noInputException if user use a kill-key (to exit buffer)
  120. **/
  121. const SearchPattern *inputSearch();
  122. /**
  123. * find occurences of search result and fill this#search_result
  124. **/
  125. virtual unsigned int search(const SearchPattern &, const JSonElement *) =0;
  126. /**
  127. * Write a message on the last line, using color
  128. **/
  129. virtual void writeTopLine(const std::string &currentBuffer, short color) const;
  130. virtual void writeBottomLine(const std::string &currentBuffer, short color) const;
  131. virtual void writeBottomLine(const std::wstring &currentBuffer, short color) const;
  132. /**
  133. * unfold all item's parents
  134. **/
  135. void unfold(const JSonElement *);
  136. // Fields
  137. /**
  138. * collapsed items
  139. **/
  140. std::set<const JSonContainer *> collapsed;
  141. /**
  142. * Program params (ac/av)
  143. **/
  144. const Params &params;
  145. /**
  146. * ncurses stuff
  147. **/
  148. SCREEN *screen;
  149. FILE *screen_fd;
  150. /**
  151. * Used by signals to stop reading input and shutdown ncurses
  152. **/
  153. bool breakLoop;
  154. /**
  155. * initialized colors
  156. **/
  157. std::set<char /* OutputFlag::TYPE_SOMETHING */> colors;
  158. class SelectionOutOfRange { };
  159. };
  160. void _resizeFnc(int signo);