sync: sync wip changes

This commit is contained in:
binekrasik
2026-05-20 13:10:34 +02:00
parent b5089251ff
commit abe52acaa7
6 changed files with 389 additions and 112 deletions

View File

@@ -25,11 +25,10 @@ import { Mkdir } from '../../program/Mkdir'
import { Cd } from '../../program/Cd'
import { Printf } from '../../program/Printf'
import { Pwd } from '../../program/Pwd'
// import { Pwd } from '../program/Pwd'
// import { Cd } from '../program/Cd'
import { ShellEnvironment } from './ShellEnvironment'
export class Wush extends Shell {
public readonly Version = "0.1.0"
public readonly Version = "0.3.1"
public readonly Name = "wush"
// buffer
@@ -47,8 +46,7 @@ export class Wush extends Shell {
*/
private execExitCode: number = 0
// todo: workers
// private workersAllowed: boolean = false
readonly environment: ShellEnvironment = new ShellEnvironment()
// streams
readonly stdin: SimpleStream<string>
@@ -66,6 +64,7 @@ export class Wush extends Shell {
}
async Init() {
// initialize keyboard listener
this.broadcaster.on('keydown', (key: string, isCharacter: boolean) =>
this.HandleKeyInput(key, isCharacter),
)
@@ -73,6 +72,9 @@ export class Wush extends Shell {
// load workdir
this.workingDirectory = await Item.Root()
// register streams
this.stdout.on(data => this.WriteEscapedString(data))
// load core programs
this.programs['clear'] = new Clear()
this.programs['eval'] = new Eval()
@@ -92,7 +94,7 @@ export class Wush extends Shell {
this.programs['cd'] = new Cd(this)
this.programs['printf'] = new Printf()
this.stdout.on(data => this.WriteEscapedString(data))
// initial prompt
this.Prompt()
}
@@ -158,9 +160,9 @@ export class Wush extends Shell {
}
WriteEscapedString(data: string) {
let i = 0
let buffer = ''
// write the buffer to screen
const flush = () => {
if (buffer.length === 0)
return
@@ -169,11 +171,15 @@ export class Wush extends Shell {
buffer = ''
}
// process the string
let i = 0
while (i < data.length) {
const char = data[i]
// check for escape sequences
if (char === '\x1b') {
flush()
const nextIndex = this.ProcessEscapeSequence(data, i)
if (nextIndex !== null) {
i = nextIndex
@@ -181,10 +187,13 @@ export class Wush extends Shell {
}
}
// check for control codes
if (char === '\f' || char === '\n') {
flush()
this.ProcessSimpleControlCode(char)
this.ProcessControlCode(char)
i++
continue
}
@@ -200,15 +209,11 @@ export class Wush extends Shell {
this.buffer.splice(this.bufferPos, 0, char)
this.bufferPos++
})
// console.log(this.buffer)
}
RemoveCharFromBuffer(amount: number, index: number) {
this.buffer.splice(index - amount, amount)
this.bufferPos -= amount
// console.log(this.buffer)
}
MoveBufferPos(index: number, absolute: boolean = false) {
@@ -220,7 +225,8 @@ export class Wush extends Shell {
this.bufferPos = 0
}
async ExecuteBuffer() {
// takes the prompt buffer, parses it and executes the contents
async ExecuteLineBuffer(data?: string) {
type OperatorToken = '&&' | '||' | '|' | ';'
type Token = { type: 'word'; value: string } | { type: 'operator'; value: OperatorToken }
type ParsedCommand = {
@@ -231,23 +237,32 @@ export class Wush extends Shell {
type ParsedPipeline = { commands: ParsedCommand[] }
type ParsedListItem = { pipeline: ParsedPipeline; operator: '&&' | '||' | ';' | null }
// splits the buffer into processable pieces (tokens)
const tokenize = (text: string): { tokens: Token[]; error: string | null } => {
const tokens: Token[] = []
// state machine stuff
let current = ''
let wordStarted = false
let inSingleQuote = false
let inDoubleQuote = false
let escapeNext = false
/**
* adds a new word to the token buffer
*/
const pushWord = () => {
if (!wordStarted)
return
tokens.push({ type: 'word', value: current })
// reset the state
current = ''
wordStarted = false
}
// iterates over the provided text buffer
for (let i = 0; i < text.length; i++) {
const char = text[i]
@@ -258,6 +273,7 @@ export class Wush extends Shell {
continue
}
// process strict (single quote) strings
if (inSingleQuote) {
if (char === "'") {
inSingleQuote = false
@@ -270,6 +286,7 @@ export class Wush extends Shell {
continue
}
// process classic (double quote) strings
if (inDoubleQuote) {
if (char === '"') {
inDoubleQuote = false
@@ -289,18 +306,23 @@ export class Wush extends Shell {
continue
}
// state checks
// escapes
if (char === '\\') {
escapeNext = true
wordStarted = true
continue
}
// single quote strings
if (char === "'") {
inSingleQuote = true
wordStarted = true
continue
}
// double quote strings
if (char === '"') {
inDoubleQuote = true
wordStarted = true
@@ -365,6 +387,7 @@ export class Wush extends Shell {
return { tokens, error: null }
}
// parse the tokens into a list of parsed commands and pipelines
const parseTokens = (tokens: Token[]): { list: ParsedListItem[]; error: string | null } => {
const list: ParsedListItem[] = []
let currentArgs: string[] = []
@@ -426,8 +449,18 @@ export class Wush extends Shell {
return { list, error: null }
}
const line = this.buffer.join('')
this.FlushBuffer()
// process the buffer
let line: string
if (data) {
// data overrides the buffer
this.buffer = data.split('')
this.bufferPos = 0
line = data
} else {
// use shell buffer if no arbitrary string was provided
line = this.buffer.join('')
this.FlushBuffer()
}
const { tokens, error } = tokenize(line)
if (error) {
@@ -483,7 +516,7 @@ export class Wush extends Shell {
this.Prompt()
}
ProcessSimpleControlCode(code: string): boolean {
ProcessControlCode(code: string): boolean {
switch (code) {
case '\f':
this.terminal.NewPage()
@@ -542,79 +575,6 @@ export class Wush extends Shell {
}
}
HandleKeyInput(key: string, isCharacter: boolean) {
if (this.execExitCode === -1) console.log('program running')
if (!isCharacter) {
switch (key) {
case 'ArrowLeft':
if (this.bufferPos > 0) {
this.bufferPos -= 1
this.SyncCursorToBuffer()
}
break
case 'ArrowRight':
if (this.bufferPos < this.buffer.length) {
this.bufferPos += 1
this.SyncCursorToBuffer()
}
break
case 'ArrowUp':
this.NavigateHistory(-1)
break
case 'ArrowDown':
this.NavigateHistory(1)
break
case 'Backspace':
// don't erase anything if there's nothing left in the buffer
if (this.bufferPos === 0)
break
this.ExitHistoryNavigation()
this.terminal.RemoveCell()
this.RemoveCharFromBuffer(1, this.bufferPos)
this.SyncCursorToBuffer()
break
case 'Delete':
if (this.bufferPos >= this.buffer.length || this.buffer.length <= 0)
break
this.ExitHistoryNavigation()
this.bufferPos += 1
this.SyncCursorToBuffer()
this.terminal.RemoveCell()
this.RemoveCharFromBuffer(1, this.bufferPos)
this.SyncCursorToBuffer()
break
case 'Enter':
// send the buffer to stdin if an exec is running
if (this.execExitCode === -1) {
this.WriteStdin(`${this.buffer.join('')}\n`)
this.FlushBuffer()
} else {
// "execute" the buffer
this.terminal.MoveCursor(0, 1, { x: true, y: false })
const command = this.buffer.join('')
if (command.length > 0)
this.history.push(command)
this.historyIndex = null
this.historyDraft = ''
this.ExecuteBuffer()
}
break
case 'F5':
location.reload()
break
}
} else {
this.ExitHistoryNavigation()
// push the character into the buffer
this.InsertText(key)
}
}
private GetWrapWidth(): number {
const width = Math.floor(this.terminal.GetWidthCells())
if (!Number.isFinite(width) || width <= 0)