Skip to content

Commit

Permalink
fix prompt that return string or promise (in animation and set_prompt)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Dec 23, 2021
1 parent 9134a67 commit 83bb146
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 110 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 2.30.3
### Bugfix
* fix prompt that return string or promise (in animation and set_prompt) [#724](https://github.com/jcubic/jquery.terminal/issues/724)

## 2.30.2
### Bugfix
* fix animated exec array with sync commands [#722](https://github.com/jcubic/jquery.terminal/issues/722)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -12,7 +12,7 @@
[![npm](https://img.shields.io/badge/npm-2.30.2-blue.svg)](https://www.npmjs.com/package/jquery.terminal)
![bower](https://img.shields.io/badge/bower-2.30.2-yellow.svg)
[![Build and test](https://github.com/jcubic/jquery.terminal/actions/workflows/build.yaml/badge.svg?branch=master&event=push)](https://github.com/jcubic/jquery.terminal/actions/workflows/build.yaml)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=master&a3d8f5c3567979aca210315cafaca955)](https://coveralls.io/github/jcubic/jquery.terminal?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=master&f89d5d7e8a915c7f958e2407b66acf15)](https://coveralls.io/github/jcubic/jquery.terminal?branch=master)
![downloads](https://img.shields.io/npm/dm/jquery.terminal.svg?style=flat)
[![Paid Support](https://data.jsdelivr.com/v1/package/npm/jquery.terminal/badge?style=rounded)](https://www.jsdelivr.com/package/npm/jquery.terminal)
[![](https://img.shields.io/badge/paid-support-354465.svg)](https://support.jcubic.pl/)
Expand Down
93 changes: 89 additions & 4 deletions __tests__/terminal.spec.js
Expand Up @@ -2995,16 +2995,41 @@ describe('sub plugins', function() {
describe('Terminal plugin', function() {
describe('jQuery Terminal options', function() {
describe('prompt', function() {
it('should have default prompt', function() {
var term = $('<div/>').terminal($.noop);
expect(term.get_prompt()).toEqual('> ');
});
it('should set prompt', function() {
var prompt = '>>> ';
var term = $('<div/>').terminal($.noop, {
prompt: prompt
});
expect(term.get_prompt()).toEqual(prompt);
});
it('should have default prompt', function() {
var term = $('<div/>').terminal($.noop);
expect(term.get_prompt()).toEqual('> ');
it('should render function that return string', () => {
var term = $('<div/>').terminal($.noop, {
prompt: function() {
return '>>>'
}
});
expect(term.find('.cmd-prompt').text()).toEqual('>>>');
});
it('should render funtion that return promise', async () => {
var term = $('<div/>').terminal($.noop, {
prompt: function() {
return Promise.resolve('>>>');
}
});
await delay(10);
expect(term.find('.cmd-prompt').text()).toEqual('>>>');
});
it('should render funtion that call callback', () => {
var term = $('<div/>').terminal($.noop, {
prompt: function(fn) {
fn('>>>');
}
});
expect(term.find('.cmd-prompt').text()).toEqual('>>>');
});
});
describe('history', function() {
Expand Down Expand Up @@ -4971,7 +4996,8 @@ describe('Terminal plugin', function() {
});
it('should animate exec array with sync commands #722', async function() {
await term.exec(['echo HELLO', 'echo WORLD', 'echo !'], {
typing: 10
typing: true,
delay: 0
});
expect(term.get_output().split('\n')).toEqual([
'> echo HELLO',
Expand All @@ -4982,6 +5008,37 @@ describe('Terminal plugin', function() {
'!'
]);
});
it('should handle exec animation when using prompt that return string #724', async () => {
var term = $('<div/>').terminal({
echo: function(arg) {
this.echo(arg);
}
}, {
greetings: false,
prompt: function() {
return '>>> '
}
});
async function test() {
await term.exec(['echo HELLO', 'echo WORLD', 'echo !'], {
typing: true,
delay: 0
});
expect(term.get_output().split('\n')).toEqual([
'>>> echo HELLO',
'HELLO',
'>>> echo WORLD',
'WORLD',
'>>> echo !',
'!'
]);
}
await test();
term.clear().set_prompt(function() {
return Promise.resolve('>>> ');
});
await test();
});
});

describe('exec', function() {
Expand Down Expand Up @@ -5733,6 +5790,34 @@ describe('Terminal plugin', function() {
expect(last_line.text()).toEqual('Lorem');
});
});
describe('set_prompt', function() {
var term;
beforeEach(() => {
term = $('<div/>').terminal();
});
function get_prompt() {
return term.find('.cmd-prompt').text();
}
it('should render prompt when using function that returns promise', async function() {
term.set_prompt(function() {
return Promise.resolve('>>>');
});
await delay(10);
expect(get_prompt()).toEqual('>>>');
});
it('should render prompt when using function that return string', function() {
term.set_prompt(function() {
return '>>>';
});
expect(get_prompt()).toEqual('>>>');
});
it('should render prompt when using function that use callback', function() {
term.set_prompt(function(fn) {
fn('>>>');
});
expect(get_prompt()).toEqual('>>>');
});
});
describe('echo', function() {
var numChars = 100;
var numRows = 25;
Expand Down
77 changes: 43 additions & 34 deletions js/jquery.terminal-2.30.2.js
Expand Up @@ -41,7 +41,7 @@
*
* broken image by Sophia Bai from the Noun Project (CC-BY)
*
* Date: Thu, 23 Dec 2021 14:52:26 +0000
* Date: Thu, 23 Dec 2021 18:40:32 +0000
*/
/* global define, Map */
/* eslint-disable */
Expand Down Expand Up @@ -1901,6 +1901,37 @@
this.clear();
};
// -------------------------------------------------------------------------
// :: function that handle all cases of prompt and call the function set
// :: with a string
// -------------------------------------------------------------------------
function with_prompt(prompt, set, context) {
function error(e) {
var prompt = $.terminal.escape_brackets('[ERR]> ');
set('[[;red;]' + prompt + ']');
alert_exception('Prompt', e);
}
switch (typeof prompt) {
case 'string':
set(prompt);
break;
case 'function':
try {
var ret = prompt.call(context, function(string) {
set(string);
});
if (typeof ret === 'string') {
set(ret);
}
if (ret && ret.then) {
ret.then(set).catch(error);
}
} catch(e) {
error(e);
}
break;
}
}
// -------------------------------------------------------------------------
// :: COMMAND LINE PLUGIN
// -------------------------------------------------------------------------
var cmd_index = 0;
Expand Down Expand Up @@ -3454,29 +3485,10 @@
// remove reference for garbage collector
prev_prompt_data = null;
}
switch (typeof prompt) {
case 'string':
set(prompt);
break;
case 'function':
var data = prev_prompt_data = {
set: set
};
var ret = prompt.call(self, function(string) {
data.set(string);
});
if (typeof ret === 'string') {
data.set(ret);
}
if (ret && ret.then) {
ret.then(data.set).catch(function(e) {
var prompt = $.terminal.escape_brackets('[ERR]> ');
data.set('[[;red;]' + prompt + ']');
alert_exception('Prompt', e);
});
}
break;
}
var data = prev_prompt_data = {
set: set
};
with_prompt(prompt, set, self);
};
})();
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -5116,7 +5128,7 @@
// -------------------------------------------------------------------------
$.terminal = {
version: '2.30.2',
date: 'Thu, 23 Dec 2021 14:52:26 +0000',
date: 'Thu, 23 Dec 2021 18:40:32 +0000',
// colors from https://www.w3.org/wiki/CSS/Properties/color/keywords
color_names: [
'transparent', 'currentcolor', 'black', 'silver', 'gray', 'white',
Expand Down Expand Up @@ -8830,7 +8842,9 @@
var typed_enter = (function() {
var helper = typed(function(message, prompt, options) {
self.set_prompt(prompt);
self.echo(prompt + message, $.extend({}, options, {typing: false}));
with_prompt(prompt, function(prompt) {
self.echo(prompt + message, $.extend({}, options, {typing: false}));
}, self);
});
return function(prompt, message, options) {
return helper(message, $.extend({}, options, {prompt: prompt}));
Expand Down Expand Up @@ -9787,7 +9801,7 @@
});
} else if (is_function(prompt)) {
command_line.prompt(function(callback) {
prompt.call(self, callback, self);
return prompt.call(self, callback, self);
});
} else {
command_line.prompt(prompt);
Expand Down Expand Up @@ -10312,14 +10326,9 @@
} else if (type === 'echo') {
typed_message(string, settings);
} else if (type === 'enter') {
var prompt = self.get_prompt();
if (typeof prompt === 'function') {
prompt(function(prompt) {
typed_enter(prompt, string, settings);
});
} else {
with_prompt(self.get_prompt(), function(prompt) {
typed_enter(prompt, string, settings);
}
}, self);
}
} else {
d.reject('Invalid type only `echo` and `prompt` are supported');
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.terminal-2.30.2.min.js

Large diffs are not rendered by default.

73 changes: 41 additions & 32 deletions js/jquery.terminal-src.js
Expand Up @@ -1901,6 +1901,37 @@
this.clear();
};
// -------------------------------------------------------------------------
// :: function that handle all cases of prompt and call the function set
// :: with a string
// -------------------------------------------------------------------------
function with_prompt(prompt, set, context) {
function error(e) {
var prompt = $.terminal.escape_brackets('[ERR]> ');
set('[[;red;]' + prompt + ']');
alert_exception('Prompt', e);
}
switch (typeof prompt) {
case 'string':
set(prompt);
break;
case 'function':
try {
var ret = prompt.call(context, function(string) {
set(string);
});
if (typeof ret === 'string') {
set(ret);
}
if (ret && ret.then) {
ret.then(set).catch(error);
}
} catch(e) {
error(e);
}
break;
}
}
// -------------------------------------------------------------------------
// :: COMMAND LINE PLUGIN
// -------------------------------------------------------------------------
var cmd_index = 0;
Expand Down Expand Up @@ -3454,29 +3485,10 @@
// remove reference for garbage collector
prev_prompt_data = null;
}
switch (typeof prompt) {
case 'string':
set(prompt);
break;
case 'function':
var data = prev_prompt_data = {
set: set
};
var ret = prompt.call(self, function(string) {
data.set(string);
});
if (typeof ret === 'string') {
data.set(ret);
}
if (ret && ret.then) {
ret.then(data.set).catch(function(e) {
var prompt = $.terminal.escape_brackets('[ERR]> ');
data.set('[[;red;]' + prompt + ']');
alert_exception('Prompt', e);
});
}
break;
}
var data = prev_prompt_data = {
set: set
};
with_prompt(prompt, set, self);
};
})();
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -8830,7 +8842,9 @@
var typed_enter = (function() {
var helper = typed(function(message, prompt, options) {
self.set_prompt(prompt);
self.echo(prompt + message, $.extend({}, options, {typing: false}));
with_prompt(prompt, function(prompt) {
self.echo(prompt + message, $.extend({}, options, {typing: false}));
}, self);
});
return function(prompt, message, options) {
return helper(message, $.extend({}, options, {prompt: prompt}));
Expand Down Expand Up @@ -9787,7 +9801,7 @@
});
} else if (is_function(prompt)) {
command_line.prompt(function(callback) {
prompt.call(self, callback, self);
return prompt.call(self, callback, self);
});
} else {
command_line.prompt(prompt);
Expand Down Expand Up @@ -10312,14 +10326,9 @@
} else if (type === 'echo') {
typed_message(string, settings);
} else if (type === 'enter') {
var prompt = self.get_prompt();
if (typeof prompt === 'function') {
prompt(function(prompt) {
typed_enter(prompt, string, settings);
});
} else {
with_prompt(self.get_prompt(), function(prompt) {
typed_enter(prompt, string, settings);
}
}, self);
}
} else {
d.reject('Invalid type only `echo` and `prompt` are supported');
Expand Down

0 comments on commit 83bb146

Please sign in to comment.