From 141204164a2b2ad4c3dbc40429258967b29a8d76 Mon Sep 17 00:00:00 2001 From: binekrasik Date: Wed, 25 Mar 2026 23:07:44 +0100 Subject: [PATCH] sync: umm progress --- src/shell/Wush.ts | 6 +++-- src/terminal/Terminal.ts | 52 ++++++++++++++++++++++------------------ 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/shell/Wush.ts b/src/shell/Wush.ts index 3b507fa..3d01c8e 100644 --- a/src/shell/Wush.ts +++ b/src/shell/Wush.ts @@ -25,15 +25,17 @@ export class Wush extends Shell { if (!isCharacter) switch (key) { case 'Backspace': - this.terminal.RemoveCells(1) + this.terminal.RemoveCell() + this.terminal.MoveCursor(-1, 0) break case 'Enter': + this.terminal.MoveCursor(0, 4, { x: true, y: false }) this.Prompt() break } else { this.terminal.Write(key) - this.terminal.AdjustCursorPosition(1, 0) + this.terminal.MoveCursor(1, 0) } } } diff --git a/src/terminal/Terminal.ts b/src/terminal/Terminal.ts index f6f6d3c..dee6cb8 100644 --- a/src/terminal/Terminal.ts +++ b/src/terminal/Terminal.ts @@ -1,6 +1,7 @@ import type { Shell } from '../shell/Shell' import type { EventBroadcaster } from '../utils/EventBroadcaster' import sqs from '../utils/sqs' +import type { CellContent } from './CellContent' import type { CursorPosition, CursorStyle } from './CursorProperties' export class Terminal { @@ -36,11 +37,7 @@ export class Terminal { this.SetCursorPosition(0, 0) } - AppendLine(content: string, moveCursor: boolean = true) { - if (moveCursor) { - this.AdjustCursorPosition(0, 1) - } - + AppendLine() { const paragraph = document.createElement('p') paragraph.style.height = `${this.cellHeight}px` paragraph.style.lineHeight = `${this.cellHeight}px` @@ -48,18 +45,20 @@ export class Terminal { paragraph.className = 'line' this.terminal.appendChild(paragraph) - this.AppendCells(content) } + /** + * @returns index of the last line on the page. -1 if there are no lines + */ GetLastLineIndex(): number { - return Math.max(this.terminal.children.length - 1, 0) + return this.terminal.children.length - 1 } Write(text: string) { let adjustment = 0 - text.split('').forEach(char => { - this.AdjustCursorPosition(adjustment, 0) + text.split('').forEach((char) => { + this.MoveCursor(adjustment, 0) adjustment = 1 this.SetCell(char) @@ -69,12 +68,12 @@ export class Terminal { SetCell(char: string) { const selector = `#line-${this.cursorPosition.row} .cell-${this.cursorPosition.col}` + // adjust for the cursor console.log(`going from ${this.terminal.children.length - 1} to ${this.cursorPosition.row}`) - if (!document.querySelector(`#line-${this.cursorPosition.row}`)) { for (let i = this.terminal.children.length - 1; i < this.cursorPosition.row; i++) { console.log(i) - this.AppendLine('', false) + this.AppendLine() } } @@ -98,19 +97,17 @@ export class Terminal { AppendCells(content: string) { const paragraph = sqs(`#line-${this.cursorPosition.row}`) - content.split('').forEach(char => { + content.split('').forEach((char) => { paragraph.textContent += char.replace(' ', '\u00A0') - this.AdjustCursorPosition(1, 0) + this.MoveCursor(1, 0) }) } - RemoveCells(amount: number) { - const line = sqs(`#line-${this.cursorPosition.row}`) - const originalAmount = this.cursorPosition.row + RemoveCell() { + try { + sqs(`#line-${this.cursorPosition.row} .cell-${this.cursorPosition.col - 1}`).remove() + } catch (_) { - for (let i = originalAmount; i < originalAmount - amount; i--) { - line.querySelector(`.cell-${i}`)?.remove() - this.AdjustCursorPosition(-amount, 0) } } @@ -148,17 +145,26 @@ export class Terminal { this.cursorPosition.col = Math.max(col, 0) this.cursorPosition.row = Math.max(row, 0) - this.UpdateCursor() + try { + sqs(`#line-${this.cursorPosition.row} .cell-${this.cursorPosition.col}`) + } catch (_) { + this.SetCell(' ') + } finally { + this.UpdateCursor() + } } - AdjustCursorPosition(col: number, row: number) { - this.SetCursorPosition(this.cursorPosition.col + col, this.cursorPosition.row + row) + MoveCursor(col: number, row: number, absolute: { x: boolean; y: boolean } = { x: false, y: false }) { + this.SetCursorPosition( + !absolute.x ? this.cursorPosition.col + col : col, + !absolute.y ? this.cursorPosition.row + row : row, + ) } SetCursorStyle(style: CursorStyle) { switch (style) { default: - this.cursor.style.width = '1px' + this.cursor.style.width = `${this.cellWidth}px` this.cursor.style.height = `${this.cellHeight}px` } }