diff --git a/cli.cpp b/cli.cpp index 99b9eda4..1ec4bccd 100644 --- a/cli.cpp +++ b/cli.cpp @@ -892,12 +892,36 @@ bool OptionBrightness(std::vector* current_devices, std::string a return found; } -bool OptionSize(std::vector* current_devices, std::string argument, Options* /*options*/, std::vector& rgb_controllers) +bool OptionSize(std::vector* current_devices, std::string argument, Options* options, std::vector& rgb_controllers) { - const unsigned int new_size = std::stoi(argument); + int new_size_int = 0; + + try + { + new_size_int = std::stoi(argument); + } + catch(...) + { + std::cout << "Error: Invalid size \"" << argument << "\"" << std::endl; + return false; + } + + if(new_size_int < 0) + { + std::cout << "Error: New size is out of range" << std::endl; + return false; + } + + const unsigned int new_size = static_cast(new_size_int); ResourceManager::get()->WaitForDetection(); + if(current_devices->empty()) + { + std::cout << "Error: No devices for size \"" << argument << "\" (specify --device and --zone first)" << std::endl; + return false; + } + for(size_t i = 0; i < current_devices->size(); i++) { int current_device = current_devices->at(i).device; @@ -914,12 +938,13 @@ bool OptionSize(std::vector* current_devices, std::string argumen } else if((current_zone >= static_cast(rgb_controllers[current_device]->GetZoneCount())) || (current_zone < 0)) { - std::cout << "Error: Zone is out of range" << std::endl; + std::cout << "Error: Zone is out of range (specify --zone before --size)" << std::endl; return false; } else if((new_size < rgb_controllers[current_device]->GetZoneLEDsMin(current_zone)) || (new_size > rgb_controllers[current_device]->GetZoneLEDsMax(current_zone))) { std::cout << "Error: New size is out of range" << std::endl; + return false; } /*-------------------------------------------------*\ @@ -927,6 +952,27 @@ bool OptionSize(std::vector* current_devices, std::string argumen \*-------------------------------------------------*/ rgb_controllers[current_device]->ResizeZone(current_zone, new_size); + /*-------------------------------------------------*\ + | Mark this device as having a valid CLI option | + \*-------------------------------------------------*/ + current_devices->at(i).hasOption = true; + current_devices->at(i).hasSize = true; + current_devices->at(i).size = new_size; + + if(options != nullptr) + { + for(size_t j = 0; j < options->devices.size(); j++) + { + if(options->devices[j].device == current_device + && options->devices[j].zone == current_zone) + { + options->devices[j].hasOption = true; + options->devices[j].hasSize = true; + options->devices[j].size = new_size; + } + } + } + /*-------------------------------------------------*\ | Save configuration | \*-------------------------------------------------*/