From 10a7462e795bc88e409533fadeab012dd1859da2 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 22 Mar 2018 18:44:54 +0100 Subject: extsh: add linenoise input handling --- .gitmodules | 3 +++ CMakeLists.txt | 5 ++++- external/CMakeLists.txt | 3 +++ external/linenoise | 1 + src/CMakeLists.txt | 1 + src/extsh.cpp | 22 +++++++++++++++++----- 6 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 .gitmodules create mode 100644 external/CMakeLists.txt create mode 160000 external/linenoise 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 index 0000000..2105ce4 --- /dev/null +++ b/external/linenoise @@ -0,0 +1 @@ +Subproject commit 2105ce445821381cf1bca87b6d386d4ea88ee20d 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_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 + #include #include #include @@ -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 -- cgit v1.2.3