From 22276404e5c3f3833a2de5f1971be24f0729aa6e Mon Sep 17 00:00:00 2001
From: Adrian Kummerlaender
Date: Sun, 9 Aug 2015 20:10:38 +0200
Subject: Implement basic command dispatch logic
Moved command implementation into separate ressource.
Commands are plain JavaScript functions in this separate ressource and as such callable via a simple call to `eval`.
Introduced the concept of a command prefix to enable implementation of e.g. a search command analogously to _vim_.
---
qml/CommandInput.qml | 19 ++++++++++++-------
qml/StateHandler.qml | 2 +-
qml/commands.js | 19 +++++++++++++++++++
qml/ui.qrc | 1 +
4 files changed, 33 insertions(+), 8 deletions(-)
create mode 100644 qml/commands.js
(limited to 'qml')
diff --git a/qml/CommandInput.qml b/qml/CommandInput.qml
index 835ccae..e7defbf 100644
--- a/qml/CommandInput.qml
+++ b/qml/CommandInput.qml
@@ -2,6 +2,8 @@ import QtQuick 2.0
import QtQuick.Layouts 1.1
import Qt.labs.settings 1.0
+import "commands.js" as Commands
+
Item {
id: item
@@ -17,8 +19,9 @@ Item {
property color fontColor : "white"
}
- function focus() {
- visible = true;
+ function focus(prefix) {
+ visible = true;
+ command.text = prefix;
command.forceActiveFocus();
}
@@ -41,9 +44,9 @@ Item {
color: settings.fontColor
selectionColor: settings.fontColor
selectedTextColor: settings.background
- text: ":"
+ selectByMouse: true
- selectByMouse: true
+ function reset() { text = '' }
onAccepted: {
const prefix = String(text).charAt(0);
@@ -51,11 +54,13 @@ Item {
switch ( prefix ) {
case ':': {
- eval(cmd);
- text = ':';
+ Commands.execute(cmd);
+ reset();
break;
}
- default: { }
+ default: {
+ console.log('"' + prefix + '"' + " is not a command prefix");
+ }
}
}
}
diff --git a/qml/StateHandler.qml b/qml/StateHandler.qml
index 8c78e2f..75c5d6b 100644
--- a/qml/StateHandler.qml
+++ b/qml/StateHandler.qml
@@ -103,7 +103,7 @@ Item {
onTriggered: {
item.state = "COMMAND";
- commandInput.focus();
+ commandInput.focus(shortcut);
}
}
diff --git a/qml/commands.js b/qml/commands.js
new file mode 100644
index 0000000..ee13a1b
--- /dev/null
+++ b/qml/commands.js
@@ -0,0 +1,19 @@
+function execute(command) {
+ var args = command.split(' ');
+ var func = args[0];
+ args.shift();
+
+ eval(func + '(' + JSON.stringify(args) + ')');
+}
+
+function exec(args) {
+ eval(args.join(' '));
+}
+
+function next() {
+ terminalList.selectNext();
+}
+
+function prev() {
+ terminalList.selectPrev();
+}
diff --git a/qml/ui.qrc b/qml/ui.qrc
index df6d4e3..c8bd040 100644
--- a/qml/ui.qrc
+++ b/qml/ui.qrc
@@ -7,5 +7,6 @@
CommandInput.qml
TerminalList.qml
Highlighter.qml
+ commands.js
--
cgit v1.2.3