Skip to content

Commit b50cc10

Browse files
WebTerminal API extension with onOutput function, some API fixes
1 parent 9effcbc commit b50cc10

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-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.3.1",
8+
"version": "4.4.0",
99
"gaID": "UA-83005064-2",
1010
"releaseNumber": 26,
1111
"scripts": {

src/client/js/index.js

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { get, getURLParams } from "./lib";
1010

1111
let onAuthHandlers = [],
1212
userInputHandlers = [],
13+
outputHandlers = [],
14+
bufferedOutput = [],
1315
AUTHORIZED = false,
1416
terminal = null;
1517

@@ -33,9 +35,30 @@ export function authDone () {
3335
onAuthHandlers.forEach(h => h(terminal));
3436
}
3537

36-
export function onUserInput (text, mode) {
38+
export const inputActivated = () => {
39+
if (bufferedOutput.length) {
40+
for (const handler of outputHandlers) {
41+
if (handler.stream)
42+
continue;
43+
handler.callback(bufferedOutput);
44+
}
45+
bufferedOutput = [];
46+
}
47+
};
48+
49+
export const onUserInput = (text, mode) => {
3750
userInputHandlers.forEach((h) => h(text, mode));
38-
}
51+
bufferedOutput = [];
52+
};
53+
54+
export const onOutput = (string) => {
55+
bufferedOutput.push(string);
56+
for (const handler of outputHandlers) {
57+
if (!handler.stream)
58+
continue;
59+
handler.callback([ string ]);
60+
}
61+
};
3962

4063
/**
4164
* Register the callback which will be executed right after terminal is initialized. This callback
@@ -58,14 +81,43 @@ Terminal.prototype.MODE_PROMPT = 1;
5881
Terminal.prototype.MODE_SQL = 2;
5982
Terminal.prototype.MODE_READ = 3;
6083
Terminal.prototype.MODE_READ_CHAR = 4;
61-
Terminal.prototype.MODE_SPECIAL = 1;
84+
Terminal.prototype.MODE_SPECIAL = 5;
85+
86+
/**
87+
* Function accepts the callback, which is fired when user enter a command, character or a string.
88+
* @param {{ [stream]: boolean=false, [callback]: function }} [options]
89+
* @param {terminalOutputCallback} callback
90+
* @returns {function} - Your callback.
91+
*/
92+
Terminal.prototype.onOutput = function (options, callback) {
93+
if (!options || typeof options === "function") {
94+
callback = options || (() => { throw new Error("onOutput: no callback provided!"); });
95+
options = {};
96+
}
97+
if (typeof options.stream === "undefined")
98+
options.stream = false;
99+
options.callback = callback;
100+
outputHandlers.push(options);
101+
return callback;
102+
};
103+
104+
/**
105+
* Handles output both in stream or prompt mode.
106+
* @callback terminalOutputCallback
107+
* @param {string[]} - Output data presented as an array of string chunks. You can get the full
108+
* output as a single string by doing chunks.join("").
109+
*/
62110

63111
/**
64112
* Function accepts the callback, which is fired when user enter a command, character or a string.
65113
* @param {terminalUserEntryCallback} callback
114+
* @returns {function} - Your callback.
66115
*/
67116
Terminal.prototype.onUserInput = function (callback) {
117+
if (typeof callback !== "function")
118+
throw new Error("onUserInput: no callback provided!");
68119
userInputHandlers.push(callback);
120+
return callback;
69121
};
70122

71123
/**

src/client/js/input/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export function prompt (text, options = {}, callback = null, specialEnabled = tr
8787
PROMPT_CLEARED = false;
8888
SPECIAL_ENABLED = specialEnabled;
8989

90+
terminal.inputActivated();
91+
9092
if (text)
9193
output.print(text);
9294

@@ -145,6 +147,7 @@ export function getKey (options = {}, callback) {
145147
INPUT_MODE = Terminal.prototype.MODE_READ_CHAR;
146148

147149
// for mobile devices the keyboard needs to appear
150+
terminal.inputActivated();
148151
showInput();
149152
focusInput();
150153
caret.hide();
@@ -391,7 +394,7 @@ function getSelectionText() {
391394
let text = "";
392395
if (window.getSelection) {
393396
text = window.getSelection().toString();
394-
} else if (document.selection && document.selection.type != "Control") {
397+
} else if (document.selection && document.selection.type !== "Control") {
395398
text = document.selection.createRange().text;
396399
}
397400
return text;

src/client/js/output/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ESC_CHARS_MASK, applyEscapeSequence } from "./escStateMachine";
55
import esc from "./esc";
66
import * as input from "../input";
77
import { onInit } from "../init";
8+
import { onOutput } from "../index";
89

910
const LINE_UPDATE_TIMEOUT = 10;
1011

@@ -120,15 +121,14 @@ let tabs = [];
120121

121122
export function print (text) {
122123

124+
onOutput(text);
123125
stack += text;
124126
if (!INITIALIZED)
125127
return;
126128
freeStack();
127129

128130
}
129131

130-
window.pr = print;
131-
132132
export function printLine (text) {
133133
print(`${ text }\r\n`);
134134
}
@@ -502,7 +502,7 @@ function sizeChanged () {
502502
let m = `[WebTerminal] Size calculations delayed for 25s due to WebTerminal is not`
503503
+ ` attached to the page. If this message doesn't stop appearing, check if`
504504
+ ` WebTerminal's iFrame is visible and is attached to the DOM.`;
505-
try { console.warn(m); } catch (e) { console.log(m); }
505+
try { console.warn(m); } catch (e) { }
506506
}
507507

508508
// elements.output.style.width = `${ WIDTH * SYMBOL_WIDTH + scrollBarWidth }px`;

0 commit comments

Comments
 (0)