From 7de521bd393b8622b891665dc27f97a25b56ef4a Mon Sep 17 00:00:00 2001 From: dylan <> Date: Wed, 3 May 2023 13:44:28 -0700 Subject: [PATCH] Show caret, backspace, and left-right arrows --- builtins.ts | 5 +++++ keyboard.ts | 5 +++++ repl.ts | 23 +++++++++++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/builtins.ts b/builtins.ts index 890da72..5432817 100644 --- a/builtins.ts +++ b/builtins.ts @@ -54,10 +54,15 @@ const drawText = (x: number, y: number, text: string) => { }); } +const drawRect = (x: number, y: number, w: number, h: number, color: number) => { + setPixelsInRect(x, y, w, Array(w*h).fill(color)); +} + const faux = { clear_screen: clearScreen, draw_sprite: drawSprite, draw_text: drawText, + draw_rect: drawRect, key_down: keyDown, key_pressed: keyPressed, key_released: keyReleased, diff --git a/keyboard.ts b/keyboard.ts index d0d16f3..bbc39df 100644 --- a/keyboard.ts +++ b/keyboard.ts @@ -5,11 +5,16 @@ export const K = { ENTER: 257, TAB: 258, BACKSPACE: 259, + INSERT: 260, DELETE: 261, ARROW_RIGHT: 262, ARROW_LEFT: 263, ARROW_DOWN: 264, ARROW_UP: 265, + PAGE_UP: 266, + PAGE_DOWN: 267, + HOME: 268, + END: 269, CAPS_LOCK: 280, F1: 290, F2: 291, diff --git a/repl.ts b/repl.ts index 67947e0..8ec35df 100644 --- a/repl.ts +++ b/repl.ts @@ -1,8 +1,9 @@ import faux from "./builtins.ts"; -import { getKeysPressed, shiftKeyDown, shiftMap } from "./keyboard.ts"; +import { getKeysPressed, shiftKeyDown, shiftMap, K } from "./keyboard.ts"; import { font } from "./font.ts"; let line = ""; +let index = 0; const update = () => { for (const key of getKeysPressed()) { @@ -15,13 +16,31 @@ const update = () => { } } if (char in font) { - line += char; + line = line.slice(0, index)+char+line.slice(index); + index += 1; + } else if (key === K.BACKSPACE) { + line = line.slice(0, -1); + index -= 1; + if (index < 0) { + index = 0; + } + } else if (key === K.ARROW_LEFT) { + index -= 1; + if (index < 0) { + index = 0; + } + } else if (key === K.ARROW_RIGHT) { + index += 1; + if (index > line.length) { + index = line.length; + } } } } const draw = () => { faux.clear_screen(); + faux.draw_rect((2+index)*4, 0, 5, 6, 3); faux.draw_text(0, 0, "> "+line); }