Browse Source

[Fix #11] pressing escape while reading search query abort it

isundil 9 years ago
parent
commit
31d6e58339
4 changed files with 38 additions and 7 deletions
  1. 6 3
      CMakeLists.txt
  2. 9 0
      include/except.hh
  3. 15 4
      src/curseOutput.cpp
  4. 8 0
      src/except.cpp

+ 6 - 3
CMakeLists.txt

@@ -8,19 +8,22 @@ set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
 
 add_executable(jsonstroll
     src/main.cpp
-    src/jsonContainer.cpp
     src/warning.cpp
     src/params.cpp
     src/curseOutput.cpp
     src/linearHistory.cpp
     src/outputFlag.cpp
     src/streamConsumer.cpp
+
+    src/jsonElement.cpp
     src/jsonArray.cpp
-    src/jsonObjectEntry.cpp
     src/jsonObject.cpp
-    src/jsonElement.cpp
+    src/jsonContainer.cpp
+    src/jsonObjectEntry.cpp
     src/jsonPrimitive.cpp
+
     src/jsonException.cpp
+    src/except.cpp
     )
 
 set_property(TARGET jsonstroll PROPERTY RUNTIME_OUTPUT_DIRECTORY ${CUSTOM_BINARY_OUTPUT_DIR})

+ 9 - 0
include/except.hh

@@ -0,0 +1,9 @@
+#pragma once
+
+class interruptedException
+{
+    public:
+        interruptedException();
+        virtual ~interruptedException();
+};
+

+ 15 - 4
src/curseOutput.cpp

@@ -11,12 +11,12 @@
 #include <unistd.h>
 #include <signal.h>
 #include <string.h>
-#include <stack>
 
 #include "curseOutput.hh"
 #include "jsonPrimitive.hh"
 #include "jsonObject.hh"
 #include "jsonArray.hh"
+#include "except.hh"
 
 static CurseOutput *runningInst = nullptr;
 class SelectionOutOfRange {};
@@ -501,7 +501,14 @@ bool CurseOutput::readInput()
 
             case '/':
             {
-                const std::string search_pattern = inputSearch();
+                std::string search_pattern;
+                try {
+                    search_pattern = inputSearch();
+                }
+                catch (interruptedException &e)
+                {
+                    return true;
+                }
                 search_result.clear();
                 if (search_pattern.empty())
                     return true;
@@ -608,11 +615,12 @@ void CurseOutput::unfold(const JSonElement *item)
 const std::string CurseOutput::inputSearch()
 {
     std::string buffer;
+    bool abort = false;
 
     curs_set(true);
     keypad(stdscr, false);
     wtimeout(stdscr, -1);
-    while (true)
+    while (!abort)
     {
         int c;
 
@@ -626,7 +634,8 @@ const std::string CurseOutput::inputSearch()
             if (!buffer.empty())
                 buffer.pop_back();
         }
-        // TODO check kill-key and throw noInputException
+        else if (c == 27)
+            abort = true;
         else
             buffer += c;
     }
@@ -634,6 +643,8 @@ const std::string CurseOutput::inputSearch()
     keypad(stdscr, true);
     curs_set(false);
 
+    if (abort)
+        throw interruptedException();
     return buffer;
 }
 

+ 8 - 0
src/except.cpp

@@ -0,0 +1,8 @@
+#include "except.hh"
+
+interruptedException::interruptedException()
+{ }
+
+interruptedException::~interruptedException()
+{ }
+