From a87f50eec7169824420771e9d83a799cc19a39c4 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Fri, 27 Jan 2023 14:34:25 +0100 Subject: [PATCH] added disk cli program --- src/bin/Disk.hx | 139 ++++++++++++++++++++++++++++++++++++++++++++ src/bin/Terminal.hx | 2 + 2 files changed, 141 insertions(+) create mode 100644 src/bin/Disk.hx diff --git a/src/bin/Disk.hx b/src/bin/Disk.hx new file mode 100644 index 0000000..314654d --- /dev/null +++ b/src/bin/Disk.hx @@ -0,0 +1,139 @@ +package bin; + +import kernel.peripherals.Peripherals.Peripheral; +import lib.cli.TermHandle; +import lib.cli.CLIBase; + +using tink.CoreApi; +using Lambda; + +class Disk extends CLIBase { + + private var handle:TermHandle; + + public function new() { + super(); + } + + public function invoke(handle:TermHandle):Future { + 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.writeLn('${addr} => ${label} [AUDIO]'); + }else{ + handle.writeLn('${addr} => ${label} (${id})'); + } + }else { + handle.writeLn('${addr} => [NO DISK]'); + } + + return true; + }); + case "play": + var drive = Peripheral.instance.getDrive(driveAddr); + + if (drive == null){ + handle.writeLn("Drive not found: " + driveAddr); + return Future.sync(false); + } + + if (!drive.isDiskPresent()){ + handle.writeLn("No disk in drive: " + driveAddr); + return Future.sync(false); + } + + if (!drive.hasAudio()){ + handle.writeLn("Disk in drive " + driveAddr + " does not have audio"); + return Future.sync(false); + } + + drive.playAudio(); + case "stop": + var drive = Peripheral.instance.getDrive(driveAddr); + + if (drive == null){ + handle.writeLn("Drive not found: " + driveAddr); + return Future.sync(false); + } + + if (!drive.isDiskPresent()){ + handle.writeLn("No disk in drive: " + driveAddr); + return Future.sync(false); + } + + if (!drive.hasAudio()){ + handle.writeLn("Disk in drive: " + driveAddr + " does not have audio"); + return Future.sync(false); + } + + drive.stopAudio(); + + case "eject": + var drive = Peripheral.instance.getDrive(driveAddr); + + if (drive == null){ + handle.writeLn("Drive not found: " + driveAddr); + return Future.sync(false); + } + + if (!drive.isDiskPresent()){ + handle.writeLn("No disk in drive: " + driveAddr); + return Future.sync(false); + } + + drive.ejectDisk(); + case "lable": + var drive = Peripheral.instance.getDrive(driveAddr); + var label:String = handle.args[2]; + + if (drive == null){ + handle.writeLn("Drive not found: " + driveAddr); + return Future.sync(false); + } + + if (!drive.isDiskPresent()){ + handle.writeLn("No disk in drive: " + driveAddr); + return Future.sync(false); + } + + if (label == null || label == ""){ + handle.writeLn(drive.getDiskLabel()); + }else{ + var err = drive.setDiskLabel(label); + if (err != null){ + handle.writeLn("Failed to set lable"); + return Future.sync(false); + } + } + case "help": + case null: + printHelp(); + default: + handle.writeLn("Unknown subcommand: " + subcommand); + printHelp(); + return Future.sync(false); + } + + return Future.sync(true); + } + + private function printHelp() { + handle.writeLn("Usage: disk [args]"); + handle.writeLn("Subcommands:"); + handle.writeLn(" ls"); + handle.writeLn(" play "); + handle.writeLn(" stop "); + handle.writeLn(" eject "); + handle.writeLn(" label [label]"); + } +} diff --git a/src/bin/Terminal.hx b/src/bin/Terminal.hx index eddd316..f5881e6 100644 --- a/src/bin/Terminal.hx +++ b/src/bin/Terminal.hx @@ -143,6 +143,8 @@ class Terminal { return new Net(); case "rs": return new Redstone(); + case "disk": + return new Disk(); default: return null; }