stamen

Stamen - static menu generator
git clone git://git.dimitrijedobrota.com/stamen.git
Log | Files | Refs | README | LICENSE

commit 1d14de3dcbe573ba1a156facdc0d477558f3c97e
parent 260569531d18b4c35adeaa7b53c866ebea1e2d28
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed,  8 Nov 2023 18:59:02 +0000

Print menu topology

Diffstat:
Minclude/menu.h | 2++
Msrc/CMakeLists.txt | 12++++++++++++
Msrc/display.cpp | 18++++++++++++++++++
Msrc/main.cpp | 3++-
4 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/include/menu.h b/include/menu.h @@ -35,6 +35,7 @@ public: } } + void print(const std::string code = "main", const int depth = 1) const; void operator()() const { get_callback("main")(); } struct record_t { @@ -60,6 +61,7 @@ private: }; struct function_t { + friend class Menu; function_t(const Menu &m, const std::string &n, const std::vector<item_t> &i) : menu(m), name(n), items(i) {} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt @@ -17,3 +17,15 @@ set_target_properties(demo PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR} RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" ) + +add_executable(generate generate.cpp) + +target_include_directories(generate + PUBLIC ../include +) + +set_target_properties(generate PROPERTIES + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" +) diff --git a/src/display.cpp b/src/display.cpp @@ -40,3 +40,21 @@ int Menu::function_t::display() const { return 1; } + +void Menu::print(const std::string code, const int depth) const { + const auto it = lookup.find(code); + if (it == lookup.end()) return; + const callback_t &cb = it->second; + + if (depth == 1) { + std::cout << std::format("{}({})\n", cb.menu_func->name, code); + } + + if (cb.menu_func) { + for (const auto &item : cb.menu_func->items) { + std::cout << std::format("{}{}({})\n", std::string(depth << 1, ' '), + item.prompt, item.callback); + if (cb.menu_func) cb.menu_func->menu.print(item.callback, depth + 1); + } + } +} diff --git a/src/main.cpp b/src/main.cpp @@ -25,6 +25,7 @@ int main(void) { { "settings", settings}, { "finish", finish} }); - menu(); + // menu(); + menu.print(); return 0; }