stamen

Static Menu Generator
git clone git://git.dimitrijedobrota.com/stamen.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

stamen.cpp (1431B)


0 #include <cmath> 1 #include <format> 2 #include <iostream> 3 4 #include "stamen/stamen.hpp" 5 6 #include "stamen/menu.h" 7 8 namespace stamen { 9 10 int builtin_display(const char* title, const item_t itemv[], size_t size) 11 { 12 const auto items = std::span(itemv, size); 13 const size_t dgts = static_cast<size_t>(std::log10(size)) + 1; 14 int choice = 0; 15 16 while (true) 17 { 18 std::cout << std::format("{}:\n", title); 19 for (auto i = 0UL; i < size; i++) 20 { 21 std::cout << std::format(" {:{}}. {}\n", i, dgts, items[i].prompt); 22 } 23 24 while (true) 25 { 26 std::cout << "Choose an option: "; 27 if (std::cin >> choice && choice >= -1 28 && choice < static_cast<int>(size)) 29 { 30 if (choice == -1) 31 { 32 std::cout << "Choice: back\n"; 33 return 1; 34 } 35 36 const auto uchoice = static_cast<size_t>(choice); 37 std::cout << "Choice: " << items[uchoice].prompt << "\n\n"; 38 const int res = items[uchoice].callback(uchoice); 39 40 if (res < 2) break; 41 return res - 1; 42 } 43 44 if (std::cin.eof()) 45 { 46 std::cerr << "encountered end of input!\n"; 47 return std::numeric_limits<int>::max(); 48 } 49 50 std::cin.clear(); 51 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 52 std::cout << "Invalid option, please choose again!\n"; 53 } 54 std::cout << std::endl; 55 } 56 57 return 1; 58 } 59 60 } // namespace stamen