Prechádzať zdrojové kódy

[add] print input's filenames

isundil 9 rokov pred
rodič
commit
1fcae5c10f

+ 4 - 3
include/curseOutput.hh

@@ -51,7 +51,7 @@ class CurseOutput
         /**
          * Initialize ncurses
         **/
-        virtual void init() =0;
+        virtual void init();
 
         /**
          * Release ncurses
@@ -144,8 +144,9 @@ class CurseOutput
         /**
          * Write a message on the last line, using color
         **/
-        void writeBottomLine(const std::string &currentBuffer, short color) const;
-        void writeBottomLine(const std::wstring &currentBuffer, short color) const;
+        virtual void writeTopLine(const std::string &currentBuffer, short color) const;
+        virtual void writeBottomLine(const std::string &currentBuffer, short color) const;
+        virtual void writeBottomLine(const std::wstring &currentBuffer, short color) const;
 
         /**
          * unfold all item's parents

+ 6 - 6
include/curseSimpleOutput.hh

@@ -19,17 +19,12 @@ class CurseSimpleOutput: public CurseOutput
         /**
          * Display data, and shutdown ncurses at the end
         **/
-        void run(JSonElement *);
+        void run(JSonElement *, const std::string &inputName);
 
         bool jumpToNextSearch(const JSonElement *current, bool &selectFound);
         bool jumpToNextSearch();
         unsigned int search(const SearchPattern &search_pattern, const JSonElement *current);
 
-        /**
-         * Initialize ncurses
-        **/
-        void init();
-
         /**
          * Release ncurses
         **/
@@ -83,5 +78,10 @@ class CurseSimpleOutput: public CurseOutput
          * prev/next items to be selected on up/down keys
         **/
         const JSonElement *select_up, *select_down;
+
+        /**
+         * input name
+        **/
+        std::string inputName;
 };
 

+ 2 - 5
include/curseSplitOutput.hh

@@ -31,6 +31,8 @@ class CurseSplitOutput: public CurseOutput
         unsigned int write(const int &x, const int &y, const std::string &str, const size_t strlen, unsigned int maxWidth, const OutputFlag flags);
         void write(const std::string &str, const OutputFlag flags) const;
 
+        void writeTopLine(const std::string &currentBuffer, short color) const;
+
         bool jumpToNextSearch(const JSonElement *current, bool &selectFound);
         bool jumpToNextSearch();
         unsigned int search(const SearchPattern &search_pattern);
@@ -41,11 +43,6 @@ class CurseSplitOutput: public CurseOutput
         **/
         const std::pair<unsigned int, unsigned int> getScreenSize() const;
 
-        /**
-         * Initialize ncurses
-        **/
-        void init();
-
         /**
          * Release ncurses
         **/

+ 2 - 0
include/outputFlag.hh

@@ -56,5 +56,7 @@ class OutputFlag
         static const char SPECIAL_NONE;
         static const char SPECIAL_SEARCH;
         static const char SPECIAL_ERROR;
+        static const char SPECIAL_INPUTNAME;
+        static const char SPECIAL_ACTIVEINPUTNAME;
 };
 

+ 62 - 3
src/curseOutput.cpp

@@ -34,12 +34,14 @@ CurseOutput::~CurseOutput()
 
 void CurseOutput::loop()
 {
+    inputResult read;
+
     breakLoop = false;
     do
     {
-        while (!redraw()) //TODO opti going down
-            ;
-    } while(readInput());
+        while (!redraw());
+        read = readInput();
+    } while (read != inputResult::quit);
 }
 
 bool CurseOutput::onsig(int signo)
@@ -221,6 +223,18 @@ const SearchPattern *CurseOutput::inputSearch()
     }
 }
 
+void CurseOutput::writeTopLine(const std::string &buffer, short color) const
+{
+    const std::pair<unsigned int, unsigned int> screenSize = getScreenSize();
+    const size_t bufsize = buffer.size();
+
+    if (params.colorEnabled())
+        attron(COLOR_PAIR(color));
+    mvprintw(0, 0, "%s%*c", buffer.c_str(), screenSize.first - bufsize, ' ');
+    if (params.colorEnabled())
+        attroff(COLOR_PAIR(color));
+}
+
 void CurseOutput::writeBottomLine(const std::string &buffer, short color) const
 {
     const std::pair<unsigned int, unsigned int> screenSize = getScreenSize();
@@ -247,3 +261,48 @@ void CurseOutput::writeBottomLine(const std::wstring &buffer, short color) const
         attroff(COLOR_PAIR(color));
 }
 
