ソースを参照

[WIP-BROKEN][add] empty split view

isundil 9 年 前
コミット
42ee9bd09b

+ 5 - 3
include/curseOutput.hh

@@ -41,12 +41,12 @@ class CurseOutput
         /**
          * Initialize ncurses
         **/
-        void init();
+        virtual void init() =0;
 
         /**
          * Release ncurses
         **/
-        void shutdown();
+        virtual void shutdown() =0;
 
         /**
          * return false if bottom of screen is touched
@@ -71,7 +71,7 @@ class CurseOutput
         /**
          * get the screen size
         **/
-        const std::pair<unsigned int, unsigned int> getScreenSize() const;
+        virtual const std::pair<unsigned int, unsigned int> getScreenSize() const;
 
         /**
          * set the select_up and select_down pointers, scroll to selection if it is above view port
@@ -199,3 +199,5 @@ class CurseOutput
         class SelectionOutOfRange { };
 };
 
+void _resizeFnc(int signo);
+

+ 10 - 0
include/curseSimpleOutput.hh

@@ -25,5 +25,15 @@ class CurseSimpleOutput: public CurseOutput
          * Root item
         **/
         JSonElement *data;
+
+        /**
+         * Initialize ncurses
+        **/
+        void init();
+
+        /**
+         * Release ncurses
+        **/
+        void shutdown();
 };
 

+ 26 - 1
include/curseSplitOutput.hh

@@ -1,5 +1,6 @@
 #pragma once
 
+#include <deque>
 #include "curseOutput.hh"
 
 class CurseSplitOutput: public CurseOutput
@@ -11,8 +12,32 @@ class CurseSplitOutput: public CurseOutput
         /**
          * Display data, and shutdown ncurses at the end
         **/
-        void run(std::list<JSonElement *>);
+        void run(const std::deque<std::string> &, const std::deque<JSonElement *> &);
 
         bool redraw();
+
+        /**
+         * get the screen size
+        **/
+        const std::pair<unsigned int, unsigned int> getScreenSize() const;
+
+        /**
+         * Initialize ncurses
+        **/
+        void init();
+
+        /**
+         * Release ncurses
+        **/
+        void shutdown();
+
+        void destroyAllSubWin();
+
+    protected:
+        std::deque<JSonElement *> roots;
+        std::deque<JSonElement *> selections;
+        std::deque<std::string> fileNames;
+        std::deque<WINDOW *> subwindows;
+        size_t nbInputs, currentWin;
 };
 

+ 1 - 59
src/curseOutput.cpp

@@ -25,12 +25,10 @@ static CurseOutput *runningInst = nullptr;
 CurseOutput::CurseOutput(const Params &p): data(nullptr), params(p)
 {
     runningInst = this;
-    init();
 }
 
 CurseOutput::~CurseOutput()
 {
-    shutdown();
     runningInst = nullptr;
 }
 
@@ -69,7 +67,7 @@ bool CurseOutput::onsig(int signo)
     return true;
 }
 
-static void _resizeFnc(int signo)
+void _resizeFnc(int signo)
 {
     if (!runningInst)
         return;
@@ -646,59 +644,3 @@ 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);
-        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 CurseOutput::shutdown()
-{
-    endwin();
-    delscreen(screen);
-    if (screen_fd)
-    {
-        fclose(screen_fd);
-        screen_fd = nullptr;
-    }
-    screen = nullptr;
-}
-

+ 67 - 2
src/curseSimpleOutput.cpp

@@ -4,14 +4,23 @@
  * Author: isundil <isundill@gmail.com>
 **/
 
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <signal.h>
+
 #include "curseSimpleOutput.hh"
 #include "jsonContainer.hh"
 
 CurseSimpleOutput::CurseSimpleOutput(const Params &p): CurseOutput(p)
-{ }
+{
+    init();
+}
+
 
 CurseSimpleOutput::~CurseSimpleOutput()
-{ }
+{
+    shutdown();
+}
 
 void CurseSimpleOutput::run(JSonElement *root)
 {
@@ -62,3 +71,59 @@ bool CurseSimpleOutput::redraw()
     return true;
 }
 
