Add PageUp, PageDown, Home, and End input handling
This commit is contained in:
parent
61d222a9af
commit
3f1326ebd0
@ -12,6 +12,10 @@ export enum KeyCommand {
|
|||||||
ArrowDown = `${ANSI_PREFIX}B`,
|
ArrowDown = `${ANSI_PREFIX}B`,
|
||||||
ArrowRight = `${ANSI_PREFIX}C`,
|
ArrowRight = `${ANSI_PREFIX}C`,
|
||||||
ArrowLeft = `${ANSI_PREFIX}D`,
|
ArrowLeft = `${ANSI_PREFIX}D`,
|
||||||
|
PageUp = `${ANSI_PREFIX}5~`,
|
||||||
|
PageDown = `${ANSI_PREFIX}6~`,
|
||||||
|
Home = 'LineHome',
|
||||||
|
End = 'LineEnd',
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Ansi = {
|
export const Ansi = {
|
||||||
|
@ -29,6 +29,28 @@ export class Editor {
|
|||||||
this.clearScreen().then(() => {});
|
this.clearScreen().then(() => {});
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
case KeyCommand.Home:
|
||||||
|
this.#cursor.x = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case KeyCommand.End:
|
||||||
|
this.#cursor.x = this.#screen.cols - 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case KeyCommand.PageUp:
|
||||||
|
case KeyCommand.PageDown:
|
||||||
|
{
|
||||||
|
let times = this.#screen.rows;
|
||||||
|
while (times--) {
|
||||||
|
this.moveCursor(
|
||||||
|
input === KeyCommand.PageUp
|
||||||
|
? KeyCommand.ArrowUp
|
||||||
|
: KeyCommand.ArrowDown,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case KeyCommand.ArrowUp:
|
case KeyCommand.ArrowUp:
|
||||||
case KeyCommand.ArrowDown:
|
case KeyCommand.ArrowDown:
|
||||||
case KeyCommand.ArrowRight:
|
case KeyCommand.ArrowRight:
|
||||||
@ -43,16 +65,24 @@ export class Editor {
|
|||||||
private moveCursor(char: string): void {
|
private moveCursor(char: string): void {
|
||||||
switch (char) {
|
switch (char) {
|
||||||
case KeyCommand.ArrowLeft:
|
case KeyCommand.ArrowLeft:
|
||||||
this.#cursor.x--;
|
if (this.#cursor.x > 0) {
|
||||||
|
this.#cursor.x--;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case KeyCommand.ArrowRight:
|
case KeyCommand.ArrowRight:
|
||||||
this.#cursor.x++;
|
if (this.#cursor.x < this.#screen.cols) {
|
||||||
|
this.#cursor.x++;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case KeyCommand.ArrowUp:
|
case KeyCommand.ArrowUp:
|
||||||
this.#cursor.y--;
|
if (this.#cursor.y > 0) {
|
||||||
|
this.#cursor.y--;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case KeyCommand.ArrowDown:
|
case KeyCommand.ArrowDown:
|
||||||
this.#cursor.y++;
|
if (this.#cursor.y < this.#screen.rows) {
|
||||||
|
this.#cursor.y++;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,37 @@
|
|||||||
* The starting point for running scroll
|
* The starting point for running scroll
|
||||||
*/
|
*/
|
||||||
import { Editor, getRuntime, getTermios } from './common/mod.ts';
|
import { Editor, getRuntime, getTermios } from './common/mod.ts';
|
||||||
|
import { KeyCommand } from './common/editor/ansi.ts';
|
||||||
|
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
|
|
||||||
|
function readKey(raw: Uint8Array): string {
|
||||||
|
const parsed = decoder.decode(raw);
|
||||||
|
|
||||||
|
// Return the input if it's unambiguous
|
||||||
|
if (parsed in KeyCommand) {
|
||||||
|
return parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Some keycodes have multiple potential inputs
|
||||||
|
switch (parsed) {
|
||||||
|
case '\x1bOH':
|
||||||
|
case '\x1b[7~':
|
||||||
|
case '\x1b[1~':
|
||||||
|
case '\x1b[H':
|
||||||
|
return KeyCommand.Home;
|
||||||
|
|
||||||
|
case '\x1bOF':
|
||||||
|
case '\x1b[8~':
|
||||||
|
case '\x1b[4~':
|
||||||
|
case '\x1b[F':
|
||||||
|
return KeyCommand.End;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return parsed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function main() {
|
export async function main() {
|
||||||
const runTime = await getRuntime();
|
const runTime = await getRuntime();
|
||||||
const { io, onExit } = runTime;
|
const { io, onExit } = runTime;
|
||||||
@ -25,7 +53,7 @@ export async function main() {
|
|||||||
// The main event loop
|
// The main event loop
|
||||||
for await (const chunk of io.inputLoop()) {
|
for await (const chunk of io.inputLoop()) {
|
||||||
// Process input
|
// Process input
|
||||||
const char = String(decoder.decode(chunk));
|
const char = readKey(chunk);
|
||||||
const shouldLoop = editor.processKeyPress(char);
|
const shouldLoop = editor.processKeyPress(char);
|
||||||
if (!shouldLoop) {
|
if (!shouldLoop) {
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user