Skip to content

Commit

Permalink
Merge b9277b3 into a52aeae
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Feb 27, 2021
2 parents a52aeae + b9277b3 commit c5828ec
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 117 deletions.
24 changes: 24 additions & 0 deletions __tests__/terminal.spec.js
Expand Up @@ -2651,12 +2651,36 @@ describe('extensions', function() {
var command = 'hello';
term.set_prompt(prompt);
term.echo('.', {newline: false});
expect(term.get_output()).toEqual('.');
term.echo('.', {newline: false});
expect(term.get_output()).toEqual('..');
term.echo('.', {newline: false});
expect(term.get_output()).toEqual('...');
enter(term, command);
expect(term.get_output()).toEqual('...' + prompt + command);
expect(term.get_prompt()).toEqual(prompt);
});
it('get_prompt and set_prompt work as expected', function() {
var prompt1 = '>>> ';
var prompt2 = '~~~ ';
var command = 'hello';
term.set_prompt(prompt1);
term.echo('.', {newline: false});
expect(term.get_prompt()).toEqual(prompt1);
term.echo('.', {newline: false});
expect(term.get_prompt()).toEqual(prompt1);
term.set_prompt(prompt2);
term.echo('.', {newline: false});
expect(term.get_prompt()).toEqual(prompt2);
enter(term, command);
expect(term.get_output()).toEqual('...' + prompt2 + command);
expect(term.get_prompt()).toEqual(prompt2);
});
it('finalize with newline : false', function() {
term.echo('foo', {finalize: (a) => a.children().children().css("color", "red"), newline : false});
var color = term[0].querySelector(`[data-index='${term.last_index()}`).firstChild.firstChild.style.color;
expect(color).toEqual("red");
});
});
describe('autocomplete_menu', function() {
function completion(term) {
Expand Down
12 changes: 8 additions & 4 deletions css/jquery.terminal.css
Expand Up @@ -684,14 +684,18 @@ terminal .terminal-output > div {
margin-bottom: 10px;
position: relative;
}
div[data-index] > div {
width: -moz-fit-content;
width: -webkit-fit-content;
width: fit-content;
}

@supports (--css: variables) {
.terminal,
.terminal-output > :not(.raw) span[data-text]:not(.token):not(.inverted):not(.terminal-inverted):not(.cmd-inverted):not(.terminal-error):not(.emoji),
.terminal-output > :not(.raw) a,
.terminal-output > :not(.raw) div,
.cmd,
.cmd span[data-text]:not(.cmd-inverted):not(.token):not(.emoji),
.cmd div {
.terminal-output > :not(.raw) div
{
color: var(--color, #aaa);
background-color: var(--background, #000);
}
Expand Down
98 changes: 1 addition & 97 deletions js/echo_newline.js
Expand Up @@ -49,100 +49,4 @@
// istanbul ignore next
factory(root.jQuery);
}
})(function($) {
var init = $.fn.terminal;
$.fn.terminal = function(interpreter, options) {
return init.call(this, interpreter, patch_options(options)).each(function() {
patch_term($(this).data('terminal'), should_echo_command(options));
});
};
var last = null;
var prompt = null;
function should_echo_command(options) {
return options && options.echoCommand !== false || !options;
}
function patch_options(options) {
var keymap = {
'ENTER': function(e, original) {
var term = this;
if (last === null) {
if (should_echo_command(options)) {
term.echo_command();
}
} else {
this.__echo(last + prompt + this.get_command());
this.set_prompt(prompt);
last = null;
}
if (options && options.keymap && options.keymap.ENTER) {
options.keymap.ENTER.call(this, e, original);
} else {
original.call(this, e);
}
}
};
var settings = {
echoCommand: false,
keymap: $.extend({}, options && options.keymap || {}, keymap)
};
return $.extend({}, options || {}, settings);
}
function patch_term(term, echo_command) {
if (term.__echo) {
return term;
}
term.__echo = term.echo;
term.__exec = term.exec;
term.__set_prompt = term.set_prompt;
term.exec = function() {
last = null;
if (echo_command) {
this.settings().echoCommand = true;
}
var ret = term.__exec.apply(this, arguments);
if (echo_command) {
this.settings().echoCommand = false;
}
return ret;
};
term.echo = function(arg, options) {
var settings = $.extend({
newline: true
}, options);
function process(prompt) {
if (last === null) {
last = arg;
} else {
last += arg;
}
term.__set_prompt(last + prompt);
}
if (settings.newline === false) {
if (prompt === null) {
prompt = term.get_prompt();
}
if (typeof prompt === 'string') {
process(prompt);
} else {
prompt(process);
}
} else {
if (prompt !== null) {
term.__set_prompt(prompt);
}
if (last !== null) {
term.__echo(last + arg, options);
} else if (!arguments.length) {
// original echo check length to test if someone call echo
// with value that is undefined
term.__echo();
} else {
term.__echo(arg, options);
}
prompt = null;
last = null;
}
return term;
};
}
});
})(function() {}); // eslint-disable-line no-empty-function
103 changes: 87 additions & 16 deletions js/jquery.terminal-src.js
Expand Up @@ -1739,6 +1739,8 @@
var char_width;
var last_rendered_prompt;
var prompt_last_line;
var just_prompt_len;
var extra_prompt_margin = 0;
var prompt_len;
var prompt_node = self.find('.cmd-prompt');
var reverse_search = false;
Expand Down Expand Up @@ -3120,10 +3122,9 @@
});
prompt_node.show();
}
prompt_len = strlen(text(encoded_last_line));
} else if (formatted === '') {
prompt_len = 0;
}
just_prompt_len = strlen(text(encoded_last_line));
prompt_len = just_prompt_len + extra_prompt_margin;
}
return function() {
// the data is used as cancelable reference because we have ref
Expand Down Expand Up @@ -3352,6 +3353,11 @@
}
return before.split(/\n/).length - 1;
},
// inform cursor about size of partial output line
__set_prompt_margin: function(len) {
extra_prompt_margin = len;
prompt_len = just_prompt_len + extra_prompt_margin;
},
prompt: function(user_prompt) {
if (user_prompt === true) {
return last_rendered_prompt;
Expand Down Expand Up @@ -7416,7 +7422,8 @@
}
output_buffer.push({
finalize: options.finalize,
index: index
index: index,
newline: options.newline
});
}
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -9298,14 +9305,24 @@
}, options || {});
try {
var bottom = self.is_bottom();
output.css('visibilty', 'hidden');
var wrapper;
// print all lines
// var output_buffer = lines.flush();
var first = true;
var appendingToPartial = false;
var partial = $();
if (!options.update) {
partial = self.find(".partial");
}
while (output_buffer.length) {
var data = output_buffer.shift();
if (data === NEW_LINE) {
wrapper = $(document.createElement('div'));
if (!partial.length) {
wrapper = $('<div/>');
} else if (first) {
appendingToPartial = true;
wrapper = partial;
}
first = false;
} else if ($.isPlainObject(data) && is_function(data.finalize)) {
// this is finalize function from echo
if (options.update) {
Expand All @@ -9318,21 +9335,55 @@
wrapper.appendTo(output);
}
wrapper.attr('data-index', data.index);
appendingToPartial = !data.newline;
wrapper.toggleClass("partial", appendingToPartial);
data.finalize(wrapper);
} else {
var line = data.line;
var div = document.createElement('div');
div.style.width = '100%';
div.innerHTML = line;
if (data.newline) {
div.className = 'cmd-end-line';
if (appendingToPartial) {
wrapper.children().last().append(line);
appendingToPartial = false;
} else {
var div = $('<div/>').html(line);
// width = "100%" does some weird extra magic
// that makes the height correct. Any other
// value doesn't work.
div.css('width', "100%");
if (data.newline) {
div.addClass('cmd-end-line');
}
wrapper.append(div);
}
wrapper[0].appendChild(div);
}
}
var cmd_prompt = self.find(".cmd-prompt");
var cmd_outer = self.find('.cmd');
partial = self.find(".partial");
if (partial.length === 0) {
cmd_prompt.css("marginLeft", 0);
cmd_outer.css("top", 0);
command_line.__set_prompt_margin(0);
} else {
var lastRow = partial.children().last();
// Remove width="100%" temporarily so we can measure it
lastRow.css("width", "");
var lastRowRect = lastRow[0].getBoundingClientRect();
// Put width="100%" back so height is correct
lastRow.css("width", "100%");
var partial_width = lastRowRect.width;
// Shift command prompt up one line and to the right
// enough so that it appears directly next to the
// partially constructed output line
cmd_prompt.css("margin-left", partial_width);
cmd_outer.css("top", -lastRowRect.height);
// Measure length of partial line in characters
var char_width = command_line.char_width;
var prompt_margin = Math.round(partial_width / char_width);
command_line.__set_prompt_margin(prompt_margin);
}
limit_lines();
output.css('visibilty', '');
fire_event('onFlush');
var cmd_cursor = self.find(".cmd-cursor");
var offset = self.find('.cmd').offset();
var self_offset = self.offset();
setTimeout(function() {
Expand All @@ -9342,7 +9393,14 @@
'--terminal-y': offset.top - self_offset.top,
'--terminal-scroll': self.prop('scrollTop')
});
// Firefox won't reflow the cursor automatically, so
// hide it briefly then reshow it
cmd_cursor.hide();
}, 0);
setTimeout(function() {
// reshow cursor for firefox
cmd_cursor.show();
}, 10 /* this didn't work with 5ms */);
if ((settings.scrollOnEcho && options.scroll) || bottom) {
self.scroll_to_bottom();
}
Expand Down Expand Up @@ -9450,7 +9508,8 @@
invokeMethods: settings.invokeMethods,
onClear: null,
formatters: true,
allowedAttributes: settings.allowedAttributes
allowedAttributes: settings.allowedAttributes,
newline: true
}, options || {});
// finalize function is passed around and invoked
// in terminal::flush after content is added to DOM
Expand Down Expand Up @@ -9516,7 +9575,19 @@
var defer = new DelayQueue();
echo_ready = ready(defer);
}
lines.push([value, $.extend({exec: false}, locals)]);
// Did previous value end in newline?
var last_line = lines[lines.length - 1];
if (lines.length === 0 || last_line[1].newline) {
lines.push([value, $.extend({exec: false}, locals)]);
} else {
// If not append current value to it
var old_value = last_line[0];
var string_old_val = stringify_value(old_value);
var string_val = stringify_value(value);
last_line[0] = string_old_val + string_val;
// Update newline field
last_line[1].newline = locals.newline;
}
unpromise(next, function() {
// extended commands should be processed only
// once in echo and not on redraw
Expand Down

0 comments on commit c5828ec

Please sign in to comment.