Added -r option for random module

This commit is contained in:
Bob Polis 2021-10-05 14:25:37 +02:00
parent 66b8909e67
commit c484e8e6e3

View File

@ -39,6 +39,7 @@ void print_help() {
std::cout << "usage: screensaver [-h|-l|--version]\n"; std::cout << "usage: screensaver [-h|-l|--version]\n";
std::cout << " -h, --help show this help text and exit\n"; std::cout << " -h, --help show this help text and exit\n";
std::cout << " -l, --list show available screensaver modules and exit\n"; std::cout << " -l, --list show available screensaver modules and exit\n";
std::cout << " -r, --random choose random module from available ones\n";
std::cout << " --version show version number and exit\n"; std::cout << " --version show version number and exit\n";
} }
@ -92,15 +93,16 @@ int main(int argc, const char * argv[]) {
sc::plugin<ScreensaverPlugin>::scan_plugins("/usr/local/share/screensaver/plugins", "saver"); sc::plugin<ScreensaverPlugin>::scan_plugins("/usr/local/share/screensaver/plugins", "saver");
#endif #endif
std::unique_ptr<ScreensaverPlugin> saver; std::unique_ptr<ScreensaverPlugin> saver;
bool random_saver {false};
int opt_char, opt_val; int opt_char, opt_val;
struct option long_options[] = { struct option long_options[] = {
{"help", no_argument, nullptr, 'h'}, {"help", no_argument, nullptr, 'h'},
{"list", no_argument, nullptr, 'l'}, {"list", no_argument, nullptr, 'l'},
{"random", no_argument, nullptr, 'r'},
{"version", no_argument, &opt_val, 1}, {"version", no_argument, &opt_val, 1},
{nullptr, 0, nullptr, 0} {nullptr, 0, nullptr, 0}
}; };
while ((opt_char = getopt_long(argc, const_cast<char* const *>(argv), "hl", long_options, nullptr)) != -1) { while ((opt_char = getopt_long(argc, const_cast<char* const *>(argv), "hlr", long_options, nullptr)) != -1) {
std::string arg {optarg ? optarg : ""}; std::string arg {optarg ? optarg : ""};
switch (opt_char) { switch (opt_char) {
case 0: { case 0: {
@ -118,13 +120,20 @@ int main(int argc, const char * argv[]) {
case 'l': case 'l':
list_plugins(); list_plugins();
return EXIT_SUCCESS; return EXIT_SUCCESS;
case 'r':
random_saver = true;
break;
case '?': case '?':
throw std::runtime_error("unrecognized option"); throw std::runtime_error("unrecognized option");
} }
} }
if (optind == argc) { if (optind == argc) {
// here when no file args // here when no file args
saver = sc::plugin<ScreensaverPlugin>::get("Default")(); if (random_saver) {
saver = sc::plugin<ScreensaverPlugin>::random_choice()();
} else {
saver = sc::plugin<ScreensaverPlugin>::get("Default")();
}
} }
for (int i = optind; i < argc; ++i) { for (int i = optind; i < argc; ++i) {
try { try {