sync: umm progress
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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`
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user