improved terminal stuff
This commit is contained in:
parent
c6e5a836ea
commit
3cb1811dcb
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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>) {
|
||||
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;
|
||||
}
|
||||
|
||||
// var result = kernel.net.Net.instance.ping(toID);
|
||||
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);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -115,14 +115,14 @@ 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();
|
||||
}
|
||||
|
||||
this.redrawInput();
|
||||
});
|
||||
}
|
||||
|
||||
private function parseArgs(command:String):Array<String> {
|
||||
|
@ -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>;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user