-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
86 lines (70 loc) · 2.39 KB
/
cli.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
82
83
84
85
86
#!/usr/bin/env node
const { hideBin } = require('yargs/helpers');
const { argv } = require('yargs');
const cheerio = require("cheerio");
const Bardie = require('./lib/bardie');
const promptSync = require('prompt-sync');
const Markdown = require('markdown-it');
const chalk = require("chalk");
const prompt = promptSync({
autocomplete: getAutoComplete(['!exit']),
});
function getAutoComplete(commands) {
return function (partial) {
return commands.filter((c) => c.startsWith(partial));
};
}
async function htmlToCommandLine(html) {
const $ = cheerio.load(html);
const elements = $('body').children();
const styledElements = [];
elements.each(function (i, element) {
let styledElement;
switch (element.name) {
case 'p':
styledElement = chalk.bold($(element).text());
break;
case 'ul':
const $listItems = $(element).find('li');
styledElement = $listItems
.map((i, li) => chalk.yellow(`- ${$(li).text()}`))
.get()
.join('\n');
break;
case 'pre':
styledElement = chalk.gray($(element).find('code').text());
break;
case 'code':
styledElement = chalk.gray($(element).text());
break;
default:
styledElement = $(element).text();
}
styledElements.push(styledElement);
});
return styledElements.join('\n\n');
}
const markdown = new Markdown();
(async function () {
console.log(`
Bardie - A command-line interface to Google's Bard Server
Docs: https://bard.rizzy.eu.org
NPM: https://www.npmjs.com/package/bardie
Enter Ctrl+C or type "!exit" to quit.
`);
const bard = new Bardie();
try {
while (true) {
const userPrompt = prompt('You: ');
if (userPrompt === '!exit' || userPrompt === null || userPrompt === undefined || userPrompt === '') {
console.log('Exiting...');
break;
}
const response = await bard.question({ ask: userPrompt });
console.log('\nBardie : ', await htmlToCommandLine(markdown.render(response.content)) + '\n\n');
}
} catch (error) {
console.error(error.message);
console.log('Exiting...');
}
})();