curseOutput.hh 6.0 KB

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