关闭 Laravel artisan 命令的 ANSI 颜色

问题描述 投票:0回答:2

当我运行任何 php artisan 命令时,它总是会放置一些尾随字符 [32m]、[39m] 和所有这些数字。我该如何解决这个问题?

A:\xampp\htdocs\5.2>php artisan --version
[32mLaravel Framework[39m version [33m5.2.41[39m
laravel laravel-5 command-prompt
2个回答
7
投票

这些是 ANSI 颜色 - 试图让它看起来更好、更容易阅读。如果您的终端不支持 ANSI,您可以通过传递命令行参数来关闭它们

--no-ansi
:

php artisan --version --no-ansi

0
投票

以防万一,有时 CLI 选项

--no-ansi
或类似选项可能无法工作,因为该选项如何将其自身状态传递给其他命令,如 Symfony 的函数
dump
。例如,在
Laravel 10.38
中,以下命令仍将以 ANSI 颜色输出:

./artisan --no-ansi tinker --execute \
    $'dump(DB::select(\'SHOW CREATE TABLE `failed_jobs`;\'));';

1

环境变量
NO_COLOR

因此,传统的环境变量

NO_COLOR
可能更适合情况:

NO_COLOR= \
    ./artisan tinker --execute \
        $'dump(DB::select(\'SHOW CREATE TABLE `failed_jobs`;\'));';

2


说明

当 Laravel Tinker 执行时,我们创建一个 Laravel 应用程序 Artisan:

// :/artisan

$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);

$status = $kernel->handle(
    $input = new Symfony\Component\Console\Input\ArgvInput,
    new Symfony\Component\Console\Output\ConsoleOutput
);
// \Symfony\Component\Console\Output\ConsoleOutput::__construct

parent::__construct($this->openOutputStream(), $verbosity, $decorated, $formatter);
// \Symfony\Component\Console\Output\StreamOutput::__construct

$decorated ??= $this->hasColorSupport();

parent::__construct($verbosity, $decorated, $formatter);

这里,由于

$decorated
null
,因此方法
hasColorSupport
被调用:

// \Symfony\Component\Console\Output\StreamOutput::hasColorSupport

/**
 * Returns true if the stream supports colorization.
 *
 * Colorization is disabled if not supported by the stream:
 *
 * This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo
 * terminals via named pipes, so we can only check the environment.
 *
 * Reference: Composer\XdebugHandler\Process::supportsColor
 * https://github.com/composer/xdebug-handler
 *
 * @return bool true if the stream supports colorization, false otherwise
 */
protected function hasColorSupport(): bool
{
    // Follow https://no-color.org/
    if (isset($_SERVER['NO_COLOR']) || false !== getenv('NO_COLOR')) {
        return false;
    }

    if ('Hyper' === getenv('TERM_PROGRAM')) {
        return true;
    }

    if (\DIRECTORY_SEPARATOR === '\\') {
        return (\function_exists('sapi_windows_vt100_support')
            && @sapi_windows_vt100_support($this->stream))
            || false !== getenv('ANSICON')
            || 'ON' === getenv('ConEmuANSI')
            || 'xterm' === getenv('TERM');
    }

    return stream_isatty($this->stream);
}

正如我们所见,选项

--no-ansi
不存在于条件中,而仅存在于一般环境中,包括
NO_COLOR

// \Symfony\Component\Console\Output\Output::__construct

$this->formatter->setDecorated($decorated);
// \Symfony\Component\Console\Formatter\OutputFormatter::setDecorated

$this->decorated = $decorated;

稍后,通过特定的项目转储,样式会继承之前确定的

decorated
值:

// \Symfony\Component\VarDumper\Dumper\CliDumper::dumpScalar

$this->line .= $this->style($style, $value, $attr);
// \Symfony\Component\VarDumper\Dumper\CliDumper::style

$this->colors ??= $this->supportsColors();
// \Illuminate\Foundation\Console\CliDumper::supportsColors

return $this->output->isDecorated();
// \Symfony\Component\Console\Output\Output::isDecorated

return $this->formatter->isDecorated();
// \Symfony\Component\Console\Formatter\OutputFormatter::isDecorated

return $this->decorated; // true

环境变量
FORCE_COLOR

Laravel 10 取决于 软件包

symfony/var-dumper
^6.2
仅支持
NO_COLOR

Laravel 11,取决于

symfony/var-dumper
^7.0
。该软件包现在包含许多不同的更改,包括称为 FORCE_COLOR 的约定的支持

// \Symfony\Component\Console\Output\StreamOutput::hasColorSupport

// Follow https://no-color.org/
if ('' !== (($_SERVER['NO_COLOR'] ?? getenv('NO_COLOR'))[0] ?? '')) {
    return false;
}

// Follow https://force-color.org/
if ('' !== (($_SERVER['FORCE_COLOR'] ?? getenv('FORCE_COLOR'))[0] ?? '')) {
    return true;
}
© www.soinside.com 2019 - 2024. All rights reserved.