Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

add methods to prompt and validate different form of choice index

Geo91c75af6a4fe
diff --git a/src/Managers/UserInputManager.cpp b/src/Managers/UserInputManager.cpp
index 0ee5c8ec..dd190656 100644
--- a/src/Managers/UserInputManager.cpp
+++ b/src/Managers/UserInputManager.cpp
@@ -321,4 +321,64 @@ int UserInputManager::readValidatedChoiceIndex(const std::string& label, const s
}
return index - 1; // Convert to 0 based index
+}
+
+int UserInputManager::readValidatedInt(const std::string& label, int def, int min /* = -127 */, int max /* = 127 */) {
+ while (true) {
+ terminalView.print(label + " [" + std::to_string(def) + "]: ");
+ std::string input = getLine();
+ if (input.empty()) return def;
+
+ int val = 0;
+ if (argTransformer.parseInt(input, val) && val >= min && val <= max) {
+ return val;
+ }
+ terminalView.println("Invalid input. Must be " + std::to_string(min) + "-" + std::to_string(max));
+ }
+}
+
+int UserInputManager::readValidatedChoiceIndex(const std::string& label, const std::vector<int>& choices, int defaultIndex) {
+ std::vector<std::string> strChoices;
+ strChoices.reserve(choices.size());
+ for (int val : choices) {
+ strChoices.push_back(std::to_string(val));
+ }
+ return readValidatedChoiceIndex(label, strChoices, defaultIndex);
+}
+
+int UserInputManager::readValidatedChoiceIndex(const std::string& label,
+ const std::vector<float>& choices,
+ int defaultIndex) {
+ std::vector<std::string> strChoices;
+ strChoices.reserve(choices.size());
+
+ for (float f : choices) {
+ std::ostringstream oss;
+ oss.setf(std::ios::fixed);
+ oss << std::setprecision(2) << f;
+ strChoices.push_back(oss.str());
+ }
+ return readValidatedChoiceIndex(label, strChoices, defaultIndex);
+}
+
+float UserInputManager::readValidatedFloat(const std::string& label,
+ float def,
+ float min,
+ float max) {
+ while (true) {
+ terminalView.print(label + " [" + std::to_string(def) + "]: ");
+ std::string input = getLine();
+ if (input.empty()) return def;
+
+ // Remove spaces
+ input.erase(std::remove_if(input.begin(), input.end(), ::isspace), input.end());
+
+ try {
+ float v = std::stof(input);
+ if (v >= min && v <= max) return v;
+ } catch (...) {
+ // fallthrough
+ }
+ terminalView.println("Invalid input. Must be " + std::to_string(min) + " .. " + std::to_string(max));
+ }
}
\ No newline at end of file
diff --git a/src/Managers/UserInputManager.h b/src/Managers/UserInputManager.h
index ba071bf7..52c7e137 100644
--- a/src/Managers/UserInputManager.h
+++ b/src/Managers/UserInputManager.h
@@ -18,6 +18,8 @@ public:
uint8_t readValidatedUint8(const std::string& label, uint8_t def, uint8_t min, uint8_t max);
uint8_t readValidatedUint8(const std::string& label, uint8_t defaultVal);
uint32_t readValidatedUint32(const std::string& label, uint32_t def);
+ int readValidatedInt(const std::string& label, int def, int min = -127, int max = 127);
+
char readCharChoice(const std::string& label, char def, const std::vector<char>& allowed);
bool readYesNo(const std::string& label, bool def);
uint8_t readModeNumber();
@@ -28,9 +30,17 @@ public:
const std::vector<uint8_t>& defaultPins,
const std::vector<uint8_t>& protectedPins
);
+ float readValidatedFloat(const std::string& label,
+ float def,
+ float min = -1e9f,
+ float max = 1e9f
+ );
+
std::string readValidatedHexString(const std::string& label, size_t numBytes, bool ignoreLen = false, size_t digitsPerItem = 2);
uint16_t readValidatedCanId(const std::string& label, uint16_t defaultValue);
int readValidatedChoiceIndex(const std::string& label, const std::vector<std::string>& choices, int defaultIndex = 0);
+ int readValidatedChoiceIndex(const std::string& label, const std::vector<int>& choices, int defaultIndex);
+ int readValidatedChoiceIndex(const std::string& label, const std::vector<float>& choices, int defaultIndex);
private:
ITerminalView& terminalView;
IInput& terminalInput;