curseOutput.hh 6.1 KB

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