convert Terminal to Process

This commit is contained in:
Djeeberjr 2023-05-22 22:25:37 +02:00
parent 8dca828cf3
commit ec89107262

View File

@ -1,52 +1,56 @@
package bin; package bin;
import kernel.ps.ProcessHandle;
import kernel.ps.Process; import kernel.ps.Process;
import kernel.ps.ProcessManager; import kernel.ps.ProcessManager;
import kernel.ps.ProcessHandle;
import lib.ui.UIApp;
import lib.Color; import lib.Color;
import kernel.ui.WindowContext; import kernel.ui.WindowContext;
import kernel.ui.WindowManager;
using tink.CoreApi; using tink.CoreApi;
class Terminal extends UIApp { class Terminal implements Process {
private var context:WindowContext; private var context:WindowContext;
private var input:String = ""; private var input:String = "";
private var backlog:Array<String> = []; private var backlog:Array<String> = [];
private var exitTrigger: Bool -> Void; private var handle:ProcessHandle;
private var requestRender: () -> Void;
public function new() {} public function new() {}
public function invoke(context: WindowContext): Future<Bool> { public function run(handle: ProcessHandle): Void {
this.context = context; this.handle = handle;
var statelessContext = handle.createStatelessWindowContext();
this.context = statelessContext.ctx;
this.requestRender = statelessContext.requestRender;
statelessContext.setRenderFunc(this.render);
this.context.onChar.handle(char -> { this.context.onChar.handle(char -> {
this.input += char; this.input += char;
this.redrawInput(); this.requestRender();
}); });
this.context.onKey.handle(e -> { this.context.onKey.handle(e -> {
if (e.keyCode == 259) { if (e.keyCode == 259) { // Backspace
this.input = this.input.substr(0, this.input.length - 1); this.input = this.input.substr(0, this.input.length - 1);
this.redrawInput(); this.requestRender();
} else if (e.keyCode == 257) { } else if (e.keyCode == 257) { // Enter
this.backlog.push("> " + this.input); this.backlog.push("> " + this.input);
var command = this.input; var command = this.input;
this.input = ""; this.input = "";
this.redrawBacklog(); this.requestRender();
this.redrawInput();
this.invokeCommand(command); this.invokeCommand(command);
} }
}); });
WindowManager.instance.focusContextToOutput(context, "main"); this.requestRender();
this.redrawInput(); }
return new Future<Bool>(cb -> { private function render() {
this.exitTrigger = cb; redrawBacklog();
return null; redrawInput();
});
} }
private function redrawBacklog() { private function redrawBacklog() {
@ -117,14 +121,14 @@ class Terminal extends UIApp {
hadInput = true; hadInput = true;
} }
this.backlog[this.backlog.length - 1] += s; this.backlog[this.backlog.length - 1] += s;
this.redrawBacklog(); this.requestRender();
}, },
onExit: (success:Bool) -> { onExit: (success:Bool) -> {
if (this.backlog[this.backlog.length - 1] == "") { if (this.backlog[this.backlog.length - 1] == "") {
this.backlog.pop(); this.backlog.pop();
} }
this.redrawInput(); this.requestRender();
} }
}); });