Skip to content

Commit

Permalink
fix typing animation with prism #726 + add insert animation
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Feb 9, 2022
1 parent 10b229c commit e44bcfb
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 43 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
## 2.31.2
## 2.32.0
### Features
* add insert typing animation
### Bugfix
* fix calculating number of rows (affecting less command)
* fix glow with prism and error messages [#729](https://github.com/jcubic/jquery.terminal/issues/729)
* fix prism and typing animation [#726](https://github.com/jcubic/jquery.terminal/issues/726)
* fix various typing errors

## 2.31.1
### Bugfix
Expand Down
81 changes: 69 additions & 12 deletions js/jquery.terminal-2.31.1.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: Fri, 31 Dec 2021 18:43:12 +0000
* Date: Tue, 04 Jan 2022 16:49:09 +0000
*/
/* global define, Map */
/* eslint-disable */
Expand Down Expand Up @@ -1910,26 +1910,32 @@
set('[[;red;]' + prompt + ']');
alert_exception('Prompt', e);
}
function done(prompt) {
set(prompt);
deferred.resolve();
}
var deferred = new $.Deferred();
switch (typeof prompt) {
case 'string':
set(prompt);
done(prompt);
break;
case 'function':
try {
var ret = prompt.call(context, function(string) {
set(string);
done(string);
});
if (typeof ret === 'string') {
set(ret);
done(ret);
}
if (ret && ret.then) {
ret.then(set).catch(error);
ret.then(done).catch(error);
}
} catch (e) {
error(e);
}
break;
}
return deferred.promise();
}
// -------------------------------------------------------------------------
// :: COMMAND LINE PLUGIN
Expand Down Expand Up @@ -5130,7 +5136,7 @@
// -------------------------------------------------------------------------
$.terminal = {
version: 'DEV',
date: 'Fri, 31 Dec 2021 18:43:12 +0000',
date: 'Tue, 04 Jan 2022 16:49:09 +0000',
// colors from https://www.w3.org/wiki/CSS/Properties/color/keywords
color_names: [
'transparent', 'currentcolor', 'black', 'silver', 'gray', 'white',
Expand Down Expand Up @@ -8811,7 +8817,9 @@
// ---------------------------------------------------------------------
function typed(finish_typing_fn) {
return function typing_animation(message, options) {
var formattted = $.terminal.apply_formatters(message);
var formattted = $.terminal.apply_formatters(message, {
animation: true
});
animating = true;
var prompt = self.get_prompt();
var char_i = 0;
Expand Down Expand Up @@ -8850,6 +8858,19 @@
options.finalize();
});
// ---------------------------------------------------------------------
var typed_insert = (function() {
var helper = typed(function(message, prompt, options) {
self.set_prompt(prompt);
self.insert(message);
options.finalize();
});
return function(prompt, command, options) {
return helper(command, $.extend({}, options, {
prompt: prompt + self.get_command()
}));
};
})();
// ---------------------------------------------------------------------
var typed_message = typed(function(message, prompt, options) {
self.set_prompt(prompt);
self.echo(message, $.extend({}, options, {typing: false}));
Expand Down Expand Up @@ -9811,15 +9832,43 @@
// -------------------------------------------------------------
// :: Insert text into the command line after the cursor
// -------------------------------------------------------------
insert: function(string, stay) {
insert: function(string, options) {
if (typeof string === 'string') {
var locals;
var defaults = {
stay: false,
typing: false,
delay: 100
};
if (!is_object(options)) {
options = {
stay: options
};
}
locals = $.extend(defaults, options);
var d = new $.Deferred();
when_ready(function ready() {
function done() {
if (settings.scrollOnEcho || bottom) {
self.scroll_to_bottom();
}
}
var bottom = self.is_bottom();
command_line.insert(string, stay);
if (settings.scrollOnEcho || bottom) {
self.scroll_to_bottom();
if (locals.typing) {
var delay = locals.delay;
var p = self.typing('insert', delay, string, settings);
p.then(function() {
done();
d.resolve();
});
} else {
command_line.insert(string, settings.stay);
done();
}
});
if (locals.typing) {
return d.promise();
}
return self;
} else {
throw new Error(sprintf(strings().notAString, 'insert'));
Expand Down Expand Up @@ -10345,8 +10394,12 @@
finish.apply(self, arguments);
}
}
var animations = ['prompt', 'echo', 'enter', 'insert'];
function valid_animation() {
return animations.indexOf(type) >= 0;
}
when_ready(function ready() {
if (['prompt', 'echo', 'enter'].indexOf(type) >= 0) {
if (valid_animation()) {
if (type === 'prompt') {
typed_prompt(string, settings);
} else if (type === 'echo') {
Expand All @@ -10355,6 +10408,10 @@
with_prompt(self.get_prompt(), function(prompt) {
typed_enter(prompt, string, settings);
}, self);
} else if (type === 'insert') {
with_prompt(self.get_prompt(), function(prompt) {
typed_insert(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.31.1.min.js

Large diffs are not rendered by default.

77 changes: 67 additions & 10 deletions js/jquery.terminal-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -1910,26 +1910,32 @@
set('[[;red;]' + prompt + ']');
alert_exception('Prompt', e);
}
function done(prompt) {
set(prompt);
deferred.resolve();
}
var deferred = new $.Deferred();
switch (typeof prompt) {
case 'string':
set(prompt);
done(prompt);
break;
case 'function':
try {
var ret = prompt.call(context, function(string) {
set(string);
done(string);
});
if (typeof ret === 'string') {
set(ret);
done(ret);
}
if (ret && ret.then) {
ret.then(set).catch(error);
ret.then(done).catch(error);
}
} catch (e) {
error(e);
}
break;
}
return deferred.promise();
}
// -------------------------------------------------------------------------
// :: COMMAND LINE PLUGIN
Expand Down Expand Up @@ -8811,7 +8817,9 @@
// ---------------------------------------------------------------------
function typed(finish_typing_fn) {
return function typing_animation(message, options) {
var formattted = $.terminal.apply_formatters(message);
var formattted = $.terminal.apply_formatters(message, {
animation: true
});
animating = true;
var prompt = self.get_prompt();
var char_i = 0;
Expand Down Expand Up @@ -8850,6 +8858,19 @@
options.finalize();
});
// ---------------------------------------------------------------------
var typed_insert = (function() {
var helper = typed(function(message, prompt, options) {
self.set_prompt(prompt);
self.insert(message);
options.finalize();
});
return function(prompt, command, options) {
return helper(command, $.extend({}, options, {
prompt: prompt + self.get_command()
}));
};
})();
// ---------------------------------------------------------------------
var typed_message = typed(function(message, prompt, options) {
self.set_prompt(prompt);
self.echo(message, $.extend({}, options, {typing: false}));
Expand Down Expand Up @@ -9811,15 +9832,43 @@
// -------------------------------------------------------------
// :: Insert text into the command line after the cursor
// -------------------------------------------------------------
insert: function(string, stay) {
insert: function(string, options) {
if (typeof string === 'string') {
var locals;
var defaults = {
stay: false,
typing: false,
delay: 100
};
if (!is_object(options)) {
options = {
stay: options
};
}
locals = $.extend(defaults, options);
var d = new $.Deferred();
when_ready(function ready() {
function done() {
if (settings.scrollOnEcho || bottom) {
self.scroll_to_bottom();
}
}
var bottom = self.is_bottom();
command_line.insert(string, stay);
if (settings.scrollOnEcho || bottom) {
self.scroll_to_bottom();
if (locals.typing) {
var delay = locals.delay;
var p = self.typing('insert', delay, string, settings);
p.then(function() {
done();
d.resolve();
});
} else {
command_line.insert(string, settings.stay);
done();
}
});
if (locals.typing) {
return d.promise();
}
return self;
} else {
throw new Error(sprintf(strings().notAString, 'insert'));
Expand Down Expand Up @@ -10345,8 +10394,12 @@
finish.apply(self, arguments);
}
}
var animations = ['prompt', 'echo', 'enter', 'insert'];
function valid_animation() {
return animations.indexOf(type) >= 0;
}
when_ready(function ready() {
if (['prompt', 'echo', 'enter'].indexOf(type) >= 0) {
if (valid_animation()) {
if (type === 'prompt') {
typed_prompt(string, settings);
} else if (type === 'echo') {
Expand All @@ -10355,6 +10408,10 @@
with_prompt(self.get_prompt(), function(prompt) {
typed_enter(prompt, string, settings);
}, self);
} else if (type === 'insert') {
with_prompt(self.get_prompt(), function(prompt) {
typed_insert(prompt, string, settings);
}, self);
}
} else {
d.reject('Invalid type only `echo` and `prompt` are supported');
Expand Down
24 changes: 21 additions & 3 deletions js/jquery.terminal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ declare namespace JQueryTerminal {
rows: number;
}

type insertOptions = {
typing?: boolean;
delay?: number;
stay?: boolean;
}

type readOptions = {
typing?: boolean;
delay?: number;
Expand All @@ -110,24 +116,35 @@ declare namespace JQueryTerminal {
white: string;
}

type TypingAnimations = 'echo' | 'prompt' | 'animation' | 'command';

type LessArgument = string | ((cols: number, cb: (text: string) => void) => void) | string[];

type ParsedOptions = {
_: string[];
[key: string]: boolean | string | string[];
};

type FormatterFunctionOptions = {
echo: boolean;
animation: boolean;
prompt: boolean;
command: boolean;
position: number;
}

type FormatterRegExpFunction = (...args: string[]) => string;
type FormaterRegExpReplacement = string | FormatterRegExpFunction;
type FormatterFunctionPropsInterface = {
__inherit__?: boolean;
__warn__?: boolean;
__meta__?: boolean;
};
type FormatterFunction = ((str: string, options?: JSONObject) => (string | [string, number])) & FormatterFunctionPropsInterface;
type FormatterFunction = ((str: string, options?: FormatterFunctionOptions) => (string | [string, number])) & FormatterFunctionPropsInterface;
type FormatterArrayOptions = {
loop?: boolean;
echo?: boolean;
animation?: boolean;
command?: boolean;
prompt?: boolean;
};
Expand Down Expand Up @@ -493,6 +510,7 @@ interface JQueryTerminalStatic {
prism(lang: string, text: string): string;
prism_formatters: {
command: boolean,
animation: boolean,
echo: boolean,
prompt: boolean
},
Expand Down Expand Up @@ -769,7 +787,7 @@ interface JQueryTerminal<TElement = HTMLElement> extends JQuery<TElement> {
set_command(cmd: string, silent?: boolean): JQueryTerminal;
set_position(pos: number, relative?: boolean): JQueryTerminal;
get_position(): number;
insert(str: string, stay?: boolean): JQueryTerminal;
insert(str: string, stay_or_options?: boolean | JQueryTerminal.insertOptions): JQueryTerminal;
set_prompt(prompt: JQueryTerminal.ExtendedPrompt, options?: JQueryTerminal.promptOptions): JQueryTerminal | JQuery.Promise<void>;
get_prompt<T extends JQueryTerminal.ExtendedPrompt>(): T;
set_mask(toggle?: boolean | string): JQueryTerminal;
Expand All @@ -795,7 +813,7 @@ interface JQueryTerminal<TElement = HTMLElement> extends JQuery<TElement> {
login_name<T extends string | void>(local?: boolean): T;
name(): string;
prefix_name(local?: boolean): string;
typing(type: 'echo' | 'prompt', delay: number, message: string, finish: voidFunction): JQuery.Promise<void>;
typing(type: JQueryTerminal.TypingAnimations, delay: number, message: string, finish: voidFunction): JQuery.Promise<void>;
read(message: string, success_or_options?: ((result: string) => void) | JQueryTerminal.readOptions, cancel?: voidFunction): JQuery.Promise<string>;
push(interpreter: TypeOrArray<JQueryTerminal.Interpreter>, options?: JQueryTerminal.pushOptions): JQueryTerminal;
pop(echoCommand?: string, silent?: boolean): JQueryTerminal;
Expand Down
Loading

0 comments on commit e44bcfb

Please sign in to comment.