Implement 'command minus n'.

pull/11/head
Attacktive 3 years ago
parent 69d37e0962
commit 9d618380ed

@ -26,14 +26,29 @@ export function push(command: string): History {
export function getCommandIfHistoryExpansion(input): string {
let command = undefined;
const regExpExecArray = /!(?:(\d+)|!)/.exec(input);
const regExpExecArray = /!(-?)(?:(\d+)|!)/.exec(input);
if (regExpExecArray) {
const index = regExpExecArray[1];
if (index === '!') {
const isMinus =
Boolean(regExpExecArray[1]) && regExpExecArray[1].length > 0;
/**
* either digits or '!'
*/
const n = regExpExecArray[2];
const index = parseInt(n);
if (Number.isNaN(index)) {
command = getLast();
} else {
// parseInt here is guaranteed to succeed I suppose.
command = get(parseInt(index));
/**
* !0, !-0 are invalid
*/
if (index > 0) {
if (isMinus) {
command = getMinus(index);
} else {
command = get(index);
}
}
}
}
@ -44,6 +59,11 @@ function get(index: number): string {
return history[index];
}
function getMinus(index: number): string {
// '!!' is a synonym for '!-1' so + 1
return history[historyIndex - index + 1];
}
function getLast(): string {
return history[historyIndex];
}

Loading…
Cancel
Save