Skip to content

Commit

Permalink
feat: more stable cli input, history is still bugged
Browse files Browse the repository at this point in the history
  • Loading branch information
goldbuick committed May 22, 2024
1 parent c8af562 commit ee1f8e5
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 39 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = {
'plugin:react/recommended',
'plugin:react-hooks/recommended',
// 'plugin:@react-three/recommended',
'plugin:valtio/recommended',
'plugin:prettier/recommended',
// Enables eslint-plugin-prettier and eslint-config-prettier.
// This will display prettier errors as ESLint errors.
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v18
v20
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"eslint-plugin-react": "^7.34.1",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"eslint-plugin-valtio": "^0.6.3",
"fast-json-patch": "^3.1.1",
"is-hotkey": "^0.2.0",
"jest": "^29.7.0",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2736,6 +2736,11 @@ eslint-plugin-react@^7.34.1:
semver "^6.3.1"
string.prototype.matchall "^4.0.10"

eslint-plugin-valtio@^0.6.3:
version "0.6.3"
resolved "https://registry.npmjs.org/eslint-plugin-valtio/-/eslint-plugin-valtio-0.6.3.tgz#23b2855d682d225fc614fde4f619c23a47839f5e"
integrity sha512-s1Ttxcb70pzW5NU28DQ+bUy84ohhQApzuP3ZZeZF7y3nQJlCjh3KRceXvxhf9p+1fTBXcl3psLjNVchk8NetAw==

eslint-scope@^7.2.2:
version "7.2.2"
resolved "https://registry.npmjs.org/eslint-scope/-/eslint-scope-7.2.2.tgz#deb4f92563390f32006894af62a22dba1c46423f"
Expand Down
74 changes: 49 additions & 25 deletions zss/gadget/components/tape.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,23 @@ export function TapeConsole() {
// input & selection
const visiblerange = width - 3
const inputindex = (height - 1) * width + 1
const hasselection = ispresent(selection)
const ii1 = hasselection ? Math.min(selection, cursor) : cursor
const ii2 = hasselection ? Math.max(selection, cursor) : cursor
const iic = ii2 - ii1

const inputstate = inputbuffer[inputbufferindex]

let ii1 = cursor
let ii2 = cursor
let hasselection = false
if (ispresent(selection)) {
ii1 = Math.min(cursor, selection)
ii2 = Math.max(cursor, selection)
if (cursor !== selection) {
--ii2
hasselection = true
}
}

const iic = ii2 - ii1 + 1
const inputstateselected = hasselection
? inputstate.substring(ii1, ii2 + 1)
? inputstate.substring(ii1, ii2)
: inputstate

// draw input line
Expand All @@ -130,7 +139,9 @@ export function TapeConsole() {

// draw selection
if (hasselection) {
applycolortoindexes(inputindex + ii1, inputindex + ii2, 15, 8, context)
const p1 = inputindex + ii1
const p2 = inputindex + ii2
applycolortoindexes(p1, p2, 15, 8, context)
}

// draw cursor
Expand All @@ -146,6 +157,7 @@ export function TapeConsole() {
count,
insert,
)
return inputbuffer[inputbufferindex]
}

function trackselection(index: number | undefined) {
Expand All @@ -161,7 +173,7 @@ export function TapeConsole() {
function deleteselection() {
setcursor(ii1)
setselection(undefined)
inputstatesetsplice(ii1, iic)
return inputstatesetsplice(ii1, iic)
}

return (
Expand All @@ -176,14 +188,21 @@ export function TapeConsole() {
<UserInput
MENU_BUTTON={(mods) => tapesetmode(mods.shift ? -1 : 1)}
MOVE_UP={() => {
setinputbufferindex(
clamp(inputbufferindex + 1, 0, inputbuffer.length - 1),
)
const ir = inputbuffer.length - 1
const index = clamp(inputbufferindex + 1, 0, ir)
setselection(undefined)
setinputbufferindex(index)
setcursor((inputbuffer[index] ?? '').length)
}}
MOVE_DOWN={() => {
setinputbufferindex(
clamp(inputbufferindex - 1, 0, inputbuffer.length - 1),
)
// TODO: we should actually be copying history items into an active buffer on edit ?
// so instead of overwriting the historyical entry
// we copy to our mutable entry before continuting
const ir = inputbuffer.length - 1
const index = clamp(inputbufferindex - 1, 0, ir)
setselection(undefined)
setinputbufferindex(index)
setcursor((inputbuffer[index] ?? '').length)
}}
MOVE_LEFT={(mods) => {
trackselection(mods.shift ? cursor : undefined)
Expand All @@ -195,12 +214,17 @@ export function TapeConsole() {
}}
OK_BUTTON={() => {
const invoke = hasselection ? inputstateselected : inputstate
setinputbuffer([invoke, ...inputbuffer])
setinputbufferindex(0)
setcursor(0)
setselection(undefined)
setinputbuffer(['', ...inputbuffer])
vm_cli('tape', invoke, gadgetstategetplayer())
if (invoke.length) {
setinputbuffer([
'',
invoke,
...inputbuffer.slice(1).filter((item) => item !== invoke),
])
setinputbufferindex(0)
setcursor(0)
setselection(undefined)
vm_cli('tape', invoke, gadgetstategetplayer())
}
}}
keydown={(event) => {
const { key } = event
Expand Down Expand Up @@ -249,7 +273,7 @@ export function TapeConsole() {
if (hasselection) {
inputstatesetsplice(ii1, iic, text)
setselection(undefined)
setcursor(ii2)
setcursor(ii1 + text.length)
} else {
inputstatesetsplice(cursor, 0, text)
setcursor(cursor + text.length)
Expand All @@ -269,18 +293,18 @@ export function TapeConsole() {
}
} else if (mods.alt) {
// no-op ?? - could this shove text around when you have selection ??
// or jump by 10 ?
// or jump by 10 or by word ??
} else if (
key.length === 1 &&
inputstate.length < visiblerange
) {
if (hasselection) {
setcursor(ii2)
inputstatesetsplice(ii1, iic, key)
setselection(undefined)
inputstatesetsplice(ii1, ii2, key)
setcursor(ii1 + 1)
} else {
inputstatesetsplice(cursor, 0, key)
setcursor(cursor + 1)
inputstatesetsplice(cursor, cursor, key)
}
}
break
Expand Down
6 changes: 3 additions & 3 deletions zss/lang/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function addRange(node: CodeNode | undefined): OffsetRange | undefined {
}

const offsets: (OffsetRange | undefined)[] = [
{ start: node.startOffset, end: node.endOffset || 0 },
{ start: node.startOffset, end: node.endOffset ?? 0 },
]

Object.keys(node).forEach((name) => {
Expand All @@ -33,10 +33,10 @@ function addRange(node: CodeNode | undefined): OffsetRange | undefined {

node.range = {
start: Math.min(
...offsets.filter((item) => item).map((item) => item?.start || 0),
...offsets.filter((item) => item).map((item) => item?.start ?? 0),
),
end: Math.max(
...offsets.filter((item) => item).map((item) => item?.end || 0),
...offsets.filter((item) => item).map((item) => item?.end ?? 0),
),
}

Expand Down
4 changes: 2 additions & 2 deletions zss/lang/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export function write(
chunks: (string | SourceNode)[] | SourceNode | string,
) {
return new SourceNode(
ast.startLine || 1,
ast.startColumn || 1,
ast.startLine ?? 1,
ast.startColumn ?? 1,
GENERATED_FILENAME,
chunks,
)
Expand Down
14 changes: 7 additions & 7 deletions zss/lang/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function asIToken(thing: CstNode | CstElement): IToken {

function asList(thing: ScriptVisitor, node: CstNode[] | undefined): CodeNode[] {
return (
node?.map((item) => thing.visit(item)).filter((item) => item) || []
node?.map((item) => thing.visit(item)).filter((item) => item) ?? []
).flat()
}

Expand Down Expand Up @@ -257,12 +257,12 @@ function getLocation(obj: CstChildrenDictionary): CstNodeLocation {
})

return {
startLine: Math.min(...locations.map((item) => item.startLine || 1)),
startColumn: Math.min(...locations.map((item) => item.startColumn || 1)),
startOffset: Math.min(...locations.map((item) => item.startOffset || 1)),
endLine: Math.max(...locations.map((item) => item.endLine || 1)),
endColumn: Math.max(...locations.map((item) => item.endColumn || 1)),
endOffset: Math.max(...locations.map((item) => item.endOffset || 1)),
startLine: Math.min(...locations.map((item) => item.startLine ?? 1)),
startColumn: Math.min(...locations.map((item) => item.startColumn ?? 1)),
startOffset: Math.min(...locations.map((item) => item.startOffset ?? 1)),
endLine: Math.max(...locations.map((item) => item.endLine ?? 1)),
endColumn: Math.max(...locations.map((item) => item.endColumn ?? 1)),
endOffset: Math.max(...locations.map((item) => item.endOffset ?? 1)),
}
}

Expand Down
2 changes: 2 additions & 0 deletions zss/memory/atomics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ export function listelementsbykind(
// console.info('no match on name', name)
return false
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
if (ispresent(color) && boardelementcolor(element) !== color) {
// console.info('no match on color', color)
return false
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
if (ispresent(bg) && boardelementbg(element) !== bg) {
// console.info('no match on bg', bg)
return false
Expand Down
2 changes: 1 addition & 1 deletion zss/terminal/crt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor)
float phase = time + cos(uv.x + uv.y);
float slowband = (cos(uv.x + uv.y - phase) + 1.0) / 2.0;
float fastband = (cos(uv.y + time * 0.37) + 1.0) / 2.0;
float blankdmix = 0.5 -
float blankdmix = 0.3 -
pow(slowband, viewheight * 0.01) * 0.05 -
pow(fastband, viewheight * 64.0) * 0.2 -
pow(fastband, viewheight * 128.0) * 0.1;
Expand Down

0 comments on commit ee1f8e5

Please sign in to comment.