Created
January 4, 2020 20:58
-
-
Save Kirkkt/bfef4a2d0e04dc1a5e64a55b6ef98f0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const clearScreen = () => { | |
const readline = require('readline') | |
const blank = '\n'.repeat(process.stdout.rows) | |
console.log(blank) | |
readline.cursorTo(process.stdout, 0, 0) | |
readline.clearScreenDown(process.stdout) | |
} | |
const renderContent = (lines, highlightIndex, moveMode) => { | |
clearScreen() | |
lines.forEach((line, index) => { | |
if (index === highlightIndex && moveMode) { | |
console.log('' + line); | |
} else if (index === highlightIndex) { | |
console.log('> ' + line); | |
} else { | |
console.log(' ' + line); | |
} | |
}) | |
} | |
const captureKeyPress = () => { | |
let content = 'something\nanything\nnothing' | |
let lines = content.split('\n') | |
const readline = require('readline'); | |
let highlightIndex = 0; | |
let moveMode = false | |
let inputMode = false | |
renderContent(lines, highlightIndex, moveMode); | |
readline.emitKeypressEvents(process.stdin); | |
process.stdin.setRawMode(true); | |
process.stdin.on('keypress', (str, key) => { | |
if (key.ctrl && key.name === 'c') { | |
process.exit(); | |
} else if (inputMode) { | |
console.log(str, key) | |
} else if (str === 'j' && highlightIndex < lines.length - 1) { | |
if (moveMode) { | |
[ lines[highlightIndex], lines[highlightIndex + 1] ] = [ lines[highlightIndex + 1], lines[highlightIndex] ] | |
} | |
highlightIndex++ | |
} else if (str === 'k' && highlightIndex > 0) { | |
if (moveMode) { | |
[ lines[highlightIndex], lines[highlightIndex - 1] ] = [ lines[highlightIndex - 1], lines[highlightIndex] ] | |
} | |
highlightIndex-- | |
} else if (str === 'v') { | |
moveMode = !moveMode | |
} else if (str === 'a') { | |
inputMode = true | |
} else if (str === 'd') { | |
lines.splice(highlightIndex, 1) | |
if (highlightIndex >= lines.length) { | |
highlightIndex = lines.length - 1 | |
} | |
} | |
if (!inputMode) { | |
renderContent(lines, highlightIndex, moveMode); | |
} else { | |
renderInputMode() | |
} | |
}); | |
} | |
captureKeyPress() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment