Procházet zdrojové kódy

[refactor][add] hasReachedBottom to unify and replace ternary condition
[bugfix] do not destroy window on screen resize, but resize it (causing segfault in curse#wgetch after SIGWINCH)
[bugfix][Refs #37] added win border in number-of-lines calculation in Split view (Fix longline issue)
[refactor][minor] removed unnecessaries if statements

B Thibault před 9 roky
rodič
revize
46e5cc53cf
4 změnil soubory, kde provedl 45 přidání a 33 odebrání
  1. 2 0
      include/curseOutput.hh
  2. 5 0
      src/curseOutput.cpp
  3. 15 10
      src/curseSimpleOutput.cpp
  4. 23 23
      src/curseSplitOutput.cpp

+ 2 - 0
include/curseOutput.hh

@@ -97,6 +97,8 @@ class CurseOutput
         virtual inputResult nextResult() =0;
         virtual inputResult changeWindow(char direction, bool cycle) =0;
 
+        virtual bool hasReachedBottom(unsigned int pos, unsigned int scrollTop, unsigned int maxHeight) const;
+
         /**
          * get the screen size
         **/

+ 5 - 0
src/curseOutput.cpp

@@ -175,6 +175,11 @@ unsigned int CurseOutput::getNbLines(const size_t nbChar, unsigned int maxWidth)
     return nLine +1;
 }
 
+bool CurseOutput::hasReachedBottom(unsigned int pos, unsigned int scrollTop, unsigned int height) const
+{
+    return (pos >= scrollTop && (pos -scrollTop) > height -1);
+}
+
 const t_Cursor CurseOutput::getScreenSizeUnsafe() const
 {
     t_Cursor bs;

+ 15 - 10
src/curseSimpleOutput.cpp

@@ -201,7 +201,7 @@ bool CurseSimpleOutput::redraw(t_Cursor &cursor, JSonElement *item)
     else
     {
         cursor.second += CurseOutput::write(cursor.first, cursor.second, item, screenSize.first, CurseSimpleOutput::getFlag(item));
-        if (cursor.second - scrollTop > 0 && (cursor.second - scrollTop) > screenSize.second -1)
+        if (hasReachedBottom(cursor.second, scrollTop, screenSize.second))
             return false;
     }
     return true;
@@ -225,13 +225,13 @@ bool CurseSimpleOutput::writeContainer(t_Cursor &cursor, const JSonContainer *it
     else
     {
         cursor.second += write(cursor.first, cursor.second, childDelimiter[0], screenSize.first, CurseSimpleOutput::getFlag(item));
-        if (cursor.second <= scrollTop && cursor.second > screenSize.second +scrollTop -1)
+        if (hasReachedBottom(cursor.second, scrollTop, screenSize.second))
                 return false;
         if (!writeContent(cursor, (std::list<JSonElement *> *)item))
             return false;
         cursor.second += write(cursor.first, cursor.second, childDelimiter[1], screenSize.first, CurseSimpleOutput::getFlag(item));
     }
-    return (cursor.second >= scrollTop || (cursor.second - scrollTop) <= screenSize.second -1);
+    return !hasReachedBottom(cursor.second, scrollTop, screenSize.second);
 }
 
 bool CurseSimpleOutput::writeContent(t_Cursor &cursor, std::list<JSonElement*> *_item)
@@ -254,26 +254,31 @@ bool CurseSimpleOutput::writeContent(t_Cursor &cursor, std::list<JSonElement*> *
             {
                 if (dynamic_cast<JSonObject *>(**ent))
                 {
-                    if (!writeKey(key, ent->lazystrlen(), "{ ... }", cursor, CurseSimpleOutput::getFlag(ent)) || (cursor.second - scrollTop > 0 && (cursor.second - scrollTop) > screenSize.second -1))
+                    if (!writeKey(key, ent->lazystrlen(), "{ ... }", cursor, CurseSimpleOutput::getFlag(ent)))
                         break;
                 }
-                else if (!writeKey(key, ent->lazystrlen(), "[ ... ]", cursor, CurseSimpleOutput::getFlag(ent)) || (cursor.second - scrollTop > 0 && (cursor.second - scrollTop) > screenSize.second -1))
+                else if (!writeKey(key, ent->lazystrlen(), "[ ... ]", cursor, CurseSimpleOutput::getFlag(ent)))
+                    break;
+                if (hasReachedBottom(cursor.second, scrollTop, screenSize.second))
                     break;
             }
             else if (!isContainer)
             {
                 JSonElement *eContent = **ent;
-                if (!writeKey(key, ent->lazystrlen(), eContent->stringify(), eContent->lazystrlen(), cursor, CurseSimpleOutput::getFlag(ent)) || (cursor.second - scrollTop > 0 && (cursor.second - scrollTop) > screenSize.second -1))
+                if (!writeKey(key, ent->lazystrlen(), eContent->stringify(), eContent->lazystrlen(), cursor, CurseSimpleOutput::getFlag(ent))
+                        || hasReachedBottom(cursor.second, scrollTop, screenSize.second))
                     break;
             }
             else if (((JSonContainer*)(**ent))->size() == 0)
             {
                 if (dynamic_cast<const JSonObject *>(**ent) )
                 {
-                    if (!writeKey(key, ent->lazystrlen(), "{ }", cursor, CurseSimpleOutput::getFlag(ent)) || (cursor.second - scrollTop > 0 && (cursor.second - scrollTop) > screenSize.second -1))
+                    if (!writeKey(key, ent->lazystrlen(), "{ }", cursor, CurseSimpleOutput::getFlag(ent)))
                         break;
                 }
-                else if (!writeKey(key, ent->lazystrlen(), "[ ]", cursor, CurseSimpleOutput::getFlag(ent)) || (cursor.second - scrollTop > 0 && (cursor.second - scrollTop) > screenSize.second -1))
+                else if (!writeKey(key, ent->lazystrlen(), "[ ]", cursor, CurseSimpleOutput::getFlag(ent)))
+                    break;
+                if (hasReachedBottom(cursor.second, scrollTop, screenSize.second))
                     break;
             }
             else
@@ -319,7 +324,7 @@ bool CurseSimpleOutput::writeKey(const std::string &key, const size_t keylen, t_
     flags.type(OutputFlag::TYPE_OBJ);
     write(": ", flags);
     flags.type(oldType);
-    return (cursor.second >= scrollTop || cursor.second <= screenSize.second +scrollTop);
+    return !hasReachedBottom(cursor.second, scrollTop, screenSize.second);
 }
 
 bool CurseSimpleOutput::writeKey(const std::string &key, const size_t keylen, const std::string &after, size_t afterlen, t_Cursor &cursor, OutputFlag flags)
@@ -337,7 +342,7 @@ bool CurseSimpleOutput::writeKey(const std::string &key, const size_t keylen, co
     flags.type(oldType);
     write(after.c_str(), flags);
     cursor.second += getNbLines(cursor.first +keylen +2 +afterlen, screenSize.first);
-    return (cursor.second >= scrollTop || (cursor.second - scrollTop) <= screenSize.second);
+    return !hasReachedBottom(cursor.second, scrollTop, screenSize.second);
 }
 
 bool CurseSimpleOutput::writeKey(const std::string &key, const size_t keylen, const std::string &after, t_Cursor &cursor, OutputFlag flags)

+ 23 - 23
src/curseSplitOutput.cpp

@@ -416,13 +416,12 @@ const Optional<bool> CurseSplitOutput::redrawOneItemToWorkingWin(t_subWindow &w)
 void CurseSplitOutput::onResizeHandler()
 {
     size_t i =0;
-    destroyAllSubWin();
     clear();
 
     for (t_subWindow &subwin: subWindows)
     {
-        subwin.outerWin = newwin(screenSize.second +2, screenSize.first, 0, i * (screenSize.first -1));
-        subwin.innerWin = newwin(screenSize.second, screenSize.first -2, 1, i * (screenSize.first -1) +1);
+        wresize(subwin.outerWin, screenSize.second +2, screenSize.first);
+        wresize(subwin.innerWin, screenSize.second, screenSize.first -2);
         box(subwin.outerWin, 0, 0);
         wrefresh(subwin.outerWin);
         ++i;
@@ -522,24 +521,24 @@ bool CurseSplitOutput::writeContainer(JSonContainer *item, bool opening)
     if (!opening) // Display close brackets
     {
         w.cursor.first -= INDENT_LEVEL;
-        w.cursor.second += write(w.cursor.first, w.cursor.second, childDelimiter[1], screenSize.first, CurseSplitOutput::getFlag(item));
+        w.cursor.second += write(w.cursor.first, w.cursor.second, childDelimiter[1], screenSize.first -2, CurseSplitOutput::getFlag(item));
     }
     else if (collapsed.find((const JSonContainer *)item) != collapsed.end()) // inline collapsed
     {
         std::string ss;
         ss.append(&childDelimiter[0], 1).append(" ... ").append(&childDelimiter[1], 1);
-        w.cursor.second += write(w.cursor.first, w.cursor.second, ss, 7, screenSize.first, CurseSplitOutput::getFlag(item));
+        w.cursor.second += write(w.cursor.first, w.cursor.second, ss, 7, screenSize.first -2, CurseSplitOutput::getFlag(item));
     }
     else // Display open brackets
     {
-        w.cursor.second += write(w.cursor.first, w.cursor.second, childDelimiter[0], screenSize.first, CurseSplitOutput::getFlag(item));
-        if (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > screenSize.second -1)
+        w.cursor.second += write(w.cursor.first, w.cursor.second, childDelimiter[0], screenSize.first -2, CurseSplitOutput::getFlag(item));
+        if (hasReachedBottom(w.cursor.second, w.scrollTop, screenSize.second))
             return false;
         w.parentsIterators.push(std::pair<int, JSonContainer *>(-1, item));
         w.cursor.first += INDENT_LEVEL;
         return true;
     }
-    return (w.cursor.second < w.scrollTop || (w.cursor.second - w.scrollTop) <= screenSize.second -1);
+    return !hasReachedBottom(w.cursor.second, w.scrollTop, screenSize.second);
 }
 
 bool CurseSplitOutput::writeKey(t_subWindow &w, const std::string &key, const size_t keylen, OutputFlag flags, unsigned int extraLen)
@@ -551,11 +550,11 @@ bool CurseSplitOutput::writeKey(t_subWindow &w, const std::string &key, const si
     }
     char oldType = flags.type();
     flags.type(OutputFlag::TYPE_OBJKEY);
-    w.cursor.second += write(w.cursor.first, w.cursor.second, key, keylen, screenSize.first -extraLen -2, flags);
+    w.cursor.second += write(w.cursor.first, w.cursor.second, key, keylen, screenSize.first -extraLen -2 -2, flags);
     flags.type(OutputFlag::TYPE_OBJ);
     write(": ", flags);
     flags.type(oldType);
-    return (w.cursor.second < w.scrollTop || (w.cursor.second - w.scrollTop) <= screenSize.second);
+    return !hasReachedBottom(w.cursor.second, w.scrollTop, screenSize.second);
 }
 
 bool CurseSplitOutput::writeKey(t_subWindow &w, const std::string &key, const size_t keylen, const std::string &after, const size_t afterlen, t_Cursor &cursor, OutputFlag flags)
@@ -572,8 +571,8 @@ bool CurseSplitOutput::writeKey(t_subWindow &w, const std::string &key, const si
     write(": ", flags);
     flags.type(oldType);
     write(after, flags);
-    cursor.second += getNbLines(cursor.first +keylen +2 +afterlen, screenSize.first);
-    return (cursor.second < w.scrollTop || (cursor.second - w.scrollTop) <= screenSize.second);
+    cursor.second += getNbLines(cursor.first +keylen +2 +afterlen, screenSize.first -2);
+    return !hasReachedBottom(w.cursor.second, w.scrollTop, screenSize.second);
 }
 
 bool CurseSplitOutput::writeKey(t_subWindow &w, const std::string &key, const size_t keylen, const std::string &after, t_Cursor &cursor, OutputFlag flags)
@@ -625,8 +624,8 @@ bool CurseSplitOutput::redraw(t_subWindow &w, JSonElement *item)
     }
     else
     {
-        w.cursor.second += CurseOutput::write(w.cursor.first, w.cursor.second, item, screenSize.first, CurseSplitOutput::getFlag(item));
-        if (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > screenSize.second -1)
+        w.cursor.second += CurseOutput::write(w.cursor.first, w.cursor.second, item, screenSize.first -2, CurseSplitOutput::getFlag(item));
+        if (hasReachedBottom(w.cursor.second, w.scrollTop, screenSize.second))
             return false;
     }
     return true;
@@ -641,27 +640,30 @@ bool CurseSplitOutput::writeObjectEntry(t_subWindow &w, JSonObjectEntry *ent)
     {
         if (dynamic_cast<JSonObject *>(**ent))
         {
-            if (!writeKey(w, key, ent->lazystrlen(), "{ ... }", w.cursor, getFlag(ent)) || (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > screenSize.second -1))
+            if (!writeKey(w, key, ent->lazystrlen(), "{ ... }", w.cursor, getFlag(ent)))
                 return false;
         }
-        else if (!writeKey(w, key, ent->lazystrlen(), "[ ... ]", w.cursor, getFlag(ent)) || (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > screenSize.second -1))
+        else if (!writeKey(w, key, ent->lazystrlen(), "[ ... ]", w.cursor, getFlag(ent)))
+            return false;
+        if (hasReachedBottom(w.cursor.second, w.scrollTop, screenSize.second))
             return false;
     }
     else if (!isContainer) // inline value
     {
         JSonElement *eContent = **ent;
-        if (!writeKey(w, key, ent->lazystrlen(), eContent->stringify(), eContent->lazystrlen(), w.cursor, getFlag(ent))
-                || (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > screenSize.second -1))
+        if (!writeKey(w, key, ent->lazystrlen(), eContent->stringify(), eContent->lazystrlen(), w.cursor, getFlag(ent)))
             return false;
     }
     else if (((JSonContainer*)(**ent))->size() == 0) // inline empty
     {
         if (dynamic_cast<const JSonObject *>(**ent) )
         {
-            if (!writeKey(w, key, ent->lazystrlen(), "{ }", w.cursor, getFlag(ent)) || (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > screenSize.second -1))
+            if (!writeKey(w, key, ent->lazystrlen(), "{ }", w.cursor, getFlag(ent)))
                 return false;
         }
-        else if (!writeKey(w, key, ent->lazystrlen(), "[ ]", w.cursor, getFlag(ent)) || (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > screenSize.second -1))
+        else if (!writeKey(w, key, ent->lazystrlen(), "[ ]", w.cursor, getFlag(ent)))
+            return false;
+        if (hasReachedBottom(w.cursor.second, w.scrollTop, screenSize.second))
             return false;
     }
     else // Container
@@ -670,9 +672,7 @@ bool CurseSplitOutput::writeObjectEntry(t_subWindow &w, JSonObjectEntry *ent)
             return false;
         writeContainer((JSonContainer*)(**ent), true);
     }
-    if (w.cursor.second > w.scrollTop && (w.cursor.second - w.scrollTop) > screenSize.second -1)
-        return false;
-    return true;
+    return !hasReachedBottom(w.cursor.second, w.scrollTop, screenSize.second);
 }
 
 unsigned int CurseSplitOutput::write(const int &x, const int &y, const char item, unsigned int maxWidth, OutputFlag flags)