Prechádzať zdrojové kódy

[refactor] screenSize now unsigned
[add] multi-line mode for long strings
[add] some test files

isundil 9 rokov pred
rodič
commit
c648a0e85e
6 zmenil súbory, kde vykonal 143 pridanie a 40 odobranie
  1. 11 10
      include/curseOutput.hh
  2. 40 30
      src/curseOutput.cpp
  3. 1 0
      test/longline.json
  4. 1 0
      test/longline2.json
  5. 89 0
      test/test.json
  6. 1 0
      test/test2.json

+ 11 - 10
include/curseOutput.hh

@@ -31,20 +31,21 @@ class CurseOutput
          * return false if bottom of screen is touched
         **/
         bool redraw();
-        bool redraw(std::pair<int, int> &, const std::pair<int, int>&, const JSonElement *, const JSonContainer *);
+        bool redraw(std::pair<int, int> &, const std::pair<unsigned int, unsigned int> &maxWidth, const JSonElement *, const JSonContainer *);
         bool readInput();
-        void getScreenSize(std::pair<int, int> &, std::pair<int, int> &);
+        void getScreenSize(std::pair<unsigned int, unsigned int> &, std::pair<int, int> &) const;
         static CurseOutput::t_nextKey findPrev(const JSonElement *);
         static CurseOutput::t_nextKey findNext(const JSonElement *);
         void checkSelection(const JSonElement *item, const JSonElement *parent, const std::pair<int, int>&);
-        void write(const int &x, const int &y, const JSonElement *item, bool selected =false);
-        void write(const int &x, const int &y, const std::string &item, bool selected =false);
-        void write(const int &x, const int &y, const char item, bool selected =false);
-        void write(const int &x, const int &y, const char *item, bool selected =false);
-        bool writeKey(const std::string &key, std::pair<int, int> &cursor, const int maxHeight, bool selected);
-        bool writeContainer(std::pair<int, int> &, const std::pair<int, int>&, const JSonContainer *);
-        bool writeContent(std::pair<int, int> &cursor, const std::pair<int, int> &maxSize, const JSonArray * obj);
-        bool writeContent(std::pair<int, int> &cursor, const std::pair<int, int> &maxSize, const JSonObject * obj);
+        static unsigned int getNbLines(float nbChar, unsigned int maxWidth);
+        unsigned int write(const int &x, const int &y, const JSonElement *item, unsigned int maxWidth, bool selected =false);
+        unsigned int write(const int &x, const int &y, const std::string &item, unsigned int maxWidth, bool selected =false);
+        unsigned int write(const int &x, const int &y, const char item, unsigned int maxWidth, bool selected =false);
+        unsigned int write(const int &x, const int &y, const char *item, unsigned int maxWidth, bool selected =false);
+        bool writeKey(const std::string &key, std::pair<int, int> &cursor, const std::pair<unsigned int, unsigned int> &maxWidth, bool selected);
+        bool writeContainer(std::pair<int, int> &, const std::pair<unsigned int, unsigned int> &maxSize, const JSonContainer *);
+        bool writeContent(std::pair<int, int> &cursor, const std::pair<unsigned int, unsigned int> &maxSize, const JSonArray * obj);
+        bool writeContent(std::pair<int, int> &cursor, const std::pair<unsigned int, unsigned int> &maxSize, const JSonObject * obj);
 
         std::set<const JSonContainer *> collapsed;
 

+ 40 - 30
src/curseOutput.cpp

@@ -73,7 +73,7 @@ static void _resizeFnc(int signo)
 
 bool CurseOutput::redraw()
 {
-    std::pair<int, int> screenSize;
+    std::pair<unsigned int, unsigned int> screenSize;
     std::pair<int, int> cursor;
     bool result;
 
@@ -94,7 +94,7 @@ bool CurseOutput::redraw()
     return result;
 }
 
