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;
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<Bool> {
var world:String = "world";
if (handle.args.length > 0) {
world = handle.args[0];
}
handle.write('Hello, $world!');
return true;
return Future.sync(true);
}
}

View File

@ -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<Bool> {
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<String>):Bool {
@ -59,18 +62,47 @@ class Net extends CLIBase {
return true;
}
function ping(args:ReadOnlyArray<String>) {
if (args.length != 1) {
handle.writeLn("Usage: net ping id");
function ping(args:ReadOnlyArray<String>): Future<Bool> {
return new Future<Bool>(trigger -> {
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]);
if (toID == null) {
handle.writeLn("Invalid ID");
}
// var result = kernel.net.Net.instance.ping(toID);
return true;
}
}

View File

@ -9,7 +9,7 @@ import kernel.ui.WindowManager;
class Terminal {
private var context:WindowContext;
private var input:String = "";
private var backlog: Array<String> = [];
private var backlog:Array<String> = [];
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<String> = 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<String> {
private function parseArgs(command:String):Array<String> {
// 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();

View File

@ -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<Bool>;
}