2022-05-18 13:29:28 -04:00
|
|
|
/*
|
|
|
|
* Used for prompt popup on vim
|
|
|
|
*/
|
|
|
|
const readline = require("readline")
|
|
|
|
const rl = readline.createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout,
|
2022-05-19 10:14:37 -04:00
|
|
|
escapeCodeTimeout: 0,
|
|
|
|
prompt: ''
|
2022-05-18 13:29:28 -04:00
|
|
|
})
|
|
|
|
rl.setPrompt('')
|
|
|
|
let value = process.argv[2]
|
|
|
|
if (value) {
|
|
|
|
rl.write(value)
|
|
|
|
}
|
|
|
|
rl.on('line', input => {
|
2022-05-19 10:14:37 -04:00
|
|
|
send(['confirm', input])
|
2022-05-18 13:29:28 -04:00
|
|
|
process.exit()
|
|
|
|
})
|
|
|
|
|
2022-05-19 10:14:37 -04:00
|
|
|
let original_ttyWrite = rl._ttyWrite
|
|
|
|
rl._ttyWrite = function (code, key) {
|
|
|
|
if (key.name === 'enter') {
|
|
|
|
send(['send', '<C-j>'])
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
original_ttyWrite.apply(rl, arguments)
|
|
|
|
send(['change', rl.line])
|
|
|
|
}
|
|
|
|
|
2022-05-18 13:29:28 -04:00
|
|
|
function createSequences(str) {
|
|
|
|
return '\033]51;' + str + '\x07'
|
|
|
|
}
|
|
|
|
|
2022-05-19 10:14:37 -04:00
|
|
|
function send(args) {
|
|
|
|
process.stdout.write(createSequences(JSON.stringify(['call', 'CocPopupCallback', args])))
|
|
|
|
}
|
|
|
|
|
|
|
|
process.stdin.on('keypress', (e, key) => {
|
2022-05-18 13:29:28 -04:00
|
|
|
if (key) {
|
|
|
|
let k = getKey(key)
|
|
|
|
if (k == '<bs>') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (k == '<esc>') {
|
2022-05-19 10:14:37 -04:00
|
|
|
send(['exit', ''])
|
2022-05-18 13:29:28 -04:00
|
|
|
process.exit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (k) {
|
2022-05-19 10:14:37 -04:00
|
|
|
send(['send', k])
|
|
|
|
return
|
2022-05-18 13:29:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function getKey(key) {
|
2022-05-19 10:14:37 -04:00
|
|
|
if (key.ctrl === true) {
|
|
|
|
if (key.name == 'n') {
|
|
|
|
return '<C-n>'
|
|
|
|
}
|
|
|
|
if (key.name == 'p') {
|
|
|
|
return '<C-p>'
|
|
|
|
}
|
|
|
|
if (key.name == 'j') {
|
|
|
|
return '<C-j>'
|
|
|
|
}
|
|
|
|
if (key.name == 'k') {
|
|
|
|
return '<C-k>'
|
|
|
|
}
|
|
|
|
if (key.name == 'f') {
|
|
|
|
return '<C-f>'
|
|
|
|
}
|
|
|
|
if (key.name == 'b') {
|
|
|
|
return '<C-b>'
|
|
|
|
}
|
|
|
|
if (key.sequence == '\x00') {
|
|
|
|
return '<C-@>'
|
|
|
|
}
|
|
|
|
}
|
2022-05-18 13:29:28 -04:00
|
|
|
if (key.sequence == '\u001b') {
|
|
|
|
return '<esc>'
|
|
|
|
}
|
|
|
|
if (key.sequence == '\r') {
|
|
|
|
return '<cr>'
|
|
|
|
}
|
|
|
|
if (key.sequence == '\t') {
|
|
|
|
return key.shift ? '<s-tab>' : '<tab>'
|
|
|
|
}
|
|
|
|
if (key.name == 'up') {
|
|
|
|
return '<up>'
|
|
|
|
}
|
|
|
|
if (key.name == 'down') {
|
|
|
|
return '<down>'
|
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|