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,124 +1,130 @@
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)->{
Peripheral.instance.getDrives().foreach(drive -> {
var addr = drive.getAddr();
var label = drive.getDiskLabel();
var id = drive.getDiskID();
public function new() {} if (drive.isDiskPresent()){
if (drive.hasAudio()){
public function run(handle:ProcessHandle):Void { handle.writeLine('${addr} => ${label} [AUDIO]');
this.handle = handle; }else{
var subcommand = handle.args[0]; handle.writeLine('${addr} => ${label} (${id})');
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.writeLine('${addr} => ${label} [AUDIO]');
}else{
handle.writeLine('${addr} => ${label} (${id})');
}
}else {
handle.writeLine('${addr} => [NO DISK]');
} }
}else {
return true; handle.writeLine('${addr} => [NO DISK]');
});
case "play":
var drive = Peripheral.instance.getDrive(driveAddr);
if (drive == null){
handle.writeLine("Drive not found: " + driveAddr);
} }
if (!drive.isDiskPresent()){ return true;
handle.writeLine("No disk in drive: " + driveAddr); });
});
registerSyncSubcommand("play", (args)->{
if (args.length < 1){
handle.writeLine("Missing drive address");
return false;
}
return audioDiskPlayPause(args[0], true);
},"<drive>");
registerSyncSubcommand("stop", (args) -> {
if (args.length < 1){
handle.writeLine("Missing drive address");
return false;
}
return audioDiskPlayPause(args[0], false);
});
registerSyncSubcommand("eject", (args)->{
if (args.length < 1){
handle.writeLine("Missing drive address");
return false;
}
var driveAddr = args[0];
var drive = Peripheral.instance.getDrive(driveAddr);
if (drive == null){
handle.writeLine("Drive not found: " + driveAddr);
return false;
}
if (!drive.isDiskPresent()){
handle.writeLine("No disk in drive: " + driveAddr);
return false;
}
drive.ejectDisk();
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 label:String = args[1];
if (drive == null){
handle.writeLine("Drive not found: " + driveAddr);
}
if (!drive.isDiskPresent()){
handle.writeLine("No disk in drive: " + driveAddr);
}
if (label == null || label == ""){
handle.writeLine(drive.getDiskLabel());
}else{
var err = drive.setDiskLabel(label);
if (err != null){
handle.writeLine("Failed to set lable");
return false;
} }
}
if (!drive.hasAudio()){ return true;
handle.writeLine("Disk in drive " + driveAddr + " does not have audio"); },"<drive> [label]");
} }
drive.playAudio(); private function audioDiskPlayPause(driveAddr: String, play: Bool): Bool {
case "stop": 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()){
handle.writeLine("No disk in drive: " + driveAddr);
}
if (!drive.hasAudio()){
handle.writeLine("Disk in drive: " + driveAddr + " does not have audio");
}
drive.stopAudio();
case "eject":
var drive = Peripheral.instance.getDrive(driveAddr);
if (drive == null){
handle.writeLine("Drive not found: " + driveAddr);
}
if (!drive.isDiskPresent()){
handle.writeLine("No disk in drive: " + driveAddr);
}
drive.ejectDisk();
case "lable":
var drive = Peripheral.instance.getDrive(driveAddr);
var label:String = handle.args[2];
if (drive == null){
handle.writeLine("Drive not found: " + driveAddr);
}
if (!drive.isDiskPresent()){
handle.writeLine("No disk in drive: " + driveAddr);
}
if (label == null || label == ""){
handle.writeLine(drive.getDiskLabel());
}else{
var err = drive.setDiskLabel(label);
if (err != null){
handle.writeLine("Failed to set lable");
}
}
case "help":
case null:
printHelp();
default:
handle.writeLine("Unknown subcommand: " + subcommand);
printHelp();
} }
return handle.close(true); if (!drive.isDiskPresent()){
} handle.writeLine("No disk in drive: " + driveAddr);
return false;
}
private function printHelp() { if (!drive.hasAudio()){
handle.writeLine("Usage: disk <subcommand> [args]"); handle.writeLine("Disk in drive: " + driveAddr + " does not have audio");
handle.writeLine("Subcommands:"); return false;
handle.writeLine(" ls"); }
handle.writeLine(" play <drive>");
handle.writeLine(" stop <drive>"); if (play){
handle.writeLine(" eject <drive>"); drive.playAudio();
handle.writeLine(" label <drive> [label]"); }else{
drive.stopAudio();
}
return true;
} }
} }