Skip to content

Commit

Permalink
fix: control+c to kill command selected
Browse files Browse the repository at this point in the history
resolves #22
  • Loading branch information
daretodave committed Apr 29, 2024
1 parent 5e6f34e commit fc8c1c2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/main/framework/runtime-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ export function attach({ app, workspace }: BootstrapContext): void {
)
})

ipcMain.handle('runtime.kill', async (_, commandId, runtimeId): Promise<boolean> => {
const runtime = workspace.runtimes.find((r) => r.id === runtimeId)
if (!runtime) {
return false
}
const command = runtime.history.find((c) => c.id === commandId)
if (!command) {
return false
}

if (command.process) {
try {
command.process.kill(0)
} catch (e) {
console.error(e)
}
}
if (!command.complete) {
command.aborted = true
}
command.complete = true

return true
})

ipcMain.on('open.workspace', async () => {
await shell.openPath(workspace.folder)
})
Expand Down Expand Up @@ -123,6 +148,7 @@ export function attach({ app, workspace }: BootstrapContext): void {
prompt,
error: false,
complete: false,
aborted: false,
result: {
code: 0,
stream: []
Expand Down Expand Up @@ -163,6 +189,10 @@ export function attach({ app, workspace }: BootstrapContext): void {

try {
const out = (text: string, error: boolean = false): void => {
if (command.aborted || command.complete) {
return
}

const raw = text.toString()

text = DOMPurify.sanitize(raw)
Expand All @@ -187,6 +217,10 @@ export function attach({ app, workspace }: BootstrapContext): void {
}
}
const finish = (code: number): void => {
if (command.aborted || command.complete) {
return
}

result.code = code

command.complete = true
Expand Down
2 changes: 2 additions & 0 deletions src/main/framework/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface Command {
result: Result
runtime: string
complete: boolean
aborted: boolean
error: boolean
id: string
process?: ChildProcessWithoutNullStreams
Expand All @@ -32,6 +33,7 @@ export interface CommandViewModel {
prompt: string
result: Result
runtime: string
aborted: boolean
complete: boolean
error: boolean
id: string
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/src/assets/runner.css
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ body {
.runner-history-complete {
border-left: 5px solid #23682d;
}
.runner-history-aborted {
border-left: 5px solid #ac1298;
}
.runner-history-error {
border-left: 5px solid #ff6767;
}
Expand Down
22 changes: 21 additions & 1 deletion src/renderer/src/runner/runner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ export default function Runner(): ReactElement {

await reloadRuntimesFromBackend()
}

const kill = async (): Promise<void> => {
if (!historicalExecution) {
return
}
const runtime = historicalExecution.runtime
const id = historicalExecution.id

await window.electron.ipcRenderer.invoke('runtime.kill', id, runtime)

await reloadRuntimesFromBackend()
}
const handlePromptChange = (event: ChangeEvent<HTMLInputElement>): void => {
const value = event.target.value
console.log(event)
Expand Down Expand Up @@ -107,6 +119,10 @@ export default function Runner(): ReactElement {
applyHistoryIndex(historyIndex + 1)
}
}

if (e.code === 'KeyC' && e.ctrlKey) {
kill().catch((error) => console.error(error))
}
}

useEffect(() => {
Expand Down Expand Up @@ -174,7 +190,11 @@ export default function Runner(): ReactElement {
<div
key={index}
className={`runner-history-item ${historyIndex === index ? 'runner-history-selected' : ''} ${
command.complete ? 'runner-history-complete' : 'runner-history-running'
command.complete
? command.aborted
? 'runner-history-aborted'
: 'runner-history-complete'
: 'runner-history-running'
} ${command.error ? 'runner-history-error' : ''}`}
onClick={() => onHistoryItemClicked(index)}
>
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/runner/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Command {
prompt: string
result: Result
complete: boolean
aborted: boolean
runtime: string
error: boolean
id: string
Expand Down

0 comments on commit fc8c1c2

Please sign in to comment.