sync: umm progress

This commit is contained in:
2026-03-25 23:07:44 +01:00
parent e03aa97716
commit 141204164a
2 changed files with 33 additions and 25 deletions

View File

@@ -25,15 +25,17 @@ export class Wush extends Shell {
if (!isCharacter) if (!isCharacter)
switch (key) { switch (key) {
case 'Backspace': case 'Backspace':
this.terminal.RemoveCells(1) this.terminal.RemoveCell()
this.terminal.MoveCursor(-1, 0)
break break
case 'Enter': case 'Enter':
this.terminal.MoveCursor(0, 4, { x: true, y: false })
this.Prompt() this.Prompt()
break break
} }
else { else {
this.terminal.Write(key) this.terminal.Write(key)
this.terminal.AdjustCursorPosition(1, 0) this.terminal.MoveCursor(1, 0)
} }
} }
} }

View File

@@ -1,6 +1,7 @@
import type { Shell } from '../shell/Shell' import type { Shell } from '../shell/Shell'
import type { EventBroadcaster } from '../utils/EventBroadcaster' import type { EventBroadcaster } from '../utils/EventBroadcaster'
import sqs from '../utils/sqs' import sqs from '../utils/sqs'
import type { CellContent } from './CellContent'
import type { CursorPosition, CursorStyle } from './CursorProperties' import type { CursorPosition, CursorStyle } from './CursorProperties'
export class Terminal { export class Terminal {
@@ -36,11 +37,7 @@ export class Terminal {
this.SetCursorPosition(0, 0) this.SetCursorPosition(0, 0)
} }
AppendLine(content: string, moveCursor: boolean = true) { AppendLine() {
if (moveCursor) {
this.AdjustCursorPosition(0, 1)
}
const paragraph = document.createElement('p') const paragraph = document.createElement('p')
paragraph.style.height = `${this.cellHeight}px` paragraph.style.height = `${this.cellHeight}px`
paragraph.style.lineHeight = `${this.cellHeight}px` paragraph.style.lineHeight = `${this.cellHeight}px`
@@ -48,18 +45,20 @@ export class Terminal {
paragraph.className = 'line' paragraph.className = 'line'
this.terminal.appendChild(paragraph) this.terminal.appendChild(paragraph)
this.AppendCells(content)
} }
/**
* @returns index of the last line on the page. -1 if there are no lines
*/
GetLastLineIndex(): number { GetLastLineIndex(): number {
return Math.max(this.terminal.children.length - 1, 0) return this.terminal.children.length - 1
} }
Write(text: string) { Write(text: string) {
let adjustment = 0 let adjustment = 0
text.split('').forEach(char => { text.split('').forEach((char) => {
this.AdjustCursorPosition(adjustment, 0) this.MoveCursor(adjustment, 0)
adjustment = 1 adjustment = 1
this.SetCell(char) this.SetCell(char)
@@ -69,12 +68,12 @@ export class Terminal {
SetCell(char: string) { SetCell(char: string) {
const selector = `#line-${this.cursorPosition.row} .cell-${this.cursorPosition.col}` 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}`) console.log(`going from ${this.terminal.children.length - 1} to ${this.cursorPosition.row}`)
if (!document.querySelector(`#line-${this.cursorPosition.row}`)) { if (!document.querySelector(`#line-${this.cursorPosition.row}`)) {
for (let i = this.terminal.children.length - 1; i < this.cursorPosition.row; i++) { for (let i = this.terminal.children.length - 1; i < this.cursorPosition.row; i++) {
console.log(i) console.log(i)
this.AppendLine('', false) this.AppendLine()
} }
} }
@@ -98,19 +97,17 @@ export class Terminal {
AppendCells(content: string) { AppendCells(content: string) {
const paragraph = sqs(`#line-${this.cursorPosition.row}`) const paragraph = sqs(`#line-${this.cursorPosition.row}`)
content.split('').forEach(char => { content.split('').forEach((char) => {
paragraph.textContent += char.replace(' ', '\u00A0') paragraph.textContent += char.replace(' ', '\u00A0')
this.AdjustCursorPosition(1, 0) this.MoveCursor(1, 0)
}) })
} }
RemoveCells(amount: number) { RemoveCell() {
const line = sqs(`#line-${this.cursorPosition.row}`) try {
const originalAmount = this.cursorPosition.row 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.col = Math.max(col, 0)
this.cursorPosition.row = Math.max(row, 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) { MoveCursor(col: number, row: number, absolute: { x: boolean; y: boolean } = { x: false, y: false }) {
this.SetCursorPosition(this.cursorPosition.col + col, this.cursorPosition.row + row) this.SetCursorPosition(
!absolute.x ? this.cursorPosition.col + col : col,
!absolute.y ? this.cursorPosition.row + row : row,
)
} }
SetCursorStyle(style: CursorStyle) { SetCursorStyle(style: CursorStyle) {
switch (style) { switch (style) {
default: default:
this.cursor.style.width = '1px' this.cursor.style.width = `${this.cellWidth}px`
this.cursor.style.height = `${this.cellHeight}px` this.cursor.style.height = `${this.cellHeight}px`
} }
} }