curseSplitOutput.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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())
  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. bool restart = false;
  397. workingWin = 0;
  398. for (t_subWindow &w : subWindows)
  399. {
  400. if ((writingDone & (1 << workingWin)) &&
  401. ((!w.parentsIterators.empty() && isAdded(w.parentsIterators.top())) ||
  402. (w.parentsIterators.empty() && isAdded(w.root))))
  403. {
  404. const unsigned int startY = w.cursor.second;
  405. do
  406. {
  407. const Optional<bool> wrote = redrawOneItemToWorkingWin(w, screenSize);
  408. if (wrote.absent())
  409. return false;
  410. if (wrote.get())
  411. {
  412. writingDone &= ~(1 << workingWin);
  413. break;
  414. }
  415. } while (isAdded(w.parentsIterators.top()));
  416. const unsigned int diffY = w.cursor.second - startY;
  417. unsigned int i = 0;
  418. for (t_subWindow &wi: subWindows)
  419. if (i++ != workingWin)
  420. wi.cursor.second += diffY;
  421. restart = true;
  422. break;
  423. }
  424. ++workingWin;
  425. }
  426. if (restart)
  427. continue;
  428. workingWin = 0;
  429. for (t_subWindow &w : subWindows)
  430. {
  431. if ((writingDone & (1 << workingWin)))
  432. {
  433. const Optional<bool> wrote = redrawOneItemToWorkingWin(w, screenSize);
  434. if (wrote.absent())
  435. return false;
  436. if (wrote.get())
  437. writingDone &= ~(1 << workingWin);
  438. }
  439. ++workingWin;
  440. }
  441. }
  442. for (t_subWindow &w : subWindows)
  443. wrefresh(w.innerWin);
  444. return true;
  445. }
  446. bool CurseSplitOutput::writeContainer(const t_Cursor &maxSize, JSonContainer *item, bool opening)
  447. {
  448. char childDelimiter[2];
  449. t_subWindow &w = subWindows.at(workingWin);
  450. if (dynamic_cast<const JSonObject *>(item))
  451. memcpy(childDelimiter, "{}", sizeof(*childDelimiter) * 2);
  452. else
  453. memcpy(childDelimiter, "[]", sizeof(*childDelimiter) * 2);
  454. if (!opening)
  455. {
  456. w.cursor.first -= INDENT_LEVEL;
  457. w.cursor.second += write(w.cursor.first, w.cursor.second, childDelimiter[1], maxSize.first, CurseSplitOutput::getFlag(item));
  458. }
  459. else if (collapsed.find((const JSonContainer *)item) != collapsed.end())
  460. {
  461. std::string ss;
  462. ss.append(&childDelimiter[0], 1).append(" ... ").append(&childDelimiter[1], 1);
  463. w.cursor.second += write(w.cursor.first, w.cursor.second, ss, 7, maxSize.first, CurseSplitOutput::getFlag(item));
  464. }
  465. else
  466. {
  467. w.cursor.second += write(w.cursor.first, w.cursor.second, childDelimiter[0], maxSize.first, CurseSplitOutput::getFlag(item));
  468. if (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > maxSize.second -1)
  469. return false;
  470. w.parentsIterators.push(std::pair<int, JSonContainer *>(-1, item));
  471. if (!writeContent(maxSize, (std::list<JSonElement *> *)item))
  472. {
  473. w.parentsIterators.pop();
  474. return false;
  475. }
  476. w.parentsIterators.pop();
  477. w.cursor.second += write(w.cursor.first, w.cursor.second, childDelimiter[1], maxSize.first, CurseSplitOutput::getFlag(item));
  478. }
  479. return (w.cursor.second < w.scrollTop || (w.cursor.second - w.scrollTop) <= maxSize.second -1);
  480. }
  481. bool CurseSplitOutput::writeContent(const t_Cursor &maxSize, std::list<JSonElement*> *_item)
  482. {
  483. t_subWindow &w = subWindows.at(workingWin);
  484. JSonContainer *item = (JSonContainer *)_item;
  485. bool containerIsObject = (dynamic_cast<JSonObject *>(item) != nullptr);
  486. bool result = true;
  487. w.cursor.first += INDENT_LEVEL;
  488. const unsigned int scrollTop = w.scrollTop;
  489. for (JSonElement *i : *item)
  490. {
  491. result = false;
  492. if (containerIsObject)
  493. {
  494. JSonObjectEntry *ent = (JSonObjectEntry*) i;
  495. bool isContainer = (dynamic_cast<JSonContainer *>(**ent) != nullptr);
  496. std::string key = ent->stringify();
  497. checkSelection(ent);
  498. if (isContainer && collapsed.find((JSonContainer*)(**ent)) != collapsed.cend())
  499. {
  500. if (dynamic_cast<JSonObject *>(**ent))
  501. {
  502. if (!CurseOutput::writeKey(key, ent->lazystrlen(), "{ ... }", w.cursor, maxSize, CurseSplitOutput::getFlag(ent)) || (w.cursor.second > scrollTop && (w.cursor.second - scrollTop) > maxSize.second -1))
  503. break;
  504. }
  505. else 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 (!isContainer)
  509. {
  510. JSonElement *eContent = **ent;
  511. 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))
  512. break;
  513. }
  514. else if (((JSonContainer*)(**ent))->size() == 0)
  515. {
  516. if (dynamic_cast<const JSonObject *>(**ent) )
  517. {
  518. if (!CurseOutput::writeKey(key, ent->lazystrlen(), "{ }", w.cursor, maxSize, CurseSplitOutput::getFlag(ent)) || (w.cursor.second > scrollTop && (w.cursor.second - scrollTop) > maxSize.second -1))
  519. break;
  520. }
  521. else 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
  525. {
  526. if (!writeKey(key, ent->lazystrlen(), maxSize, getFlag(ent)))
  527. break;
  528. const JSonElement *saveSelection = w.lastSelection;
  529. if (saveSelection == ent)
  530. w.selection = w.lastSelection = **ent;
  531. w.cursor.first += INDENT_LEVEL /2;
  532. throw CurseSplitOutput::reachNext();
  533. }
  534. }
  535. else
  536. throw CurseSplitOutput::reachNext();
  537. result = true;
  538. }
  539. w.cursor.first -= INDENT_LEVEL;
  540. //result will be false if for loop break'd at some time, true otherwise
  541. return result;
  542. }
  543. bool CurseSplitOutput::writeKey(const std::string &key, const size_t keylen, const t_Cursor &maxSize, OutputFlag flags, unsigned int extraLen)
  544. {
  545. t_subWindow &w = subWindows.at(workingWin);
  546. if (w.cursor.second <= w.scrollTop)
  547. {
  548. w.cursor.second++;
  549. return true;
  550. }
  551. char oldType = flags.type();
  552. flags.type(OutputFlag::TYPE_OBJKEY);
  553. w.cursor.second += write(w.cursor.first, w.cursor.second, key, keylen, maxSize.first -extraLen -2, flags);
  554. flags.type(OutputFlag::TYPE_OBJ);
  555. write(": ", flags);
  556. flags.type(oldType);
  557. return (w.cursor.second < w.scrollTop || (w.cursor.second - w.scrollTop) <= maxSize.second);
  558. }
  559. 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)
  560. {
  561. t_subWindow &w = subWindows.at(workingWin);
  562. if (cursor.second <= w.scrollTop)
  563. {
  564. cursor.second++;
  565. return true;
  566. }
  567. char oldType = flags.type();
  568. flags.type(OutputFlag::TYPE_OBJKEY);
  569. write(cursor.first, cursor.second, key, 0, 1, flags);
  570. flags.type(OutputFlag::TYPE_OBJ);
  571. write(": ", flags);
  572. flags.type(oldType);
  573. write(after, flags);
  574. cursor.second += getNbLines(cursor.first +keylen +2 +afterlen, maxWidth.first);
  575. return (cursor.second < w.scrollTop || (cursor.second - w.scrollTop) <= maxWidth.second);
  576. }
  577. bool CurseSplitOutput::isAdded(const JSonElement *e) const
  578. {
  579. return diffMatrice->getEquivalence(e) == nullptr;
  580. }
  581. bool CurseSplitOutput::isAdded(const std::pair<int, JSonContainer *> &item) const
  582. {
  583. const JSonElement *e;
  584. if ((unsigned int) (item.first +1) == item.second->size())
  585. e = item.second;
  586. e = list_at<JSonElement*>(*(item.second), item.first +1);
  587. return isAdded(e);
  588. }
  589. bool CurseSplitOutput::redraw(const t_Cursor &maxSize, std::pair<int, JSonContainer *> &item)
  590. {
  591. t_subWindow &w = subWindows.at(workingWin);
  592. JSonElement *currentItem;
  593. (item.first)++;
  594. if ((unsigned int) item.first == item.second->size())
  595. {
  596. w.parentsIterators.pop();
  597. if (!writeContainer(maxSize, item.second, false))
  598. return false;
  599. throw CurseSplitOutput::reachNext();
  600. }
  601. currentItem = list_at<JSonElement*>(*(item.second), item.first);
  602. checkSelection(currentItem);
  603. if (dynamic_cast<const JSonContainer*>(currentItem))
  604. {
  605. if (!writeContainer(maxSize, (JSonContainer*) currentItem))
  606. return false;
  607. }
  608. else
  609. {
  610. w.cursor.second += CurseOutput::write(w.cursor.first, w.cursor.second, currentItem, maxSize.first, CurseSplitOutput::getFlag(currentItem));
  611. if (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > maxSize.second -1)
  612. return false;
  613. }
  614. return true;
  615. }
  616. bool CurseSplitOutput::redraw(const t_Cursor &maxSize, JSonElement *item)
  617. {
  618. checkSelection(item);
  619. if (dynamic_cast<const JSonContainer*>(item))
  620. {
  621. if (!writeContainer(maxSize, (JSonContainer *) item))
  622. return false;
  623. }
  624. else
  625. {
  626. t_subWindow &w = subWindows.at(workingWin);
  627. w.cursor.second += CurseOutput::write(w.cursor.first, w.cursor.second, item, maxSize.first, CurseSplitOutput::getFlag(item));
  628. if (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > maxSize.second -1)
  629. return false;
  630. }
  631. return true;
  632. }
  633. unsigned int CurseSplitOutput::write(const int &x, const int &y, const char item, unsigned int maxWidth, OutputFlag flags)
  634. {
  635. int offsetY = y - subWindows.at(workingWin).scrollTop;
  636. WINDOW *currentWin = subWindows.at(workingWin).innerWin;
  637. char color = OutputFlag::SPECIAL_NONE;
  638. if (offsetY <= 0)
  639. return 1;
  640. if (flags.selected())
  641. wattron(currentWin, A_REVERSE | A_BOLD);
  642. if (flags.searched())
  643. color = OutputFlag::SPECIAL_SEARCH;
  644. else if (colors.find(flags.type()) != colors.end())
  645. color = flags.type();
  646. if (color != OutputFlag::SPECIAL_NONE)
  647. wattron(currentWin, COLOR_PAIR(color));
  648. mvwprintw(currentWin, offsetY, x, "%c", item);
  649. wattroff(currentWin, A_REVERSE | A_BOLD);
  650. if (color != OutputFlag::SPECIAL_NONE)
  651. wattroff(currentWin, COLOR_PAIR(color));
  652. switch (flags.diffOp())
  653. {
  654. case eLevenshteinOperator::equ:
  655. mvwprintw(currentWin, offsetY, 0, "=");
  656. break;
  657. case eLevenshteinOperator::add:
  658. mvwprintw(currentWin, offsetY, 0, "+");
  659. break;
  660. case eLevenshteinOperator::mod:
  661. mvwprintw(currentWin, offsetY, 0, "!");
  662. break;
  663. case eLevenshteinOperator::rem:
  664. // little strange
  665. mvwprintw(currentWin, offsetY, 0, "-");
  666. break;
  667. default:
  668. // very fucking strange
  669. mvwprintw(currentWin, offsetY, 0, "?");
  670. break;
  671. }
  672. return getNbLines(x +1, maxWidth);
  673. }
  674. unsigned int CurseSplitOutput::write(const int &x, const int &y, const std::string &str, const size_t strlen, unsigned int maxWidth, const OutputFlag flags)
  675. {
  676. const t_subWindow &w = subWindows.at(workingWin);
  677. WINDOW *currentWin = w.innerWin;
  678. int offsetY = y - w.scrollTop;
  679. if (offsetY <= 0)
  680. return 1;
  681. switch (flags.diffOp())
  682. {
  683. case eLevenshteinOperator::equ:
  684. mvwprintw(currentWin, offsetY, 0, "=");
  685. break;
  686. case eLevenshteinOperator::add:
  687. mvwprintw(currentWin, offsetY, 0, "+");
  688. break;
  689. case eLevenshteinOperator::mod:
  690. mvwprintw(currentWin, offsetY, 0, "!");
  691. break;
  692. case eLevenshteinOperator::rem:
  693. // little strange
  694. mvwprintw(currentWin, offsetY, 0, "-");
  695. break;
  696. default:
  697. // very fucking strange
  698. mvwprintw(currentWin, offsetY, 0, "?");
  699. break;
  700. }
  701. wmove(subWindows.at(workingWin).innerWin, offsetY, x);
  702. write(str, flags);
  703. return getNbLines(strlen +x, maxWidth);
  704. }
  705. void CurseSplitOutput::write(const std::string &str, const OutputFlag flags) const
  706. {
  707. char color = OutputFlag::SPECIAL_NONE;
  708. WINDOW *currentWin = subWindows.at(workingWin).innerWin;
  709. if (flags.selected())
  710. wattron(currentWin, A_REVERSE | A_BOLD);
  711. if (flags.searched())
  712. color = OutputFlag::SPECIAL_SEARCH;
  713. else if (colors.find(flags.type()) != colors.end())
  714. color = flags.type();
  715. if (color != OutputFlag::SPECIAL_NONE)
  716. wattron(currentWin, COLOR_PAIR(color));
  717. wprintw(currentWin, "%s", str.c_str());
  718. wattroff(currentWin, A_REVERSE | A_BOLD);
  719. if (color != OutputFlag::SPECIAL_NONE)
  720. wattroff(currentWin, COLOR_PAIR(color));
  721. }
  722. void CurseSplitOutput::destroyAllSubWin()
  723. {
  724. for (t_subWindow &w : subWindows)
  725. {
  726. if (w.outerWin)
  727. {
  728. wborder(w.outerWin, ' ', ' ', ' ',' ',' ',' ',' ',' ');
  729. wrefresh(w.outerWin);
  730. delwin(w.outerWin);
  731. w.outerWin = nullptr;
  732. }
  733. if (w.innerWin)
  734. {
  735. delwin(w.innerWin);
  736. w.innerWin = nullptr;
  737. }
  738. }
  739. }
  740. void CurseSplitOutput::writeTopLine(const std::string &buffer, short color) const
  741. {
  742. const t_Cursor screenSize = getScreenSize();
  743. const size_t bufsize = buffer.size();
  744. WINDOW *currentWin = subWindows.at(workingWin).innerWin;
  745. if (params.colorEnabled())
  746. wattron(currentWin, COLOR_PAIR(color));
  747. mvwprintw(currentWin, 0, 0, "%s%*c", buffer.c_str(), screenSize.first - bufsize -2, ' ');
  748. if (params.colorEnabled())
  749. wattroff(currentWin, COLOR_PAIR(color));
  750. }
  751. const t_Cursor CurseSplitOutput::getScreenSize() const
  752. {
  753. t_Cursor result = getScreenSizeUnsafe();
  754. return t_Cursor(result.first / nbInputs, result.second -2);
  755. }
  756. void CurseSplitOutput::shutdown()
  757. {
  758. destroyAllSubWin();
  759. endwin();
  760. delscreen(screen);
  761. if (screen_fd)
  762. {
  763. fclose(screen_fd);
  764. screen_fd = nullptr;
  765. }
  766. screen = nullptr;
  767. }
  768. const OutputFlag CurseSplitOutput::getFlag(const JSonElement *e) const
  769. {
  770. return getFlag(e, subWindows.at(workingWin).selection);
  771. }
  772. const OutputFlag CurseSplitOutput::getFlag(const JSonElement *item, const JSonElement *selection) const
  773. {
  774. OutputFlag res;
  775. const JSonElement *i = dynamic_cast<const JSonObjectEntry*>(item) ? **((const JSonObjectEntry*)item) : item;
  776. res.selected(item == selection);
  777. res.searched(std::find(subWindows.at(selectedWin).searchResults.cbegin(),
  778. subWindows.at(selectedWin).searchResults.cend(),
  779. item) != subWindows.at(selectedWin).searchResults.cend());
  780. try {
  781. res.diffOp(diffMatrice->get(item));
  782. }
  783. catch (std::out_of_range &e) {
  784. res.diffOp(eLevenshteinOperator::add);
  785. }
  786. if (dynamic_cast<const JSonPrimitive<std::string> *>(i))
  787. res.type(OutputFlag::TYPE_STRING);
  788. else if (dynamic_cast<const JSonPrimitive<bool> *>(i))
  789. res.type(OutputFlag::TYPE_BOOL);
  790. else if (dynamic_cast<const JSonPrimitive<Null> *>(i))
  791. res.type(OutputFlag::TYPE_NULL);
  792. else if (dynamic_cast<const AJSonPrimitive *>(i))
  793. res.type(OutputFlag::TYPE_NUMBER);
  794. else if (dynamic_cast<const JSonObject*>(i))
  795. res.type(OutputFlag::TYPE_OBJ);
  796. else if (dynamic_cast<const JSonArray*>(i))
  797. res.type(OutputFlag::TYPE_ARR);
  798. return res;
  799. }