curseOutput.hh 5.0 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 "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. * return false if bottom of screen is touched
  54. * redraw all data
  55. **/
  56. virtual bool redraw() =0;
  57. /**
  58. * Like redraw, but append a message on the last line
  59. **/
  60. bool redraw(const std::string &errorMsg);
  61. /**
  62. * redraw item and children
  63. **
  64. virtual bool redraw(t_Cursor &, const t_Cursor &maxWidth, JSonElement *item) =0;
  65. */
  66. /**
  67. * Wait for input
  68. * @return false if ncurses should stop
  69. **/
  70. inputResult readInput(WINDOW *w);
  71. inputResult evalKey(const InputSequence &k);
  72. virtual inputResult selectUp() =0;
  73. virtual inputResult selectDown() =0;
  74. virtual inputResult selectPUp() =0;
  75. virtual inputResult selectPDown() =0;
  76. virtual inputResult expandSelection() =0;
  77. virtual inputResult collapseSelection() =0;
  78. virtual inputResult initSearch() =0;
  79. virtual inputResult nextResult() =0;
  80. virtual inputResult changeWindow(char direction, bool cycle) =0;
  81. /**
  82. * get the screen size
  83. **/
  84. virtual const t_Cursor getScreenSize() const;
  85. const t_Cursor getScreenSizeUnsafe() const;
  86. /**
  87. * set the select_up and select_down pointers, scroll to selection if it is above view port
  88. **
  89. virtual void checkSelection(const JSonElement *item, const t_Cursor &) =0;
  90. */
  91. /**
  92. * Return the number of lines written while writting nbChar bytes
  93. * @param nbChar col written
  94. * @param @maxWidth screen width
  95. * @return the number of line written
  96. **/
  97. static unsigned int getNbLines(const size_t nbChar, unsigned int maxWidth);
  98. /**
  99. * get flags to be passed to write.
  100. * Contains indications on who to write item
  101. **/
  102. virtual const OutputFlag getFlag(const JSonElement *item) const =0;
  103. /**
  104. * Write data
  105. **/
  106. /**
  107. * Warning: this one does not check line height, because he's not aware of cursor position
  108. **/
  109. virtual void write(const std::string &str, const OutputFlag flags) const =0;
  110. unsigned int write(const int &x, const int &y, JSonElement *item, unsigned int maxWidth, const OutputFlag);
  111. virtual unsigned int write(const int &x, const int &y, const std::string &item, const size_t len, unsigned int maxWidth, const OutputFlag) =0;
  112. virtual unsigned int write(const int &x, const int &y, const char item, unsigned int maxWidth, const OutputFlag) =0;
  113. /**
  114. * prompt for user input, and return it
  115. * @throws noInputException if user use a kill-key (to exit buffer)
  116. **/
  117. const SearchPattern *inputSearch();
  118. /**
  119. * find occurences of search result and fill this#search_result
  120. **/
  121. virtual unsigned int search(const SearchPattern &, const JSonElement *) =0;
  122. /**
  123. * Write a message on the last line, using color
  124. **/
  125. virtual void writeTopLine(const std::string &currentBuffer, short color) const;
  126. virtual void writeBottomLine(const std::string &currentBuffer, short color) const;
  127. virtual void writeBottomLine(const std::wstring &currentBuffer, short color) const;
  128. /**
  129. * unfold all item's parents
  130. **/
  131. void unfold(const JSonElement *);
  132. // Fields
  133. /**
  134. * collapsed items
  135. **/
  136. std::set<const JSonContainer *> collapsed;
  137. /**
  138. * Program params (ac/av)
  139. **/
  140. const Params &params;
  141. /**
  142. * ncurses stuff
  143. **/
  144. SCREEN *screen;
  145. FILE *screen_fd;
  146. /**
  147. * Used by signals to stop reading input and shutdown ncurses
  148. **/
  149. bool breakLoop;
  150. /**
  151. * initialized colors
  152. **/
  153. std::set<char /* OutputFlag::TYPE_SOMETHING */> colors;
  154. class SelectionOutOfRange { };
  155. };
  156. void _resizeFnc(int signo);