refactored Disk bin to use CLIBaseApp

This commit is contained in:
Djeeberjr 2023-05-31 18:40:14 +02:00
parent 2a7f02c5e3
commit 06b3e37138

View File

@ -1,24 +1,14 @@
package bin; package bin;
import kernel.ps.ProcessHandle; import lib.CLIAppBase;
import kernel.ps.Process;
import kernel.peripherals.Peripherals.Peripheral; import kernel.peripherals.Peripherals.Peripheral;
using tink.CoreApi; using tink.CoreApi;
using Lambda; using Lambda;
class Disk implements Process { class Disk extends CLIAppBase {
private var handle:ProcessHandle; public function new() {
registerSyncSubcommand("ls", (args)->{
public function new() {}
public function run(handle:ProcessHandle):Void {
this.handle = handle;
var subcommand = handle.args[0];
var driveAddr:Null<String> = handle.args[1];
switch (subcommand) {
case "ls":
Peripheral.instance.getDrives().foreach(drive -> { Peripheral.instance.getDrives().foreach(drive -> {
var addr = drive.getAddr(); var addr = drive.getAddr();
var label = drive.getDiskLabel(); var label = drive.getDiskLabel();
@ -36,54 +26,58 @@ class Disk implements Process {
return true; return true;
}); });
case "play": });
var drive = Peripheral.instance.getDrive(driveAddr);
registerSyncSubcommand("play", (args)->{
if (drive == null){ if (args.length < 1){
handle.writeLine("Drive not found: " + driveAddr); handle.writeLine("Missing drive address");
} return false;
}
if (!drive.isDiskPresent()){
handle.writeLine("No disk in drive: " + driveAddr); return audioDiskPlayPause(args[0], true);
} },"<drive>");
if (!drive.hasAudio()){ registerSyncSubcommand("stop", (args) -> {
handle.writeLine("Disk in drive " + driveAddr + " does not have audio"); if (args.length < 1){
} handle.writeLine("Missing drive address");
return false;
drive.playAudio(); }
case "stop":
var drive = Peripheral.instance.getDrive(driveAddr); return audioDiskPlayPause(args[0], false);
});
if (drive == null){
handle.writeLine("Drive not found: " + driveAddr); registerSyncSubcommand("eject", (args)->{
} if (args.length < 1){
handle.writeLine("Missing drive address");
if (!drive.isDiskPresent()){ return false;
handle.writeLine("No disk in drive: " + driveAddr); }
}
var driveAddr = args[0];
if (!drive.hasAudio()){
handle.writeLine("Disk in drive: " + driveAddr + " does not have audio");
}
drive.stopAudio();
case "eject":
var drive = Peripheral.instance.getDrive(driveAddr); var drive = Peripheral.instance.getDrive(driveAddr);
if (drive == null){ if (drive == null){
handle.writeLine("Drive not found: " + driveAddr); handle.writeLine("Drive not found: " + driveAddr);
return false;
} }
if (!drive.isDiskPresent()){ if (!drive.isDiskPresent()){
handle.writeLine("No disk in drive: " + driveAddr); handle.writeLine("No disk in drive: " + driveAddr);
return false;
} }
drive.ejectDisk(); drive.ejectDisk();
case "lable": return true;
},"<drive>");
registerSyncSubcommand("lable",(args) -> {
if (args.length < 1){
handle.writeLine("Missing drive address");
return false;
}
var driveAddr = args[0];
var drive = Peripheral.instance.getDrive(driveAddr); var drive = Peripheral.instance.getDrive(driveAddr);
var label:String = handle.args[2]; var label:String = args[1];
if (drive == null){ if (drive == null){
handle.writeLine("Drive not found: " + driveAddr); handle.writeLine("Drive not found: " + driveAddr);
@ -99,26 +93,38 @@ class Disk implements Process {
var err = drive.setDiskLabel(label); var err = drive.setDiskLabel(label);
if (err != null){ if (err != null){
handle.writeLine("Failed to set lable"); handle.writeLine("Failed to set lable");
return false;
} }
} }
case "help":
case null:
printHelp();
default:
handle.writeLine("Unknown subcommand: " + subcommand);
printHelp();
}
return handle.close(true); return true;
},"<drive> [label]");
} }
private function printHelp() { private function audioDiskPlayPause(driveAddr: String, play: Bool): Bool {
handle.writeLine("Usage: disk <subcommand> [args]"); var drive = Peripheral.instance.getDrive(driveAddr);
handle.writeLine("Subcommands:");
handle.writeLine(" ls"); if (drive == null){
handle.writeLine(" play <drive>"); handle.writeLine("Drive not found: " + driveAddr);
handle.writeLine(" stop <drive>"); return false;
handle.writeLine(" eject <drive>"); }
handle.writeLine(" label <drive> [label]");
if (!drive.isDiskPresent()){
handle.writeLine("No disk in drive: " + driveAddr);
return false;
}
if (!drive.hasAudio()){
handle.writeLine("Disk in drive: " + driveAddr + " does not have audio");
return false;
}
if (play){
drive.playAudio();
}else{
drive.stopAudio();
}
return true;
} }
} }