Created
November 27, 2013 10:16
-
-
Save Juriy/7673488 to your computer and use it in GitHub Desktop.
POC test
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
var stdin = process.stdin; | |
var map = [ | |
" ┌───────┐ ", | |
" ┌───────┐ │·······│ ", | |
" │·······│ │·······│ ", | |
" │·······│ │·······│ ", | |
" │········░░░░░········│ ", | |
" │·······│ │·······│ ", | |
" │·······│ │·······│ ", | |
" └───────┘ └───────┘ " | |
]; | |
var x = 9; | |
var y = 5; | |
function canMoveTo(x, y) { | |
var ch = map[y - 1][x - 1]; | |
return ch == "·" || ch == "░"; | |
} | |
function cls() { | |
process.stdout.write("\u001b[2J\u001b[0;0H"); | |
} | |
function cursorTo(x, y) { | |
process.stdout.write("\u001b[" + y + ";" + x + "H"); | |
} | |
function drawCharacter() { | |
cursorTo(x, y); | |
process.stdout.write("@"); | |
cursorTo(x, y); | |
} | |
function onMove(deltaX, deltaY) { | |
if (!canMoveTo(x + deltaX, y + deltaY)) | |
return; | |
moveCharacter(deltaX, deltaY); | |
} | |
function moveCharacter(deltaX, deltaY) { | |
cursorTo(x, y); | |
process.stdout.write(map[y - 1][x - 1]); | |
x += deltaX; | |
y += deltaY; | |
drawCharacter(); | |
} | |
process.stdout.write("\u001b[2J\u001b[0;0H"); | |
for (var i = 0; i < map.length; i++) { | |
console.log(map[i]); | |
} | |
drawCharacter(); | |
// without this, we would only get streams once enter is pressed | |
stdin.setRawMode(true); | |
// resume stdin in the parent process (node app won't quit all by itself | |
// unless an error or process.exit() happens) | |
stdin.resume(); | |
// switch from binary to something more useful | |
stdin.setEncoding('utf8'); | |
// on any data into stdin | |
stdin.on( 'data', function(key) { | |
//moveTo(0, 0); | |
//process.stdout.write("" + key.charCodeAt(0)); | |
//process.stdout.write( key ); | |
// ctrl-c ( end of text ) | |
if (key === "\u001b[A") { | |
onMove(0, -1); | |
} else if (key === "\u001b[B") { | |
onMove(0, 1); | |
} else if (key === "\u001b[C") { | |
onMove(1, 0); | |
} else if (key === "\u001b[D") { | |
onMove(-1, 0); | |
} | |
if (key === "\u001b") { | |
cls(); | |
process.exit(); | |
} | |
// write the key to stdout all normal like | |
// process.stdout.write(key); | |
}); | |
// var util = require('util'); | |
// util.print("\u001b[2J\u001b[0;0H"); | |
process.stdout.write("\u001b[5;5H"); | |
process.stdout.write("$"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment