Skip to content

Instantly share code, notes, and snippets.

@linuxenko
Created December 3, 2016 07:55
Show Gist options
  • Save linuxenko/e0d6c947adbfd6ddcc2bc6c3155068d5 to your computer and use it in GitHub Desktop.
Save linuxenko/e0d6c947adbfd6ddcc2bc6c3155068d5 to your computer and use it in GitHub Desktop.
'use strict';
module.exports = {
screen: '\x1b[2J',
screenLeft: '\x1b[1J',
screenRight: '\x1b[J',
line: '\x1b[2K',
lineLeft: '\x1b[1K',
lineRight: '\x1b[K'
};
@linuxenko
Copy link
Author

move

'use strict';

var d = require('d')
, trunc = require('es5-ext/math/trunc')

, up, down, right, left
, abs = Math.abs, floor = Math.floor, max = Math.max;

var getMove = function (control) {
return function (num) {
num = isNaN(num) ? 0 : max(floor(num), 0);
return num ? ('\x1b[' + num + control) : '';
};
};
module.exports = Object.defineProperties(function (x, y) {
x = isNaN(x) ? 0 : floor(x);
y = isNaN(y) ? 0 : floor(y);
return ((x > 0) ? right(x) : left(-x)) + ((y > 0) ? down(y) : up(-y));
}, {
up: d(up = getMove('A')),
down: d(down = getMove('B')),
right: d(right = getMove('C')),
left: d(left = getMove('D')),
to: d(function (x, y) {
x = isNaN(x) ? 1 : (max(floor(x), 0) + 1);
y = isNaN(y) ? 1 : (max(floor(y), 0) + 1);
return '\x1b[' + y + ';' + x + 'H';
}),
lines: d(function (n) {
var dir;
n = trunc(n) || 0;
dir = (n >= 0) ? 'E' : 'F';
n = floor(abs(n));
return '\x1b[' + n + dir;
})
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment