-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstamen.cpp
122 lines (101 loc) · 2.46 KB
/
stamen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <cstddef>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_set>
#include <utility>
#include "stamen/stamen.hpp"
namespace {
void warning(const std::string& message, const std::string& addition)
{
std::cerr << "Stamen: " << message;
if (!addition.empty())
{
std::cerr << ": " + addition;
}
std::cerr << '\n' << std::flush;
}
} // namespace
namespace stamen {
Stamen::Stamen(std::istream& ist)
{
using namespace std::placeholders; // NOLINT
std::unordered_set<std::string> refd;
std::string line;
std::string delim;
std::string code;
std::string prompt;
auto last = m_menu_lookup.end();
while (std::getline(ist, line))
{
if (line.empty()) continue;
std::istringstream iss(line);
iss >> delim >> code >> std::ws;
std::getline(iss, prompt);
if (delim != "+")
{
last->second.insert(code,
prompt,
[&](std::size_t idx)
{ return this->display_stub(idx); });
refd.insert(code);
}
else
{
const auto [iter, succ] =
m_menu_lookup.emplace(code, Menu(code, prompt));
last = iter;
}
}
for (const auto& ref : refd)
{
if (!m_menu_lookup.contains(ref))
{
m_free_lookup.emplace(ref, nullptr);
}
}
}
void Stamen::insert(const std::string& code, const callback_f& callback)
{
auto itr = m_free_lookup.find(code);
if (itr == m_free_lookup.end())
{
warning("unknown callback registration", code);
return;
}
itr->second = callback;
}
int Stamen::dynamic(const std::string& code, const display_f& disp)
{
m_stub_default = code;
m_stub_display = disp;
return display_stub(0);
}
int Stamen::display_stub(std::size_t idx)
{
const std::string& code = !m_stub_stack.empty()
? m_stub_stack.top()->item(idx).code
: m_stub_default;
const auto mit = m_menu_lookup.find(code);
if (mit != m_menu_lookup.end())
{
m_stub_stack.push(&mit->second);
const int ret = m_stub_display(mit->second);
m_stub_stack.pop();
return ret;
}
const auto fit = m_free_lookup.find(code);
if (fit != m_free_lookup.end() && fit->second != nullptr)
{
return fit->second(0);
}
warning("no callback for", code);
return 1;
}
void Menu::insert(const std::string& code,
const std::string& prompt,
const callback_f& callback)
{
m_items.emplace_back(code, prompt, callback);
}
} // namespace stamen