improved terminal stuff

This commit is contained in:
Djeeberjr 2022-12-19 17:35:52 +01:00
parent c6e5a836ea
commit 3cb1811dcb
4 changed files with 70 additions and 35 deletions

View File

@ -1,16 +1,17 @@
package bin; package bin;
using tink.CoreApi;
import lib.TermHandle; import lib.TermHandle;
import lib.CLIBase; import lib.CLIBase;
class HelloWorld extends CLIBase { class HelloWorld extends CLIBase {
public function invoke(handle: TermHandle):Bool { public function invoke(handle: TermHandle):Future<Bool> {
var world:String = "world"; var world:String = "world";
if (handle.args.length > 0) { if (handle.args.length > 0) {
world = handle.args[0]; world = handle.args[0];
} }
handle.write('Hello, $world!'); handle.write('Hello, $world!');
return true; return Future.sync(true);
} }
} }

View File

@ -1,5 +1,7 @@
package bin; package bin;
using tink.CoreApi;
import kernel.peripherals.Peripherals.Peripheral; import kernel.peripherals.Peripherals.Peripheral;
import kernel.net.Routing; import kernel.net.Routing;
import haxe.ds.ReadOnlyArray; import haxe.ds.ReadOnlyArray;
@ -8,7 +10,7 @@ import lib.CLIBase;
class Net extends CLIBase { class Net extends CLIBase {
private var handle:TermHandle; private var handle:TermHandle;
public function invoke(handle:TermHandle):Bool { public function invoke(handle:TermHandle):Future<Bool> {
this.handle = handle; this.handle = handle;
var subcommand = handle.args[0]; var subcommand = handle.args[0];
@ -16,27 +18,28 @@ class Net extends CLIBase {
switch (subcommand) { switch (subcommand) {
case "route": case "route":
return route(subcommand_args); return Future.sync(route(subcommand_args));
case "iface": case "iface":
return iface(subcommand_args); return Future.sync(iface(subcommand_args));
case "help": case "help":
printHelp(); printHelp();
return true; return Future.sync(true);
case "ping": case "ping":
ping(subcommand_args); return ping(subcommand_args);
case "proto":
return Future.sync(protos());
default: default:
handle.writeLn("Unknown subcommand: " + subcommand); handle.writeLn("Unknown subcommand: " + subcommand);
printHelp(); printHelp();
return false; return Future.sync(false);
} }
return true;
} }
private function printHelp() { private function printHelp() {
handle.writeLn("net route"); handle.writeLn("net route");
handle.writeLn("net iface"); handle.writeLn("net iface");
handle.writeLn("net help"); handle.writeLn("net help");
handle.writeLn("net proto");
} }
private function route(args:ReadOnlyArray<String>):Bool { private function route(args:ReadOnlyArray<String>):Bool {
@ -59,18 +62,47 @@ class Net extends CLIBase {
return true; return true;
} }
function ping(args:ReadOnlyArray<String>) { function ping(args:ReadOnlyArray<String>): Future<Bool> {
if (args.length != 1) { return new Future<Bool>(trigger -> {
handle.writeLn("Usage: net ping id"); if (args.length != 1) {
handle.writeLn("Usage: net ping id");
trigger(false);
return null;
}
var toID:Null<Int> = 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<Int> = Std.parseInt(args[0]); return true;
if (toID == null) {
handle.writeLn("Invalid ID");
}
// var result = kernel.net.Net.instance.ping(toID);
} }
} }

View File

@ -9,7 +9,7 @@ import kernel.ui.WindowManager;
class Terminal { class Terminal {
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> = [];
public function new() {} public function new() {}
@ -45,7 +45,7 @@ class Terminal {
var start:Int = this.backlog.length - linesAvailable; var start:Int = this.backlog.length - linesAvailable;
for (i in 0...linesAvailable){ for (i in 0...linesAvailable) {
var line = this.backlog[start + i]; var line = this.backlog[start + i];
this.context.setCursorPos(0, i); this.context.setCursorPos(0, i);
@ -73,7 +73,7 @@ class Terminal {
this.context.setCursorBlink(true); this.context.setCursorBlink(true);
} }
private function invokeCommand(command: String):Void{ private function invokeCommand(command:String):Void {
var args = this.parseArgs(command); var args = this.parseArgs(command);
if (args.length == 0) { if (args.length == 0) {
@ -90,7 +90,7 @@ class Terminal {
var commandArgs:Array<String> = args.slice(1); var commandArgs:Array<String> = args.slice(1);
var hadInput = false; var hadInput = false;
var handle = new TermHandle(commandArgs,{ var handle = new TermHandle(commandArgs, {
onWrite: (s:String) -> { onWrite: (s:String) -> {
if (!hadInput) { if (!hadInput) {
this.backlog.push(""); this.backlog.push("");
@ -115,17 +115,17 @@ class Terminal {
this.context.setCursorBlink(false); 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 this.redrawInput();
if (this.backlog[this.backlog.length - 1] == "") { });
this.backlog.pop();
}
this.redrawInput();
} }
private function parseArgs(command: String): Array<String> { private function parseArgs(command:String):Array<String> {
// TODO: tim and quote handling // TODO: tim and quote handling
return command.split(" "); return command.split(" ");
} }
@ -135,7 +135,7 @@ class Terminal {
this.redrawBacklog(); this.redrawBacklog();
} }
private function getProgByName(name: String): CLIBase { private function getProgByName(name:String):CLIBase {
switch (name) { switch (name) {
case "hello": case "hello":
return new HelloWorld(); return new HelloWorld();

View File

@ -1,6 +1,8 @@
package lib; package lib;
using tink.CoreApi;
abstract class CLIBase { abstract class CLIBase {
public function new() {}; public function new() {};
public abstract function invoke(handle: TermHandle): Bool; public abstract function invoke(handle: TermHandle): Future<Bool>;
} }