From 86793bfdc18734b10278e3bf1c8593e465562c83 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Wed, 7 Jun 2023 20:10:51 +0200 Subject: [PATCH] terminal kill runnig apps --- src/bin/Terminal.hx | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/bin/Terminal.hx b/src/bin/Terminal.hx index 0a49471..cb578ff 100644 --- a/src/bin/Terminal.hx +++ b/src/bin/Terminal.hx @@ -1,5 +1,6 @@ package bin; +import kernel.log.Log; import kernel.binstore.BinStore; import kernel.ps.ProcessHandle; import kernel.ps.Process; @@ -15,6 +16,7 @@ class Terminal implements Process { private var backlog:Array = []; private var handle:ProcessHandle; private var requestRender: () -> Void; + private var runningPID:PID = -1; public function new() {} @@ -28,27 +30,41 @@ class Terminal implements Process { statelessContext.setRenderFunc(this.render); - this.context.onChar.handle(char -> { + handle.addCallbackLink(this.context.onChar.handle(char -> { + if (this.runningPID > 0) return; this.input += char; this.requestRender(); - }); + })); - this.context.onKey.handle(e -> { - if (e.keyCode == 259) { // Backspace - this.input = this.input.substr(0, this.input.length - 1); - this.requestRender(); - } else if (e.keyCode == 257) { // Enter - this.backlog.push("> " + this.input); - var command = this.input; - this.input = ""; - this.requestRender(); - this.invokeCommand(command); + handle.addCallbackLink(this.context.onKey.handle(e -> { + switch (e.keyCode) { + case 259: // Backspace + if (this.runningPID > 0) return; + this.input = this.input.substr(0, this.input.length - 1); + this.requestRender(); + case 257: // Enter + if (this.runningPID > 0) return; + this.backlog.push("> " + this.input); + var command = this.input; + this.input = ""; + this.requestRender(); + this.invokeCommand(command); + case 269: // END + this.stopCurrentPS(); } - }); + })); this.requestRender(); } + private function stopCurrentPS() { + if (this.runningPID < 0) { + return; + } + + ProcessManager.kill(this.runningPID); + } + private function render() { redrawBacklog(); redrawInput(); @@ -114,7 +130,7 @@ class Terminal implements Process { return; } - ProcessManager.run(ps,{ + this.runningPID = ProcessManager.run(ps,{ args: commandArgs, onWrite: (s:String) -> { if (s == "") { @@ -138,6 +154,7 @@ class Terminal implements Process { this.requestRender(); }, onExit: (success:Bool) -> { + this.runningPID = -1; if (this.backlog[this.backlog.length - 1] == "") { this.backlog.pop(); }