-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
227 lines (189 loc) · 6.85 KB
/
main.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "src/backend.h"
#include "src/error.h"
#include "src/tanto.h"
#include <algorithm>
#include <cl/cl.h>
#include <fmt/core.h>
#include <fmt/ostream.h>
#include <fstream>
#include <iostream>
#include <memory>
#include <nlohmann/json.hpp>
#include <vector>
#if defined(BACKEND_QT)
#include "src/backends/qt/backendimpl.h"
#endif
#if defined(BACKEND_GTK)
#include "src/backends/gtk/backendimpl.h"
#endif
#if !defined(NDEBUG)
extern "C" const char* __lsan_default_options() { // NOLINT
return "suppressions=leak.supp:print_suppressions=0";
} // NOLINT
#endif
namespace {
const std::vector<std::pair<std::string_view, std::string_view>> BACKENDS {
#if defined(BACKEND_GTK)
{"gtk", BackendGtkImpl::version()},
#endif
#if defined(BACKEND_QT)
{"qt", BackendQtImpl::version()},
#endif
};
std::string selectedbackend;
using BackendPtr = std::unique_ptr<Backend>;
[[nodiscard]] BackendPtr new_backend(const std::string& name, int argc,
char** argv) {
#if defined(BACKEND_GTK)
if(name == "gtk")
return std::make_unique<BackendGtkImpl>(argc, argv);
#endif // defined(BACKEND_GTK)
#if defined(BACKEND_QT)
if(name == "qt")
return std::make_unique<BackendQtImpl>(argc, argv);
#endif // defined(BACKEND_QT)
except("Backend '{}' not found", name);
}
bool has_backend(std::string_view n) {
return std::find_if(BACKENDS.begin(), BACKENDS.end(), [n](const auto& x) {
return x.first == n;
}) != BACKENDS.end();
}
tanto::FilterList parse_filter(const cl::Arg& arg) {
return tanto::parse_filter(arg ? arg.to_stringview() : std::string_view{});
}
std::string read_stdin() {
std::string input, line;
while(std::getline(std::cin, line)) {
if(!input.empty())
input += "\n";
input += line;
}
return input;
}
int execute_mode(const BackendPtr& backend, cl::Args& args) {
if(args["message"].to_bool() || args["confirm"].to_bool()) {
Backend::MessageIcon icon = Backend::MessageIcon::NONE;
if(args["info"].to_bool())
icon = Backend::MessageIcon::INFO;
else if(args["question"].to_bool())
icon = Backend::MessageIcon::QUESTION;
else if(args["warning"].to_bool())
icon = Backend::MessageIcon::WARNING;
else if(args["error"].to_bool())
icon = Backend::MessageIcon::ERROR;
backend->message(args["title"].to_string(), args["text"].to_string(),
args["confirm"].to_bool()
? Backend::MessageType::CONFIRM
: Backend::MessageType::MESSAGE,
icon);
}
else if(args["input"].to_bool() || args["password"].to_bool()) {
std::string text =
args["text"] ? args["text"].to_string() : std::string{};
std::string value =
args["value"] ? args["value"].to_string() : std::string{};
backend->input(args["title"].to_string(), text, value,
args["password"].to_bool() ? Backend::InputType::PASSWORD
: Backend::InputType::NORMAL);
}
else if(args["selectdir"].to_bool()) {
backend->select_dir(
args["title>"] ? args["title"].to_string() : std::string{},
args["dir>"] ? args["dir"].to_string() : std::string{});
}
else if(args["loadfile"].to_bool()) {
backend->load_file(
args["title>"] ? args["title"].to_string() : std::string{},
parse_filter(args["filter"]),
args["dir>"] ? args["dir"].to_string() : std::string{});
}
else if(args["savefile"].to_bool()) {
backend->save_file(
args["title"] ? args["title"].to_string() : std::string{},
parse_filter(args["filter"]),
args["dir"] ? args["dir"].to_string() : std::string{});
}
else
unreachable;
return 0;
}
bool needs_json(cl::Args& args) {
return args["stdin"].to_bool() || args["load"].to_bool();
}
int execute_json(const BackendPtr& backend, cl::Args& args) {
nlohmann::json jsonreq;
try {
if(args["stdin"].to_bool())
jsonreq = nlohmann::json::parse(read_stdin());
else if(args["load"].to_bool()) {
std::ifstream f{std::string{args["filename"].to_string()}};
jsonreq = nlohmann::json::parse(f);
}
else
unreachable;
}
catch(nlohmann::json::parse_error& e) {
spdlog::critical(e.what());
}
auto window = tanto::parse(jsonreq);
if(window)
backend->process(*window);
return backend->run();
}
} // namespace
int main(int argc, char** argv) {
using namespace cl::string_literals;
// clang-format off
cl::set_program("tanto");
cl::set_name("Tanto");
cl::set_description("The Universal GUI");
cl::set_version("1.0");
cl::Options{
cl::opt("d", "debug", "Debug mode"),
cl::opt("b", "backend"_arg, "Select backend"),
};
cl::Usage{
cl::cmd("stdin", *--"debug"__, *--"backend"__),
cl::cmd("load", "filename", *--"debug"__, *--"backend"__),
cl::cmd("message", "title", "text", *cl::one("info", "question", "warning", "error"), *--"debug"__, *--"backend"__),
cl::cmd("confirm", "title", "text", *cl::one("info", "question", "warning", "error"), *--"debug"__, *--"backend"__),
cl::cmd("input", "title", *"text"__, *"value"__, *--"debug"__, *--"backend"__),
cl::cmd("password", "title", *"text"__, *--"debug"__, *--"backend"__),
cl::cmd("selectdir", *"title"__, *"dir"__, *--"debug"__, *--"backend"__),
cl::cmd("loadfile", *"title"__, *"filter"__, *"dir"__, *--"debug"__, *--"backend"__),
cl::cmd("savefile", *"title"__, *"filter"__, *"dir"__, *--"debug"__, *--"backend"__),
cl::cmd("list", *--"debug"__),
};
// clang-format on
if(BACKENDS.empty()) {
fmt::println("ERROR: No backends available");
return 2;
}
selectedbackend = BACKENDS.begin()->first;
auto args = cl::parse(argc, argv);
if(args["debug"].to_bool()) {
for(const auto& arg : args)
fmt::println("{} - {}", arg.first, arg.second.dump());
}
if(args["list"].to_bool()) {
for(const auto& [name, version] : BACKENDS)
fmt::println("{}: {}", name, version);
return 0;
}
if(!args["backend"]) {
char* envbackend = std::getenv("TANTO_BACKEND");
if(envbackend)
selectedbackend = envbackend;
}
else
selectedbackend = args["backend"].to_string();
if(!has_backend(selectedbackend)) {
fmt::println("ERROR: Unsupported backend '{}'", selectedbackend);
return 1;
}
BackendPtr backend = new_backend(selectedbackend, argc, argv);
if(needs_json(args))
return execute_json(backend, args);
return execute_mode(backend, args);
}