From ec891072627d0b7e18b1c26e17405cd23c44da05 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Mon, 22 May 2023 22:25:37 +0200 Subject: [PATCH] convert Terminal to Process --- src/bin/Terminal.hx | 46 ++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/bin/Terminal.hx b/src/bin/Terminal.hx index 53fc1e5..5915d1b 100644 --- a/src/bin/Terminal.hx +++ b/src/bin/Terminal.hx @@ -1,52 +1,56 @@ package bin; +import kernel.ps.ProcessHandle; import kernel.ps.Process; import kernel.ps.ProcessManager; -import kernel.ps.ProcessHandle; -import lib.ui.UIApp; import lib.Color; import kernel.ui.WindowContext; -import kernel.ui.WindowManager; using tink.CoreApi; -class Terminal extends UIApp { +class Terminal implements Process { private var context:WindowContext; private var input:String = ""; private var backlog:Array = []; - private var exitTrigger: Bool -> Void; + private var handle:ProcessHandle; + private var requestRender: () -> Void; public function new() {} - public function invoke(context: WindowContext): Future { - this.context = context; + public function run(handle: ProcessHandle): Void { + 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.input += char; - this.redrawInput(); + this.requestRender(); }); 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.redrawInput(); - } else if (e.keyCode == 257) { + this.requestRender(); + } else if (e.keyCode == 257) { // Enter this.backlog.push("> " + this.input); var command = this.input; this.input = ""; - this.redrawBacklog(); - this.redrawInput(); + this.requestRender(); this.invokeCommand(command); } }); - WindowManager.instance.focusContextToOutput(context, "main"); - this.redrawInput(); + this.requestRender(); + } - return new Future(cb -> { - this.exitTrigger = cb; - return null; - }); + private function render() { + redrawBacklog(); + redrawInput(); } private function redrawBacklog() { @@ -117,14 +121,14 @@ class Terminal extends UIApp { hadInput = true; } this.backlog[this.backlog.length - 1] += s; - this.redrawBacklog(); + this.requestRender(); }, onExit: (success:Bool) -> { if (this.backlog[this.backlog.length - 1] == "") { this.backlog.pop(); } - this.redrawInput(); + this.requestRender(); } });