Skip to content

Commit 1ab39ab

Browse files
committed
feat: Shell adapter takes over log formatting and outputs it like it does other messages, prefixed with [loglevel].
1 parent 8aa513a commit 1ab39ab

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

src/adapters/Shell.mjs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,36 @@ const showHelp = () => {
2323
}
2424

2525
const bold = str => `\x1b[1m${str}\x1b[22m`
26+
const green = str => `\x1b[32m${str}\x1b[0m`
27+
const levelColors = {
28+
error: '\x1b[31m',
29+
warn: '\x1b[33m',
30+
debug: '\x1b[35m',
31+
info: '\x1b[34m',
32+
trace: '\x1b[36m',
33+
fatal: '\x1b[91m'
34+
}
35+
const reset = '\x1b[0m'
2636

2737
class Shell extends Adapter {
2838
#rl = null
2939
constructor (robot) {
3040
super(robot)
3141
this.name = 'Shell'
42+
const levels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal']
43+
levels.forEach(level => {
44+
robot.logger[level] = async (...args) => {
45+
const color = levelColors[level] || ''
46+
const msg = `${color}[${level}]${reset} ${args.map(a => typeof a === 'object' ? JSON.stringify(a) : a).join(' ')}`
47+
this.#rl.prompt()
48+
await this.send({ user: { name: 'Logger', room: 'Shell' } }, msg)
49+
}
50+
})
3251
}
3352

3453
async send (envelope, ...strings) {
3554
Array.from(strings).forEach(str => console.log(bold(str)))
55+
this.#rl.prompt()
3656
}
3757

3858
async emote (envelope, ...strings) {
@@ -41,7 +61,7 @@ class Shell extends Adapter {
4161

4262
async reply (envelope, ...strings) {
4363
strings = strings.map((s) => `${envelope.user.name}: ${s}`)
44-
this.send(envelope, ...strings)
64+
await this.send(envelope, ...strings)
4565
}
4666

4767
async run () {
@@ -59,7 +79,7 @@ class Shell extends Adapter {
5979
this.#rl = readline.createInterface({
6080
input: this.robot.stdin ?? process.stdin,
6181
output: this.robot.stdout ?? process.stdout,
62-
prompt: `${this.robot.name ?? this.robot.alias}> `,
82+
prompt: green(`${this.robot.name ?? this.robot.alias}> `),
6383
completer
6484
})
6585
this.#rl.on('line', async (line) => {
@@ -73,6 +93,7 @@ class Shell extends Adapter {
7393
case '\\?':
7494
case 'help':
7595
showHelp()
96+
this.#rl.prompt()
7697
break
7798
case '\\c':
7899
case 'clear':

0 commit comments

Comments
 (0)