feat: working-ish shell and terminal
This commit is contained in:
@@ -6,6 +6,9 @@ import type { EventBroadcaster } from '../utils/EventBroadcaster'
|
||||
import { Shell } from './Shell'
|
||||
|
||||
export class Wush extends Shell {
|
||||
private buffer: string[] = []
|
||||
private bufferPos: number = 0
|
||||
|
||||
constructor(broadcaster: EventBroadcaster, terminal: Terminal) {
|
||||
super(broadcaster, terminal)
|
||||
}
|
||||
@@ -21,20 +24,63 @@ export class Wush extends Shell {
|
||||
this.terminal.Write('hi -> ')
|
||||
}
|
||||
|
||||
PushToBuffer(text: string) {
|
||||
text.split('').forEach(char => {
|
||||
this.buffer.splice(this.bufferPos, 0, char)
|
||||
this.bufferPos++
|
||||
})
|
||||
|
||||
console.log(this.buffer)
|
||||
}
|
||||
|
||||
RemoveLastCharsFromBuffer(amount: number) {
|
||||
this.buffer.splice(this.buffer.length - amount, amount)
|
||||
this.bufferPos -= amount
|
||||
|
||||
console.log(this.buffer)
|
||||
}
|
||||
|
||||
MoveBufferPos(index: number, absolute: boolean = false) {
|
||||
this.bufferPos = Math.max(absolute ? index : this.bufferPos + index, 0)
|
||||
}
|
||||
|
||||
FlushBuffer() {
|
||||
this.buffer = []
|
||||
this.bufferPos = 0
|
||||
}
|
||||
|
||||
ExecuteBuffer() {
|
||||
const args = this.buffer.join('').split(' ')
|
||||
this.FlushBuffer()
|
||||
|
||||
console.log(`Executing ${args[0]} with args '${args}'`)
|
||||
}
|
||||
|
||||
HandleKeyInput(key: string, isCharacter: boolean) {
|
||||
if (!isCharacter)
|
||||
if (!isCharacter) {
|
||||
switch (key) {
|
||||
case 'ArrowLeft':
|
||||
this.terminal.MoveCursor(-1, 0)
|
||||
this.MoveBufferPos(-1)
|
||||
break
|
||||
case 'ArrowRight':
|
||||
this.terminal.MoveCursor(1, 0)
|
||||
this.MoveBufferPos(1)
|
||||
break
|
||||
case 'Backspace':
|
||||
this.terminal.RemoveCell()
|
||||
this.RemoveLastCharsFromBuffer(1)
|
||||
this.terminal.MoveCursor(-1, 0)
|
||||
break
|
||||
case 'Enter':
|
||||
this.terminal.MoveCursor(0, 4, { x: true, y: false })
|
||||
this.terminal.MoveCursor(0, 1, { x: true, y: false })
|
||||
this.ExecuteBuffer()
|
||||
this.Prompt()
|
||||
break
|
||||
}
|
||||
else {
|
||||
this.terminal.Write(key)
|
||||
} else {
|
||||
this.terminal.InsertCell(key)
|
||||
this.PushToBuffer(key)
|
||||
this.terminal.MoveCursor(1, 0)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user