Skip to content

Commit

Permalink
add figlet extension based on Dialogue code
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Dec 18, 2024
1 parent b793073 commit f806763
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* add `terminal::lines()` [#966](https://github.com/jcubic/jquery.terminal/issues/966)
* add small ASCII Art to signature [#972](https://github.com/jcubic/jquery.terminal/issues/972)
* add `termina::view_ready()` [#987](https://github.com/jcubic/jquery.terminal/issues/987)
* add `figlet.js` helper extension
### Bugfix
* fix `terminal::login()` when user already authenticated [#980](https://github.com/jcubic/jquery.terminal/issues/980)
* improve mobile support
Expand Down
112 changes: 112 additions & 0 deletions js/figlet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**@license
* __ _____ ________ __
* / // _ /__ __ _____ ___ __ _/__ ___/__ ___ ______ __ __ __ ___ / /
* __ / // // // // // _ // _// // / / // _ // _// // // \/ // _ \/ /
* / / // // // // // ___// / / // / / // ___// / / / / // // /\ // // / /__
* \___//____ \\___//____//_/ _\_ / /_//____//_/ /_/ /_//_//_/ /_/ \__\_\___/
* \/ /____/
* http://terminal.jcubic.pl
*
* This is wrapper over Figlet.js library by patorjk
*
* Copyright (c) 2024 Jakub Jankiewicz <https://jcubic.pl/me>
* Released under the MIT license
*
*/
/* global define */
(function(factory, undefined) {
var root;
if (typeof window !== 'undefined') {
root = window;
} else if (typeof self !== 'undefined') {
root = self;
} else if (typeof global !== 'undefined') {
root = global;
} else {
throw new Error('Unknow context');
}
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
// istanbul ignore next
define(['jquery', 'jquery.terminal', 'figlet'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = function(root, jQuery) {
if (jQuery === undefined) {
// require('jQuery') returns a factory that requires window to
// build a jQuery instance, we normalize how we use modules
// that require this pattern but the window provided is a noop
// if it's defined (how jquery works)
if (window !== undefined) {
jQuery = require('jquery');
} else {
jQuery = require('jquery')(root);
}
}
if (!jQuery.fn.terminal) {
if (window !== undefined) {
require('jquery.terminal');
} else {
require('jquery.terminal')(jQuery);
}
}
const figlet = require('figlet');
factory(jQuery, figlet);
return jQuery;
};
} else {
// Browser
// istanbul ignore next
factory(root.jQuery, root.figlet);
}
})(function($, figlet) {
if (!$) {
throw new Error('jQuery Not defined');
}
if (!$.terminal) {
throw new Error('$.terminal is not defined');
}

$.terminal.figlet = function(font, text, { color = null, ...options } = {}) {
return function() {
const cols = this.cols();
let result = figlet.textSync(text, {
font: font,
width: cols,
whitespaceBreak: true,
...options
});
result = trim(result);
if (color === null) {
return result;
}
return '[[;' + color + ';]' + result + ']';
};
};

const fontpath = 'https://cdn.jsdelivr.net/npm/figlet/fonts';

$.terminal.figlet.load = (function() {
const installed = [];
return function(fonts, fontPath = fontpath) {
if (all_include(fonts, installed)) {
return Promise.resolve();
}
let last_path;
return new Promise(resolve => {
if (last_path !== fontPath) {
last_path = fontPath;
figlet.defaults({ fontPath });
}
figlet.preloadFonts(fonts, () => {
installed.push(...fonts);
resolve();
});
});
};
})();

function all_include(items, list) {
return items.every(item => list.includes(item));
}
});

0 comments on commit f806763

Please sign in to comment.