-bool CurseOutput::redraw(std::pair<int, int> &cursor, const std::pair<int, int> &maxSize, const JSonElement *item, const JSonContainer *parent)
+bool CurseOutput::redraw(std::pair<int, int> &cursor, const std::pair<unsigned int, unsigned int> &maxSize, const JSonElement *item, const JSonContainer *parent)
 {
     checkSelection(item, parent, cursor);
     if (dynamic_cast<const JSonContainer*>(item))
@@ -104,14 +104,14 @@ bool CurseOutput::redraw(std::pair<int, int> &cursor, const std::pair<int, int>
     }
     else
     {
-        write(cursor.first, cursor.second, item, selection == item);
-        if (++cursor.second - topleft> maxSize.second -1)
+        cursor.second += write(cursor.first, cursor.second, item, maxSize.first, selection == item);
+        if (cursor.second - topleft> maxSize.second -1)
             return false;
     }
     return true;
 }
 
-bool CurseOutput::writeContainer(std::pair<int, int> &cursor, const std::pair<int, int> &maxSize, const JSonContainer *item)
+bool CurseOutput::writeContainer(std::pair<int, int> &cursor, const std::pair<unsigned int, unsigned int> &maxSize, const JSonContainer *item)
 {
     char childDelimiter[2];
     bool isObject = dynamic_cast<const JSonObject *>(item);
@@ -126,12 +126,12 @@ bool CurseOutput::writeContainer(std::pair<int, int> &cursor, const std::pair<in
     {
         std::string ss;
         ss.append(&childDelimiter[0], 1).append(" ... ").append(&childDelimiter[1], 1);
-        write(cursor.first, cursor.second, ss, selection == item);
+        cursor.second += write(cursor.first, cursor.second, ss, maxSize.first, selection == item);
     }
     else
     {
-        write(cursor.first, cursor.second, childDelimiter[0], selection == item);
-        if (++cursor.second - topleft > maxSize.second -1)
+        cursor.second += write(cursor.first, cursor.second, childDelimiter[0], maxSize.first, selection == item);
+        if (cursor.second - topleft > maxSize.second -1)
                 return false;
         if (isObject)
         {
@@ -143,13 +143,13 @@ bool CurseOutput::writeContainer(std::pair<int, int> &cursor, const std::pair<in
             if (!writeContent(cursor, maxSize, (const JSonArray *)item))
                 return false;
         }
-        write(cursor.first, cursor.second, childDelimiter[1], selection == item);
+        cursor.second += write(cursor.first, cursor.second, childDelimiter[1], maxSize.first, selection == item);
     }
     cursor.first -= indentLevel /2;
-    return (++cursor.second - topleft <= maxSize.second -1);
+    return (cursor.second - topleft <= maxSize.second -1);
 }
 
-bool CurseOutput::writeContent(std::pair<int, int> &cursor, const std::pair<int, int> &maxSize, const JSonArray *item)
+bool CurseOutput::writeContent(std::pair<int, int> &cursor, const std::pair<unsigned int, unsigned int> &maxSize, const JSonArray *item)
 {
     cursor.first += indentLevel /2;
     for (JSonArray::const_iterator i = ((JSonArray *)item)->cbegin(); i != ((JSonArray *)item)->cend(); ++i)
@@ -159,7 +159,7 @@ bool CurseOutput::writeContent(std::pair<int, int> &cursor, const std::pair<int,
     return true;
 }
 
-bool CurseOutput::writeContent(std::pair<int, int> &cursor, const std::pair<int, int> &maxSize, const JSonObject *item)
+bool CurseOutput::writeContent(std::pair<int, int> &cursor, const std::pair<unsigned int, unsigned int> &maxSize, const JSonObject *item)
 {
     for (JSonObject::const_iterator i = ((JSonObject *)item)->cbegin(); i != ((JSonObject *)item)->cend(); ++i)
     {
@@ -169,9 +169,9 @@ bool CurseOutput::writeContent(std::pair<int, int> &cursor, const std::pair<int,
                 || ((const JSonContainer *) ipair.second)->size() == 0)
         {
             checkSelection(ipair.second, item, cursor);
-            write(cursor.first, cursor.second, ipair.first +": " +ipair.second->stringify(), selection == ipair.second);
+            cursor.second += write(cursor.first, cursor.second, ipair.first +": " +ipair.second->stringify(), maxSize.first, selection == ipair.second);
             cursor.first -= indentLevel /2;
-            if (++cursor.second - topleft > maxSize.second -1)
+            if (cursor.second - topleft > maxSize.second -1)
                 return false;
         }
         else if (collapsed.find((const JSonContainer *)ipair.second) != collapsed.end())
@@ -184,14 +184,14 @@ bool CurseOutput::writeContent(std::pair<int, int> &cursor, const std::pair<int,
             std::string ss = ipair.first;
             ss.append(": ").append(&childDelimiter[0], 1).append(" ... ").append(&childDelimiter[1], 1);
             checkSelection(ipair.second, item, cursor);
-            write(cursor.first, cursor.second, ss, selection == ipair.second);
+            cursor.second += write(cursor.first, cursor.second, ss, maxSize.first, selection == ipair.second);
             cursor.first -= indentLevel /2;
-            if (++cursor.second - topleft > maxSize.second -1)
+            if (cursor.second - topleft > maxSize.second -1)
                 return false;
         }
         else
         {
-            if (!writeKey(ipair.first, cursor, maxSize.second, selection == ipair.second))
+            if (!writeKey(ipair.first, cursor, maxSize, selection == ipair.second))
                 return false;
             cursor.first -= indentLevel /2;
             if (!redraw(cursor, maxSize, ipair.second, (JSonContainer *) item))
@@ -202,23 +202,23 @@ bool CurseOutput::writeContent(std::pair<int, int> &cursor, const std::pair<int,
     return true;
 }
 
-bool CurseOutput::writeKey(const std::string &key, std::pair<int, int> &cursor, const int maxSize, bool selected)
+bool CurseOutput::writeKey(const std::string &key, std::pair<int, int> &cursor, const std::pair<unsigned int, unsigned int> &maxSize, bool selected)
 {
-    write(cursor.first, cursor.second, key +": ", selected);
+    cursor.second += write(cursor.first, cursor.second, key +": ", maxSize.first, selected);
     cursor.first += indentLevel;
-    return (++cursor.second - topleft <= maxSize -1);
+    return (cursor.second - topleft <= maxSize.second -1);
 }
 
-void CurseOutput::write(const int &x, const int &y, const JSonElement *item, bool selected)
+unsigned int CurseOutput::write(const int &x, const int &y, const JSonElement *item, unsigned int maxWidth, bool selected)
 {
-    write(x, y, item->stringify(), selected);
+    return write(x, y, item->stringify(), maxWidth, selected);
 }
 
-void CurseOutput::write(const int &x, const int &y, const char item, bool selected)
+unsigned int CurseOutput::write(const int &x, const int &y, const char item, unsigned int maxWidth, bool selected)
 {
     int offsetY = y - topleft;
     if (offsetY < 0)
-        return;
+        return 1;
     if (selected)
     {
         attron(A_REVERSE | A_BOLD);
@@ -227,13 +227,22 @@ void CurseOutput::write(const int &x, const int &y, const char item, bool select
     }
     else
         mvprintw(offsetY, x, "%c", item);
+    return getNbLines(x +1, maxWidth);
 }
 
-void CurseOutput::write(const int &x, const int &y, const char *str, bool selected)
+unsigned int CurseOutput::getNbLines(float nbChar, unsigned int maxWidth)
+{
+    float nLine = nbChar / maxWidth;
+    if (nLine == (unsigned int) nLine)
+        return nLine;
+    return nLine +1;
+}
+
+unsigned int CurseOutput::write(const int &x, const int &y, const char *str, unsigned int maxWidth, bool selected)
 {
     int offsetY = y - topleft;
     if (offsetY < 0)
-        return;
+        return 1;
     if (selected)
     {
         attron(A_REVERSE | A_BOLD);
@@ -242,16 +251,17 @@ void CurseOutput::write(const int &x, const int &y, const char *str, bool select
     }
     else
         mvprintw(offsetY, x, "%s", str);
+    return getNbLines(strlen(str) +x, maxWidth);
 }
 
-void CurseOutput::write(const int &x, const int &y, const std::string &str, bool selected)
+unsigned int CurseOutput::write(const int &x, const int &y, const std::string &str, unsigned int maxWidth, bool selected)
 {
-    write(x, y, str.c_str(), selected);
+    return write(x, y, str.c_str(), maxWidth, selected);
 }
 
-void CurseOutput::getScreenSize(std::pair<int, int> &ss, std::pair<int, int> &bs)
+void CurseOutput::getScreenSize(std::pair<unsigned int, unsigned int> &screenSize, std::pair<int, int> &bs) const
 {
-    getmaxyx(stdscr, ss.second, ss.first);
+    getmaxyx(stdscr, screenSize.second, screenSize.first);
     getbegyx(stdscr, bs.second, bs.first);
 }
 

+ 1 - 0
test/longline.json

@@ -0,0 +1 @@
+[" Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a tortor sed lectus rhoncus porttitor. Duis condimentum enim ac consequat vulputate. Sed feugiat sapien id hendrerit congue. Nunc in nulla nec mauris sollicitudin porta. Quisque sapien velit, interdum nec sodales vitae, tristique quis tortor. Nam quam libero, placerat sed erat quis, ultrices semper augue. Aenean vitae leo eu massa porta fringilla sed et arcu.  Etiam ex tellus, laoreet at ante non, auctor tempus nisi. Vestibulum blandit ipsum ligula, in ultrices neque malesuada id. Donec non convallis sem. Nunc ac orci eu dui finibus pharetra. Mauris quis purus turpis. In porta felis lectus, in bibendum nisl molestie vitae. Donec luctus quam nec tristique luctus. Donec ultrices ante id cursus scelerisque. Vestibulum tempus massa et finibus sollicitudin. ", "EOF"]

+ 1 - 0
test/longline2.json

@@ -0,0 +1 @@
+["Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a tortor sed lectus rhoncus porttitor. Duis condimentum enim ac consequat vulputate....Sed feugiat sapien id hendrerit congue. Nunc in nulla nec mauris sollicitudin porta.", "EOF", "coucou"]

+ 89 - 0
test/test.json

@@ -0,0 +1,89 @@
+{"sampleArray":[
+    {
+        "0":"897316929176464ebc9ad085f31e7284",
+        "1":"b026324c6904b2a9cb4b88d6d61c81d1",
+        "2":"26ab0db90d72e28ad0ba1e22ee510510",
+        "3":"6d7fce9fee471194aa8b5b6e47267f03",
+        "4":"48a24b70a0b376535542b996af517398",
+        "5":"1dcca23355272056f04fe8bf20edfce0",
+        "6":"9ae0ea9e3c9c6e1b9b6252c8395efdc1",
+        "7":"84bc3da1b3e33a18e8d5e1bdd7a18d7a",
+        "8":"c30f7472766d25af1dc80b3ffc9a58c7",
+        "9":"7c5aba41f53293b712fd86d08ed5b36e",
+        "10":"31d30eea8d0968d6458e0ad0027c9f80",
+        "11":"166d77ac1b46a1ec38aa35ab7e628ab5",
+        "12":"2737b49252e2a4c0fe4c342e92b13285",
+        "13":"aa6ed9e0f26a6eba784aae8267df1951",
+        "14":"367764329430db34be92fd14a7a770ee",
+        "15":"8c9eb686bf3eb5bd83d9373eadf6504b",
+        "16":"5b6b41ed9b343fed9cd05a66d36650f0",
+        "17":"4d095eeac8ed659b1ce69dcef32ed0dc",
+        "18":"cf4278314ef8e4b996e1b798d8eb92cf",
+        "19":"3bb50ff8eeb7ad116724b56a820139fa",
+        "20":"dbbf8220893d497d403bb9cdf49db7a4",
+        "21":"fe9d26c3e620eeb69bd166c8be89fb8f",
+        "22":"2fc57d6f63a9ee7e2f21a26fa522e3b6",
+        "23":"2a53da1a6fbfc0bafdd96b0a2ea29515",
+        "24":"7c67493bd72ceff21059c3d924d17518",
+        "25":"2a52a5e65fc3c43f409550dfad1f904f",
+        "26":"b0771132ab2531a40c9941375ed8e290",
+        "27":"66a7c1d5cb75ef2542524d888fd32f4a",
+        "28":"51a6d96331d5eaa300358c7a0faf168d",
+        "29":"22cab69bca05d296a2d779a52cdee643",
+        "30":"d5b4c7d9b06b60a7846c4529834c9812",
+        "31":"4f89a0f6c113ae3ff279af1e6c6286bb",
+        "32":"bb743fc2a7213949f25593c51cbee64f",
+        "33":"4fbafd6948b6529caa2b78e476359875",
+        "34":"fd1bc138d22d4f78150c6e808345c2cc",
+        "35":"649ee93d50739c656e94ec88a32c7ffe",
+        "36":"fa84f696e31d07f55cd45cc3c9e52f3b",
+        "37":"87aa60e6e440a6656bc74eb20476d662",
+        "38":"bda81ba88c634b46394ead43aff31ad5",
+        "39":"59885ebc737617addaaf0cb8090203fc",
+        "40":"90e2a51705594d033a3abe9d77b2b7ad",
+        "41":"21fa2e849284bf7681c37a8b565bd934"
+    },{
+        "0":"c41873a83e19eea34a6258aad4c4a527",
+        "1":"2ed4a722308d6e967f7d84a265ca3b4b",
+        "2":"61a24f65a87293f162df03343992ec2f",
+        "3":"3ff17033b30dae04be6dca6e135ae00f",
+        "4":"e873256168a73edba3b7b3881e9d5685",
+        "5":"6174c29c68ca8512403367ee84fe1ea6",
+        "6":"9eb6b473929141b9e643479800224395",
+        "7":"756665371de9333e6848ec351a03b6f3",
+        "8":"a0c18315b9ec259dd88938a4ddc2eb9b",
+        "9":"67919697ef00fcc251efa8a9bc8f3020",
+        "10":"e2c194394dff53368fe0e4c281b01baa",
+        "11":"7bd40bcd0debf960d90e8f720911341a",
+        "12":"9ed3fe308d8ab4ef98ffc911f91b9f3a",
+        "13":"80cf48e3d2c198c45e36631d2b4fde67",
+        "14":"50c3884ece27330448c20f0be36a938f",
+        "15":"556d34b4e553f64f0d653db6bcf32d0a",
+        "16":"81fb5fb3376f99435e9333c83fe68b1c",
+        "17":"57e544bff20613c56b133f16b3b5aec5",
+        "18":"7aae1d211529248bde5285085b53cf96",
+        "19":"a5e5f6833c607e4cb4226708b25b52fe",
+        "20":"03464b64c5395ffc59b6a912bc4b0248",
+        "21":"d166b47bb5d18f6900c21195496eacfb",
+        "22":"3efaff284c20e93548a00f3054445f6e",
+        "23":"c17e59b2e626937babf86f1a9977f483",
+        "24":"af0a574f4d72c82510a3c83cf1b0e155",
+        "25":"fc78ca8516053c4ee7eaeac4915eeccb",
+        "26":"24d240716082efaaeabf0de2ad983322",
+        "27":"18a7aef722f11310db6e6422dc5e1800",
+        "28":"822b22f9f2d43711f05dd236a03dc1a5",
+        "29":"37a699dba20043e0f6f7a2b0153db896",
+        "30":"098df28fb02909f2e32e73fb890b557f",
+        "31":"6e98e186883db4dc31d519506831d5ff",
+        "32":"7b92dca7f49d464241ddf789e4b35214",
+        "33":"2b54eb12ba0916376b6d617d15a125b2",
+        "34":"062e78cc292ac9d18bbc3e6173827343",
+        "35":"74834e5c91324838e1ead7cb292d85c5",
+        "36":"561fc791a95fffc244eb2e94b14111c8",
+        "37":"7edb46ac0ee9025637b6ad4a4e1fd785",
+        "38":"f275304670ba6851c0dd18a2ee5a1c83",
+        "39":"d5284c262d228a296ff6df041c705ac3",
+        "40":"de289e8d145ca181fed108cb9839a420",
+        "41":"bc3d59258580ab5b181298c13997cea6"
+    }
+]}

+ 1 - 0
test/test2.json

@@ -0,0 +1 @@
+{"1":{"t0":["a", true, "b",false]},"2":{"t1":["a", true, "b",false]}, "3": "to20025to", "4":[1, 2, 3], "emptyObj":{},"emptyArr":[]}