Skip to content

Commit a1a1774

Browse files
committed
JS part of command completion done
1 parent c310944 commit a1a1774

File tree

4 files changed

+64
-18
lines changed

4 files changed

+64
-18
lines changed

Diff for: src/Ockcyp/WpTerm/Command/CommandFactory.php

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public static function make($executable)
4141
return new GotoCommand;
4242
case 'help':
4343
return new HelpCommand;
44+
case 'complete':
45+
return new CompleteCommand;
4446
default:
4547
// If a valid command but no class mapping defined
4648
// instantiate a JsCommand and set name to executable

Diff for: theme-integrations/common/wpterm.js

+60-18
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,16 @@ var wpTerm = function(termElem, url) {
1818
self.historyCon[0].scrollTop = self.historyCon[0].scrollHeight;
1919
};
2020

21+
self.appendToHistoryArray = function(list) {
22+
for (var i = 0; i < list.length; i++) {
23+
self.appendToHistory(list[i]);
24+
}
25+
}
26+
2127
self.appendCommand = function(string) {
2228
self.appendToHistory(self.promptText + ' ' + string);
2329
};
2430

25-
self.appendResponse = function(response) {
26-
if (response.list && response.list.length) {
27-
for (var i = 0; i < response.list.length; i++) {
28-
self.appendToHistory(response.list[i]);
29-
}
30-
} else if (response.msg) {
31-
self.appendToHistory(response.msg);
32-
} else if (response.url) {
33-
self.appendToHistory('Redirecting to: ' + response.url);
34-
}
35-
};
36-
3731
self.executeCommand = function(command) {
3832
// Save the command if not empty string
3933
// and is different from last command
@@ -63,22 +57,31 @@ var wpTerm = function(termElem, url) {
6357
}
6458
};
6559

66-
self.sendRequest = function(command) {
67-
jQuery.get(self.url, {cmd: command}, self.handleResponse);
60+
self.sendRequest = function(command, callback) {
61+
if (typeof callback == 'undefined') {
62+
callback = self.handleResponse;
63+
}
64+
65+
jQuery.get(self.url, {cmd: command}, callback);
6866
}
6967

7068
self.showHistory = function() {
71-
for (var i in self.history.list) {
72-
self.appendToHistory(self.history.list[i]);
73-
}
69+
self.appendToHistoryArray(self.history.list);
7470
}
7571

7672
self.clearHistoryCon = function() {
7773
self.historyCon.html('');
7874
}
7975

8076
self.handleResponse = function(response) {
81-
self.appendResponse(response);
77+
if (response.list && response.list.length) {
78+
self.appendToHistoryArray(response.list);
79+
} else if (response.msg) {
80+
self.appendToHistory(response.msg);
81+
} else if (response.url) {
82+
self.appendToHistory('Redirecting to: ' + response.url);
83+
}
84+
8285
if (response.url) {
8386
var url = response.url;
8487
setTimeout(function() {
@@ -87,6 +90,45 @@ var wpTerm = function(termElem, url) {
8790
}
8891
};
8992

93+
self.handleCompleteResponse = function(response, input) {
94+
if (response.complete) {
95+
// New cursor position: complete + blank space
96+
var newCurPos = input.selectionStart + response.complete.length + 1;
97+
// Beginning part
98+
input.value = input.value.substr(0, input.selectionStart)
99+
// complete + blank space
100+
+ response.complete + ' '
101+
// ending part
102+
+ input.value.substr(input.selectionEnd);
103+
104+
// Move the cursor
105+
input.selectionStart = newCurPos;
106+
input.selectionEnd = newCurPos;
107+
} else if (response.list && response.list.length > 0) {
108+
// Remove complete from command before adding to list
109+
self.appendCommand(response.cmd.substr(9));
110+
// Show the list
111+
self.appendToHistoryArray(response.list);
112+
}
113+
};
114+
115+
self.handleKeyDown = function(e) {
116+
var KEY_TAB = 9,
117+
inputElem = this;
118+
if (e.keyCode != KEY_TAB) {
119+
return;
120+
}
121+
122+
self.sendRequest(
123+
'complete ' + inputElem.value.substr(0, inputElem.selectionStart),
124+
function(response) {
125+
self.handleCompleteResponse(response, inputElem);
126+
}
127+
);
128+
129+
return false;
130+
};
131+
90132
self.handleKeyUp = function(e) {
91133
var KEY_ENTER = 13,
92134
KEY_UPARR = 40,

Diff for: theme-integrations/twentythirteen/header.php

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function openTerm() {
3939
jQuery('.terminal').show();
4040
jQuery('.home-link,.term-toggle-link').hide();
4141
wpTerm = wpTerm(jQuery('.terminal'));
42+
jQuery('.term-input').on('keydown', wpTerm.handleKeyDown);
4243
jQuery('.term-input').on('keyup', wpTerm.handleKeyUp)
4344
.focus();
4445
};

Diff for: wp-term.php

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function json_response($array)
2626

2727
try {
2828
$result = $command->execute();
29+
$result['cmd'] = $cmd;
2930
} catch (Exception $e) {
3031
$result = array('msg' => $e->getMessage());
3132
}

0 commit comments

Comments
 (0)