-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
81 lines (69 loc) · 2.16 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const font = 'Slant';
figlet.defaults({ fontPath: 'https://cdn.jsdelivr.net/npm/figlet/fonts' });
figlet.preloadFonts([font], ready);
const formatter = new Intl.ListFormat('en', {
style: 'long',
type: 'conjunction',
});
const commands = {
echo(...args) {
this.echo(args.join(' '));
},
credits() {
return [
'',
'<white>Used libraries:</white>',
'* <a href="https://terminal.jcubic.pl">jQuery Terminal</a>',
'* <a href="https://github.com/patorjk/figlet.js/">Figlet.js</a>',
'* <a href="https://github.com/jcubic/isomorphic-lolcat">Isomorphic Lolcat</a>',
'',
'<a href="https://github.com/sponsors/jcubic">Sponsor ❤️ my Open Source work</a>',
''
].join('\n');
},
help() {
this.echo(`List of available commands: ${help}`);
}
};
const command_list = ['clear'].concat(Object.keys(commands));
const formatted_list = command_list.map(cmd => `<white class="command">${cmd}</white>`);
const help = formatter.format(formatted_list);
const term = $('body').terminal(commands, {
completion: true,
checkArity: false,
greetings: false
});
term.on('click', '.command', function() {
const command = $(this).text();
term.exec(command, { typing: true, delay: 50 });
});
function ready() {
const seed = rand(256);
term.echo(() => rainbow(render('Terminal Website'), seed))
.echo('<white>Welcome to Terminal Website Template</white>\n').resume();
}
function rainbow(string, seed) {
return lolcat.rainbow(function(char, color) {
char = $.terminal.escape_brackets(char);
return `[[;${hex(color)};]${char}]`;
}, string, seed).join('\n');
}
function rand(max) {
return Math.floor(Math.random() * (max + 1));
}
function render(text) {
const cols = term.cols();
return trim(figlet.textSync(text, {
font: font,
width: cols,
whitespaceBreak: true
}));
}
function trim(str) {
return str.replace(/[\n\s]+$/, '');
}
function hex(color) {
return '#' + [color.red, color.green, color.blue].map(n => {
return n.toString(16).padStart(2, '0');
}).join('');
}