-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (38 loc) · 886 Bytes
/
index.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
const say = require('say')
const readline = require('readline')
const stdin = process.stdin
stdin.setRawMode(true)
stdin.resume()
stdin.setEncoding('utf8')
let currentWord = ''
let talking = false
stdin.on('data', key => {
// ctrl-c
if (key === '\u0003') {
process.exit()
}
process.stdout.write(key)
if (key === '\u007f') { // delete key
readline.moveCursor(process.stdout, -1)
readline.clearLine(process.stdout, 1)
currentWord = currentWord.slice(0, -1)
} else {
currentWord += key
}
if(key === ' ') {
if (currentWord && !talking) {
talking = true
say.speak(currentWord, null, null, err => {
if (err) {
console.error(err)
}
talking = false
if(currentWord.endsWith(' ')){
say.speak(currentWord)
currentWord = ''
}
})
currentWord = ''
}
}
})