Use GNU readline for input.

This commit is contained in:
Danila Fedorin 2018-08-16 23:24:22 -07:00
parent 5f681a5971
commit ecfbd8deae
2 changed files with 9 additions and 4 deletions

View File

@ -7,6 +7,6 @@ set(CMAKE_CXX_STANDARD 11)
add_subdirectory(external/libabacus) add_subdirectory(external/libabacus)
add_executable(cmdAbacus src/main.cpp) add_executable(cmdAbacus src/main.cpp)
target_link_libraries(cmdAbacus abacus ${MPFR_LIBRARIES}) target_link_libraries(cmdAbacus abacus ${MPFR_LIBRARIES} readline)
target_include_directories(cmdAbacus PUBLIC include ${MPFR_INCLUDE_DIRS}) target_include_directories(cmdAbacus PUBLIC include ${MPFR_INCLUDE_DIRS})
target_compile_options(cmdAbacus PUBLIC ${MPFR_CFLAGS_OTHER}) target_compile_options(cmdAbacus PUBLIC ${MPFR_CFLAGS_OTHER})

View File

@ -1,6 +1,8 @@
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <mpfr.h> #include <mpfr.h>
#include <readline/readline.h>
#include <readline/history.h>
extern "C" { extern "C" {
#include "libabacus.h" #include "libabacus.h"
#include "value.h" #include "value.h"
@ -299,7 +301,7 @@ abacus::~abacus() {
} }
int main() { int main() {
abacus ab; abacus ab;
std::string buffer; rl_bind_key('\t', rl_insert);
ab.add_function("quit", function_quit, "()->unit"); ab.add_function("quit", function_quit, "()->unit");
ab.add_function("request_precision", function_request_precision, "(num)->unit"); ab.add_function("request_precision", function_request_precision, "(num)->unit");
@ -322,8 +324,11 @@ int main() {
ab.add_operator_prefix("-", "negate"); ab.add_operator_prefix("-", "negate");
while(!close_requested) { while(!close_requested) {
std::cout << "> "; char* data = readline(" > ");
std::getline(std::cin, buffer); std::string buffer(data);
add_history(data);
free(data);
abacus_ref value = ab.run(buffer); abacus_ref value = ab.run(buffer);
if(value == nullptr) { if(value == nullptr) {
std::cout << "Invalid expression." << std::endl; std::cout << "Invalid expression." << std::endl;