+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();
+    delscreen(screen);
+    if (screen_fd)
+    {
+        fclose(screen_fd);
+        screen_fd = nullptr;
+    }
+    screen = nullptr;
+}
+

+ 113 - 3
src/curseSplitOutput.cpp

@@ -1,21 +1,57 @@
+/**
+ * curseSplitOutput.cpp for jsonstroller
+ *
+ * Author: isundil <isundill@gmail.com>
+**/
+
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <signal.h>
+#include <curses.h>
+
 #include "curseSplitOutput.hh"
 #include "jsonContainer.hh"
 
 CurseSplitOutput::CurseSplitOutput(const Params &p): CurseOutput(p)
-{ }
+{
+    init();
+}
 
 CurseSplitOutput::~CurseSplitOutput()
-{ }
+{
+    shutdown();
+}
 
-void CurseSplitOutput::run(std::list<JSonElement*> roots)
+void CurseSplitOutput::run(const std::deque<std::string> &inputName, const std::deque<JSonElement*> &roots)
 {
     selection = data = *roots.begin();
+    nbInputs = inputName.size();
+    currentWin = 0;
+    for (size_t i =0; i < nbInputs; i++)
+    {
+        this->roots.push_back(roots.at(i));
+        selections.push_back(roots.at(i));
+    }
+    fileNames = inputName;
     loop();
 }
 
 bool CurseSplitOutput::redraw()
 {
     const std::pair<unsigned int, unsigned int> screenSize = getScreenSize();
+
+    destroyAllSubWin();
+    for (size_t i=0; i < nbInputs; i++)
+    {
+        WINDOW *currentWin = newwin(screenSize.second, screenSize.first, 0, i * screenSize.first - (i ? 1 : 0));
+        box(currentWin, 0, 0);
+        redraw(currentWin, screenSize); // TODO
+        wrefresh(currentWin);
+    }
+    refresh();
+    return false;
+
+    // TODO
     std::pair<int, int> cursor(0, 0);
     /**
      * Will be true if the json's last item is visible
@@ -56,3 +92,77 @@ bool CurseSplitOutput::redraw()
     return true;
 }
 
+void CurseSplitOutput::destroyAllSubWin()
+{
+    for (WINDOW *i: subwindows)
+    {
+        wborder(i, ' ', ' ', ' ',' ',' ',' ',' ',' ');
+        wrefresh(i);
+        delwin(i);
+    }
+    subwindows.clear();
+}
+
+const std::pair<unsigned int, unsigned int> CurseSplitOutput::getScreenSize() const
+{
+    std::pair<unsigned int, unsigned int> result = CurseOutput::getScreenSize();
+    result.first /= nbInputs;
+    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);
+    scrollTop = 0;
+}
+
+void CurseSplitOutput::shutdown()
+{
+    endwin();
+    delscreen(screen);
+    if (screen_fd)
+    {
+        fclose(screen_fd);
+        screen_fd = nullptr;
+    }
+    screen = nullptr;
+}
+

+ 8 - 4
src/main.cpp

@@ -34,15 +34,19 @@ StreamConsumer *readFile(std::pair<std::string, std::basic_istream<char>*> input
 void runDiff(const Params &params)
 {
     const std::map<std::string, std::basic_istream<char>*> inputs = params.getInputs();
+    const size_t nbInputs = inputs.size();
     std::set<StreamConsumer *> streams;
-    std::list<JSonElement *> roots;
-    std::list<Warning> warns;
+    std::deque<JSonElement *> roots;
+    std::deque<Warning> warns;
+    std::deque<std::string> inputNames;
 
     if (!params.isIgnoringUnicode())
         setlocale(LC_ALL, "");
     for (std::pair<std::string, std::basic_istream<char>*> input : inputs)
     {
         StreamConsumer *stream;
+
+        inputNames.push_back(input.first);
         try
         {
             stream = readFile(input, params);
@@ -68,10 +72,10 @@ void runDiff(const Params &params)
         roots.push_back(stream->getRoot());
         streams.insert(stream);
     }
-    if (streams.size() == inputs.size())
+    if (streams.size() == nbInputs)
     {
         CurseSplitOutput out(params);
-        out.run(roots);
+        out.run(inputNames, roots);
     }
     for (StreamConsumer *stream: streams)
         delete stream;