From 06b3e3713833228f632c087ff00a630c0c090d08 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Wed, 31 May 2023 18:40:14 +0200 Subject: [PATCH] refactored Disk bin to use CLIBaseApp --- src/bin/Disk.hx | 214 +++++++++++++++++++++++++----------------------- 1 file changed, 110 insertions(+), 104 deletions(-) diff --git a/src/bin/Disk.hx b/src/bin/Disk.hx index 857f033..6532167 100644 --- a/src/bin/Disk.hx +++ b/src/bin/Disk.hx @@ -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 = 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); + },""); + + 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; + },""); + + 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; + }," [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 [args]"); - handle.writeLine("Subcommands:"); - handle.writeLine(" ls"); - handle.writeLine(" play "); - handle.writeLine(" stop "); - handle.writeLine(" eject "); - handle.writeLine(" label [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; } }