curseOutput.hh 5.8 KB

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