Skip to content

Commit

Permalink
[I/O] Implement ASCII constants class
Browse files Browse the repository at this point in the history
  • Loading branch information
yannoff committed Oct 18, 2023
1 parent 3a248b5 commit 1c082b2
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 27 deletions.
15 changes: 11 additions & 4 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
use Yannoff\Component\Console\Command\VersionCommand;
use Yannoff\Component\Console\Exception\Command\UnknownCommandException;
use Yannoff\Component\Console\Exception\LogicException;
use Yannoff\Component\Console\IO\ASCII;
use Yannoff\Component\Console\IO\IOHelper;
use Yannoff\Component\Console\IO\Output\Formatter;

/**
* Class Application
Expand All @@ -31,6 +31,13 @@ class Application
{
use IOHelper;

/**
* Default width for the name columns in help
*
* @var int
*/
const PAD = 18;

/**
* Name used for the `help` command
*
Expand Down Expand Up @@ -243,12 +250,12 @@ public function has($name)
/**
* Build application usage/help message upon the registered commands and return it
*
* @param string $tab The tabulation string (defaults to `\n`)
* @param string $tab The tabulation string (defaults to `\t`)
* @param int $width Minimum width for the command names column (defaults to `18`)
*
* @return string
*/
public function getUsage($tab = Formatter::TAB, $width = Formatter::PAD)
public function getUsage($tab = ASCII::TAB, $width = self::PAD)
{
$lines = [];

Expand All @@ -260,7 +267,7 @@ public function getUsage($tab = Formatter::TAB, $width = Formatter::PAD)
$lines[] = sprintf("{$tab}%-{$width}s %s", $name, $command->getHelp());
}

return implode(Formatter::LF, $lines);
return implode(ASCII::LF, $lines);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
use Yannoff\Component\Console\Exception\Definition\UndefinedOptionException;
use Yannoff\Component\Console\Exception\LogicException;
use Yannoff\Component\Console\Exception\RuntimeException;
use Yannoff\Component\Console\IO\ASCII;
use Yannoff\Component\Console\IO\IOHelper;
use Yannoff\Component\Console\IO\Output\Formatter;

/**
* Class Command
Expand Down Expand Up @@ -229,12 +229,12 @@ public function getArgument($name)
/**
* Build the whole command usage/help message with all options/arguments documented
*
* @param string $tab The tabulation string (defaults to `\n`)
* @param string $tab The tabulation string (defaults to `\t`)
* @param int $width Minimum width for the option/argument names column (defaults to `18`)
*
* @return string
*/
public function getUsage($tab = Formatter::TAB, $width = Formatter::PAD)
public function getUsage($tab = ASCII::TAB, $width = Application::PAD)
{
$lines = [];

Expand All @@ -260,7 +260,7 @@ public function getUsage($tab = Formatter::TAB, $width = Formatter::PAD)
}
}

return implode(Formatter::LF, $lines) . Formatter::LF;
return implode(ASCII::LF, $lines) . ASCII::LF;
}

