Total Complexity | 6 |
Complexity/F | 1.2 |
Lines of Code | 58 |
Function Count | 5 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | 'use strict'; |
||
2 | |||
3 | import * as fs from 'fs'; |
||
4 | import * as readline from 'readline'; |
||
5 | |||
6 | import Botlang from '../Botlang'; |
||
7 | import pkg from '../../package.json'; |
||
8 | import Styles from './Styles'; |
||
9 | |||
10 | const options = new Map() |
||
11 | .set('botname', 'Bot') |
||
12 | .set('path', process.argv[process.argv.length - 1]) |
||
13 | .set('prompt', '> ') |
||
14 | .set('system', 'Sys') |
||
15 | .set('username', 'You'), |
||
16 | rl = readline.createInterface({ |
||
17 | input : process.stdin, |
||
18 | output : process.stdout |
||
19 | }), |
||
20 | sourceCode = fs.readFileSync(options.get('path'), { |
||
21 | encoding : 'utf8', |
||
22 | flag : 'r' |
||
23 | }), |
||
24 | bot = new Botlang(sourceCode), |
||
25 | write = (text) => { |
||
26 | process.stdout.write(`${text}`); |
||
27 | }, |
||
28 | writeBot = (text) => { |
||
29 | write(`${Styles.green.open}[${options.get('botname')}]:${Styles.green.close} ${text}`); |
||
30 | }, |
||
31 | writeSys = (text) => { |
||
32 | write(`${Styles.green.open}[${options.get('system')}]:${Styles.green.close} ${text}`); |
||
33 | }; |
||
34 | |||
35 | write(`\n${Styles.yellow.open}`); |
||
36 | write(`🤖 Welcome to botlang#${pkg.version}\n`); |
||
37 | write(` Brain from "${options.get('path')}" loaded\n`); |
||
38 | write(`${Styles.yellow.close}\n`); |
||
39 | |||
40 | rl.setPrompt(`${Styles.green.open}[${options.get('username')}]:${Styles.green.close} `); |
||
41 | rl.prompt(); |
||
42 | |||
43 | rl.on('line', (line) => { |
||
44 | const input = line.trim(); |
||
45 | |||
46 | if (/\\help/.test(input)) { |
||
47 | writeSys('Not implemented yet. Read the source code in the meantime ...\n'); |
||
48 | } else { |
||
49 | writeBot(`${bot.reply(input)}\n`); |
||
50 | } |
||
51 | |||
52 | rl.prompt(); |
||
53 | }); |
||
54 | |||
55 | rl.on('close', () => { |
||
56 | write(`\n\n${Styles.yellow.open}🤖 Bye bye ...${Styles.yellow.close}\n`); |
||
57 | process.exit(0); |
||
|
|||
58 | }); |
||
59 |