diff options
author | Slendi <slendi@socopon.com> | 2024-03-06 00:08:24 +0200 |
---|---|---|
committer | Slendi <slendi@socopon.com> | 2024-03-06 00:08:24 +0200 |
commit | 32be283591d23b2b071b75961201119808fe067f (patch) | |
tree | 37558730479fef31f143492787706d0eb8243a5e | |
parent | 8a70a42f859273cfb6dfaf608068856f7cdf17bd (diff) |
Add support for command line arguments
Signed-off-by: Slendi <slendi@socopon.com>
-rw-r--r-- | config.h | 15 | ||||
-rw-r--r-- | main.cpp | 134 |
2 files changed, 130 insertions, 19 deletions
@@ -1,7 +1,14 @@ #ifdef _WIN32 -static char const constexpr *const FONT_PATH = - "C:\\Windows\\Fonts\\DejaVuSans.ttf"; +static char const constexpr *const FONT_PATH = "C:\\Windows\\Fonts\\DejaVuSans.ttf"; #else -static char const constexpr *const FONT_PATH = - "/usr/share/fonts/TTF/DejaVuSans.ttf"; +static char const constexpr *const FONT_PATH = "/usr/share/fonts/TTF/DejaVuSans.ttf"; #endif + +#define PROGRESS_BAR_DEFAULT_POSITION ProgressBarPosition::BOTTOM +#define PROGRESS_BAR_DEFAULT_COLOR BLACK +#define TEXT_DEFAULT_COLOR BLACK +#define BACKGROUND_DEFAULT_COLOR WHITE + +#define PADDING_X_DEFAULT 32 +#define PADDING_Y_DEFAULT 32 + @@ -18,18 +18,18 @@ struct Config { int width = 1280; int height = 720; - int padding_x = 32; - int padding_y = 32; + int padding_x = PADDING_X_DEFAULT; + int padding_y = PADDING_Y_DEFAULT; bool fullscreen = true; std::string font_path = FONT_PATH; int font_size = 128; - ProgressBarPosition progress_bar_position = ProgressBarPosition::BOTTOM; + ProgressBarPosition progress_bar_position = PROGRESS_BAR_DEFAULT_POSITION; - Color background_color = WHITE; - Color text_color = BLACK; - Color progress_bar_color = BLACK; + Color background_color = BACKGROUND_DEFAULT_COLOR; + Color text_color = TEXT_DEFAULT_COLOR; + Color progress_bar_color = PROGRESS_BAR_DEFAULT_COLOR; }; struct Slide { @@ -308,19 +308,123 @@ Slides parse_input(std::istream *stream) { return slides; } -int main(int argc, char **argv) { - std::istream *is = &std::cin; - if (argc > 1) { - auto file = new std::ifstream(argv[1]); - if (!file->is_open()) { - std::cerr << "Could not open file: " << argv[1] << std::endl; - return 1; +Color parse_color(const std::string &color) { + if (color.size() != 6) + throw std::runtime_error("Invalid color: " + color); + + unsigned char r = std::stoi(color.substr(0, 2), nullptr, 16); + unsigned char g = std::stoi(color.substr(2, 2), nullptr, 16); + unsigned char b = std::stoi(color.substr(4, 2), nullptr, 16); + + return {r, g, b, 255}; +} + +std::pair<int, Config> parse_arguments(int argc, char **argv, std::istream **is) { + Config conf {}; + + *is = &std::cin; + for (int i = 1; i < argc; i++) { + std::string arg = argv[i]; + if (arg == "-h" || arg == "--help") { + std::cout << "Usage: " << argv[0] << " [FILE]" << std::endl + << "If FILE is not provided, input will be read from stdin." << std::endl + << "Options:" << std::endl + << " -pos <BOTTOM|TOP|LEFT|RIGHT> Set the position of the progress bar" << std::endl + << " -bg <rgb_hex> Set the background color" << std::endl + << " -fg <rgb_hex> Set the text color" << std::endl + << " -pb <rgb_hex> Set the progress bar color" << std::endl + << " -w Start in windowed mode" << std::endl + << " -px <int> Set the horizontal padding" << std::endl + << " -py <int> Set the vertical padding" << std::endl; + exit(0); + } else if (arg == "-") { // Read from stdin + continue; + } else if (arg == "-w") { + conf.fullscreen = false; + } else if (arg == "-pos") { + if (i + 1 < argc) { + i++; + std::string pos = argv[i]; + for (auto &c: pos) c = toupper(c); + if (pos == "BOTTOM") { + conf.progress_bar_position = Config::ProgressBarPosition::BOTTOM; + } else if (pos == "TOP") { + conf.progress_bar_position = Config::ProgressBarPosition::TOP; + } else if (pos == "LEFT") { + conf.progress_bar_position = Config::ProgressBarPosition::LEFT; + } else if (pos == "RIGHT") { + conf.progress_bar_position = Config::ProgressBarPosition::RIGHT; + } else { + std::cerr << "Invalid position: " << pos << std::endl; + return {0, conf}; + } + } else { + std::cerr << "Position not provided" << std::endl; + return {0, conf}; + } + } else if (arg == "-bg") { + if (i + 1 < argc) { + i++; + conf.background_color = parse_color(argv[i]); + } else { + std::cerr << "Background color not provided" << std::endl; + return {0, conf}; + } + } else if (arg == "-fg") { + if (i + 1 < argc) { + i++; + conf.text_color = parse_color(argv[i]); + } else { + std::cerr << "Text color not provided" << std::endl; + return {0, conf}; + } + } else if (arg == "-pb") { + if (i + 1 < argc) { + i++; + conf.progress_bar_color = parse_color(argv[i]); + } else { + std::cerr << "Progress bar color not provided" << std::endl; + return {0, conf}; + } + } else if (arg == "-px") { + if (i + 1 < argc) { + i++; + conf.padding_x = std::stoi(argv[i]); + } else { + std::cerr << "Horizontal padding not provided" << std::endl; + return {0, conf}; + } + } else if (arg == "-py") { + if (i + 1 < argc) { + i++; + conf.padding_y = std::stoi(argv[i]); + } else { + std::cerr << "Vertical padding not provided" << std::endl; + return {0, conf}; + } + } else { + auto file = new std::ifstream(argv[1]); + if (!file->is_open()) { + std::cerr << "Could not open file: " << argv[1] << std::endl; + return {0, conf}; + } + *is = file; } - is = file; } + return {1, conf}; +} + +int main(int argc, char **argv) { + std::istream *is; + auto ret = parse_arguments(argc, argv, &is); + if (!ret.first) + return 1; + + auto conf = ret.second; + auto slides = parse_input(is); - Application app({}, slides); + Application app(conf, slides); app.run(); return 0; } |