stamenStatic Menu Generator |
git clone git://git.dimitrijedobrota.com/stamen.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
static.cpp (2108B)
0 #include <cmath> 1 #include <cstddef> 2 #include <format> 3 #include <iostream> 4 #include <limits> 5 6 #include "demo_menu.hpp" 7 8 namespace example { 9 10 int menu_t::visit(const menu_t& menu) 11 { 12 const int sizei = static_cast<int>(menu.items.size()); 13 const size_t dgts = static_cast<size_t>(std::log10(sizei)) + 1; 14 int choice = 0; 15 16 while (true) 17 { 18 std::cout << std::format("{}:\n", menu.title); 19 for (auto i = 0UL; i < menu.items.size(); i++) 20 { 21 std::cout << std::format(" {:{}}. {}\n", i, dgts, menu.items[i].prompt); 22 } 23 24 while (true) 25 { 26 std::cout << "Choose an option: "; 27 if (std::cin >> choice && choice >= -1 && choice < sizei) 28 { 29 if (choice == -1) 30 { 31 std::cout << "Choice: back\n"; 32 return 1; 33 } 34 35 const auto uchoice = static_cast<size_t>(choice); 36 std::cout << "Choice: " << menu.items[uchoice].prompt << "\n\n"; 37 const int res = menu.items[uchoice].callback(uchoice); 38 39 if (res < 2) break; 40 return res - 1; 41 } 42 43 if (std::cin.eof()) 44 { 45 std::cerr << "encountered end of input!\n"; 46 return std::numeric_limits<int>::max(); 47 } 48 49 std::cin.clear(); 50 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 51 std::cout << "Invalid option, please choose again!\n"; 52 } 53 54 std::cout << '\n' << std::flush; 55 } 56 57 return 1; 58 } 59 60 int operation1(std::size_t /* unused */) 61 { 62 std::cout << "operation 1\n\n"; 63 std::cout << "Some operation is done\n\n"; 64 std::cout << std::flush; 65 return 1; 66 } 67 68 int operation2(std::size_t /* unused */) 69 { 70 std::cout << "operation 2\n"; 71 std::cout << "Some other operation is done\n"; 72 std::cout << std::flush; 73 return 1; 74 } 75 76 int operation3(std::size_t /* unused */) 77 { 78 std::cout << "operation 3\n"; 79 std::cout << "Yet another operation is done\n"; 80 std::cout << std::flush; 81 return 1; 82 } 83 84 int finish(std::size_t /* unused */) 85 { 86 std::cout << "finishing...\n"; 87 std::cout << std::flush; 88 exit(0); // NOLINT 89 } 90 91 } // namespace example 92 93 int main() 94 { 95 example::menu_main(0); 96 return 0; 97 }