Files

72 lines
2.0 KiB
C++

#include "ui/app_window.hpp"
#include <cstdlib>
#include <iostream>
#include <string>
namespace {
bool parse_geometry(const std::string& geom, int& w_out, int& h_out) {
size_t x_pos = geom.find('x');
if (x_pos == std::string::npos) x_pos = geom.find('X');
if (x_pos == std::string::npos || x_pos == 0) return false;
size_t end_h = geom.find_first_of("+-", x_pos + 1);
std::string ws = geom.substr(0, x_pos);
std::string hs = (end_h == std::string::npos)
? geom.substr(x_pos + 1)
: geom.substr(x_pos + 1, end_h - (x_pos + 1));
if (ws.empty() || hs.empty()) return false;
int w = std::atoi(ws.c_str());
int h = std::atoi(hs.c_str());
if (w <= 0 || h <= 0) return false;
w_out = w;
h_out = h;
return true;
}
void print_usage(const char* prog) {
std::cout << "Usage: " << prog << " [OPTIONS] [FILE]\n"
<< "Options:\n"
<< " -g, --geometry WxH Initial window dimensions (e.g. 240x296 or 960x640)\n"
<< " -h, --help Show this help message\n";
}
} // namespace
int main(int argc, char** argv) {
int req_w = 960;
int req_h = 640;
bool explicit_geom = false;
std::string initial_file;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "-h" || arg == "--help") {
print_usage(argv[0]);
return 0;
}
if ((arg == "-g" || arg == "--geometry") && i + 1 < argc) {
if (!parse_geometry(argv[++i], req_w, req_h)) {
std::cerr << "Invalid geometry: " << argv[i] << "\n";
return 1;
}
explicit_geom = true;
continue;
}
if (arg.rfind("--geometry=", 0) == 0) {
if (!parse_geometry(arg.substr(11), req_w, req_h)) {
std::cerr << "Invalid geometry: " << arg.substr(11) << "\n";
return 1;
}
explicit_geom = true;
continue;
}
if (!arg.empty() && arg[0] != '-') {
initial_file = arg;
}
}
htab::ui::AppWindow app;
return app.run(req_w, req_h, explicit_geom, initial_file);
}