Jelajahi Sumber

[add] change current window

isundil 9 tahun lalu
induk
melakukan
92e7a1b7dc

+ 5 - 3
include/curseOutput.hh

@@ -25,8 +25,9 @@ class SearchPattern;
 enum inputResult: char
 {
     redraw      =0
-    ,quit       =1
-    ,nextInput  =2
+    ,redrawAll  =1
+    ,quit       =2
+    ,nextInput  =3
 };
 
 class CurseOutput
@@ -75,7 +76,7 @@ class CurseOutput
          * Wait for input
          * @return false if ncurses should stop
         **/
-        bool readInput();
+        inputResult readInput();
         inputResult evalKey(const InputSequence &k);
 
         virtual inputResult selectUp() =0;
@@ -86,6 +87,7 @@ class CurseOutput
         virtual inputResult collapseSelection() =0;
         virtual inputResult initSearch() =0;
         virtual inputResult nextResult() =0;
+        virtual inputResult changeWindow(char direction, bool cycle) =0;
 
         /**
          * get the screen size

+ 1 - 0
include/curseSimpleOutput.hh

@@ -61,6 +61,7 @@ class CurseSimpleOutput: public CurseOutput
         inputResult collapseSelection();
         inputResult initSearch();
         inputResult nextResult();
+        inputResult changeWindow(char, bool);
 
         /**
          * Root item

+ 1 - 0
include/curseSplitOutput.hh

@@ -69,6 +69,7 @@ class CurseSplitOutput: public CurseOutput
         inputResult collapseSelection();
         inputResult initSearch();
         inputResult nextResult();
+        inputResult changeWindow(char, bool);
 
         std::deque<std::string> fileNames;
         std::deque<JSonElement *> roots;

+ 4 - 2
include/inputSequence.hh

@@ -1,5 +1,7 @@
 #pragma once
 
+#include <string>
+
 class InputSequence
 {
     private:
@@ -11,9 +13,9 @@ class InputSequence
 
         static InputSequence read();
 
-        int key() const;
+        const std::string &key() const;
 
     protected:
-        int _key;
+        std::string seq;
 };
 

+ 40 - 46
src/curseOutput.cpp

@@ -82,61 +82,55 @@ void _resizeFnc(int signo)
  * false on:
  *  - exit signal
 **/
-bool CurseOutput::readInput()
+inputResult CurseOutput::readInput()
 {
     while (!breakLoop)
     {
         inputResult r = evalKey(InputSequence::read());
-        if (r == inputResult::redraw)
-            return true;
-        if (r == inputResult::quit)
-            return false;
-        // else nextInput;
+        if (r != inputResult::nextInput)
+            return r;
     }
-    return false;
+    return inputResult::quit;
 }
 
 inputResult CurseOutput::evalKey(const InputSequence &c)
 {
-    switch (c.key())
-    {
-        case 'q':
-        case 'Q':
-            return inputResult::quit;
-
-        case KEY_UP:
-        case 'K':
-        case 'k':
-            return selectUp();
-
-        case KEY_DOWN:
-        case 'j':
-        case 'J':
-            return selectDown();
-
-        case KEY_PPAGE:
-            return selectPUp();
-
-        case KEY_NPAGE:
-            return selectPDown();
-
-        case 'l':
-        case 'L':
-        case KEY_RIGHT:
-            return expandSelection();
-
-        case 'h':
-        case 'H':
-        case KEY_LEFT:
-            return collapseSelection();
-
-        case '/':
-            return initSearch();
-
-        case 'n':
-        case 'N':
-            return nextResult();
-    }
+    const std::string key = c.key();
+
+    if (key == "Q")
+        return inputResult::quit;
+
+    else if (key == "K" || key == "KEY_UP")
+        return selectUp();
+
+    else if (key == "J" || key == "KEY_DOWN")
+        return selectDown();
+
+    else if (key == "KEY_PPAGE")
+        return selectPUp();
+
+    else if (key == "KEY_NPAGE")
+        return selectPDown();
+
+    else if (key == "L" || key == "KEY_RIGHT")
+        return expandSelection();
+
+    else if (key == "H" || key == "KEY_LEFT")
+        return collapseSelection();
+
+    else if (key == "/")
+        return initSearch();
+
+    else if (key == "N")
+        return nextResult();
+
+    else if (key == "^W-W")
+        return changeWindow(1, true);
+    else if (key == "^W-KEY_RIGHT" || key == "^W-L")
+        return changeWindow(1, false);
+    else if (key == "^W-KEY_LEFT" || key == "^W-H")
+        return changeWindow(-1, false);
+
     return inputResult::nextInput;
 }
 

+ 5 - 0
src/curseSimpleOutput.cpp

@@ -182,6 +182,11 @@ inputResult CurseSimpleOutput::nextResult()
     return inputResult::nextInput;
 }
 
+inputResult CurseSimpleOutput::changeWindow(char, bool)
+{
+    //TODO tab mode ?
+    return inputResult::nextInput;
+}
 
 bool CurseSimpleOutput::redraw(std::pair<int, int> &cursor, const std::pair<unsigned int, unsigned int> &maxSize, JSonElement *item)
 {

+ 16 - 2
src/curseSplitOutput.cpp

@@ -52,9 +52,15 @@ void CurseSplitOutput::loop()
     breakLoop = false;
 
     while (!redraw());
-    while(readInput())
+    while(true)
     {
-        while (!redrawCurrent(selectedWin));
+        inputResult read = readInput();
+        if (read == inputResult::quit)
+            break;
+        if (read == inputResult::redraw)
+            while (!redrawCurrent(selectedWin));
+        else if (read == inputResult::redrawAll)
+            while (!redraw());
     }
 }
 
@@ -167,6 +173,14 @@ inputResult CurseSplitOutput::nextResult()
     return inputResult::nextInput;
 }
 
+inputResult CurseSplitOutput::changeWindow(char d, bool c)
+{
+    if ((selectedWin +d < 0 || selectedWin +d >= nbInputs) && !c)
+        return inputResult::nextInput;
+    selectedWin = (selectedWin +d) % nbInputs;
+    return inputResult::redrawAll;
+}
+
 void CurseSplitOutput::checkSelection(const JSonElement *item, const std::pair<int, int> &cursor)
 {
     if (!selectFound)

+ 17 - 4
src/inputSequence.cpp

@@ -4,7 +4,7 @@
 InputSequence::InputSequence()
 { }
 
-InputSequence::InputSequence(const InputSequence &o): _key(o._key)
+InputSequence::InputSequence(const InputSequence &o): seq(o.seq)
 { }
 
 InputSequence::~InputSequence()
@@ -13,11 +13,24 @@ InputSequence::~InputSequence()
 InputSequence InputSequence::read()
 {
     InputSequence result;
+    const char *kname = nullptr;
 
-    result._key = getch();
+    do
+    {
+        const int input = getch();
+
+        if (input == -1)
+            continue;
+
+        kname = keyname(toupper(input));
+
+        if (!result.seq.empty())
+            result.seq += "-";
+        result.seq += kname;
+    }   while (!kname || *kname == '^');
     return result;
 }
 
-int InputSequence::key() const
-{ return _key; }
+const std::string &InputSequence::key() const
+{ return seq; }