curseOutput.hh 5.3 KB

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