/**
Expand All @@ -280,7 +280,7 @@ public function isSystem()
*
* @return string
*/
protected function getSynopsis($tab = Formatter::TAB)
protected function getSynopsis($tab = ASCII::TAB)
{
$help = [];

Expand Down
5 changes: 3 additions & 2 deletions src/Definition/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@

namespace Yannoff\Component\Console\Definition;

use Yannoff\Component\Console\Application;
use Yannoff\Component\Console\Exception\Definition\InvalidArgumentTypeException;
use Yannoff\Component\Console\IO\Output\Formatter;
use Yannoff\Component\Console\IO\ASCII;

/**
* Class Argument
Expand Down Expand Up @@ -102,7 +103,7 @@ public static function getValidTypes()
/**
* {@inheritdoc}
*/
public function getSynopsis($tab = Formatter::TAB, $width = Formatter::PAD)
public function getSynopsis($tab = ASCII::TAB, $width = Application::PAD)
{
$help = sprintf("%s%-{$width}s %s", $tab, $this->name, $this->help);

Expand Down
7 changes: 4 additions & 3 deletions src/Definition/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

namespace Yannoff\Component\Console\Definition;

use Yannoff\Component\Console\IO\Output\Formatter;
use Yannoff\Component\Console\Application;
use Yannoff\Component\Console\IO\ASCII;

/**
* Class Item
Expand Down Expand Up @@ -63,12 +64,12 @@ abstract class Item
/**
* Return the formatted help for the item
*
* @param string $tab The tabulation string (defaults to `\n`)
* @param string $tab The tabulation string (defaults to `\t`)
* @param int $width Minimum width for the names column (defaults to `18`)
*
* @return string
*/
abstract public function getSynopsis($tab = Formatter::TAB, $width = Formatter::PAD);
abstract public function getSynopsis($tab = ASCII::TAB, $width = Application::PAD);

/**
* Give the authorized values for the type property
Expand Down
5 changes: 3 additions & 2 deletions src/Definition/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@

namespace Yannoff\Component\Console\Definition;

use Yannoff\Component\Console\Application;
use Yannoff\Component\Console\Exception\Definition\InvalidOptionTypeException;
use Yannoff\Component\Console\IO\Output\Formatter;
use Yannoff\Component\Console\IO\ASCII;

/**
* Class Option
Expand Down Expand Up @@ -123,7 +124,7 @@ public static function getValidTypes()
/**
* {@inheritdoc}
*/
public function getSynopsis($tab = Formatter::TAB, $width = Formatter::PAD)
public function getSynopsis($tab = ASCII::TAB, $width = Application::PAD)
{
$synopsis = implode(', ', $this->getNames());

Expand Down
48 changes: 48 additions & 0 deletions src/IO/ASCII.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* This file is part of the yannoff/console library
*
* (c) Yannoff (https://github.com/yannoff)
*
* @project yannoff/console
* @link https://github.com/yannoff/console
* @license https://github.com/yannoff/console/blob/master/LICENSE
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Yannoff\Component\Console\IO;

/**
* Class ASCII
* ASCII character/sequence representation constants
*
* @package Yannoff\Component\Console\IO
*/
class ASCII
{
/**
* New line string representations
*
* @var string
*/
const LF = "\n";
const CR = "\r";
const CRLF = "\r\n";

/**
* Tabulation character
*
* @var string
*/
const TAB = "\t";

/**
* Soft-tab string: 4 spaces
*
* @var string
*/
const STAB = " ";
}
7 changes: 3 additions & 4 deletions src/IO/IOHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
namespace Yannoff\Component\Console\IO;

use Yannoff\Component\Console\Exception\IO\UnsupportedStreamException;
use Yannoff\Component\Console\IO\Output\Formatter;
use Yannoff\Component\Console\IO\Output\FormatterRegistry;
use Yannoff\Component\Console\IO\Output\Verbosity;
use Yannoff\Component\Console\IO\Stream\IOStream;
Expand Down Expand Up @@ -107,7 +106,7 @@ public function read($interactive = false)
*
* @return bool|int
*/
public function error($text = '', $ending = Formatter::LF)
public function error($text = '', $ending = ASCII::LF)
{
return $this->output($this->stderr, $text, $ending);
}
Expand All @@ -120,7 +119,7 @@ public function error($text = '', $ending = Formatter::LF)
*
* @return bool|int
*/
public function write($text = '', $ending = Formatter::LF)
public function write($text = '', $ending = ASCII::LF)
{
return $this->output($this->stdout, $text, $ending);
}
Expand All @@ -147,7 +146,7 @@ protected function dmesg($message, $level = Verbosity::ERROR)
*
* @return bool|int
*/
private function output(IOWriter $stream, $contents = '', $ending = Formatter::LF)
private function output(IOWriter $stream, $contents = '', $ending = ASCII::LF)
{
$contents = FormatterRegistry::get($stream)->format($contents);

Expand Down
27 changes: 20 additions & 7 deletions src/IO/Output/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

namespace Yannoff\Component\Console\IO\Output;

use Yannoff\Component\Console\Application;
use Yannoff\Component\Console\IO\ASCII;

/**
* Interface Formatter
* Contract for console output formatters
Expand All @@ -33,32 +36,42 @@ interface Formatter
const OS_CYGWIN = 'Cygwin';

/**
* Line feed characters or strings
* New line characters or strings
*
* @deprecated Use ASCII constants instead
*
* @var string
*/
const LF = "\n";
const CR = "\r";
const CRLF = "\r\n";
const LF = ASCII::LF;
const CR = ASCII::CR;
const CRLF = ASCII::CRLF;

/**
* Tabulation character
*
* @deprecated Use ASCII constant instead
*
* @var string
*/
const TAB = "\t";
const TAB = ASCII::TAB;

/**
* Soft-tab string: 4 spaces
*
* @deprecated Use ASCII constant instead
*
* @var string
*/
const STAB = " ";
const STAB = ASCII::STAB;

/**
* Minimal left-padding width for the name columns in help
*
* @deprecated Use Application constant instead
*
* @var string
*/
const PAD = 18;
const PAD = Application::PAD;

/**
* Render the given markup text into a terminal-compatible format
Expand Down

0 comments on commit 1c082b2

Please sign in to comment.