Skip to content

Commit

Permalink
multiline fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-v2 committed May 29, 2024
1 parent 9b8a62e commit bf8d643
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mterm",
"version": "1.0.32-beta",
"version": "1.0.33-beta",
"description": "An Electron application with React and TypeScript",
"main": "./out/main/index.js",
"author": "mterm.io",
Expand All @@ -14,7 +14,7 @@
"typecheck:web": "tsc --noEmit -p tsconfig.web.json --composite false",
"typecheck": "npm run typecheck:node && npm run typecheck:web",
"start": "electron-vite preview",
"dev": "electron-vite dev",
"dev": "electron-vite dev -w",
"build": "npm run typecheck && electron-vite build",
"postinstall": "electron-builder install-app-deps",
"build:unpack": "npm run build && electron-builder --dir",
Expand Down Expand Up @@ -76,5 +76,5 @@
"typescript": "^5.4.5",
"vite": "^5.0.12"
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
"packageManager": "[email protected]+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
}
61 changes: 44 additions & 17 deletions src/renderer/src/runner/runner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export default function Runner(): ReactElement {
list: []
})

setMultiLineArgs('')
setIsMultiLine(false)

await reloadRuntimesFromBackend()

// renderer -> "backend"
Expand Down Expand Up @@ -167,7 +170,7 @@ export default function Runner(): ReactElement {
runtime?.prompt.slice(cursorPosition, runtime.prompt.length) +
(multiLineArgs ? '\n' + multiLineArgs : '')
)
if (runtime) runtime.prompt = runtime.prompt.slice(0, cursorPosition)
if (runtime) setPrompt(runtime.prompt.slice(0, cursorPosition))
textAreaRef.current?.focus()
}
}
Expand All @@ -176,7 +179,7 @@ export default function Runner(): ReactElement {
if (e.key === 'Enter' && e.shiftKey) {
handleLineBreak(e)
}
if (e.code === 'Backslash' || e.code === 'Backquote') {
if ((e.code === 'Backslash' || e.code === 'Backquote') && !e.shiftKey) {
e.preventDefault()
setIsMultiLine(!isMultiLine)
}
Expand All @@ -185,7 +188,7 @@ export default function Runner(): ReactElement {
e.preventDefault()
const multiLineSplit = multiLineArgs.trim().split('\n')
const _firstLine = runtime?.prompt.trim()
if (runtime) runtime.prompt = _firstLine + (_firstLine ? ' ' : '') + multiLineSplit.shift()
setPrompt(_firstLine + (_firstLine ? ' ' : '') + multiLineSplit.shift())
setMultiLineArgs(multiLineSplit.join('\n'))

if (multiLineSplit.length < 1) {
Expand All @@ -197,21 +200,33 @@ export default function Runner(): ReactElement {
}
if (e.key === 'Enter' && !e.shiftKey) {
if (!runtime) return
runtime.prompt =
runtime.prompt.trim() +
(isMultiLine && multiLineArgs ? '\n' + multiLineArgs : multiLineArgs)
execute(runtime).catch((error) => console.error(error))
setMultiLineArgs('')
setIsMultiLine(false)
execute({
...runtime,
prompt:
runtime.prompt.trim() +
(isMultiLine && multiLineArgs ? '\n' + multiLineArgs : multiLineArgs)
}).catch((error) => console.error(error))
}
if (e.code === 'ArrowDown') {
if (historyIndex === -1) {
if (suggestionSelection < suggestion.list.length - 1) {
setSuggestionSelection(suggestionSelection + 1)
} else if (isMultiLine && !e.ctrlKey && e.target === inputRef.current) {
const linebreakIndex = multiLineArgs.indexOf('\n')
const cursorPosition = e.target?.selectionStart
window.electron.ipcRenderer.invoke('runtime.complete', runtime?.prompt, 0).then(() => {
textAreaRef.current?.focus()
cursorPosition < linebreakIndex || linebreakIndex === -1
? textAreaRef.current?.setSelectionRange(cursorPosition, cursorPosition)
: textAreaRef.current?.setSelectionRange(linebreakIndex, linebreakIndex)
})
}
} else if (isMultiLine && !e.ctrlKey) {
textAreaRef.current?.focus()
} else if (runtime && historyIndex > -1) {
if (historyIndex === 0) {
setPrompt('')
setIsMultiLine(false)
setMultiLineArgs('')
}
applyHistoryIndex(historyIndex - 1)
}
}
Expand Down Expand Up @@ -250,12 +265,24 @@ export default function Runner(): ReactElement {
}
}

if (e.code === 'Escape') {
//exit suggestions
setSuggestion({
list: []
})
}

if (e.code === 'ArrowUp') {
if (suggestionSelection > 0) {
setSuggestionSelection(suggestionSelection - 1)
} else if (isMultiLine && !e.ctrlKey) {
if (e.target?.selectionStart === 0) {
inputRef.current?.focus()
const linebreakIndex = multiLineArgs.indexOf('\n')
const cursorPosition = e.target?.selectionStart
if (linebreakIndex < 0 || cursorPosition <= linebreakIndex) {
window.electron.ipcRenderer.invoke('runtime.complete', runtime?.prompt, 0).then(() => {
inputRef.current?.focus()
inputRef.current?.setSelectionRange(cursorPosition, cursorPosition)
})
}
} else if (runtime && historyIndex < runtime.history.length - 1) {
applyHistoryIndex(historyIndex + 1)
Expand Down Expand Up @@ -407,7 +434,7 @@ export default function Runner(): ReactElement {
return
}

if (runtime) runtime.prompt = runtime.prompt.trim() + (multiLineArgs ? ' ' : '') + multiLineArgs
if (runtime) setPrompt(runtime.prompt.trim() + (multiLineArgs ? ' ' : '') + multiLineArgs)
setMultiLineArgs('')
inputRef.current?.focus()
}, [isMultiLine])
Expand All @@ -421,16 +448,16 @@ export default function Runner(): ReactElement {
if (!splitPoint || splitPoint === -1) {
setIsMultiLine(false)
setMultiLineArgs('')
if (runtime) runtime.prompt = historicalExecution.prompt
setPrompt(historicalExecution.prompt)
return
}

setIsMultiLine(true)
if (runtime) runtime.prompt = historicalExecution.prompt.substring(0, splitPoint)
setPrompt(historicalExecution.prompt.substring(0, splitPoint))
setMultiLineArgs(
historicalExecution.prompt.substring(splitPoint + 1, historicalExecution.prompt.length)
)
}, [historicalExecution?.prompt])
}, [historicalExecution])

if (!runtime) {
return <p>Loading</p>
Expand Down

0 comments on commit bf8d643

Please sign in to comment.