Skip to content

Commit

Permalink
add id option #978
Browse files Browse the repository at this point in the history
This feature allow to create same terminal with hot reload
  • Loading branch information
jcubic committed Oct 26, 2024
1 parent 301b096 commit 4c270bf
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.45.0
### Features
* add `id` option that allow to create same terminal using hot reload [#978](https://github.com/jcubic/jquery.terminal/issues/978)

## 2.44.1
### Bugfix
* fix errors with form autofill outside of the terminal [#977](https://github.com/jcubic/jquery.terminal/issues/977)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![npm](https://img.shields.io/badge/npm-2.44.1-blue.svg)](https://www.npmjs.com/package/jquery.terminal)
![bower](https://img.shields.io/badge/bower-2.44.1-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&0f20bf6eaa4edcca15f83f8bbf243c1e)](https://coveralls.io/github/jcubic/jquery.terminal?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/jcubic/jquery.terminal/badge.svg?branch=master&3a08274fb3d1c4cbcdf3ca25c25dabb7)](https://coveralls.io/github/jcubic/jquery.terminal?branch=master)
![NPM Downloads](https://img.shields.io/npm/dm/jquery.terminal.svg?style=flat)
[![jsDelivr Downloads](https://data.jsdelivr.com/v1/package/npm/jquery.terminal/badge?style=rounded&n=1)](https://www.jsdelivr.com/package/npm/jquery.terminal)
[![Paid Support](https://img.shields.io/badge/paid-support-354465.svg)](https://support.jcubic.pl/)
Expand Down
49 changes: 42 additions & 7 deletions __tests__/terminal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,9 @@ function shortcut(ctrl, alt, shift, which, key) {
doc.trigger(keypress(key));
doc.trigger($.Event("keyup"));
}

function key(ord, key) {
shortcut(false, false, false, ord, key);
}
function click(element) {
var e = $.Event('mouseup');
e.button = 0;
Expand Down Expand Up @@ -1690,6 +1692,24 @@ describe('Terminal utils', function() {
});
});
describe('set', function() {
var a = {a: 1};
var b = {a: 2};
var c = {a: 2};
var cycle;
beforeEach(function() {
cycle = new $.terminal.Cycle(a, b);
});
it('should replace item', () => {
cycle.remove(1);
cycle.set(1, c);
expect(cycle.get()).toEqual([a, c]);
});
it('should ignore value', () => {
cycle.set(1, c);
expect(cycle.get()).toEqual([a, b]);
});
});
describe('active', function() {
var a = {a: 1};
var b = {a: 2};
var c = {a: 3};
Expand All @@ -1699,12 +1719,12 @@ describe('Terminal utils', function() {
cycle = new $.terminal.Cycle(a, b, c, d);
});
it('should set existing element', function() {
cycle.set(c);
cycle.active(c);
expect(cycle.front()).toEqual(c);
});
it('should add new item if not exists', function() {
var e = {a: 5};
cycle.set(e);
cycle.active(e);
expect(cycle.length()).toEqual(5);
expect(cycle.index()).toEqual(4);
expect(cycle.front()).toEqual(e);
Expand Down Expand Up @@ -1772,7 +1792,7 @@ describe('Terminal utils', function() {
});
it('should add element if cycle at the end', function() {
var cycle = new $.terminal.Cycle(1,2,3);
cycle.set(3);
cycle.active(3);
cycle.append(4);
expect(cycle.get()).toEqual([1,2,3,4]);
});
Expand Down Expand Up @@ -2231,9 +2251,6 @@ describe('Terminal utils', function() {
term.find('.terminal-output').css('width', 800);
term.focus();
});
function key(ord, key) {
shortcut(false, false, false, ord, key);
}
function selected() {
return term.find('.terminal-output > div div span.terminal-inverted');
}
Expand Down Expand Up @@ -7412,6 +7429,24 @@ describe('Terminal plugin', function() {
expect(term.get_output()).toEqual(greetings);
});
});
describe('id', function() {
it('should restore the history', () => {
var term = $('<div/>').terminal($.noop, {
name: 'id'
});
term.focus();
var id = term.id();
enter(term, 'ls');
term.destroy();
term = $('<div/>').terminal($.noop, {
name: 'id',
id
});
term.focus();
key('ARROWUP');
expect(term.get_command()).toEqual('ls');
});
});
describe('purge', function() {
var token = 'purge_TOKEN';
var password = 'password';
Expand Down
35 changes: 26 additions & 9 deletions js/jquery.terminal-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,7 @@
remove: function(index) {
delete data[index];
},
set: function(item) {
active: function(item) {
for (var i = data.length; i--;) {
if (data[i] === item) {
pos = i;
Expand Down Expand Up @@ -1519,6 +1519,11 @@
}
});
},
set: function(index, value) {
if (!data[index]) {
data[index] = value;
}
},
append: function(item) {
data.push(item);
}
Expand Down Expand Up @@ -7661,7 +7666,7 @@
redrawError: 'Internal error, wrong position in cmd redraw',
invalidStrings: 'Command %s have unclosed strings',
invalidMask: 'Invalid mask used only string or boolean allowed',
defunctTerminal: "You can't call method on terminal that was destroyed",
defunctTerminal: "You can't call method '%s' on terminal that was destroyed",
abortError: 'Abort with CTRL+D',
timeoutError: 'Signal timed out'
}
Expand Down Expand Up @@ -10419,7 +10424,7 @@
}
}
}
terminals.set(self);
terminals.active(self);
self.enable(silent);
}
});
Expand Down Expand Up @@ -11953,7 +11958,8 @@
var prev_exec_cmd;
var tab_count = 0; // for tab completion
var output; // .terminal-output jquery object
var terminal_id = terminals.length();
var have_custom_id = typeof options.id === 'number';
var terminal_id = have_custom_id ? options.id : terminals.length();
var force_awake = false; // flag used to don't pause when user return read() call
var num_chars; // numer of chars in line
var num_rows; // number of lines that fit without scrollbar
Expand Down Expand Up @@ -12068,7 +12074,11 @@
(typeof settings.login === 'string' || settings.login === true)) {
global_login_fn = make_json_rpc_login(base_interpreter, settings.login);
}
terminals.append(self);
if (have_custom_id) {
terminals.set(settings.id, self);
} else {
terminals.append(self);
}
function focus_terminal() {
if (old_enabled) {
self.focus();
Expand Down Expand Up @@ -12700,15 +12710,22 @@
// don't make sense
observe_visibility();
}
// wait for custom font to load #892
if (document.fonts && document.fonts.ready) {
document.fonts.ready.then(function() {
function fonts_ready() {
if (!defunct) {
if (have_custom_font(self)) {
calculate_char_size();
self.resize();
}
command_queue.resolve();
});
}
}
// wait for custom font to load #892
if (document.fonts && document.fonts.ready) {
if (document.fonts.status === 'loaded') {
fonts_ready();
} else {
document.fonts.ready.then(fonts_ready);
}
} else {
command_queue.resolve();
}
Expand Down
1 change: 1 addition & 0 deletions js/jquery.terminal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ declare namespace JQueryTerminal {
invokeMethods?: boolean;
useCache?: boolean;
anyLinks?: boolean;
id?: number;
raw?: boolean;
allowedAttributes?: Array<RegExp | string>;
tabindex?: number;
Expand Down
39 changes: 28 additions & 11 deletions js/jquery.terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*
* broken image by Sophia Bai from the Noun Project (CC-BY)
*
* Date: Tue, 08 Oct 2024 20:29:53 +0000
* Date: Sat, 26 Oct 2024 19:45:38 +0000
*/
/* global define, Map, BigInt */
/* eslint-disable */
Expand Down Expand Up @@ -1477,7 +1477,7 @@
remove: function(index) {
delete data[index];
},
set: function(item) {
active: function(item) {
for (var i = data.length; i--;) {
if (data[i] === item) {
pos = i;
Expand Down Expand Up @@ -1519,6 +1519,11 @@
}
});
},
set: function(index, value) {
if (!data[index]) {
data[index] = value;
}
},
append: function(item) {
data.push(item);
}
Expand Down Expand Up @@ -5343,7 +5348,7 @@
// -------------------------------------------------------------------------
$.terminal = {
version: '2.44.1',
date: 'Tue, 08 Oct 2024 20:29:53 +0000',
date: 'Sat, 26 Oct 2024 19:45:38 +0000',
// colors from https://www.w3.org/wiki/CSS/Properties/color/keywords
color_names: [
'transparent', 'currentcolor', 'black', 'silver', 'gray', 'white',
Expand Down Expand Up @@ -7661,7 +7666,7 @@
redrawError: 'Internal error, wrong position in cmd redraw',
invalidStrings: 'Command %s have unclosed strings',
invalidMask: 'Invalid mask used only string or boolean allowed',
defunctTerminal: "You can't call method on terminal that was destroyed",
defunctTerminal: "You can't call method '%s' on terminal that was destroyed",
abortError: 'Abort with CTRL+D',
timeoutError: 'Signal timed out'
}
Expand Down Expand Up @@ -10419,7 +10424,7 @@
}
}
}
terminals.set(self);
terminals.active(self);
self.enable(silent);
}
});
Expand Down Expand Up @@ -11953,7 +11958,8 @@
var prev_exec_cmd;
var tab_count = 0; // for tab completion
var output; // .terminal-output jquery object
var terminal_id = terminals.length();
var have_custom_id = typeof options.id === 'number';
var terminal_id = have_custom_id ? options.id : terminals.length();
var force_awake = false; // flag used to don't pause when user return read() call
var num_chars; // numer of chars in line
var num_rows; // number of lines that fit without scrollbar
Expand Down Expand Up @@ -12068,7 +12074,11 @@
(typeof settings.login === 'string' || settings.login === true)) {
global_login_fn = make_json_rpc_login(base_interpreter, settings.login);
}
terminals.append(self);
if (have_custom_id) {
terminals.set(settings.id, self);
} else {
terminals.append(self);
}
function focus_terminal() {
if (old_enabled) {
self.focus();
Expand Down Expand Up @@ -12700,15 +12710,22 @@
// don't make sense
observe_visibility();
}
// wait for custom font to load #892
if (document.fonts && document.fonts.ready) {
document.fonts.ready.then(function() {
function fonts_ready() {
if (!defunct) {
if (have_custom_font(self)) {
calculate_char_size();
self.resize();
}
command_queue.resolve();
});
}
}
// wait for custom font to load #892
if (document.fonts && document.fonts.ready) {
if (document.fonts.status === 'loaded') {
fonts_ready();
} else {
document.fonts.ready.then(fonts_ready);
}
} else {
command_queue.resolve();
}
Expand Down
4 changes: 2 additions & 2 deletions js/jquery.terminal.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/jquery.terminal.min.js.map

Large diffs are not rendered by default.

0 comments on commit 4c270bf

Please sign in to comment.