curseSplitOutput.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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(w, screenSize, w.root);
  338. else
  339. result = redraw(w, screenSize, w.parentsIterators.top());
  340. }
  341. catch (SelectionOutOfRange &e)
  342. {
  343. return Optional<bool>::empty;
  344. }
  345. if (!result || w.parentsIterators.empty())
  346. {
  347. if (!result)
  348. {
  349. if (!w.selectFound)
  350. {
  351. w.scrollTop++;
  352. return Optional<bool>::empty;
  353. }
  354. if (!w.select_down)
  355. w.selectIsLast = true;
  356. }
  357. if (!w.select_down)
  358. {
  359. const JSonContainer *pselect = dynamic_cast<const JSonContainer*>(w.selection);
  360. if (pselect && !pselect->empty() && collapsed.find(pselect) == collapsed.cend())
  361. w.select_down = *(pselect->cbegin());
  362. else
  363. {
  364. const JSonElement *next = w.lastSelection->findNext();
  365. w.select_down = next ? next : w.lastSelection;
  366. }
  367. }
  368. if (!w.select_up)
  369. w.select_up = subWindows.at(workingWin).lastSelection;
  370. return Optional<bool>::of(true);
  371. }
  372. return Optional<bool>::of(false);
  373. }
  374. bool CurseSplitOutput::redraw()
  375. {
  376. const t_Cursor screenSize = getScreenSize();
  377. short writingDone = (1 << nbInputs) -1;
  378. workingWin = 0;
  379. for (t_subWindow &w : subWindows)
  380. {
  381. w.cursor = t_Cursor(2, 1);
  382. w.select_up = w.select_down = nullptr;
  383. w.selectFound = w.selectIsLast = false;
  384. wclear(w.innerWin);
  385. writeTopLine(w.fileName,
  386. workingWin == selectedWin ? OutputFlag::SPECIAL_ACTIVEINPUTNAME : OutputFlag::SPECIAL_INPUTNAME);
  387. w.parentsIterators = std::stack<std::pair<int, JSonContainer*> >();
  388. ++workingWin;
  389. }
  390. while (writingDone)
  391. {
  392. // Display Gap (--)
  393. bool restart = false;
  394. workingWin = 0;
  395. for (t_subWindow &w : subWindows)
  396. {
  397. if ((writingDone & (1 << workingWin)) &&
  398. ((!w.parentsIterators.empty() && isAdded(w.parentsIterators.top())) ||
  399. (w.parentsIterators.empty() && isAdded(w.root))))
  400. {
  401. const unsigned int startY = w.cursor.second;
  402. do
  403. {
  404. const Optional<bool> wrote = redrawOneItemToWorkingWin(w, screenSize);
  405. if (wrote.absent())
  406. return false;
  407. if (wrote.get())
  408. {
  409. writingDone &= ~(1 << workingWin);
  410. break;
  411. }
  412. } while (isAdded(w.parentsIterators.top()));
  413. const unsigned int diffY = w.cursor.second - startY;
  414. unsigned int i = 0;
  415. for (t_subWindow &wi: subWindows)
  416. if (i++ != workingWin)
  417. for (unsigned int j = 0; j < diffY; ++j)
  418. displayDiffOp(wi.innerWin, (wi.cursor.second)++, eLevenshteinOperator::rem);
  419. restart = true;
  420. break;
  421. }
  422. ++workingWin;
  423. }
  424. if (restart)
  425. continue;
  426. // Actual display
  427. workingWin = 0;
  428. for (t_subWindow &w : subWindows)
  429. {
  430. if ((writingDone & (1 << workingWin)))
  431. {
  432. const Optional<bool> wrote = redrawOneItemToWorkingWin(w, screenSize);
  433. if (wrote.absent())
  434. return false;
  435. if (wrote.get())
  436. writingDone &= ~(1 << workingWin);
  437. }
  438. ++workingWin;
  439. }
  440. }
  441. for (t_subWindow &w : subWindows)
  442. wrefresh(w.innerWin);
  443. return true;
  444. }
  445. bool CurseSplitOutput::writeContainer(const t_Cursor &maxSize, JSonContainer *item, bool opening)
  446. {
  447. char childDelimiter[2];
  448. t_subWindow &w = subWindows.at(workingWin);
  449. if (dynamic_cast<const JSonObject *>(item))
  450. memcpy(childDelimiter, "{}", sizeof(*childDelimiter) * 2);
  451. else
  452. memcpy(childDelimiter, "[]", sizeof(*childDelimiter) * 2);
  453. if (!opening)
  454. {
  455. w.cursor.first -= INDENT_LEVEL;
  456. w.cursor.second += write(w.cursor.first, w.cursor.second, childDelimiter[1], maxSize.first, CurseSplitOutput::getFlag(item));
  457. }
  458. else if (collapsed.find((const JSonContainer *)item) != collapsed.end())
  459. {
  460. std::string ss;
  461. ss.append(&childDelimiter[0], 1).append(" ... ").append(&childDelimiter[1], 1);
  462. w.cursor.second += write(w.cursor.first, w.cursor.second, ss, 7, maxSize.first, CurseSplitOutput::getFlag(item));
  463. }
  464. else
  465. {
  466. w.cursor.second += write(w.cursor.first, w.cursor.second, childDelimiter[0], maxSize.first, CurseSplitOutput::getFlag(item));
  467. if (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > maxSize.second -1)
  468. return false;
  469. w.parentsIterators.push(std::pair<int, JSonContainer *>(-1, item));
  470. w.cursor.first += INDENT_LEVEL;
  471. return true;
  472. }
  473. return (w.cursor.second < w.scrollTop || (w.cursor.second - w.scrollTop) <= maxSize.second -1);
  474. }
  475. bool CurseSplitOutput::writeKey(const std::string &key, const size_t keylen, const t_Cursor &maxSize, OutputFlag flags, unsigned int extraLen)
  476. {
  477. t_subWindow &w = subWindows.at(workingWin);
  478. if (w.cursor.second <= w.scrollTop)
  479. {
  480. w.cursor.second++;
  481. return true;
  482. }
  483. char oldType = flags.type();
  484. flags.type(OutputFlag::TYPE_OBJKEY);
  485. w.cursor.second += write(w.cursor.first, w.cursor.second, key, keylen, maxSize.first -extraLen -2, flags);
  486. flags.type(OutputFlag::TYPE_OBJ);
  487. write(": ", flags);
  488. flags.type(oldType);
  489. return (w.cursor.second < w.scrollTop || (w.cursor.second - w.scrollTop) <= maxSize.second);
  490. }
  491. 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)
  492. {
  493. t_subWindow &w = subWindows.at(workingWin);
  494. if (cursor.second <= w.scrollTop)
  495. {
  496. cursor.second++;
  497. return true;
  498. }
  499. char oldType = flags.type();
  500. flags.type(OutputFlag::TYPE_OBJKEY);
  501. write(cursor.first, cursor.second, key, 0, 1, flags);
  502. flags.type(OutputFlag::TYPE_OBJ);
  503. write(": ", flags);
  504. flags.type(oldType);
  505. write(after, flags);
  506. cursor.second += getNbLines(cursor.first +keylen +2 +afterlen, maxWidth.first);
  507. return (cursor.second < w.scrollTop || (cursor.second - w.scrollTop) <= maxWidth.second);
  508. }
  509. bool CurseSplitOutput::isAdded(const JSonElement *e) const
  510. {
  511. return diffMatrice->getEquivalence(e) == nullptr;
  512. }
  513. bool CurseSplitOutput::isAdded(const std::pair<int, JSonContainer *> &item) const
  514. {
  515. const JSonElement *e;
  516. if ((unsigned int) (item.first +1) >= item.second->size())
  517. e = item.second;
  518. else
  519. e = list_at<JSonElement*>(*(item.second), item.first +1);
  520. return isAdded(e);
  521. }
  522. bool CurseSplitOutput::redraw(t_subWindow &w, const t_Cursor &maxSize, std::pair<int, JSonContainer *> &item)
  523. {
  524. JSonElement *currentItem;
  525. (item.first)++;
  526. if ((unsigned int) item.first == item.second->size())
  527. {
  528. w.parentsIterators.pop();
  529. return writeContainer(maxSize, item.second, false);
  530. }
  531. currentItem = list_at<JSonElement*>(*(item.second), item.first);
  532. return redraw(w, maxSize, currentItem);
  533. }
  534. bool CurseSplitOutput::redraw(t_subWindow &w, const t_Cursor &maxSize, JSonElement *item)
  535. {
  536. checkSelection(item);
  537. if (dynamic_cast<const JSonContainer*>(item))
  538. {
  539. if (!writeContainer(maxSize, (JSonContainer *) item))
  540. return false;
  541. }
  542. else
  543. {
  544. w.cursor.second += CurseOutput::write(w.cursor.first, w.cursor.second, item, maxSize.first, CurseSplitOutput::getFlag(item));
  545. if (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > maxSize.second -1)
  546. return false;
  547. }
  548. return true;
  549. }
  550. unsigned int CurseSplitOutput::write(const int &x, const int &y, const char item, unsigned int maxWidth, OutputFlag flags)
  551. {
  552. int offsetY = y - subWindows.at(workingWin).scrollTop;
  553. WINDOW *currentWin = subWindows.at(workingWin).innerWin;
  554. char color = OutputFlag::SPECIAL_NONE;
  555. if (offsetY <= 0)
  556. return 1;
  557. if (flags.selected())
  558. wattron(currentWin, A_REVERSE | A_BOLD);
  559. if (flags.searched())
  560. color = OutputFlag::SPECIAL_SEARCH;
  561. else if (colors.find(flags.type()) != colors.end())
  562. color = flags.type();
  563. if (color != OutputFlag::SPECIAL_NONE)
  564. wattron(currentWin, COLOR_PAIR(color));
  565. mvwprintw(currentWin, offsetY, x, "%c", item);
  566. wattroff(currentWin, A_REVERSE | A_BOLD);
  567. if (color != OutputFlag::SPECIAL_NONE)
  568. wattroff(currentWin, COLOR_PAIR(color));
  569. displayDiffOp(currentWin, offsetY, flags.diffOp());
  570. return getNbLines(x +1, maxWidth);
  571. }
  572. void CurseSplitOutput::displayDiffOp(WINDOW *w, const int &y, const eLevenshteinOperator &op) const
  573. {
  574. switch (op)
  575. {
  576. case eLevenshteinOperator::add:
  577. wattron(w, A_REVERSE | A_BOLD);
  578. wattron(w, COLOR_PAIR(OutputFlag::DIFF_ADD));
  579. mvwprintw(w, y, 0, "++");
  580. wattroff(w, COLOR_PAIR(OutputFlag::DIFF_ADD));
  581. wattroff(w, A_REVERSE | A_BOLD);
  582. break;
  583. case eLevenshteinOperator::mod:
  584. wattron(w, A_REVERSE | A_BOLD);
  585. wattron(w, COLOR_PAIR(OutputFlag::DIFF_MOD));
  586. mvwprintw(w, y, 0, "!!");
  587. wattroff(w, COLOR_PAIR(OutputFlag::DIFF_MOD));
  588. wattroff(w, A_REVERSE | A_BOLD);
  589. break;
  590. case eLevenshteinOperator::rem:
  591. wattron(w, A_REVERSE | A_BOLD);
  592. wattron(w, COLOR_PAIR(OutputFlag::DIFF_REM));
  593. mvwprintw(w, y, 0, "--");
  594. wattroff(w, COLOR_PAIR(OutputFlag::DIFF_REM));
  595. wattroff(w, A_REVERSE | A_BOLD);
  596. break;
  597. case eLevenshteinOperator::equ: // skip
  598. break;
  599. }
  600. }
  601. unsigned int CurseSplitOutput::write(const int &x, const int &y, const std::string &str, const size_t strlen, unsigned int maxWidth, const OutputFlag flags)
  602. {
  603. const t_subWindow &w = subWindows.at(workingWin);
  604. WINDOW *currentWin = w.innerWin;
  605. int offsetY = y - w.scrollTop;
  606. if (offsetY <= 0)
  607. return 1;
  608. displayDiffOp(currentWin, y, flags.diffOp());
  609. wmove(subWindows.at(workingWin).innerWin, offsetY, x);
  610. write(str, flags);
  611. return getNbLines(strlen +x, maxWidth);
  612. }
  613. void CurseSplitOutput::write(const std::string &str, const OutputFlag flags) const
  614. {
  615. char color = OutputFlag::SPECIAL_NONE;
  616. WINDOW *currentWin = subWindows.at(workingWin).innerWin;
  617. if (flags.selected())
  618. wattron(currentWin, A_REVERSE | A_BOLD);
  619. if (flags.searched())
  620. color = OutputFlag::SPECIAL_SEARCH;
  621. else if (colors.find(flags.type()) != colors.end())
  622. color = flags.type();
  623. if (color != OutputFlag::SPECIAL_NONE)
  624. wattron(currentWin, COLOR_PAIR(color));
  625. wprintw(currentWin, "%s", str.c_str());
  626. wattroff(currentWin, A_REVERSE | A_BOLD);
  627. if (color != OutputFlag::SPECIAL_NONE)
  628. wattroff(currentWin, COLOR_PAIR(color));
  629. }
  630. void CurseSplitOutput::destroyAllSubWin()
  631. {
  632. for (t_subWindow &w : subWindows)
  633. {
  634. if (w.outerWin)
  635. {
  636. wborder(w.outerWin, ' ', ' ', ' ',' ',' ',' ',' ',' ');
  637. wrefresh(w.outerWin);
  638. delwin(w.outerWin);
  639. w.outerWin = nullptr;
  640. }
  641. if (w.innerWin)
  642. {
  643. delwin(w.innerWin);
  644. w.innerWin = nullptr;
  645. }
  646. }
  647. }
  648. void CurseSplitOutput::writeTopLine(const std::string &buffer, short color) const
  649. {
  650. const t_Cursor screenSize = getScreenSize();
  651. const size_t bufsize = buffer.size();
  652. WINDOW *currentWin = subWindows.at(workingWin).innerWin;
  653. if (params.colorEnabled())
  654. wattron(currentWin, COLOR_PAIR(color));
  655. mvwprintw(currentWin, 0, 0, "%s%*c", buffer.c_str(), screenSize.first - bufsize -2, ' ');
  656. if (params.colorEnabled())
  657. wattroff(currentWin, COLOR_PAIR(color));
  658. }
  659. const t_Cursor CurseSplitOutput::getScreenSize() const
  660. {
  661. t_Cursor result = getScreenSizeUnsafe();
  662. return t_Cursor(result.first / nbInputs, result.second -2);
  663. }
  664. void CurseSplitOutput::shutdown()
  665. {
  666. destroyAllSubWin();
  667. endwin();
  668. delscreen(screen);
  669. if (screen_fd)
  670. {
  671. fclose(screen_fd);
  672. screen_fd = nullptr;
  673. }
  674. screen = nullptr;
  675. }
  676. const OutputFlag CurseSplitOutput::getFlag(const JSonElement *e) const
  677. {
  678. return getFlag(e, subWindows.at(workingWin).selection);
  679. }
  680. const OutputFlag CurseSplitOutput::getFlag(const JSonElement *item, const JSonElement *selection) const
  681. {
  682. OutputFlag res;
  683. const JSonElement *i = dynamic_cast<const JSonObjectEntry*>(item) ? **((const JSonObjectEntry*)item) : item;
  684. res.selected(item == selection);
  685. res.searched(std::find(subWindows.at(selectedWin).searchResults.cbegin(),
  686. subWindows.at(selectedWin).searchResults.cend(),
  687. item) != subWindows.at(selectedWin).searchResults.cend());
  688. try {
  689. res.diffOp(diffMatrice->get(item));
  690. }
  691. catch (std::out_of_range &e) {
  692. res.diffOp(eLevenshteinOperator::add);
  693. }
  694. if (dynamic_cast<const JSonPrimitive<std::string> *>(i))
  695. res.type(OutputFlag::TYPE_STRING);
  696. else if (dynamic_cast<const JSonPrimitive<bool> *>(i))
  697. res.type(OutputFlag::TYPE_BOOL);
  698. else if (dynamic_cast<const JSonPrimitive<Null> *>(i))
  699. res.type(OutputFlag::TYPE_NULL);
  700. else if (dynamic_cast<const AJSonPrimitive *>(i))
  701. res.type(OutputFlag::TYPE_NUMBER);
  702. else if (dynamic_cast<const JSonObject*>(i))
  703. res.type(OutputFlag::TYPE_OBJ);
  704. else if (dynamic_cast<const JSonArray*>(i))
  705. res.type(OutputFlag::TYPE_ARR);
  706. return res;
  707. }