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 (1465B)


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