Skip to content

Commit

Permalink
Merge branch 'release/v0.9.7' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
betterthanclay committed Nov 28, 2022
2 parents 92230b1 + 6df1fda commit b6a9f11
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v0.9.7 (2022-11-28)
* Added platform adapter structure
* Removed Systemic dependency

## v0.9.6 (2022-11-25)
* Added getUnnamedArguments()
* Added getPassthroughArguments()
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
"require": {
"php": "^8.0",

"decodelabs/archetype": "^0.2.11",
"decodelabs/coercion": "^0.2",
"decodelabs/deliverance": "^0.2",
"decodelabs/exceptional": "^0.4",
"decodelabs/glitch-support": "^0.4",
"decodelabs/systemic": "^0.9|^0.10",
"decodelabs/tightrope": "^0.1.1",
"decodelabs/veneer": "^0.10.10",

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

/**
* @package Terminus
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace DecodeLabs\Terminus;

interface Adapter
{
public function hasStty(): bool;
public function setStty(string $config): void;

public function getShellWidth(): int;
public function getShellHeight(): int;
public function canColorShell(): bool;
}
103 changes: 103 additions & 0 deletions src/Terminus/Adapter/Unix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/**
* @package Terminus
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace DecodeLabs\Terminus\Adapter;

use DecodeLabs\Terminus\AdapterAbstract;

class Unix extends AdapterAbstract
{
/**
* Can the shell support TTY
*/
public function hasStty(): bool
{
static $output;

if (isset($output)) {
return $output;
}

exec('which stty', $result);
return $output = !empty(trim($result[0]));
}

/**
* Set stty config
*/
public function setStty(string $config): void
{
system('stty \'' . $config . '\'');
}


/**
* Get shell width
*/
public function getShellWidth(): int
{
static $output;

if (isset($output)) {
return $output;
}

exec('tput cols 2>/dev/null', $result);
return $output = (int)($result[0] ?? 80);
}

/**
* Get shell height
*/
public function getShellHeight(): int
{
static $output;

if (isset($output)) {
return $output;
}

exec('tput lines 2>/dev/null', $result);
return $output = (int)($result[0] ?? 30);
}

/**
* Get shell be coloured?
*/
public function canColorShell(): bool
{
static $output;

if (isset($output)) {
return $output;
}

if (!defined('STDOUT')) {
return $output = false;
}

if (function_exists('stream_isatty')) {
return $output = stream_isatty(\STDOUT);
}

if (function_exists('posix_isatty')) {
return $output = posix_isatty(\STDOUT);
}

if (($_SERVER['TERM'] ?? null) === 'xterm-256color') {
return $output = true;
}

if (($_SERVER['CLICOLOR'] ?? null) === '1') {
return $output = true;
}

return $output = false;
}
}
38 changes: 38 additions & 0 deletions src/Terminus/AdapterAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* @package Terminus
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace DecodeLabs\Terminus;

use DecodeLabs\Archetype;
use DecodeLabs\Terminus\Adapter\Unix;

abstract class AdapterAbstract implements Adapter
{
/**
* Load for current OS
*/
public static function load(?string $name = null): Adapter
{
if ($name === null) {
$name = php_uname('s');

if (substr(strtolower($name), 0, 3) == 'win') {
$name = 'Windows';
}
}

$class = Archetype::resolve(
Adapter::class,
$name,
Unix::class
);

return new $class($name);
}
}
19 changes: 14 additions & 5 deletions src/Terminus/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@

use DecodeLabs\Deliverance;
use DecodeLabs\Deliverance\Broker;

use DecodeLabs\Systemic;
use DecodeLabs\Terminus\Command\Definition;
use DecodeLabs\Terminus\Command\Request;
use DecodeLabs\Veneer\LazyLoad;

use Stringable;

/**
* @mixin Session
*/
#[LazyLoad]
class Context
{
protected ?Session $session = null;


/**
* Is CLI sapi?
*/
Expand All @@ -33,6 +34,14 @@ public function isActiveSapi(): bool
return \PHP_SAPI === 'cli';
}

