From e03aa97716d4b2a7f2d3b185625d9d93eec62441 Mon Sep 17 00:00:00 2001 From: binekrasik Date: Tue, 24 Mar 2026 23:49:51 +0100 Subject: [PATCH] sync: wip terminal cell system --- src/shell/Wush.ts | 3 -- src/terminal/Terminal.ts | 75 ++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/shell/Wush.ts b/src/shell/Wush.ts index ba35f1f..3b507fa 100644 --- a/src/shell/Wush.ts +++ b/src/shell/Wush.ts @@ -22,9 +22,6 @@ export class Wush extends Shell { } HandleKeyInput(key: string, isCharacter: boolean) { - console.log(key) - console.log(isCharacter) - if (!isCharacter) switch (key) { case 'Backspace': diff --git a/src/terminal/Terminal.ts b/src/terminal/Terminal.ts index f7b2874..f6f6d3c 100644 --- a/src/terminal/Terminal.ts +++ b/src/terminal/Terminal.ts @@ -36,8 +36,10 @@ export class Terminal { this.SetCursorPosition(0, 0) } - AppendLine(content: string) { - this.AdjustCursorPosition(0, 1) + AppendLine(content: string, moveCursor: boolean = true) { + if (moveCursor) { + this.AdjustCursorPosition(0, 1) + } const paragraph = document.createElement('p') paragraph.style.height = `${this.cellHeight}px` @@ -49,12 +51,14 @@ export class Terminal { this.AppendCells(content) } + GetLastLineIndex(): number { + return Math.max(this.terminal.children.length - 1, 0) + } + Write(text: string) { let adjustment = 0 text.split('').forEach(char => { - console.log(this.cursorPosition) - this.AdjustCursorPosition(adjustment, 0) adjustment = 1 @@ -63,28 +67,32 @@ export class Terminal { } SetCell(char: string) { - const id = `#cell-${this.cursorPosition.row}_${this.cursorPosition.col}` + const selector = `#line-${this.cursorPosition.row} .cell-${this.cursorPosition.col}` - try { - sqs(id) - } catch (_) { - const cell = document.createElement('span') - cell.id = id - cell.style.width = `${this.cellWidth}px` - cell.style.height = `${this.cellHeight}px` - cell.style.lineHeight = `${this.cellHeight}px` + console.log(`going from ${this.terminal.children.length - 1} to ${this.cursorPosition.row}`) - const lineId = `#line-${this.cursorPosition.row}` - - try { - sqs(lineId) - } catch (_) { - // todo: create as many lines as we need - for (let i = sqs('#terminal'); ;) {} + 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) } - } finally { - sqs(id).innerText = char[0] } + + if (!document.querySelector(selector)) { + const line = sqs(`#line-${this.cursorPosition.row}`) + + for (let i = line.children.length; i < this.cursorPosition.col + 1; i++) { + const cell = document.createElement('span') + cell.className = `cell-${i}` + cell.style.width = `${this.cellWidth}px` + cell.style.height = `${this.cellHeight}px` + cell.style.lineHeight = `${this.cellHeight}px` + + line.appendChild(cell) + } + } + + sqs(selector).innerText = char[0] } AppendCells(content: string) { @@ -97,13 +105,13 @@ export class Terminal { } RemoveCells(amount: number) { - const paragraph = sqs(`#line-${this.cursorPosition.row}`) - paragraph.innerText = paragraph.innerText.slice( - 0, - Math.max(paragraph.innerText.length - amount, 0), - ) + const line = sqs(`#line-${this.cursorPosition.row}`) + const originalAmount = this.cursorPosition.row - this.AdjustCursorPosition(-amount, 0) + for (let i = originalAmount; i < originalAmount - amount; i--) { + line.querySelector(`.cell-${i}`)?.remove() + this.AdjustCursorPosition(-amount, 0) + } } ResetCellSize() { @@ -137,15 +145,8 @@ export class Terminal { } SetCursorPosition(col: number, row: number) { - this.cursorPosition.col = Math.min( - this.GetWidthCells(), - Math.max(col, 0), - ) - - this.cursorPosition.row = Math.min( - this.GetHeightCells(), - Math.max(row, 0), - ) + this.cursorPosition.col = Math.max(col, 0) + this.cursorPosition.row = Math.max(row, 0) this.UpdateCursor() }