diff options
| author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-26 18:29:44 +0200 | 
|---|---|---|
| committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-26 18:29:44 +0200 | 
| commit | a070c649ddbe70a22f6265b9f5b48f6bde7eac08 (patch) | |
| tree | 310c2a7f0ae51eff0172168673e64f50a01f01a1 | |
| parent | 6781cee55a4e7f232eae3e5f2a652045412339ae (diff) | |
add readline cli routines
| -rw-r--r-- | CMakeLists.txt | 7 | ||||
| -rw-r--r-- | backend/Dungeon.h | 6 | ||||
| -rw-r--r-- | frontend/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | frontend/cli.cpp | 35 | ||||
| -rw-r--r-- | frontend/cli.h | 8 | ||||
| -rw-r--r-- | frontend/main.cpp | 20 | ||||
| -rw-r--r-- | frontend/strings.cpp | 28 | ||||
| -rw-r--r-- | frontend/strings.h | 16 | 
8 files changed, 121 insertions, 1 deletions
| diff --git a/CMakeLists.txt b/CMakeLists.txt index 98c41b3..af125c8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,13 @@ project(main CXX)  add_executable(main) +target_include_directories(main PRIVATE .) +  add_subdirectory(backend)  add_subdirectory(frontend) -target_link_libraries(main pugixml) +target_link_libraries(main +	pugixml +	readline +) diff --git a/backend/Dungeon.h b/backend/Dungeon.h new file mode 100644 index 0000000..34fa776 --- /dev/null +++ b/backend/Dungeon.h @@ -0,0 +1,6 @@ +#pragma once + +class Dungeon { + +}; + diff --git a/frontend/CMakeLists.txt b/frontend/CMakeLists.txt index dd74089..eafbe55 100644 --- a/frontend/CMakeLists.txt +++ b/frontend/CMakeLists.txt @@ -1,4 +1,6 @@  target_sources(main PUBLIC  	main.cpp +	cli.cpp +	strings.cpp  ) diff --git a/frontend/cli.cpp b/frontend/cli.cpp new file mode 100644 index 0000000..352de4a --- /dev/null +++ b/frontend/cli.cpp @@ -0,0 +1,35 @@ +#include <cstdlib> +#include <cstdio> + +#include <readline/readline.h> +#include <readline/history.h> + +#include "cli.h" + +using namespace std; + +static void handle_line(const string & line) { +	printf("CMD: %s\n", line.c_str()); +} + +string cli_readline() { +	const char * PROMPT = "> "; + +	char * input = readline(PROMPT); +	// ctrl-d +	if (input == NULL) exit(EXIT_SUCCESS); +	string out = string(input); +	if (out.size() > 0) add_history(input); +	free(input); + +	return out; +} + +void cli_main() { +	while (1) { +		string cmd = cli_readline(); +		if (cmd.size() == 0) continue; +		handle_line(cmd); +	} +} + diff --git a/frontend/cli.h b/frontend/cli.h new file mode 100644 index 0000000..a069d7a --- /dev/null +++ b/frontend/cli.h @@ -0,0 +1,8 @@ +#pragma once + +#include <string> + +void cli_main(); + +std::string cli_readline(); + diff --git a/frontend/main.cpp b/frontend/main.cpp index acaf028..90cb1d2 100644 --- a/frontend/main.cpp +++ b/frontend/main.cpp @@ -1,7 +1,27 @@ +#include <memory>  #include <cstdlib>  #include <cstdio> +#include "backend/Dungeon.h" + +#include "cli.h" +#include "strings.h" + +using namespace std; +  int main() { +	auto dungeon = make_unique<Dungeon>(); + +	print_string(strings::INTRO); +	string filename = cli_readline(); +	if (filename.size() == 0) { +		printf("TODO: generate dungeon\n"); +	} else { +		printf("TODO: load %s\n", filename.c_str()); +	} + +	cli_main(); +  	return EXIT_SUCCESS;  } diff --git a/frontend/strings.cpp b/frontend/strings.cpp new file mode 100644 index 0000000..0d4dee0 --- /dev/null +++ b/frontend/strings.cpp @@ -0,0 +1,28 @@ +#include "strings.h" + +using namespace std; + +void print_string(const char * str) { +	printf("%s\n", wrap_string(str).c_str()); +} + +string wrap_string(const char * str) { +	string out; + +	for (; *str != '\0'; str++) { +		if (str[0] == '\n') { +			if (str[1] == '\n') { +				out += "\n\n"; +				str++; +			} else { +				out += " "; +			} +			continue; +		} + +		out += *str; +	} + +	return out; +} + diff --git a/frontend/strings.h b/frontend/strings.h new file mode 100644 index 0000000..910eaa8 --- /dev/null +++ b/frontend/strings.h @@ -0,0 +1,16 @@ +#pragma once + +#include <string> + +namespace strings { + +static constexpr const char * INTRO = R"(Voer de naam van een (.xml) bestand in +om hieruit een kerker te laden, of druk direct op ENTER om een kerker te +genereren.)"; + +} + +void print_string(const char *); + +std::string wrap_string(const char *); + |