curseSplitOutput.cpp 29 KB

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