refactored Disk bin to use CLIBaseApp
This commit is contained in:
parent
2a7f02c5e3
commit
06b3e37138
138
src/bin/Disk.hx
138
src/bin/Disk.hx
@ -1,24 +1,14 @@
|
||||
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;
|
||||
|
||||
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":
|
||||
class Disk extends CLIAppBase {
|
||||
public function new() {
|
||||
registerSyncSubcommand("ls", (args)->{
|
||||
Peripheral.instance.getDrives().foreach(drive -> {
|
||||
var addr = drive.getAddr();
|
||||
var label = drive.getDiskLabel();
|
||||
@ -36,54 +26,58 @@ class Disk implements Process {
|
||||
|
||||
return true;
|
||||
});
|
||||
case "play":
|
||||
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.playAudio();
|
||||
case "stop":
|
||||
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":
|
||||
});
|
||||
|
||||
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();
|
||||
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 label:String = handle.args[2];
|
||||
var label:String = args[1];
|
||||
|
||||
if (drive == null){
|
||||
handle.writeLine("Drive not found: " + driveAddr);
|
||||
@ -99,26 +93,38 @@ class Disk implements Process {
|
||||
var err = drive.setDiskLabel(label);
|
||||
if (err != null){
|
||||
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() {
|
||||
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]");
|
||||
private function audioDiskPlayPause(driveAddr: String, play: Bool): Bool {
|
||||
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;
|
||||
}
|
||||
|
||||
if (!drive.hasAudio()){
|
||||
handle.writeLine("Disk in drive: " + driveAddr + " does not have audio");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (play){
|
||||
drive.playAudio();
|
||||
}else{
|
||||
drive.stopAudio();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user