+void CurseOutput::init()
+{
+    if (!isatty(fileno(stdin)) || !isatty(fileno(stdout)))
+    {
+        screen_fd = fopen("/dev/tty", "r+");
+        setbuf(screen_fd, nullptr);
+        screen = newterm(nullptr, screen_fd, screen_fd);
+    }
+    else
+    {
+        screen = newterm(nullptr, stdout, stdin);
+        screen_fd = nullptr;
+    }
+    wtimeout(stdscr, 150);
+    cbreak();
+    clear();
+    noecho();
+    curs_set(false);
+    keypad(stdscr, true);
+
+    if (params.colorEnabled())
+    {
+        start_color();
+        init_pair(OutputFlag::TYPE_NUMBER, COLOR_GREEN, COLOR_BLACK);
+        init_pair(OutputFlag::TYPE_BOOL, COLOR_RED, COLOR_BLACK);
+        init_pair(OutputFlag::TYPE_NULL, COLOR_RED, COLOR_BLACK);
+        init_pair(OutputFlag::TYPE_STRING, COLOR_CYAN, COLOR_BLACK);
+        init_pair(OutputFlag::TYPE_OBJKEY, COLOR_CYAN, COLOR_BLACK);
+        init_pair(OutputFlag::SPECIAL_SEARCH, COLOR_WHITE, COLOR_BLUE);
+        init_pair(OutputFlag::SPECIAL_ERROR, COLOR_WHITE, COLOR_RED);
+        init_pair(OutputFlag::SPECIAL_ACTIVEINPUTNAME, COLOR_WHITE, COLOR_GREEN);
+        init_pair(OutputFlag::SPECIAL_INPUTNAME, COLOR_BLACK, COLOR_WHITE);
+        colors.insert(OutputFlag::TYPE_NUMBER);
+        colors.insert(OutputFlag::TYPE_BOOL);
+        colors.insert(OutputFlag::TYPE_STRING);
+        colors.insert(OutputFlag::TYPE_OBJKEY);
+        colors.insert(OutputFlag::TYPE_NULL);
+    }
+
+    signal(SIGWINCH, _resizeFnc);
+    signal(SIGINT, _resizeFnc);
+    signal(SIGTERM, _resizeFnc);
+    signal(SIGKILL, _resizeFnc);
+}
+

+ 9 - 50
src/curseSimpleOutput.cpp

@@ -24,16 +24,18 @@ CurseSimpleOutput::~CurseSimpleOutput()
     shutdown();
 }
 
