Skip to content

Commit 19dea61

Browse files
Callback add to terminal.execute function
1 parent 6e43198 commit 19dea61

File tree

6 files changed

+58
-8
lines changed

6 files changed

+58
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"printableName": "Cache Web Terminal",
66
"description": "Web-based terminal emulator for Caché administering.",
77
"author": "ZitRo",
8-
"version": "4.4.1",
8+
"version": "4.5.0",
99
"gaID": "UA-83005064-2",
1010
"releaseNumber": 26,
1111
"scripts": {

readme.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,20 @@ The next table demonstrates available API. Left column are `terminal` object pro
100100
<td>Description</td>
101101
</tr>
102102
<tr>
103-
<td>execute(<b>command</b>, <b>options</b>)</td>
103+
<td>execute(<b>command</b>, [<b>options</b>], [<b>callback</b>])</td>
104104
<td>
105105
Executes the COS <b>command</b> right as if it is entered
106106
to the terminal. However, <b>options</b> provide an
107107
additional flags setup.<br/>
108108
<b>options.echo</b> (<b>false</b> by default) - prints the
109109
<b>command</b> on the screen.<br/>
110110
<b>options.prompt</b> (<b>false</b> by default) - prompts
111-
the user after execution (prints "NAMESPACE > " as well).
111+
the user after execution (prints "NAMESPACE > " as well). If <b>callback</b> is passed,
112+
the output buffer will come as a first argument of the <b>callback</b> function.
112113
</td>
113114
</tr>
114115
<tr>
115-
<td>onOutput([ <b>options</b> ], <b>callback</b>)</td>
116+
<td>onOutput([<b>options</b>], <b>callback</b>)</td>
116117
<td>
117118
By default, <b>callback</b>(<u>strings</u>) will be called before the user is
118119
prompted for input, and <code>strings</code> array will always contain an array of

src/client/js/index.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,39 @@ Terminal.prototype.print = function (text) {
140140
input.reprompt();
141141
};
142142

143+
let executeCallback = null;
144+
143145
/**
144146
* Print the text on terminal.
145147
* @param {string} command
146-
* @param {boolean=false} echo
147-
* @param {boolean=false} prompt
148+
* @param {{ [echo]: boolean=false, [prompt]: boolean=false }} [options]
149+
* @param {function} [callback]
148150
*/
149-
Terminal.prototype.execute = function (command, { echo = false, prompt = false } = {}) {
150-
server.send("Execute", { command, echo: +echo, prompt: +prompt });
151+
Terminal.prototype.execute = function (command, options, callback) {
152+
if (typeof options === "function") {
153+
callback = options;
154+
}
155+
if (typeof options !== "object") {
156+
options = {};
157+
}
158+
server.send("Execute", {
159+
command,
160+
echo: +(options.echo || 0),
161+
prompt: +(options.prompt || 0),
162+
bufferOutput: +(typeof callback === "function")
163+
});
164+
if (typeof callback === "function") {
165+
executeCallback = callback;
166+
}
151167
};
152168

169+
export function promptCallback (data) {
170+
if (typeof executeCallback === "function") {
171+
executeCallback([ data ]);
172+
executeCallback = null;
173+
}
174+
}
175+
153176
function initialize () {
154177
let text = locale.get(`beforeInit`),
155178
urlParams = getURLParams();

src/client/js/server/handlers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export function prompt (namespace) {
2727
});
2828
}
2929

30+
export function promptCallback (data) {
31+
terminal.promptCallback(data);
32+
}
33+
3034
function cleanCWTLabel (string) {
3135
let s = string.replace(/(\w+(?:\+[0-9]+)?\^(?:\w\.?)+)/, "\x1b[(special)m$1\x1b[0m")
3236
.replace(/^(<.*>)/, `\x1b[31m$1\x1b[0m`),

src/cls/WebTerminal/Engine.cls

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ Property StartupRoutine As %String;
2626
/// Output flag
2727
Property echo As %Boolean [ InitialExpression = 1 ];
2828

29+
/// flag which enables output buffering
30+
Property bufferOutput As %Boolean [ InitialExpression = 0 ];
31+
32+
/// Used to buffer the output ("o" flag) when bufferOutput flag is set
33+
Property outputBuffer As %Stream.TmpCharacter [ InitialExpression = {##class(%Stream.TmpCharacter).%New()} ];
34+
2935
/// Output flag
3036
Property handler As %Boolean [ InitialExpression = 0, Private ];
3137

@@ -43,6 +49,10 @@ Method GetMessage(timeout As %Integer = 86400) As %ZEN.proxyObject
4349
/// Do not remove this method in future versions of WebTerminal, it is used by update.
4450
Method Send(handler As %String = "", data = "") As %Status
4551
{
52+
if (handler = "o") && (..bufferOutput = 1) {
53+
do ..outputBuffer.Write(data)
54+
return $$$OK
55+
}
4656
return:((handler = "o") && (..echo = 0)) $$$OK
4757
return:(handler = "o") ..Write("o"_data) // Enables 2013.2 support (no JSON)
4858
set obj = ##class(%ZEN.proxyObject).%New()

src/cls/WebTerminal/Handlers.cls

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ ClassMethod Execute (client As WebTerminal.Engine, data, bareStart As %Boolean =
1919
if (data.echo = 0) {
2020
set client.echo = 0
2121
}
22+
if (data.bufferOutput = 1) {
23+
set client.bufferOutput = 1
24+
}
2225
} else {
2326
set command = data
2427
}
@@ -83,6 +86,15 @@ loop
8386
if (data.echo = 0) {
8487
set client.echo = 1
8588
}
89+
if (data.bufferOutput = 1) {
90+
set client.bufferOutput = 0
91+
set str = ""
92+
while ('client.outputBuffer.AtEnd) {
93+
set str = str _ client.outputBuffer.Read()
94+
}
95+
do client.Send("promptCallback", str)
96+
do client.outputBuffer.Clear()
97+
}
8698
}
8799
do:('($IsObject(data) && (data.prompt = 0)) && '(bareStart = 1)) client.Send("prompt", client.childNamespace)
88100
return $$$OK

0 commit comments

Comments
 (0)