|
|
|
@ -1,12 +1,13 @@
|
|
|
|
|
import Ansi, * as _Ansi from './ansi.ts';
|
|
|
|
|
import Buffer from './buffer.ts';
|
|
|
|
|
import Document from './document.ts';
|
|
|
|
|
import Editor from './editor.ts';
|
|
|
|
|
import Position from './position.ts';
|
|
|
|
|
import Row from './row.ts';
|
|
|
|
|
import { Ansi, KeyCommand } from './ansi.ts';
|
|
|
|
|
|
|
|
|
|
import * as Fn from './fns.ts';
|
|
|
|
|
import { defaultTerminalSize, SCROLL_TAB_SIZE } from './config.ts';
|
|
|
|
|
import { getTestRunner } from './runtime.ts';
|
|
|
|
|
import { Position } from './types.ts';
|
|
|
|
|
import * as Fn from './fns.ts';
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
assertEquals,
|
|
|
|
@ -19,16 +20,33 @@ const {
|
|
|
|
|
testSuite,
|
|
|
|
|
} = await getTestRunner();
|
|
|
|
|
|
|
|
|
|
const ANSITest = {
|
|
|
|
|
'moveCursor()': () => {
|
|
|
|
|
assertEquals(Ansi.moveCursor(1, 2), '\x1b[2;3H');
|
|
|
|
|
},
|
|
|
|
|
'moveCursorForward()': () => {
|
|
|
|
|
assertEquals(Ansi.moveCursorForward(2), '\x1b[2C');
|
|
|
|
|
},
|
|
|
|
|
'moveCursorDown()': () => {
|
|
|
|
|
assertEquals(Ansi.moveCursorDown(7), '\x1b[7B');
|
|
|
|
|
},
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
const ANSITest = () => {
|
|
|
|
|
const { AnsiColor, Ground } = _Ansi;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'color()': () => {
|
|
|
|
|
assertEquals(Ansi.color(AnsiColor.FgBlue), '\x1b[34m');
|
|
|
|
|
},
|
|
|
|
|
'color256()': () => {
|
|
|
|
|
assertEquals(Ansi.color256(128, Ground.Back), '\x1b[48;5;128m');
|
|
|
|
|
assertEquals(Ansi.color256(128, Ground.Fore), '\x1b[38;5;128m');
|
|
|
|
|
},
|
|
|
|
|
'rgb()': () => {
|
|
|
|
|
assertEquals(Ansi.rgb(32, 64, 128, Ground.Back), '\x1b[48;2;32;64;128m');
|
|
|
|
|
assertEquals(Ansi.rgb(32, 64, 128, Ground.Fore), '\x1b[38;2;32;64;128m');
|
|
|
|
|
},
|
|
|
|
|
'moveCursor()': () => {
|
|
|
|
|
assertEquals(Ansi.moveCursor(1, 2), '\x1b[2;3H');
|
|
|
|
|
},
|
|
|
|
|
'moveCursorForward()': () => {
|
|
|
|
|
assertEquals(Ansi.moveCursorForward(2), '\x1b[2C');
|
|
|
|
|
},
|
|
|
|
|
'moveCursorDown()': () => {
|
|
|
|
|
assertEquals(Ansi.moveCursorDown(7), '\x1b[7B');
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
@ -244,185 +262,187 @@ const RowTest = {
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
const fnTest = {
|
|
|
|
|
'defined()': () => {
|
|
|
|
|
const { defined } = Fn;
|
|
|
|
|
assertFalse(defined(null));
|
|
|
|
|
assertFalse(defined(void 0));
|
|
|
|
|
assertFalse(defined(undefined));
|
|
|
|
|
assertTrue(defined(0));
|
|
|
|
|
assertTrue(defined(false));
|
|
|
|
|
},
|
|
|
|
|
'nullish()': () => {
|
|
|
|
|
const { nullish } = Fn;
|
|
|
|
|
const fnTest = () => {
|
|
|
|
|
const {
|
|
|
|
|
some,
|
|
|
|
|
none,
|
|
|
|
|
arrayInsert,
|
|
|
|
|
noop,
|
|
|
|
|
posSub,
|
|
|
|
|
minSub,
|
|
|
|
|
maxAdd,
|
|
|
|
|
ord,
|
|
|
|
|
strChars,
|
|
|
|
|
ctrlKey,
|
|
|
|
|
isControl,
|
|
|
|
|
isAscii,
|
|
|
|
|
isAsciiDigit,
|
|
|
|
|
strlen,
|
|
|
|
|
truncate,
|
|
|
|
|
} = Fn;
|
|
|
|
|
|
|
|
|
|
assertTrue(nullish(null));
|
|
|
|
|
assertTrue(nullish(void 0));
|
|
|
|
|
assertTrue(nullish(undefined));
|
|
|
|
|
assertFalse(nullish(0));
|
|
|
|
|
assertFalse(nullish(false));
|
|
|
|
|
},
|
|
|
|
|
'arrayInsert() strings': () => {
|
|
|
|
|
const { arrayInsert } = Fn;
|
|
|
|
|
return {
|
|
|
|
|
'some()': () => {
|
|
|
|
|
assertFalse(some(null));
|
|
|
|
|
assertFalse(some(void 0));
|
|
|
|
|
assertFalse(some(undefined));
|
|
|
|
|
assertTrue(some(0));
|
|
|
|
|
assertTrue(some(false));
|
|
|
|
|
},
|
|
|
|
|
'none()': () => {
|
|
|
|
|
assertTrue(none(null));
|
|
|
|
|
assertTrue(none(void 0));
|
|
|
|
|
assertTrue(none(undefined));
|
|
|
|
|
assertFalse(none(0));
|
|
|
|
|
assertFalse(none(false));
|
|
|
|
|
},
|
|
|
|
|
'arrayInsert() strings': () => {
|
|
|
|
|
const a = ['😺', '😸', '😹'];
|
|
|
|
|
const b = arrayInsert(a, 1, 'x');
|
|
|
|
|
const c = ['😺', 'x', '😸', '😹'];
|
|
|
|
|
assertEquals(b, c);
|
|
|
|
|
|
|
|
|
|
const a = ['😺', '😸', '😹'];
|
|
|
|
|
const b = arrayInsert(a, 1, 'x');
|
|
|
|
|
const c = ['😺', 'x', '😸', '😹'];
|
|
|
|
|
assertEquals(b, c);
|
|
|
|
|
const d = arrayInsert(c, 17, 'y');
|
|
|
|
|
const e = ['😺', 'x', '😸', '😹', 'y'];
|
|
|
|
|
assertEquals(d, e);
|
|
|
|
|
|
|
|
|
|
const d = arrayInsert(c, 17, 'y');
|
|
|
|
|
const e = ['😺', 'x', '😸', '😹', 'y'];
|
|
|
|
|
assertEquals(d, e);
|
|
|
|
|
assertEquals(arrayInsert([], 0, 'foo'), ['foo']);
|
|
|
|
|
},
|
|
|
|
|
'arrayInsert() numbers': () => {
|
|
|
|
|
const a = [1, 3, 5];
|
|
|
|
|
const b = [1, 3, 4, 5];
|
|
|
|
|
assertEquals(arrayInsert(a, 2, 4), b);
|
|
|
|
|
|
|
|
|
|
assertEquals(arrayInsert([], 0, 'foo'), ['foo']);
|
|
|
|
|
},
|
|
|
|
|
'arrayInsert() numbers': () => {
|
|
|
|
|
const { arrayInsert } = Fn;
|
|
|
|
|
const c = [1, 2, 3, 4, 5];
|
|
|
|
|
assertEquals(arrayInsert(b, 1, 2), c);
|
|
|
|
|
},
|
|
|
|
|
'noop fn': () => {
|
|
|
|
|
assertExists(noop);
|
|
|
|
|
assertEquals(noop(), undefined);
|
|
|
|
|
},
|
|
|
|
|
'posSub()': () => {
|
|
|
|
|
assertEquals(posSub(14, 15), 0);
|
|
|
|
|
assertEquals(posSub(15, 1), 14);
|
|
|
|
|
},
|
|
|
|
|
'minSub()': () => {
|
|
|
|
|
assertEquals(minSub(13, 25, -1), -1);
|
|
|
|
|
assertEquals(minSub(25, 13, 0), 12);
|
|
|
|
|
},
|
|
|
|
|
'maxAdd()': () => {
|
|
|
|
|
assertEquals(maxAdd(99, 99, 75), 75);
|
|
|
|
|
assertEquals(maxAdd(25, 74, 101), 99);
|
|
|
|
|
},
|
|
|
|
|
'ord()': () => {
|
|
|
|
|
// Invalid output
|
|
|
|
|
assertEquals(ord(''), 256);
|
|
|
|
|
|
|
|
|
|
const a = [1, 3, 5];
|
|
|
|
|
const b = [1, 3, 4, 5];
|
|
|
|
|
assertEquals(arrayInsert(a, 2, 4), b);
|
|
|
|
|
// Valid output
|
|
|
|
|
assertEquals(ord('a'), 97);
|
|
|
|
|
},
|
|
|
|
|
'strChars() properly splits strings into unicode characters': () => {
|
|
|
|
|
assertEquals(strChars('😺😸😹'), ['😺', '😸', '😹']);
|
|
|
|
|
},
|
|
|
|
|
'ctrlKey()': () => {
|
|
|
|
|
const ctrl_a = ctrlKey('a');
|
|
|
|
|
assertTrue(isControl(ctrl_a));
|
|
|
|
|
assertEquals(ctrl_a, String.fromCodePoint(0x01));
|
|
|
|
|
|
|
|
|
|
const c = [1, 2, 3, 4, 5];
|
|
|
|
|
assertEquals(arrayInsert(b, 1, 2), c);
|
|
|
|
|
},
|
|
|
|
|
'noop fn': () => {
|
|
|
|
|
assertExists(Fn.noop);
|
|
|
|
|
assertEquals(Fn.noop(), undefined);
|
|
|
|
|
},
|
|
|
|
|
'posSub()': () => {
|
|
|
|
|
assertEquals(Fn.posSub(14, 15), 0);
|
|
|
|
|
assertEquals(Fn.posSub(15, 1), 14);
|
|
|
|
|
},
|
|
|
|
|
'minSub()': () => {
|
|
|
|
|
assertEquals(Fn.minSub(13, 25, -1), -1);
|
|
|
|
|
assertEquals(Fn.minSub(25, 13, 0), 12);
|
|
|
|
|
},
|
|
|
|
|
'maxAdd()': () => {
|
|
|
|
|
assertEquals(Fn.maxAdd(99, 99, 75), 75);
|
|
|
|
|
assertEquals(Fn.maxAdd(25, 74, 101), 99);
|
|
|
|
|
},
|
|
|
|
|
'ord()': () => {
|
|
|
|
|
const { ord } = Fn;
|
|
|
|
|
const invalid = ctrlKey('😺');
|
|
|
|
|
assertFalse(isControl(invalid));
|
|
|
|
|
assertEquals(invalid, '😺');
|
|
|
|
|
},
|
|
|
|
|
'isAscii()': () => {
|
|
|
|
|
assertTrue(isAscii('asjyverkjhsdf1928374'));
|
|
|
|
|
assertFalse(isAscii('😺acalskjsdf'));
|
|
|
|
|
assertFalse(isAscii('ab😺ac'));
|
|
|
|
|
},
|
|
|
|
|
'isAsciiDigit()': () => {
|
|
|
|
|
assertTrue(isAsciiDigit('1234567890'));
|
|
|
|
|
assertFalse(isAsciiDigit('A1'));
|
|
|
|
|
assertFalse(isAsciiDigit('/'));
|
|
|
|
|
assertFalse(isAsciiDigit(':'));
|
|
|
|
|
},
|
|
|
|
|
'isControl()': () => {
|
|
|
|
|
assertFalse(isControl('abc'));
|
|
|
|
|
assertTrue(isControl(String.fromCodePoint(0x01)));
|
|
|
|
|
assertFalse(isControl('😺'));
|
|
|
|
|
},
|
|
|
|
|
'strlen()': () => {
|
|
|
|
|
// Ascii length
|
|
|
|
|
assertEquals(strlen('abc'), 'abc'.length);
|
|
|
|
|
|
|
|
|
|
// Invalid output
|
|
|
|
|
assertEquals(ord(''), 256);
|
|
|
|
|
// Get number of visible unicode characters
|
|
|
|
|
assertEquals(strlen('😺😸😹'), 3);
|
|
|
|
|
assertNotEquals('😺😸😹'.length, strlen('😺😸😹'));
|
|
|
|
|
|
|
|
|
|
// Valid output
|
|
|
|
|
assertEquals(ord('a'), 97);
|
|
|
|
|
},
|
|
|
|
|
'strChars() properly splits strings into unicode characters': () => {
|
|
|
|
|
const { strChars } = Fn;
|
|
|
|
|
// Skin tone modifier + base character
|
|
|
|
|
assertEquals(strlen('🤰🏼'), 2);
|
|
|
|
|
assertNotEquals('🤰🏼'.length, strlen('🤰🏼'));
|
|
|
|
|
|
|
|
|
|
assertEquals(strChars('😺😸😹'), ['😺', '😸', '😹']);
|
|
|
|
|
},
|
|
|
|
|
'ctrlKey()': () => {
|
|
|
|
|
const { ctrlKey, isControl } = Fn;
|
|
|
|
|
|
|
|
|
|
const ctrl_a = ctrlKey('a');
|
|
|
|
|
assertTrue(isControl(ctrl_a));
|
|
|
|
|
assertEquals(ctrl_a, String.fromCodePoint(0x01));
|
|
|
|
|
|
|
|
|
|
const invalid = ctrlKey('😺');
|
|
|
|
|
assertFalse(isControl(invalid));
|
|
|
|
|
assertEquals(invalid, '😺');
|
|
|
|
|
},
|
|
|
|
|
'isAscii()': () => {
|
|
|
|
|
const { isAscii } = Fn;
|
|
|
|
|
|
|
|
|
|
assertTrue(isAscii('asjyverkjhsdf1928374'));
|
|
|
|
|
assertFalse(isAscii('😺acalskjsdf'));
|
|
|
|
|
assertFalse(isAscii('ab😺ac'));
|
|
|
|
|
},
|
|
|
|
|
'isAsciiDigit()': () => {
|
|
|
|
|
const { isAsciiDigit } = Fn;
|
|
|
|
|
|
|
|
|
|
assertTrue(isAsciiDigit('1234567890'));
|
|
|
|
|
assertFalse(isAsciiDigit('A1'));
|
|
|
|
|
assertFalse(isAsciiDigit('/'));
|
|
|
|
|
assertFalse(isAsciiDigit(':'));
|
|
|
|
|
},
|
|
|
|
|
'isControl()': () => {
|
|
|
|
|
const { isControl } = Fn;
|
|
|
|
|
|
|
|
|
|
assertFalse(isControl('abc'));
|
|
|
|
|
assertTrue(isControl(String.fromCodePoint(0x01)));
|
|
|
|
|
assertFalse(isControl('😺'));
|
|
|
|
|
},
|
|
|
|
|
'strlen()': () => {
|
|
|
|
|
const { strlen } = Fn;
|
|
|
|
|
|
|
|
|
|
// Ascii length
|
|
|
|
|
assertEquals(strlen('abc'), 'abc'.length);
|
|
|
|
|
|
|
|
|
|
// Get number of visible unicode characters
|
|
|
|
|
assertEquals(strlen('😺😸😹'), 3);
|
|
|
|
|
assertNotEquals('😺😸😹'.length, strlen('😺😸😹'));
|
|
|
|
|
|
|
|
|
|
// Skin tone modifier + base character
|
|
|
|
|
assertEquals(strlen('🤰🏼'), 2);
|
|
|
|
|
assertNotEquals('🤰🏼'.length, strlen('🤰🏼'));
|
|
|
|
|
|
|
|
|
|
// This has 4 sub-characters, and 3 zero-width-joiners
|
|
|
|
|
assertEquals(strlen('👨👩👧👦'), 7);
|
|
|
|
|
assertNotEquals('👨👩👧👦'.length, strlen('👨👩👧👦'));
|
|
|
|
|
},
|
|
|
|
|
'truncate()': () => {
|
|
|
|
|
const { truncate } = Fn;
|
|
|
|
|
|
|
|
|
|
assertEquals(truncate('😺😸😹', 1), '😺');
|
|
|
|
|
assertEquals(truncate('😺😸😹', 5), '😺😸😹');
|
|
|
|
|
assertEquals(truncate('👨👩👧👦', 5), '👨👩👧');
|
|
|
|
|
},
|
|
|
|
|
// This has 4 sub-characters, and 3 zero-width-joiners
|
|
|
|
|
assertEquals(strlen('👨👩👧👦'), 7);
|
|
|
|
|
assertNotEquals('👨👩👧👦'.length, strlen('👨👩👧👦'));
|
|
|
|
|
},
|
|
|
|
|
'truncate()': () => {
|
|
|
|
|
assertEquals(truncate('😺😸😹', 1), '😺');
|
|
|
|
|
assertEquals(truncate('😺😸😹', 5), '😺😸😹');
|
|
|
|
|
assertEquals(truncate('👨👩👧👦', 5), '👨👩👧');
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const readKeyTest = () => {
|
|
|
|
|
const { KeyCommand } = _Ansi;
|
|
|
|
|
const { readKey, ctrlKey } = Fn;
|
|
|
|
|
|
|
|
|
|
const testKeyMap = (codes: string[], expected: string) => {
|
|
|
|
|
codes.forEach((code) => {
|
|
|
|
|
assertEquals(Fn.readKey(encoder.encode(code)), expected);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
|
|
|
|
|
const readKeyTest = {
|
|
|
|
|
'empty input': () => {
|
|
|
|
|
assertEquals(Fn.readKey(new Uint8Array(0)), '');
|
|
|
|
|
},
|
|
|
|
|
'passthrough': () => {
|
|
|
|
|
// Ignore unhandled escape sequences
|
|
|
|
|
assertEquals(Fn.readKey(encoder.encode('\x1b[]')), '\x1b[]');
|
|
|
|
|
const testKeyMap = (codes: string[], expected: string) => {
|
|
|
|
|
codes.forEach((code) => {
|
|
|
|
|
assertEquals(readKey(encoder.encode(code)), expected);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Pass explicitly mapped values right through
|
|
|
|
|
assertEquals(
|
|
|
|
|
Fn.readKey(encoder.encode(KeyCommand.ArrowUp)),
|
|
|
|
|
KeyCommand.ArrowUp,
|
|
|
|
|
);
|
|
|
|
|
assertEquals(
|
|
|
|
|
Fn.readKey(encoder.encode(KeyCommand.Home)),
|
|
|
|
|
KeyCommand.Home,
|
|
|
|
|
);
|
|
|
|
|
assertEquals(
|
|
|
|
|
Fn.readKey(encoder.encode(KeyCommand.Delete)),
|
|
|
|
|
KeyCommand.Delete,
|
|
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
'empty input': () => {
|
|
|
|
|
assertEquals(readKey(new Uint8Array(0)), '');
|
|
|
|
|
},
|
|
|
|
|
'passthrough': () => {
|
|
|
|
|
// Ignore unhandled escape sequences
|
|
|
|
|
assertEquals(readKey(encoder.encode('\x1b[]')), '\x1b[]');
|
|
|
|
|
|
|
|
|
|
// And pass through whatever else
|
|
|
|
|
assertEquals(Fn.readKey(encoder.encode('foobaz')), 'foobaz');
|
|
|
|
|
},
|
|
|
|
|
// Pass explicitly mapped values right through
|
|
|
|
|
assertEquals(
|
|
|
|
|
readKey(encoder.encode(KeyCommand.ArrowUp)),
|
|
|
|
|
KeyCommand.ArrowUp,
|
|
|
|
|
);
|
|
|
|
|
assertEquals(
|
|
|
|
|
readKey(encoder.encode(KeyCommand.Home)),
|
|
|
|
|
KeyCommand.Home,
|
|
|
|
|
);
|
|
|
|
|
assertEquals(
|
|
|
|
|
readKey(encoder.encode(KeyCommand.Delete)),
|
|
|
|
|
KeyCommand.Delete,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
'Esc': () => testKeyMap(['\x1b', Fn.ctrlKey('l')], KeyCommand.Escape),
|
|
|
|
|
'Backspace': () =>
|
|
|
|
|
testKeyMap(
|
|
|
|
|
[Fn.ctrlKey('h'), '\x7f'],
|
|
|
|
|
KeyCommand.Backspace,
|
|
|
|
|
),
|
|
|
|
|
'Home': () =>
|
|
|
|
|
testKeyMap(['\x1b[1~', '\x1b[7~', '\x1b[H', '\x1bOH'], KeyCommand.Home),
|
|
|
|
|
'End': () =>
|
|
|
|
|
testKeyMap(['\x1b[4~', '\x1b[8~', '\x1b[F', '\x1bOF'], KeyCommand.End),
|
|
|
|
|
'Enter': () => testKeyMap(['\n', '\r', '\v'], KeyCommand.Enter),
|
|
|
|
|
// And pass through whatever else
|
|
|
|
|
assertEquals(readKey(encoder.encode('foobaz')), 'foobaz');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
'Esc': () => testKeyMap(['\x1b', ctrlKey('l')], KeyCommand.Escape),
|
|
|
|
|
'Backspace': () =>
|
|
|
|
|
testKeyMap(
|
|
|
|
|
[ctrlKey('h'), '\x7f'],
|
|
|
|
|
KeyCommand.Backspace,
|
|
|
|
|
),
|
|
|
|
|
'Home': () =>
|
|
|
|
|
testKeyMap(['\x1b[1~', '\x1b[7~', '\x1b[H', '\x1bOH'], KeyCommand.Home),
|
|
|
|
|
'End': () =>
|
|
|
|
|
testKeyMap(['\x1b[4~', '\x1b[8~', '\x1b[F', '\x1bOF'], KeyCommand.End),
|
|
|
|
|
'Enter': () => testKeyMap(['\n', '\r', '\v'], KeyCommand.Enter),
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
@ -430,12 +450,12 @@ const readKeyTest = {
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
testSuite({
|
|
|
|
|
'ANSI utils': ANSITest,
|
|
|
|
|
'ANSI utils': ANSITest(),
|
|
|
|
|
Buffer: BufferTest,
|
|
|
|
|
Document: DocumentTest,
|
|
|
|
|
Editor: EditorTest,
|
|
|
|
|
Position: PositionTest,
|
|
|
|
|
Row: RowTest,
|
|
|
|
|
fns: fnTest,
|
|
|
|
|
'readKey()': readKeyTest,
|
|
|
|
|
fns: fnTest(),
|
|
|
|
|
'readKey()': readKeyTest(),
|
|
|
|
|
});
|
|
|
|
|