diff --git a/src/commands.hxx b/src/commands.hxx index 92f3885..02c3147 100644 --- a/src/commands.hxx +++ b/src/commands.hxx @@ -2365,7 +2365,7 @@ class If: public CommandContainer { } QString description() const { return - tag()+" \n" + tag()+" [not] \n" " \n" " \n" " <...>\n" @@ -2374,7 +2374,7 @@ class If: public CommandContainer { " \n" " <...>\n" "\n\n"+ - tag()+" -> \n" + tag()+" [not] -> \n" " \n" " \n" " <...>\n" @@ -2392,10 +2392,11 @@ class If: public CommandContainer { "The second variant checks for a text in a selector, " "similar to command exists. The text can be empty to just " "check for the existence of a selector. " - "There is an optional else part."; + "There is an optional else part. Optionally start with " + "not to invert the test."; } QString command() const { - return tag()+" "+_variable+" "+_cmp+" "+_value + return tag()+(_not?" not ":" ")+_variable+" "+_cmp+" "+_value +(_script.get()?"\n "+_script->print().join("\n "):"") +(_else.get()?"\nelse\n "+_else->print().join("\n "):""); } @@ -2403,6 +2404,11 @@ class If: public CommandContainer { QStringList& in, QString file, int line, int indent) { std::shared_ptr cmd(new If()); + QRegularExpressionMatch m; + if (args.contains(QRegularExpression("^ *not +"), &m)) { + args.remove(0, m.capturedLength()); + cmd->_not = true; + } int pos(args.indexOf(QRegularExpression("[=!.^~<>]"))); int len(1); if (args.contains("->")) { @@ -2437,8 +2443,9 @@ class If: public CommandContainer { break; } } - log(QString("evaluated expression to ")+(check?"true":"false")+": " - +selector+" "+_cmp+" "+value); + if (_not) check=!check; + log(QString("evaluated expression to ")+(check?"true":"false") + +(_not?": not ":": ")+selector+" "+_cmp+" "+value); } else { switch (_cmp[0].toLatin1()) { case '=': check = script->variable(_variable)==value; @@ -2458,8 +2465,9 @@ class If: public CommandContainer { break; default:; } - log(QString("evaluated expression to ")+(check?"true":"false")+": " - +script->variable(_variable)+" "+_cmp+" "+value); + if (_not) check=!check; + log(QString("evaluated expression to ")+(check?"true":"false") + +(_not?": not ":": ")+script->variable(_variable)+" "+_cmp+" "+value); } if (check) return runScript(log, this, _script, script, frame); else if (_else) return runScript(log, this, _else, script, frame); @@ -2475,6 +2483,7 @@ class If: public CommandContainer { QString _cmp; QString _value; std::shared_ptr