diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2018-03-22 18:44:54 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2018-03-22 18:44:54 +0100 |
| commit | 10a7462e795bc88e409533fadeab012dd1859da2 (patch) | |
| tree | 0743a8fecd401db32636fd29c278ad5cc593c096 | |
| parent | 66f66b7818b7cf47a20eae6b940e06c2ee12fe8a (diff) | |
| download | extfs-10a7462e795bc88e409533fadeab012dd1859da2.tar.xz extfs-10a7462e795bc88e409533fadeab012dd1859da2.zip | |
extsh: add linenoise input handlingmaster
| -rw-r--r-- | .gitmodules | 3 | ||||
| -rw-r--r-- | CMakeLists.txt | 5 | ||||
| -rw-r--r-- | external/CMakeLists.txt | 3 | ||||
| m--------- | external/linenoise | 0 | ||||
| -rw-r--r-- | src/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/extsh.cpp | 22 |
6 files changed, 28 insertions, 6 deletions
diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..d0c7fc0 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "external/linenoise"] + path = external/linenoise + url = https://github.com/antirez/linenoise diff --git a/CMakeLists.txt b/CMakeLists.txt index be4bea8..4000d4c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.2) project("extfs" - LANGUAGES CXX + LANGUAGES C CXX VERSION 1.0 ) @@ -18,7 +18,10 @@ include("ConanPackages") option(EXTFS_BUILD_STATIC "Build extfs as a static library" ON) option(EXTFS_ENABLE_TESTS "Enable CUTE unit tests" ON) +add_subdirectory("external") + include_directories("include") +include_directories(SYSTEM "external/linenoise") add_subdirectory("src") if(${EXTFS_ENABLE_TESTS}) diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt new file mode 100644 index 0000000..522aee8 --- /dev/null +++ b/external/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(linenoise OBJECT + "linenoise/linenoise.c" + ) diff --git a/external/linenoise b/external/linenoise new file mode 160000 +Subproject 2105ce445821381cf1bca87b6d386d4ea88ee20 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e09c4a2..467cf46 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,6 +2,7 @@ add_subdirectory(fs) add_executable(extsh extsh.cpp + $<TARGET_OBJECTS:linenoise> ) target_link_libraries(extsh extfs diff --git a/src/extsh.cpp b/src/extsh.cpp index 9dfeea8..1c223d9 100644 --- a/src/extsh.cpp +++ b/src/extsh.cpp @@ -1,5 +1,7 @@ #include "fs/extfs.hpp" +#include <linenoise.h> + #include <cstdint> #include <iostream> #include <stdexcept> @@ -15,7 +17,7 @@ enum struct result : std::uint8_t result process(std::string const & command) { - if(command == "exit") + if(command == "exit" || command.empty()) { return result::exit; } @@ -25,10 +27,19 @@ result process(std::string const & command) std::string prompt(fs::extfs const & disk) { - std::cout << '[' << (disk.has_label() ? disk.label() : "No Label") << "] >>> "; - std::string command{}; - std::cin >> command; - return command; + using namespace std::string_literals; + + auto const promptText = "["s + (disk.has_label() ? disk.label() : "No Label") + "] > "; + auto const input = linenoise(promptText.c_str()); + if(input) + { + linenoiseHistoryAdd(input); + auto const inputString = std::string{input}; + linenoiseFree(input); + return inputString; + } + + return {}; } void repl(fs::extfs & disk) @@ -59,6 +70,7 @@ int main(int argc, char const * argv[]) if(disk.open()) { + linenoiseHistorySetMaxLen(1024); repl(disk); } else |
