curseSplitOutput.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /**
  2. * curseSplitOutput.cpp for jsonstroller
  3. *
  4. * Author: isundil <isundill@gmail.com>
  5. **/
  6. #include <sys/ioctl.h>
  7. #include <unistd.h>
  8. #include <signal.h>
  9. #include <curses.h>
  10. #include "searchPattern.hh"
  11. #include "curseSplitOutput.hh"
  12. #include "jsonObject.hh"
  13. #include "jsonArray.hh"
  14. #include "jsonPrimitive.hh"
  15. #include "levenshteinMatrice.hpp"
  16. template<class T> const T &list_at(const std::list<T> &l, unsigned int pos)
  17. {
  18. typename std::list<T>::const_iterator it = l.cbegin();
  19. std::advance(it, pos);
  20. return *it;
  21. }
  22. CurseSplitOutput::CurseSplitOutput(const Params &p): CurseOutput(p)
  23. {
  24. diffMatrice = nullptr;
  25. init();
  26. }
  27. CurseSplitOutput::~CurseSplitOutput()
  28. {
  29. shutdown();
  30. if (diffMatrice)
  31. {
  32. delete diffMatrice;
  33. diffMatrice = nullptr;
  34. }
  35. }
  36. void CurseSplitOutput::run(const std::deque<std::string> &inputName, const std::deque<JSonElement*> &roots)
  37. {
  38. nbInputs = inputName.size();
  39. const t_Cursor screenSize = getScreenSize();
  40. selectedWin = 0;
  41. destroyAllSubWin();
  42. subWindows.clear();
  43. for (size_t i =0; i < nbInputs; ++i)
  44. {
  45. t_subWindow subwin;
  46. subwin.fileName = inputName.at(i);
  47. subwin.selection = subwin.lastSelection = subwin.root = roots.at(i);
  48. subwin.select_up = subwin.select_down = nullptr;
  49. subwin.innerWin = subwin.outerWin = nullptr;
  50. subwin.scrollTop = 0;
  51. subwin.outerWin = newwin(screenSize.second +2, screenSize.first, 0, i * (screenSize.first -1));
  52. subwin.innerWin = newwin(screenSize.second, screenSize.first -2, 1, i * (screenSize.first -1) +1);
  53. keypad(subwin.outerWin, true);
  54. subWindows.push_back(subwin);
  55. box(subwin.outerWin, 0, 0);
  56. wrefresh(subwin.outerWin);
  57. }
  58. computeDiff();
  59. loop(subWindows.at(0).outerWin);
  60. }
  61. void CurseSplitOutput::computeDiff()
  62. {
  63. //TODO diffMatrice should be LevenshteinMatrice_base[nbInputs -1]
  64. //And we should iterate such as diffMatrice[n] = diff(n, n+1) ?
  65. if (diffMatrice)
  66. delete diffMatrice;
  67. LevenshteinMatrice_base::Builder builder;
  68. if (nbInputs == 2)
  69. diffMatrice = builder.build(subWindows.at(0).root, subWindows.at(1).root);
  70. else if (nbInputs == 3)
  71. throw std::runtime_error("3-input diff not implemented");
  72. }
  73. inputResult CurseSplitOutput::selectUp()
  74. {
  75. size_t i =0;
  76. const JSonElement *newSelection = subWindows.at(selectedWin).select_up;
  77. for (t_subWindow &w : subWindows) {
  78. if (i == selectedWin)
  79. w.selection = w.lastSelection = w.select_up;
  80. else
  81. {
  82. w.selection = diffMatrice->getEquivalence(newSelection);
  83. if (w.selection)
  84. w.lastSelection = w.selection;
  85. }
  86. ++i;
  87. }
  88. return inputResult::redraw;
  89. }
  90. inputResult CurseSplitOutput::selectDown()
  91. {
  92. const JSonElement *newSelection = subWindows.at(selectedWin).select_down;
  93. size_t i = 0;
  94. for (t_subWindow &w : subWindows)
  95. {
  96. if (w.selectIsLast)
  97. w.scrollTop += 2;
  98. if (i == selectedWin)
  99. w.selection = w.lastSelection = newSelection;
  100. else
  101. {
  102. w.selection = diffMatrice->getEquivalence(newSelection);
  103. if (w.selection)
  104. w.lastSelection = w.selection;
  105. }
  106. ++i;
  107. }
  108. return inputResult::redraw;
  109. }
  110. inputResult CurseSplitOutput::selectPUp()
  111. {
  112. const JSonElement *_selection = subWindows.at(selectedWin).selection;
  113. const JSonElement *brother = _selection->findPrev();
  114. if (brother == nullptr)
  115. {
  116. const JSonElement *parent = _selection->getParent();
  117. if (parent && dynamic_cast<const JSonContainer*>(parent))
  118. {
  119. subWindows.at(selectedWin).selection = _selection = parent;
  120. if (_selection->getParent() && dynamic_cast<const JSonObjectEntry*> (_selection->getParent()))
  121. subWindows.at(selectedWin).selection = _selection->getParent();
  122. }
  123. else
  124. return inputResult::nextInput;
  125. }
  126. else
  127. subWindows.at(selectedWin).selection = brother;
  128. return inputResult::redraw;
  129. }
  130. inputResult CurseSplitOutput::selectPDown()
  131. {
  132. const JSonElement *brother = subWindows.at(selectedWin).selection->findNext();
  133. if (brother)
  134. {
  135. subWindows.at(selectedWin).selection = brother;
  136. return inputResult::redraw;
  137. }
  138. return inputResult::nextInput;
  139. }
  140. inputResult CurseSplitOutput::expandSelection()
  141. {
  142. const JSonElement *_selection = subWindows.at(selectedWin).selection;
  143. if (dynamic_cast<const JSonObjectEntry*>(_selection))
  144. _selection = **((const JSonObjectEntry*)_selection);
  145. if (!dynamic_cast<const JSonContainer*>(_selection))
  146. return inputResult::nextInput;
  147. if (collapsed.erase((const JSonContainer *)_selection))
  148. return inputResult::redraw;
  149. if (!((const JSonContainer*)_selection)->size())
  150. return inputResult::nextInput;
  151. subWindows.at(selectedWin).selection = subWindows.at(selectedWin).select_down;
  152. return inputResult::redraw;
  153. }
  154. inputResult CurseSplitOutput::collapseSelection()
  155. {
  156. const JSonElement *_selection = subWindows.at(selectedWin).selection;
  157. if (dynamic_cast<const JSonObjectEntry*>(_selection))
  158. _selection = **((const JSonObjectEntry*)_selection);
  159. if (_selection->getParent() && (!dynamic_cast<const JSonContainer*>(_selection)
  160. || collapsed.find((const JSonContainer *)_selection) != collapsed.end()
  161. || (dynamic_cast<const JSonContainer*>(_selection) && ((const JSonContainer*)_selection)->size() == 0)))
  162. {
  163. subWindows.at(selectedWin).selection = _selection = subWindows.at(selectedWin).selection->getParent();
  164. if (_selection->getParent() && dynamic_cast<const JSonObjectEntry*>(_selection->getParent()))
  165. subWindows.at(selectedWin).selection = _selection->getParent();
  166. }
  167. else
  168. collapsed.insert((const JSonContainer *)_selection);
  169. return inputResult::redraw;
  170. }
  171. inputResult CurseSplitOutput::initSearch()
  172. {
  173. const SearchPattern *searchPattern = inputSearch();
  174. if (!searchPattern)
  175. return inputResult::redraw;
  176. for (t_subWindow &s : subWindows)
  177. s.searchResults.clear();
  178. if (searchPattern->isEmpty())
  179. return inputResult::redraw;
  180. search(*searchPattern);
  181. delete searchPattern;
  182. return nextResult();
  183. }
  184. inputResult CurseSplitOutput::nextResult()
  185. {
  186. if (subWindows.at(selectedWin).searchResults.empty())
  187. CurseOutput::redraw("Pattern not found");
  188. else if (jumpToNextSearch())
  189. return inputResult::redraw;
  190. return inputResult::nextInput;
  191. }
  192. inputResult CurseSplitOutput::changeWindow(char d, bool c)
  193. {
  194. if ((selectedWin +d < 0 || selectedWin +d >= nbInputs) && !c)
  195. return inputResult::nextInput;
  196. selectedWin = (selectedWin +d) % nbInputs;
  197. t_subWindow &w = subWindows.at(selectedWin);
  198. if (!w.selection)
  199. {
  200. size_t i =0;
  201. for (t_subWindow &it : subWindows)
  202. {
  203. if (i == selectedWin)
  204. it.selection = it.lastSelection;
  205. else
  206. {
  207. it.selection = diffMatrice->getEquivalence(w.lastSelection);
  208. if (it.selection)
  209. it.lastSelection = it.selection;
  210. }
  211. ++i;
  212. }
  213. }
  214. return inputResult::redrawAll;
  215. }
  216. void CurseSplitOutput::checkSelection(const JSonElement *item)
  217. {
  218. t_subWindow &w = subWindows.at(workingWin);
  219. if (!w.selectFound)
  220. {
  221. if (w.lastSelection == item)
  222. {
  223. if (w.cursor.second < w.scrollTop) //Selection is above vp, move scroll pos to selection and start drawing
  224. w.scrollTop = w.cursor.second;
  225. w.selectFound = true;
  226. }
  227. else if (!item->getParent() || !dynamic_cast<const JSonObjectEntry*>(item->getParent()))
  228. w.select_up = item;
  229. }
  230. else if (!w.select_down)
  231. {
  232. const JSonElement *parent = item->getParent();
  233. if (!dynamic_cast<const JSonContainer*>(item) &&
  234. parent &&
  235. w.lastSelection != parent &&
  236. dynamic_cast<const JSonObjectEntry*>(parent))
  237. item = parent;
  238. if (!parent || !dynamic_cast<const JSonObjectEntry*>(parent))
  239. w.select_down = item;
  240. }
  241. }
  242. bool CurseSplitOutput::jumpToNextSearch(const JSonElement *current, bool &selectFound)
  243. {
  244. const JSonContainer *container = dynamic_cast<const JSonContainer *> (current);
  245. const JSonObjectEntry *objEntry = dynamic_cast<const JSonObjectEntry *> (current);
  246. if (subWindows.at(selectedWin).lastSelection == current)
  247. selectFound = true;
  248. if (container)
  249. {
  250. if (!container->empty())
  251. for (const JSonElement *it : *container)
  252. if (jumpToNextSearch(it, selectFound))
  253. return true;
  254. }
  255. else
  256. {
  257. if (current &&
  258. std::find(subWindows.at(selectedWin).searchResults.cbegin(),
  259. subWindows.at(selectedWin).searchResults.cend(),
  260. current) != subWindows.at(selectedWin).searchResults.cend() &&
  261. current != subWindows.at(selectedWin).selection &&
  262. selectFound)
  263. {
  264. subWindows.at(selectedWin).lastSelection = current;
  265. return true;
  266. }
  267. if (objEntry)
  268. if (jumpToNextSearch(**objEntry, selectFound))
  269. return true;
  270. }
  271. return false;
  272. }
  273. unsigned int CurseSplitOutput::search(const SearchPattern &searchPattern)
  274. {
  275. unsigned int result =0;
  276. for (t_subWindow &w : subWindows)
  277. result += search(searchPattern, w.root);
  278. return result;
  279. }
  280. unsigned int CurseSplitOutput::search(const SearchPattern &searchPattern, const JSonElement *current)
  281. {
  282. const JSonContainer *container = dynamic_cast<const JSonContainer *> (current);
  283. const JSonObjectEntry *objEntry = dynamic_cast<const JSonObjectEntry *> (current);
  284. if (container)
  285. {
  286. if (!container->empty())
  287. for (const JSonElement *it : *container)
  288. search(searchPattern, it);
  289. }
  290. else
  291. {
  292. if (current && current->match(searchPattern))
  293. {
  294. if (current->getParent() && dynamic_cast<const JSonObjectEntry*>(current->getParent()))
  295. subWindows.at(workingWin).searchResults.push_back(current->getParent());
  296. else
  297. subWindows.at(workingWin).searchResults.push_back(current);
  298. }
  299. if (objEntry)
  300. search(searchPattern, **objEntry);
  301. }
  302. return subWindows.at(workingWin).searchResults.size();
  303. }
  304. bool CurseSplitOutput::jumpToNextSearch()
  305. {
  306. bool selectFound = false;
  307. bool res = jumpToNextSearch(subWindows.at(selectedWin).root, selectFound);
  308. if (!res)
  309. {
  310. subWindows.at(selectedWin).lastSelection = *(subWindows.at(selectedWin).searchResults.cbegin());
  311. unfold(subWindows.at(selectedWin).selection);
  312. CurseOutput::redraw("Search hit BOTTOM, continuing at TOP");
  313. return false;
  314. }
  315. unfold(subWindows.at(selectedWin).selection);
  316. return true;
  317. }
  318. bool CurseSplitOutput::redraw()
  319. {
  320. const t_Cursor screenSize = getScreenSize();
  321. short writingDone = (1 << nbInputs) -1;
  322. workingWin = 0;
  323. for (t_subWindow &w : subWindows)
  324. {
  325. w.cursor = t_Cursor(2, 1);
  326. w.select_up = w.select_down = nullptr;
  327. w.selectFound = w.selectIsLast = false;
  328. wclear(w.innerWin);
  329. writeTopLine(w.fileName,
  330. workingWin == selectedWin ? OutputFlag::SPECIAL_ACTIVEINPUTNAME : OutputFlag::SPECIAL_INPUTNAME);
  331. w.parentsIterators = std::stack<std::pair<int, JSonContainer*> >();
  332. ++workingWin;
  333. }
  334. while (writingDone)
  335. {
  336. workingWin = 0;
  337. for (t_subWindow &w : subWindows)
  338. {
  339. bool result;
  340. if ((writingDone & (1 << workingWin)))
  341. {
  342. try {
  343. if (w.parentsIterators.empty())
  344. result = redraw(screenSize, w.root, true);
  345. else
  346. result = redraw(screenSize, w.parentsIterators.top(), true);
  347. }
  348. catch (SelectionOutOfRange &e)
  349. {
  350. return false;
  351. }
  352. catch (CurseSplitOutput::reachNext &)
  353. {
  354. result = true;
  355. }
  356. if (!result || w.parentsIterators.empty())
  357. {
  358. if (!result)
  359. {
  360. if (!w.selectFound)
  361. {
  362. w.scrollTop++;
  363. return false;
  364. }
  365. if (!w.select_down)
  366. w.selectIsLast = true;
  367. }
  368. if (!w.select_down)
  369. {
  370. const JSonContainer *pselect = dynamic_cast<const JSonContainer*>(w.selection);
  371. if (pselect && !pselect->empty())
  372. w.select_down = *(pselect->cbegin());
  373. else
  374. {
  375. const JSonElement *next = w.lastSelection->findNext();
  376. w.select_down = next ? next : w.lastSelection;
  377. }
  378. }
  379. if (!w.select_up)
  380. w.select_up = subWindows.at(workingWin).lastSelection;
  381. writingDone &= ~(1 << workingWin);
  382. }
  383. }
  384. ++workingWin;
  385. }
  386. }
  387. for (t_subWindow &w : subWindows)
  388. wrefresh(w.innerWin);
  389. return true;
  390. }
  391. bool CurseSplitOutput::writeContainer(const t_Cursor &maxSize, JSonContainer *item, bool opening)
  392. {
  393. char childDelimiter[2];
  394. t_subWindow &w = subWindows.at(workingWin);
  395. if (dynamic_cast<const JSonObject *>(item))
  396. memcpy(childDelimiter, "{}", sizeof(*childDelimiter) * 2);
  397. else
  398. memcpy(childDelimiter, "[]", sizeof(*childDelimiter) * 2);
  399. if (!opening)
  400. {
  401. w.cursor.first -= INDENT_LEVEL;
  402. w.cursor.second += write(w.cursor.first, w.cursor.second, childDelimiter[1], maxSize.first, CurseSplitOutput::getFlag(item));
  403. }
  404. else if (collapsed.find((const JSonContainer *)item) != collapsed.end())
  405. {
  406. std::string ss;
  407. ss.append(&childDelimiter[0], 1).append(" ... ").append(&childDelimiter[1], 1);
  408. w.cursor.second += write(w.cursor.first, w.cursor.second, ss, 7, maxSize.first, CurseSplitOutput::getFlag(item));
  409. }
  410. else
  411. {
  412. w.cursor.second += write(w.cursor.first, w.cursor.second, childDelimiter[0], maxSize.first, CurseSplitOutput::getFlag(item));
  413. if (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > maxSize.second -1)
  414. return false;
  415. w.parentsIterators.push(std::pair<int, JSonContainer *>(-1, item));
  416. if (!writeContent(maxSize, (std::list<JSonElement *> *)item))
  417. {
  418. w.parentsIterators.pop();
  419. return false;
  420. }
  421. w.parentsIterators.pop();
  422. w.cursor.second += write(w.cursor.first, w.cursor.second, childDelimiter[1], maxSize.first, CurseSplitOutput::getFlag(item));
  423. }
  424. return (w.cursor.second < w.scrollTop || (w.cursor.second - w.scrollTop) <= maxSize.second -1);
  425. }
  426. bool CurseSplitOutput::writeContent(const t_Cursor &maxSize, std::list<JSonElement*> *_item)
  427. {
  428. t_subWindow &w = subWindows.at(workingWin);
  429. JSonContainer *item = (JSonContainer *)_item;
  430. bool containerIsObject = (dynamic_cast<JSonObject *>(item) != nullptr);
  431. bool result = true;
  432. w.cursor.first += INDENT_LEVEL;
  433. const unsigned int scrollTop = w.scrollTop;
  434. for (JSonElement *i : *item)
  435. {
  436. result = false;
  437. if (containerIsObject)
  438. {
  439. JSonObjectEntry *ent = (JSonObjectEntry*) i;
  440. bool isContainer = (dynamic_cast<JSonContainer *>(**ent) != nullptr);
  441. std::string key = ent->stringify();
  442. checkSelection(ent);
  443. if (isContainer && collapsed.find((JSonContainer*)(**ent)) != collapsed.cend())
  444. {
  445. if (dynamic_cast<JSonObject *>(**ent))
  446. {
  447. if (!CurseOutput::writeKey(key, ent->lazystrlen(), "{ ... }", w.cursor, maxSize, CurseSplitOutput::getFlag(ent)) || (w.cursor.second > scrollTop && (w.cursor.second - scrollTop) > maxSize.second -1))
  448. break;
  449. }
  450. else if (!CurseOutput::writeKey(key, ent->lazystrlen(), "[ ... ]", w.cursor, maxSize, CurseSplitOutput::getFlag(ent)) || (w.cursor.second > scrollTop && (w.cursor.second - scrollTop) > maxSize.second -1))
  451. break;
  452. }
  453. else if (!isContainer)
  454. {
  455. JSonElement *eContent = **ent;
  456. if (!writeKey(key, ent->lazystrlen(), eContent->stringify(), eContent->lazystrlen(), w.cursor, maxSize, CurseSplitOutput::getFlag(ent)) || (w.cursor.second > scrollTop && (w.cursor.second - scrollTop) > maxSize.second -1))
  457. break;
  458. }
  459. else if (((JSonContainer*)(**ent))->size() == 0)
  460. {
  461. if (dynamic_cast<const JSonObject *>(**ent) )
  462. {
  463. if (!CurseOutput::writeKey(key, ent->lazystrlen(), "{ }", w.cursor, maxSize, CurseSplitOutput::getFlag(ent)) || (w.cursor.second > scrollTop && (w.cursor.second - scrollTop) > maxSize.second -1))
  464. break;
  465. }
  466. else if (!CurseOutput::writeKey(key, ent->lazystrlen(), "[ ]", w.cursor, maxSize, CurseSplitOutput::getFlag(ent)) || (w.cursor.second > scrollTop && (w.cursor.second - scrollTop) > maxSize.second -1))
  467. break;
  468. }
  469. else
  470. {
  471. if (!writeKey(key, ent->lazystrlen(), maxSize, getFlag(ent)))
  472. break;
  473. const JSonElement *saveSelection = w.lastSelection;
  474. if (saveSelection == ent)
  475. w.selection = w.lastSelection = **ent;
  476. w.cursor.first += INDENT_LEVEL /2;
  477. if (!redraw(maxSize, **ent))
  478. {
  479. w.selection = w.lastSelection = saveSelection;
  480. w.cursor.first -= INDENT_LEVEL /2;
  481. return false;
  482. }
  483. w.selection = w.lastSelection = saveSelection;
  484. w.cursor.first -= INDENT_LEVEL /2;
  485. }
  486. }
  487. else
  488. {
  489. if (!redraw(maxSize, i))
  490. break;
  491. }
  492. result = true;
  493. }
  494. w.cursor.first -= INDENT_LEVEL;
  495. //result will be false if for loop break'd at some time, true otherwise
  496. return result;
  497. }
  498. bool CurseSplitOutput::writeKey(const std::string &key, const size_t keylen, const t_Cursor &maxSize, OutputFlag flags, unsigned int extraLen)
  499. {
  500. t_subWindow &w = subWindows.at(workingWin);
  501. if (w.cursor.second <= w.scrollTop)
  502. {
  503. w.cursor.second++;
  504. return true;
  505. }
  506. char oldType = flags.type();
  507. flags.type(OutputFlag::TYPE_OBJKEY);
  508. w.cursor.second += write(w.cursor.first, w.cursor.second, key, keylen, maxSize.first -extraLen -2, flags);
  509. flags.type(OutputFlag::TYPE_OBJ);
  510. write(": ", flags);
  511. flags.type(oldType);
  512. return (w.cursor.second < w.scrollTop || (w.cursor.second - w.scrollTop) <= maxSize.second);
  513. }
  514. bool CurseSplitOutput::writeKey(const std::string &key, const size_t keylen, const std::string &after, const size_t afterlen, t_Cursor &cursor, const t_Cursor &maxWidth, OutputFlag flags)
  515. {
  516. t_subWindow &w = subWindows.at(workingWin);
  517. if (cursor.second <= w.scrollTop)
  518. {
  519. cursor.second++;
  520. return true;
  521. }
  522. char oldType = flags.type();
  523. flags.type(OutputFlag::TYPE_OBJKEY);
  524. write(cursor.first, cursor.second, key, 0, 1, flags);
  525. flags.type(OutputFlag::TYPE_OBJ);
  526. write(": ", flags);
  527. flags.type(oldType);
  528. write(after, flags);
  529. cursor.second += getNbLines(cursor.first +keylen +2 +afterlen, maxWidth.first);
  530. return (cursor.second < w.scrollTop || (cursor.second - w.scrollTop) <= maxWidth.second);
  531. }
  532. bool CurseSplitOutput::redraw(const t_Cursor &maxSize, std::pair<int, JSonContainer *> &item, bool isRoot)
  533. {
  534. t_subWindow &w = subWindows.at(workingWin);
  535. static short dirty =0;
  536. JSonElement *currentItem;
  537. if (dirty && !isRoot)
  538. throw CurseSplitOutput::reachNext();
  539. dirty = 1;
  540. (item.first)++;
  541. if ((unsigned int) item.first == item.second->size())
  542. {
  543. w.parentsIterators.pop();
  544. if (!writeContainer(maxSize, item.second, false))
  545. return false;
  546. throw CurseSplitOutput::reachNext();
  547. }
  548. currentItem = list_at<JSonElement*>(*(item.second), item.first);
  549. checkSelection(currentItem);
  550. if (dynamic_cast<const JSonContainer*>(currentItem))
  551. {
  552. if (!writeContainer(maxSize, (JSonContainer*) currentItem))
  553. {
  554. dirty = 0;
  555. return false;
  556. }
  557. dirty = 0;
  558. }
  559. else
  560. {
  561. w.cursor.second += CurseOutput::write(w.cursor.first, w.cursor.second, currentItem, maxSize.first, CurseSplitOutput::getFlag(currentItem));
  562. dirty = 0;
  563. if (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > maxSize.second -1)
  564. return false;
  565. }
  566. return true;
  567. }
  568. bool CurseSplitOutput::redraw(const t_Cursor &maxSize, JSonElement *item, bool isRoot)
  569. {
  570. static short dirty =0;
  571. if (dirty && !isRoot)
  572. throw CurseSplitOutput::reachNext();
  573. dirty = 1;
  574. checkSelection(item);
  575. if (dynamic_cast<const JSonContainer*>(item))
  576. {
  577. if (!writeContainer(maxSize, (JSonContainer *) item))
  578. {
  579. dirty = 0;
  580. return false;
  581. }
  582. dirty = 0;
  583. }
  584. else
  585. {
  586. t_subWindow &w = subWindows.at(workingWin);
  587. w.cursor.second += CurseOutput::write(w.cursor.first, w.cursor.second, item, maxSize.first, CurseSplitOutput::getFlag(item));
  588. dirty = 0;
  589. if (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > maxSize.second -1)
  590. return false;
  591. }
  592. return true;
  593. }
  594. unsigned int CurseSplitOutput::write(const int &x, const int &y, const char item, unsigned int maxWidth, OutputFlag flags)
  595. {
  596. int offsetY = y - subWindows.at(workingWin).scrollTop;
  597. WINDOW *currentWin = subWindows.at(workingWin).innerWin;
  598. char color = OutputFlag::SPECIAL_NONE;
  599. if (offsetY <= 0)
  600. return 1;
  601. if (flags.selected())
  602. wattron(currentWin, A_REVERSE | A_BOLD);
  603. if (flags.searched())
  604. color = OutputFlag::SPECIAL_SEARCH;
  605. else if (colors.find(flags.type()) != colors.end())
  606. color = flags.type();
  607. if (color != OutputFlag::SPECIAL_NONE)
  608. wattron(currentWin, COLOR_PAIR(color));
  609. mvwprintw(currentWin, offsetY, x, "%c", item);
  610. wattroff(currentWin, A_REVERSE | A_BOLD);
  611. if (color != OutputFlag::SPECIAL_NONE)
  612. wattroff(currentWin, COLOR_PAIR(color));
  613. switch (flags.diffOp())
  614. {
  615. case eLevenshteinOperator::equ:
  616. mvwprintw(currentWin, offsetY, 0, "=");
  617. break;
  618. case eLevenshteinOperator::add:
  619. mvwprintw(currentWin, offsetY, 0, "+");
  620. break;
  621. case eLevenshteinOperator::mod:
  622. mvwprintw(currentWin, offsetY, 0, "!");
  623. break;
  624. case eLevenshteinOperator::rem:
  625. // little strange
  626. mvwprintw(currentWin, offsetY, 0, "-");
  627. break;
  628. default:
  629. // very fucking strange
  630. mvwprintw(currentWin, offsetY, 0, "?");
  631. break;
  632. }
  633. return getNbLines(x +1, maxWidth);
  634. }
  635. unsigned int CurseSplitOutput::write(const int &x, const int &y, const std::string &str, const size_t strlen, unsigned int maxWidth, const OutputFlag flags)
  636. {
  637. const t_subWindow &w = subWindows.at(workingWin);
  638. WINDOW *currentWin = w.innerWin;
  639. int offsetY = y - w.scrollTop;
  640. if (offsetY <= 0)
  641. return 1;
  642. switch (flags.diffOp())
  643. {
  644. case eLevenshteinOperator::equ:
  645. mvwprintw(currentWin, offsetY, 0, "=");
  646. break;
  647. case eLevenshteinOperator::add:
  648. mvwprintw(currentWin, offsetY, 0, "+");
  649. break;
  650. case eLevenshteinOperator::mod:
  651. mvwprintw(currentWin, offsetY, 0, "!");
  652. break;
  653. case eLevenshteinOperator::rem:
  654. // little strange
  655. mvwprintw(currentWin, offsetY, 0, "-");
  656. break;
  657. default:
  658. // very fucking strange
  659. mvwprintw(currentWin, offsetY, 0, "?");
  660. break;
  661. }
  662. wmove(subWindows.at(workingWin).innerWin, offsetY, x);
  663. write(str, flags);
  664. return getNbLines(strlen +x, maxWidth);
  665. }
  666. void CurseSplitOutput::write(const std::string &str, const OutputFlag flags) const
  667. {
  668. char color = OutputFlag::SPECIAL_NONE;
  669. WINDOW *currentWin = subWindows.at(workingWin).innerWin;
  670. if (flags.selected())
  671. wattron(currentWin, A_REVERSE | A_BOLD);
  672. if (flags.searched())
  673. color = OutputFlag::SPECIAL_SEARCH;
  674. else if (colors.find(flags.type()) != colors.end())
  675. color = flags.type();
  676. if (color != OutputFlag::SPECIAL_NONE)
  677. wattron(currentWin, COLOR_PAIR(color));
  678. wprintw(currentWin, "%s", str.c_str());
  679. wattroff(currentWin, A_REVERSE | A_BOLD);
  680. if (color != OutputFlag::SPECIAL_NONE)
  681. wattroff(currentWin, COLOR_PAIR(color));
  682. }
  683. void CurseSplitOutput::destroyAllSubWin()
  684. {
  685. for (t_subWindow &w : subWindows)
  686. {
  687. if (w.outerWin)
  688. {
  689. wborder(w.outerWin, ' ', ' ', ' ',' ',' ',' ',' ',' ');
  690. wrefresh(w.outerWin);
  691. delwin(w.outerWin);
  692. w.outerWin = nullptr;
  693. }
  694. if (w.innerWin)
  695. {
  696. delwin(w.innerWin);
  697. w.innerWin = nullptr;
  698. }
  699. }
  700. }
  701. void CurseSplitOutput::writeTopLine(const std::string &buffer, short color) const
  702. {
  703. const t_Cursor screenSize = getScreenSize();
  704. const size_t bufsize = buffer.size();
  705. WINDOW *currentWin = subWindows.at(workingWin).innerWin;
  706. if (params.colorEnabled())
  707. wattron(currentWin, COLOR_PAIR(color));
  708. mvwprintw(currentWin, 0, 0, "%s%*c", buffer.c_str(), screenSize.first - bufsize -2, ' ');
  709. if (params.colorEnabled())
  710. wattroff(currentWin, COLOR_PAIR(color));
  711. }
  712. const t_Cursor CurseSplitOutput::getScreenSize() const
  713. {
  714. t_Cursor result = getScreenSizeUnsafe();
  715. return t_Cursor(result.first / nbInputs, result.second -2);
  716. }
  717. void CurseSplitOutput::shutdown()
  718. {
  719. destroyAllSubWin();
  720. endwin();
  721. delscreen(screen);
  722. if (screen_fd)
  723. {
  724. fclose(screen_fd);
  725. screen_fd = nullptr;
  726. }
  727. screen = nullptr;
  728. }
  729. const OutputFlag CurseSplitOutput::getFlag(const JSonElement *e) const
  730. {
  731. return getFlag(e, subWindows.at(workingWin).selection);
  732. }
  733. const OutputFlag CurseSplitOutput::getFlag(const JSonElement *item, const JSonElement *selection) const
  734. {
  735. OutputFlag res;
  736. const JSonElement *i = dynamic_cast<const JSonObjectEntry*>(item) ? **((const JSonObjectEntry*)item) : item;
  737. res.selected(item == selection);
  738. res.searched(std::find(subWindows.at(selectedWin).searchResults.cbegin(),
  739. subWindows.at(selectedWin).searchResults.cend(),
  740. item) != subWindows.at(selectedWin).searchResults.cend());
  741. try {
  742. res.diffOp(diffMatrice->get(item));
  743. }
  744. catch (std::out_of_range &e) {
  745. res.diffOp(eLevenshteinOperator::add);
  746. }
  747. if (dynamic_cast<const JSonPrimitive<std::string> *>(i))
  748. res.type(OutputFlag::TYPE_STRING);
  749. else if (dynamic_cast<const JSonPrimitive<bool> *>(i))
  750. res.type(OutputFlag::TYPE_BOOL);
  751. else if (dynamic_cast<const JSonPrimitive<Null> *>(i))
  752. res.type(OutputFlag::TYPE_NULL);
  753. else if (dynamic_cast<const AJSonPrimitive *>(i))
  754. res.type(OutputFlag::TYPE_NUMBER);
  755. else if (dynamic_cast<const JSonObject*>(i))
  756. res.type(OutputFlag::TYPE_OBJ);
  757. else if (dynamic_cast<const JSonArray*>(i))
  758. res.type(OutputFlag::TYPE_ARR);
  759. return res;
  760. }