/**
* Get adapter
*/
public function getAdapter(): Adapter
{
return $this->getSession()->getAdapter();
}

/**
* Set active session
*/
Expand Down Expand Up @@ -149,23 +158,23 @@ public function prepareCommand(callable $builder): Session
*/
public function getShellWidth(): int
{
return Systemic::$os->getShellWidth();
return $this->getAdapter()->getShellWidth();
}

/**
* Get TTY height
*/
public function getShellHeight(): int
{
return Systemic::$os->getShellHeight();
return $this->getAdapter()->getShellHeight();
}

/**
* Can color output?
*/
public function canColor(): bool
{
return Systemic::$os->canColorShell();
return $this->getAdapter()->canColorShell();
}


Expand Down
34 changes: 22 additions & 12 deletions src/Terminus/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use DecodeLabs\Deliverance\Channel\Buffer;
use DecodeLabs\Deliverance\DataReceiver;
use DecodeLabs\Exceptional;
use DecodeLabs\Systemic;
use DecodeLabs\Terminus\Command\Definition;
use DecodeLabs\Terminus\Command\Request;
use DecodeLabs\Terminus\Io\Controller;
Expand Down Expand Up @@ -52,6 +51,8 @@ class Session implements
protected bool $hasStty = false;
protected ?string $sttyReset = null;

protected Adapter $adapter;

/**
* Init with IO broker and command info
*/
Expand All @@ -63,10 +64,11 @@ public function __construct(
$this->request = $request;
$this->definition = $definition;
$this->broker = $broker;
$this->isAnsi = Systemic::$os->canColorShell();
$this->adapter = AdapterAbstract::load();
$this->isAnsi = $this->adapter->canColorShell();

if ($this->isAnsi) {
$this->hasStty = Systemic::$os->which('stty') !== 'stty';
$this->hasStty = $this->adapter->hasStty();
$this->sttyReset = $this->snapshotStty();
}
}
Expand All @@ -79,6 +81,14 @@ public function __destruct()
$this->resetStty();
}

/**
* Get adapter
*/
public function getAdapter(): Adapter
{
return $this->adapter;
}

/**
* Set request - must be done early in process
*
Expand Down Expand Up @@ -177,7 +187,7 @@ public function restoreStty(?string $snapshot): bool
return true;
}

system('stty \'' . $snapshot . '\'');
$this->adapter->setStty($snapshot);
return true;
}

Expand All @@ -190,7 +200,7 @@ public function resetStty(): bool
return false;
}

system('stty \'' . $this->sttyReset . '\'');
$this->adapter->setStty((string)$this->sttyReset);
return true;
}

Expand All @@ -200,15 +210,15 @@ public function resetStty(): bool
*/
public function getWidth(): int
{
return Systemic::$os->getShellWidth();
return $this->adapter->getShellWidth();
}

/**
* Get TTY height
*/
public function getHeight(): int
{
return Systemic::$os->getShellHeight();
return $this->adapter->getShellHeight();
}


Expand Down Expand Up @@ -272,11 +282,11 @@ public function getPassthroughArguments(
}

if (substr($name, 0, 7) === 'unnamed') {
$output[] = $value;
$output[] = Coercion::toString($value);
} elseif (is_string($value)) {
$output[] = '--'.$name.'="'.$value.'"';
$output[] = '--' . $name . '="' . $value . '"';
} else {
$output[] = '--'.$name;
$output[] = '--' . $name;
}
}

Expand Down Expand Up @@ -1082,7 +1092,7 @@ public function toggleInputEcho(bool $flag): bool
return false;
}

system('stty ' . ($flag ? '' : '-') . 'echo');
$this->adapter->setStty(($flag ? '' : '-') . 'echo');
return true;
}

Expand All @@ -1095,7 +1105,7 @@ public function toggleInputBuffer(bool $flag): bool
return false;
}

system('stty ' . ($flag ? '' : '-') . 'icanon');
$this->adapter->setStty(($flag ? '' : '-') . 'icanon');
return true;
}

Expand Down
Loading

0 comments on commit b6a9f11

Please sign in to comment.