From 0c8297c3a87f04b649441896360f4ff0ac7bca74 Mon Sep 17 00:00:00 2001 From: = <=> Date: Mon, 26 Oct 2015 06:30:19 -0700 Subject: [PATCH] Added Progress --- Hathor.php | 2 +- src/Hathor/Command/ScannerCommand.php | 72 ++++++++++++++++++++------- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/Hathor.php b/Hathor.php index 9ad5673..49104fc 100644 --- a/Hathor.php +++ b/Hathor.php @@ -14,7 +14,7 @@ use Symfony\Component\Config; use Symfony\Component\Console\Application; use Sikofitt\Hathor\Command\ScannerCommand; - +define('BASEDIR', dirname(__FILE__)); $app = new Application('Hathor', 'v0.0.1'); $app->add(new ScannerCommand()); $app->run(); diff --git a/src/Hathor/Command/ScannerCommand.php b/src/Hathor/Command/ScannerCommand.php index 21761b5..e98e583 100644 --- a/src/Hathor/Command/ScannerCommand.php +++ b/src/Hathor/Command/ScannerCommand.php @@ -5,6 +5,7 @@ namespace Sikofitt\Hathor\Command; +use Symfony\Component\Config\Definition\Exception\Exception; use Symfony\Component\Finder\Finder; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; @@ -22,36 +23,71 @@ class ScannerCommand extends BaseCommand $this ->setName('scan') ->setDescription('Scans Music') - ->addArgument('file', InputArgument::OPTIONAL, 'the file to work on'); + ->addOption('directory', 'd', InputOption::VALUE_OPTIONAL, 'Directory to scan for music.',BASEDIR) + ->addArgument('extensions',InputArgument::IS_ARRAY, 'Extensions to scan for separated by a space',['mp2','mp3','mp4','wma','ogg']); + + } protected function execute(InputInterface $input, OutputInterface $output) { - $output->writeln("Please wait ..."); + $extensions = $input->getArgument('extensions'); + + // \b(\.mp2|\.mp3|\.mp4|\.wma|\.ogg)\b$ + $extensionRegex = '/\b('; + foreach($extensions as $extension) { + $extensionRegex .= sprintf('\.%s|',$extension); + } + + $extensionRegex[strlen($extensionRegex)-1] = ')'; + $extensionRegex .= '\b$/i'; + + $output->writeln("\n Please wait ... finding files.\n"); $finder = new Finder(); $finder ->files() - ->in("/home/eric/Music") + ->in($input->getOption('directory')) ->ignoreDotFiles(true) ->ignoreUnreadableDirs(true) - ->ignoreVCS(true) - ->sortByType() - ->name("*.mp3") - ->name("*.mp4") - ->name("*.ogg") - ->name("*.wma") - ->name("*.mp2"); + ->name($extensionRegex); $progress = new ProgressBar($output, $finder->count()); - $progress->setFormat("debug"); + $progress->setBarWidth(50); + $progress->setFormat(" %message%\n %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%"); + $progress->clear(); $progress->start(); - foreach ($finder as $file) { - $progress->setOverwrite(true); - $progress->setMessage($file->getFilename()); - $progress->advance(); - } - $progress->finish(); $getID3 = new ID3; - //$getID3->setFile($input->getArgument('file'))->getFileData()->dumpAll(); + $progress->setBarCharacter('#'); + $progress->setProgressCharacter('#'); + $progress->setEmptyBarCharacter('#'); + //$progress->setRedrawFrequency(100); + + $cols = shell_exec('tput cols'); + foreach ($finder as $file) { + $progress->setMessage(str_repeat('-', $cols - 5)); + //$currentDir = explode(DIRECTORY_SEPARATOR, $file->getRelativePath()); + //$currentDir = $currentDir[count($currentDir)-1]; + //$currentDir = substr($currentDir[count($currentDir)-1],0,60); + $currentFile = substr($file->getFilename(), 0,$cols - 30); + + $progress->setMessage("Current File - $currentFile ..."); + + //$getID3->setFile($file->getRealPath()); + try { + // $allMusicData[] = $getID3->getFileData (); + } catch(\Exception $e) { + $progress->setMessage('Caught RuntimeException'); + continue; + } + + $progress->advance(); + + } + $progress->setMessage('Finished!'); + $progress->finish(); + $output->writeln(''); + //var_dump($allMusicData); + + } }