-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
move.js
43 lines (39 loc) · 1.06 KB
/
move.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"use strict";
var d = require("d")
, trunc = require("es5-ext/math/trunc");
var 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 ? "B" : "A";
n = floor(abs(n));
return "\x1b[" + n + dir + "\x1b[1G";
}),
top: d("\x1b[5000F"),
bottom: d("\x1b[5000B"),
lineBegin: d("\x1b[5000D"),
lineEnd: d("\x1b[5000C")
}
);