From 3cb1811dcb435399de6af8a0052785d330cb4e1b Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Mon, 19 Dec 2022 17:35:52 +0100 Subject: [PATCH] improved terminal stuff --- src/bin/HelloWorld.hx | 5 ++-- src/bin/Net.hx | 70 +++++++++++++++++++++++++++++++------------ src/bin/Terminal.hx | 26 ++++++++-------- src/lib/CLIBase.hx | 4 ++- 4 files changed, 70 insertions(+), 35 deletions(-) diff --git a/src/bin/HelloWorld.hx b/src/bin/HelloWorld.hx index 0a37dd4..54b8317 100644 --- a/src/bin/HelloWorld.hx +++ b/src/bin/HelloWorld.hx @@ -1,16 +1,17 @@ package bin; +using tink.CoreApi; import lib.TermHandle; import lib.CLIBase; class HelloWorld extends CLIBase { - public function invoke(handle: TermHandle):Bool { + public function invoke(handle: TermHandle):Future { var world:String = "world"; if (handle.args.length > 0) { world = handle.args[0]; } handle.write('Hello, $world!'); - return true; + return Future.sync(true); } } diff --git a/src/bin/Net.hx b/src/bin/Net.hx index e21c6e3..ea00d47 100644 --- a/src/bin/Net.hx +++ b/src/bin/Net.hx @@ -1,5 +1,7 @@ package bin; +using tink.CoreApi; + import kernel.peripherals.Peripherals.Peripheral; import kernel.net.Routing; import haxe.ds.ReadOnlyArray; @@ -8,7 +10,7 @@ import lib.CLIBase; class Net extends CLIBase { private var handle:TermHandle; - public function invoke(handle:TermHandle):Bool { + public function invoke(handle:TermHandle):Future { this.handle = handle; var subcommand = handle.args[0]; @@ -16,27 +18,28 @@ class Net extends CLIBase { switch (subcommand) { case "route": - return route(subcommand_args); + return Future.sync(route(subcommand_args)); case "iface": - return iface(subcommand_args); + return Future.sync(iface(subcommand_args)); case "help": printHelp(); - return true; + return Future.sync(true); case "ping": - ping(subcommand_args); + return ping(subcommand_args); + case "proto": + return Future.sync(protos()); default: handle.writeLn("Unknown subcommand: " + subcommand); printHelp(); - return false; + return Future.sync(false); } - - return true; } private function printHelp() { handle.writeLn("net route"); handle.writeLn("net iface"); handle.writeLn("net help"); + handle.writeLn("net proto"); } private function route(args:ReadOnlyArray):Bool { @@ -59,18 +62,47 @@ class Net extends CLIBase { return true; } - function ping(args:ReadOnlyArray) { - if (args.length != 1) { - handle.writeLn("Usage: net ping id"); + function ping(args:ReadOnlyArray): Future { + return new Future(trigger -> { + if (args.length != 1) { + handle.writeLn("Usage: net ping id"); + trigger(false); + return null; + } + + var toID:Null = Std.parseInt(args[0]); + + if (toID == null) { + handle.writeLn("Invalid ID"); + trigger(false); + return null; + } + + kernel.net.Net.instance.ping(toID).handle(result -> { + switch (result){ + case Success(acc): + if (acc) { + handle.writeLn("Ping succeeded"); + trigger(true); + } else { + handle.writeLn("Ping failed"); + trigger(false); + } + case Failure(failure): + handle.writeLn("Ping failed: " + failure); + trigger(false); + } + }); + }); + } + + function protos():Bool { + var protos = kernel.net.Net.instance.getActiveProtocols(); + + for (proto in protos) { + handle.writeLn(proto); } - var toID:Null = Std.parseInt(args[0]); - - if (toID == null) { - handle.writeLn("Invalid ID"); - } - - // var result = kernel.net.Net.instance.ping(toID); - + return true; } } diff --git a/src/bin/Terminal.hx b/src/bin/Terminal.hx index 7f724a3..d4b5cc3 100644 --- a/src/bin/Terminal.hx +++ b/src/bin/Terminal.hx @@ -9,7 +9,7 @@ import kernel.ui.WindowManager; class Terminal { private var context:WindowContext; private var input:String = ""; - private var backlog: Array = []; + private var backlog:Array = []; public function new() {} @@ -45,7 +45,7 @@ class Terminal { var start:Int = this.backlog.length - linesAvailable; - for (i in 0...linesAvailable){ + for (i in 0...linesAvailable) { var line = this.backlog[start + i]; this.context.setCursorPos(0, i); @@ -73,7 +73,7 @@ class Terminal { this.context.setCursorBlink(true); } - private function invokeCommand(command: String):Void{ + private function invokeCommand(command:String):Void { var args = this.parseArgs(command); if (args.length == 0) { @@ -90,7 +90,7 @@ class Terminal { var commandArgs:Array = args.slice(1); var hadInput = false; - var handle = new TermHandle(commandArgs,{ + var handle = new TermHandle(commandArgs, { onWrite: (s:String) -> { if (!hadInput) { this.backlog.push(""); @@ -115,17 +115,17 @@ class Terminal { this.context.setCursorBlink(false); - prog.invoke(handle); + prog.invoke(handle).handle((exitCode) -> { + // Cleanup extra newline + if (this.backlog[this.backlog.length - 1] == "") { + this.backlog.pop(); + } - // Cleanup extra newline - if (this.backlog[this.backlog.length - 1] == "") { - this.backlog.pop(); - } - - this.redrawInput(); + this.redrawInput(); + }); } - private function parseArgs(command: String): Array { + private function parseArgs(command:String):Array { // TODO: tim and quote handling return command.split(" "); } @@ -135,7 +135,7 @@ class Terminal { this.redrawBacklog(); } - private function getProgByName(name: String): CLIBase { + private function getProgByName(name:String):CLIBase { switch (name) { case "hello": return new HelloWorld(); diff --git a/src/lib/CLIBase.hx b/src/lib/CLIBase.hx index 6460e2e..5fa5725 100644 --- a/src/lib/CLIBase.hx +++ b/src/lib/CLIBase.hx @@ -1,6 +1,8 @@ package lib; +using tink.CoreApi; + abstract class CLIBase { public function new() {}; - public abstract function invoke(handle: TermHandle): Bool; + public abstract function invoke(handle: TermHandle): Future; }