Skip to content

Commit

Permalink
Add figlet command line
Browse files Browse the repository at this point in the history
  • Loading branch information
Povilas Susinskas committed Dec 15, 2016
1 parent 30c1127 commit 5514696
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 28 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ $renderedFiglet = $figlet->render('Another Figlet')
- setFontDir(__DIR_ . '/fonts') // Change default font directory
- setFontStretching(3) // Add spaces between letters
```

#### Also there is figlet command line. Usage is quite straightforward.
```bash
./figlet 'some figlet text' --font block --color yellow
```

##### To make figlet executable from everywhere
- (Linux and OSX) Symlink figlet script file to one of the $PATH (e.g /usr/local/bin/figlet)

##### For more options:
```bash
figlet -h
```
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
}
],
"require": {
"php": ">=5.4"
"php": ">=5.4",
"symfony/console": "~3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.2"
Expand All @@ -28,7 +29,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "0.1-dev"
"dev-master": "1.0-dev"
}
}
}
20 changes: 20 additions & 0 deletions figlet
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env php
<?php

require __DIR__.'/vendor/autoload.php';

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use Povils\Figlet\Command\FigletCommand;

$application = new Application('figlet', '1.0.0');
$command = new FigletCommand();

$application->add($command);

$application->setDefaultCommand($command->getName(), true);
$application->run();
14 changes: 2 additions & 12 deletions src/Figlet/ColorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class ColorManager
];

/**
* Colorize Figlet text.
*
* @param string $text
* @param string|null $fontColor
* @param string|null $backgroundColor
Expand All @@ -79,8 +77,6 @@ public function colorize($text, $fontColor, $backgroundColor)
}

/**
* Colorize font color.
*
* @param string $fontColor
* @param string $coloredText
*
Expand All @@ -90,9 +86,7 @@ public function colorize($text, $fontColor, $backgroundColor)
private function colorizeFont($fontColor, $coloredText)
{
if (isset($this->fontColors[$fontColor])) {
$coloredText = $this->addColorCode($coloredText, $this->fontColors[$fontColor]);

return $coloredText;
return $this->addColorCode($coloredText, $this->fontColors[$fontColor]);
} else {
throw new \InvalidArgumentException(
'Font color "' . $fontColor . '" doesn\'t exist' . PHP_EOL .
Expand All @@ -102,8 +96,6 @@ private function colorizeFont($fontColor, $coloredText)
}

/**
* Colorize background color.
*
* @param string $backgroundColor
* @param string $coloredText
*
Expand All @@ -113,9 +105,7 @@ private function colorizeFont($fontColor, $coloredText)
private function colorizeBackground($backgroundColor, $coloredText)
{
if (isset($this->backgroundColors[$backgroundColor])) {
$coloredText = $this->addColorCode($coloredText, $this->backgroundColors[$backgroundColor]);

return $coloredText;
return $this->addColorCode($coloredText, $this->backgroundColors[$backgroundColor]);
} else {
throw new \InvalidArgumentException(
'Background color "' . $backgroundColor . '" doesn\'t exist ' . PHP_EOL .
Expand Down
88 changes: 88 additions & 0 deletions src/Figlet/Command/FigletCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/**
* This is the part of Povils open-source library.
*
* @author Povilas Susinskas
*/

namespace Povils\Figlet\Command;

use Povils\Figlet\Figlet;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class FigletCommand
*
* @package Povils\Figlet\Command
*/
class FigletCommand extends Command
{
/**
* @inheritdoc
*/
protected function configure()
{
$this
->setName('figlet')
->setDescription('Writes figlet text in terminal')
->addArgument('text', InputArgument::REQUIRED, 'Here should be figlet text')
->addOption(
'font',
'f',
InputOption::VALUE_OPTIONAL,
'Figlet font',
'big'
)
->addOption(
'color',
'c',
InputOption::VALUE_OPTIONAL,
'Figlet font color'
)
->addOption(
'bg-color',
'b',
InputOption::VALUE_OPTIONAL,
'Figlet background color'
)
->addOption(
'stretching',
's',
InputOption::VALUE_OPTIONAL,
'Add spaces between characters'
)
;
}

/**
* @inheritdoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{

$figlet = new Figlet();
$figlet
->setFont($input->getOption('font'));

if(null !== $input->getOption('color')){
$figlet->setFontColor($input->getOption('color'));
}

if(null !== $input->getOption('bg-color')){
$figlet->setBackgroundColor($input->getOption('bg-color'));
}

if(null !== $input->getOption('stretching')){
$figlet->setFontStretching($input->getOption('stretching'));
}

$output->write($figlet->render($input->getArgument('text')));

return 0;
}
}
14 changes: 3 additions & 11 deletions src/Figlet/Figlet.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ class Figlet implements FigletInterface
*/
private $characters = [];

/**
* Figlet constructor.
*/
public function __construct()
{
$this->fontDir = __DIR__ . DIRECTORY_SEPARATOR . 'fonts' . DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -158,8 +155,7 @@ public function setFontStretching($stretching)
*/
public function clear()
{
unset($this->characters);
unset($this->font);
unset($this->characters, $this->font);
}

/**
Expand Down Expand Up @@ -234,8 +230,6 @@ private function getFigletCharacterLines($character)
}

/**
* Combines Figlet characters to one.
*
* @param array $figletCharacters
*
* @return string
Expand Down Expand Up @@ -310,9 +304,7 @@ private function getFontManager()
*/
private function removeNewlines($singleLine)
{
$singleLine = preg_replace('/[\\r\\n]*/', '', $singleLine);

return $singleLine;
return preg_replace('/[\\r\\n]*/', '', $singleLine);
}

/**
Expand All @@ -322,7 +314,7 @@ private function removeNewlines($singleLine)
*/
private function addStretching()
{
if (is_int($this->stretching) && 0 < $this->stretching) {
if (is_numeric($this->stretching) && 0 < $this->stretching) {
$stretchingSpace = ' ';
} else {
$stretchingSpace = '';
Expand Down
4 changes: 1 addition & 3 deletions src/Figlet/FontManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ class FontManager
public function loadFont($fontName, $fontDirectory)
{
if ($this->needLoad($fontName)) {
$font = $this->createFont($fontName, $fontDirectory);

return $font;
return $this->createFont($fontName, $fontDirectory);
}

return $this->currentFont();
Expand Down

0 comments on commit 5514696

Please sign in to comment.