stamen

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

stamen.cpp (1451B)


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