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;
import kernel.ps.ProcessHandle;
import kernel.ps.Process;
import lib.CLIAppBase;
import kernel.peripherals.Peripherals.Peripheral;
using tink.CoreApi;
using Lambda;
class Disk implements Process {
private var handle:ProcessHandle;
class Disk extends CLIAppBase {
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() {}
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 -> {
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]');
if (drive.isDiskPresent()){
if (drive.hasAudio()){
handle.writeLine('${addr} => ${label} [AUDIO]');
}else{
handle.writeLine('${addr} => ${label} (${id})');
}
return true;
});
case "play":
var drive = Peripheral.instance.getDrive(driveAddr);
if (drive == null){
handle.writeLine("Drive not found: " + driveAddr);
}else {
handle.writeLine('${addr} => [NO DISK]');
}
if (!drive.isDiskPresent()){
handle.writeLine("No disk in drive: " + driveAddr);
return true;
});
});
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()){
handle.writeLine("Disk in drive " + driveAddr + " does not have audio");
}
return true;
},"<drive> [label]");
}
drive.playAudio();
case "stop":
var drive = Peripheral.instance.getDrive(driveAddr);
private function audioDiskPlayPause(driveAddr: String, play: Bool): Bool {
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);
}
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();
if (drive == null){
handle.writeLine("Drive not found: " + driveAddr);
return false;
}
return handle.close(true);
}
if (!drive.isDiskPresent()){
handle.writeLine("No disk in drive: " + driveAddr);
return false;
}
private function printHelp() {
handle.writeLine("Usage: disk <subcommand> [args]");
handle.writeLine("Subcommands:");
handle.writeLine(" ls");
handle.writeLine(" play <drive>");
handle.writeLine(" stop <drive>");
handle.writeLine(" eject <drive>");
handle.writeLine(" label <drive> [label]");
if (!drive.hasAudio()){
handle.writeLine("Disk in drive: " + driveAddr + " does not have audio");
return false;
}
if (play){
drive.playAudio();
}else{
drive.stopAudio();
}
return true;
}
}