-
-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add figlet extension based on Dialogue code
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
}); |