/* * Used for prompt popup on vim */ const readline = require("readline") const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: true, escapeCodeTimeout: 0, prompt: '' }) let value = process.argv[2] let placeholder = process.argv[3] let clear = false if (value) { rl.write(value) } else if (placeholder) { clear = true rl.write('\x1B[90m' + placeholder + '\x1B[39m') rl.write('', {ctrl: true, name: 'a'}) } rl.on('line', input => { send(['confirm', clear ? '' : input]) process.exit() }) let original_ttyWrite = rl._ttyWrite rl._ttyWrite = function (code, key) { if (key.name === 'enter') { send(['send', '']) return '' } original_ttyWrite.apply(rl, arguments) if (clear && rl.line.includes('\x1B')) { clear = false rl.write('', {ctrl: true, name: 'k'}) return } send(['change', rl.line]) } function createSequences(str) { return '\033]51;' + str + '\x07' } function send(args) { process.stdout.write(createSequences(JSON.stringify(['call', 'CocPopupCallback', args]))) } process.stdin.on('keypress', (e, key) => { if (key) { let k = getKey(key) if (k == '') { return } if (k == '') { send(['exit', '']) process.exit() } if (k) { send(['send', k]) return } } }) function getKey(key) { if (key.ctrl === true) { if (key.name == 'n') { return '' } if (key.name == 'p') { return '' } if (key.name == 'j') { return '' } if (key.name == 'k') { return '' } if (key.name == 'f') { return '' } if (key.name == 'b') { return '' } if (key.sequence == '\x00') { return '' } } if (key.sequence == '\u001b') { return '' } if (key.sequence == '\r') { return '' } if (key.sequence == '\t') { return key.shift ? '' : '' } if (key.name == 'up') { return '' } if (key.name == 'down') { return '' } return '' }