-void CurseSimpleOutput::run(JSonElement *root)
+void CurseSimpleOutput::run(JSonElement *root, const std::string &i)
 {
+    scrollTop = 0;
     selection = data = root;
+    inputName = i;
     loop();
 }
 
 bool CurseSimpleOutput::redraw()
 {
     const std::pair<unsigned int, unsigned int> screenSize = getScreenSize();
-    std::pair<int, int> cursor(0, 0);
+    std::pair<int, int> cursor(0, 1);
     /**
      * Will be true if the json's last item is visible
     **/
@@ -42,6 +44,7 @@ bool CurseSimpleOutput::redraw()
     select_up = select_down = nullptr;
     selectFound = selectIsLast = false;
     clear();
+    writeTopLine(inputName, OutputFlag::SPECIAL_ACTIVEINPUTNAME);
     try {
         result = redraw(cursor, screenSize, data);
     }
@@ -306,7 +309,7 @@ bool CurseSimpleOutput::writeContent(std::pair<int, int> &cursor, const std::pai
 
 bool CurseSimpleOutput::writeKey(const std::string &key, const size_t keylen, std::pair<int, int> &cursor, const std::pair<unsigned int, unsigned int> &maxSize, OutputFlag flags, unsigned int extraLen)
 {
-    if (cursor.second - scrollTop < 0)
+    if (cursor.second - scrollTop <= 0)
     {
         cursor.second++;
         return true;
@@ -322,7 +325,7 @@ bool CurseSimpleOutput::writeKey(const std::string &key, const size_t keylen, st
 
 bool CurseSimpleOutput::writeKey(const std::string &key, const size_t keylen, const std::string &after, size_t afterlen, std::pair<int, int> &cursor, const std::pair<unsigned int, unsigned int> &maxSize, OutputFlag flags)
 {
-    if (cursor.second - scrollTop < 0)
+    if (cursor.second - scrollTop <= 0)
     {
         cursor.second++;
         return true;
@@ -343,7 +346,7 @@ unsigned int CurseSimpleOutput::write(const int &x, const int &y, const char ite
     int offsetY = y - scrollTop;
     char color = OutputFlag::SPECIAL_NONE;
 
-    if (offsetY < 0)
+    if (offsetY <= 0)
         return 1;
 
     if (flags.selected())
@@ -365,7 +368,7 @@ unsigned int CurseSimpleOutput::write(const int &x, const int &y, const char ite
 unsigned int CurseSimpleOutput::write(const int &x, const int &y, const std::string &str, const size_t strlen, unsigned int maxWidth, const OutputFlag flags)
 {
     int offsetY = y - scrollTop;
-    if (offsetY < 0)
+    if (offsetY <= 0)
         return 1;
     move(offsetY, x);
     write(str, flags);
@@ -513,50 +516,6 @@ void CurseSimpleOutput::checkSelection(const JSonElement *item, const std::pair<
     }
 }
 
-void CurseSimpleOutput::init()
-{
-    if (!isatty(fileno(stdin)) || !isatty(fileno(stdout)))
-    {
-        screen_fd = fopen("/dev/tty", "r+");
-        setbuf(screen_fd, nullptr);
-        screen = newterm(nullptr, screen_fd, screen_fd);
-    }
-    else
-    {
-        screen = newterm(nullptr, stdout, stdin);
-        screen_fd = nullptr;
-    }
-    wtimeout(stdscr, 150);
-    cbreak();
-    clear();
-    noecho();
-    curs_set(false);
-    keypad(stdscr, true);
-
-    if (params.colorEnabled())
-    {
-        start_color();
-        init_pair(OutputFlag::TYPE_NUMBER, COLOR_GREEN, COLOR_BLACK);
-        init_pair(OutputFlag::TYPE_BOOL, COLOR_RED, COLOR_BLACK);
-        init_pair(OutputFlag::TYPE_NULL, COLOR_RED, COLOR_BLACK);
-        init_pair(OutputFlag::TYPE_STRING, COLOR_CYAN, COLOR_BLACK);
-        init_pair(OutputFlag::TYPE_OBJKEY, COLOR_CYAN, COLOR_BLACK);
-        init_pair(OutputFlag::SPECIAL_SEARCH, COLOR_WHITE, COLOR_BLUE);
-        init_pair(OutputFlag::SPECIAL_ERROR, COLOR_WHITE, COLOR_RED);
-        colors.insert(OutputFlag::TYPE_NUMBER);
-        colors.insert(OutputFlag::TYPE_BOOL);
-        colors.insert(OutputFlag::TYPE_STRING);
-        colors.insert(OutputFlag::TYPE_OBJKEY);
-        colors.insert(OutputFlag::TYPE_NULL);
-    }
-
-    signal(SIGWINCH, _resizeFnc);
-    signal(SIGINT, _resizeFnc);
-    signal(SIGTERM, _resizeFnc);
-    signal(SIGKILL, _resizeFnc);
-    scrollTop = 0;
-}
-
 void CurseSimpleOutput::shutdown()
 {
     endwin();

+ 16 - 46
src/curseSplitOutput.cpp

@@ -293,7 +293,7 @@ bool CurseSplitOutput::redrawCurrent(short selectedWin)
 
 bool CurseSplitOutput::redrawCurrent(const std::pair<unsigned int, unsigned int> &screenSize)
 {
-    std::pair<int, int> cursor(0, 0);
+    std::pair<int, int> cursor(0, 1);
     bool result;
 
     select_up[workingWin] = select_down[workingWin] = nullptr;
@@ -301,6 +301,7 @@ bool CurseSplitOutput::redrawCurrent(const std::pair<unsigned int, unsigned int>
 
     wclear(currentWin);
     box(outerWin[workingWin], 0, 0);
+    writeTopLine(fileNames[workingWin], workingWin == selectedWin ? OutputFlag::SPECIAL_ACTIVEINPUTNAME : OutputFlag::SPECIAL_INPUTNAME); //TODO
     try {
         result = redraw(cursor, screenSize, roots[workingWin]);
     }
@@ -508,7 +509,7 @@ unsigned int CurseSplitOutput::write(const int &x, const int &y, const char item
     int offsetY = y - scrollTop[workingWin];
     char color = OutputFlag::SPECIAL_NONE;
 
-    if (offsetY < 0)
+    if (offsetY <= 0)
         return 1;
 
     if (flags.selected())
@@ -531,7 +532,7 @@ unsigned int CurseSplitOutput::write(const int &x, const int &y, const std::stri
 {
     int offsetY = y - scrollTop[workingWin];
 
-    if (offsetY < 0)
+    if (offsetY <= 0)
         return 1;
     wmove(currentWin, offsetY, x);
     write(str, flags);
@@ -571,6 +572,18 @@ void CurseSplitOutput::destroyAllSubWin()
     outerWin.clear();
 }
 
+void CurseSplitOutput::writeTopLine(const std::string &buffer, short color) const
+{
+    const std::pair<unsigned int, unsigned int> screenSize = getScreenSize();
+    const size_t bufsize = buffer.size();
+
+    if (params.colorEnabled())
+        wattron(currentWin, COLOR_PAIR(color));
+    mvwprintw(currentWin, 0, 0, "%s%*c", buffer.c_str(), screenSize.first - bufsize -2, ' ');
+    if (params.colorEnabled())
+        wattroff(currentWin, COLOR_PAIR(color));
+}
+
 const std::pair<unsigned int, unsigned int> CurseSplitOutput::getScreenSize() const
 {
     std::pair<unsigned int, unsigned int> result = CurseOutput::getScreenSize();
@@ -579,49 +592,6 @@ const std::pair<unsigned int, unsigned int> CurseSplitOutput::getScreenSize() co
     return result;
 }
 
-void CurseSplitOutput::init()
-{
-    if (!isatty(fileno(stdin)) || !isatty(fileno(stdout)))
-    {
-        screen_fd = fopen("/dev/tty", "r+");
-        setbuf(screen_fd, nullptr);
-        screen = newterm(nullptr, screen_fd, screen_fd);
-    }
-    else
-    {
-        screen = newterm(nullptr, stdout, stdin);
-        screen_fd = nullptr;
-    }
-    wtimeout(stdscr, 150);
-    cbreak();
-    clear();
-    noecho();
-    curs_set(false);
-    keypad(stdscr, true);
-
-    if (params.colorEnabled())
-    {
-        start_color();
-        init_pair(OutputFlag::TYPE_NUMBER, COLOR_GREEN, COLOR_BLACK);
-        init_pair(OutputFlag::TYPE_BOOL, COLOR_RED, COLOR_BLACK);
-        init_pair(OutputFlag::TYPE_NULL, COLOR_RED, COLOR_BLACK);
-        init_pair(OutputFlag::TYPE_STRING, COLOR_CYAN, COLOR_BLACK);
-        init_pair(OutputFlag::TYPE_OBJKEY, COLOR_CYAN, COLOR_BLACK);
-        init_pair(OutputFlag::SPECIAL_SEARCH, COLOR_WHITE, COLOR_BLUE);
-        init_pair(OutputFlag::SPECIAL_ERROR, COLOR_WHITE, COLOR_RED);
-        colors.insert(OutputFlag::TYPE_NUMBER);
-        colors.insert(OutputFlag::TYPE_BOOL);
-        colors.insert(OutputFlag::TYPE_STRING);
-        colors.insert(OutputFlag::TYPE_OBJKEY);
-        colors.insert(OutputFlag::TYPE_NULL);
-    }
-
-    signal(SIGWINCH, _resizeFnc);
-    signal(SIGINT, _resizeFnc);
-    signal(SIGTERM, _resizeFnc);
-    signal(SIGKILL, _resizeFnc);
-}
-
 void CurseSplitOutput::shutdown()
 {
     endwin();

+ 1 - 1
src/main.cpp

@@ -121,7 +121,7 @@ void run(const Params &params)
             w.filename(input.first);
             warns.push_back(Warning(w));
         }
-        out->run(stream->getRoot());
+        out->run(stream->getRoot(), input.first);
         delete stream;
     }
     if (out)

+ 5 - 3
src/outputFlag.cpp

@@ -15,9 +15,11 @@ const char OutputFlag::TYPE_OBJKEY  =5;
 const char OutputFlag::TYPE_ARR     =6;
 const char OutputFlag::TYPE_NULL    =7;
 
-const char OutputFlag::SPECIAL_NONE     =50;
-const char OutputFlag::SPECIAL_SEARCH   =51;
-const char OutputFlag::SPECIAL_ERROR    =52;
+const char OutputFlag::SPECIAL_NONE             =50;
+const char OutputFlag::SPECIAL_SEARCH           =51;
+const char OutputFlag::SPECIAL_ERROR            =52;
+const char OutputFlag::SPECIAL_INPUTNAME        =53;
+const char OutputFlag::SPECIAL_ACTIVEINPUTNAME  =54;
 
 OutputFlag::OutputFlag(short m): mode(m)
 { }