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`,
|
||||
ArrowRight = `${ANSI_PREFIX}C`,
|
||||
ArrowLeft = `${ANSI_PREFIX}D`,
|
||||
PageUp = `${ANSI_PREFIX}5~`,
|
||||
PageDown = `${ANSI_PREFIX}6~`,
|
||||
Home = 'LineHome',
|
||||
End = 'LineEnd',
|
||||
}
|
||||
|
||||
export const Ansi = {
|
||||
|
@ -29,6 +29,28 @@ export class Editor {
|
||||
this.clearScreen().then(() => {});
|
||||
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.ArrowDown:
|
||||
case KeyCommand.ArrowRight:
|
||||
@ -43,16 +65,24 @@ export class Editor {
|
||||
private moveCursor(char: string): void {
|
||||
switch (char) {
|
||||
case KeyCommand.ArrowLeft:
|
||||
if (this.#cursor.x > 0) {
|
||||
this.#cursor.x--;
|
||||
}
|
||||
break;
|
||||
case KeyCommand.ArrowRight:
|
||||
if (this.#cursor.x < this.#screen.cols) {
|
||||
this.#cursor.x++;
|
||||
}
|
||||
break;
|
||||
case KeyCommand.ArrowUp:
|
||||
if (this.#cursor.y > 0) {
|
||||
this.#cursor.y--;
|
||||
}
|
||||
break;
|
||||
case KeyCommand.ArrowDown:
|
||||
if (this.#cursor.y < this.#screen.rows) {
|
||||
this.#cursor.y++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,37 @@
|
||||
* The starting point for running scroll
|
||||
*/
|
||||
import { Editor, getRuntime, getTermios } from './common/mod.ts';
|
||||
import { KeyCommand } from './common/editor/ansi.ts';
|
||||
|
||||
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() {
|
||||
const runTime = await getRuntime();
|
||||
const { io, onExit } = runTime;
|
||||
@ -25,7 +53,7 @@ export async function main() {
|
||||
// The main event loop
|
||||
for await (const chunk of io.inputLoop()) {
|
||||
// Process input
|
||||
const char = String(decoder.decode(chunk));
|
||||
const char = readKey(chunk);
|
||||
const shouldLoop = editor.processKeyPress(char);
|
||||
if (!shouldLoop) {
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user