added disk cli program

This commit is contained in:
Djeeberjr 2023-01-27 14:34:25 +01:00
parent 95a9ab63d0
commit a87f50eec7
2 changed files with 141 additions and 0 deletions

139
src/bin/Disk.hx Normal file
View File

@ -0,0 +1,139 @@
package bin;
import kernel.peripherals.Peripherals.Peripheral;
import lib.cli.TermHandle;
import lib.cli.CLIBase;
using tink.CoreApi;
using Lambda;
class Disk extends CLIBase {
private var handle:TermHandle;
public function new() {
super();
}
public function invoke(handle:TermHandle):Future<Bool> {
this.handle = handle;
var subcommand = handle.args[0];
var driveAddr:Null<String> = handle.args[1];
switch (subcommand) {
case "ls":
Peripheral.instance.getDrives().foreach(drive -> {
var addr = drive.getAddr();
var label = drive.getDiskLabel();
var id = drive.getDiskID();
if (drive.isDiskPresent()){
if (drive.hasAudio()){
handle.writeLn('${addr} => ${label} [AUDIO]');
}else{
handle.writeLn('${addr} => ${label} (${id})');
}
}else {
handle.writeLn('${addr} => [NO DISK]');
}
return true;
});
case "play":
var drive = Peripheral.instance.getDrive(driveAddr);
if (drive == null){
handle.writeLn("Drive not found: " + driveAddr);
return Future.sync(false);
}
if (!drive.isDiskPresent()){
handle.writeLn("No disk in drive: " + driveAddr);
return Future.sync(false);
}
if (!drive.hasAudio()){
handle.writeLn("Disk in drive " + driveAddr + " does not have audio");
return Future.sync(false);
}
drive.playAudio();
case "stop":
var drive = Peripheral.instance.getDrive(driveAddr);
if (drive == null){
handle.writeLn("Drive not found: " + driveAddr);
return Future.sync(false);
}
if (!drive.isDiskPresent()){
handle.writeLn("No disk in drive: " + driveAddr);
return Future.sync(false);
}
if (!drive.hasAudio()){
handle.writeLn("Disk in drive: " + driveAddr + " does not have audio");
return Future.sync(false);
}
drive.stopAudio();
case "eject":
var drive = Peripheral.instance.getDrive(driveAddr);
if (drive == null){
handle.writeLn("Drive not found: " + driveAddr);
return Future.sync(false);
}
if (!drive.isDiskPresent()){
handle.writeLn("No disk in drive: " + driveAddr);
return Future.sync(false);
}
drive.ejectDisk();
case "lable":
var drive = Peripheral.instance.getDrive(driveAddr);
var label:String = handle.args[2];
if (drive == null){
handle.writeLn("Drive not found: " + driveAddr);
return Future.sync(false);
}
if (!drive.isDiskPresent()){
handle.writeLn("No disk in drive: " + driveAddr);
return Future.sync(false);
}
if (label == null || label == ""){
handle.writeLn(drive.getDiskLabel());
}else{
var err = drive.setDiskLabel(label);
if (err != null){
handle.writeLn("Failed to set lable");
return Future.sync(false);
}
}
case "help":
case null:
printHelp();
default:
handle.writeLn("Unknown subcommand: " + subcommand);
printHelp();
return Future.sync(false);
}
return Future.sync(true);
}
private function printHelp() {
handle.writeLn("Usage: disk <subcommand> [args]");
handle.writeLn("Subcommands:");
handle.writeLn(" ls");
handle.writeLn(" play <drive>");
handle.writeLn(" stop <drive>");
handle.writeLn(" eject <drive>");
handle.writeLn(" label <drive> [label]");
}
}

View File

@ -143,6 +143,8 @@ class Terminal {
return new Net();
case "rs":
return new Redstone();
case "disk":
return new Disk();
default:
return null;
}