metashock / jm_console
Jm_Console 是一个用于终端颜色的库
Requires
- pear-www.metashock.de/pear/jm_autoloader: *
This package is not auto-updated.
Last update: 2024-09-22 02:42:00 UTC
README
PHP 的 ANSI 终端库
安装
要安装 Jm_Console,您可以使用 PEAR 安装程序或下载 tarball 并手动安装文件。
使用 PEAR 安装程序
如果您还没有发现 metashock pear 通道,您需要这样做。同时,您应该发布通道更新
pear channel-discover metashock.de/pear
pear channel-update metashock
之后,您可以安装 Jm_Console。以下命令将安装最新稳定版本及其依赖项
pear install -a metashock/Jm_Console
如果您想安装特定版本或测试版本,您必须在命令行上指定此版本。例如
pear install -a metashock/Jm_Console-0.3.0
手动下载和安装文件
或者,您可以直接从 http://www.metashock.de/pear 下载包并将其放入您的 include_path 中列出的文件夹中。请参阅 php.net 关于 include_path 指令的文档。
使用方法
基础知识
在访问 Jm_Console 的功能之前,您首先需要获取对其的引用。Jm_Console 是一个单例类,这意味着只有一个引用可用。要获取引用,请调用
// require Jm_Autoloader require_once 'Jm/Autoloader.php'; // get an instance of Jm_Console $console = Jm_Console::singleton();
打印输出
Jm_Console 提供对 STDOUT 和 STDERR 的写入访问。输出使用以下函数完成
$console->write('foo'); // writes foo to stdout $console->writeln('foo'); // writes foo to stdout and adds a newline $console->error('foo'); // writes foo to stderr $console->errorln('foo'); // writes foo to stderr and adds a newline
终端颜色
ANSI 终端标准允许定义前景颜色、背景颜色并选择文本装饰模式。Jm_Console 的目标是在打印文本时提供对终端颜色的直观访问。
最简单的事情就是只指定前景颜色
$console->writeln('hello, world!', 'green'); // writes green text to stdout $console->errorln('an error occured!', 'red'); // writes red text to stderr
或只指定文本装饰
$console->writeln('Booh!', 'bold'); // writes bold text to stdout $console->writeln('I\'m a link!', 'underline'); // writes underlined text to stdout
或同时指定前景颜色和文本装饰
$console->writeln('Booh!', 'blue,bold'); // writes bold blue text to stdout $console->writeln('I\'m a link!', 'yellow, underline'); // writes underlined yellow text to stdout
如果您想设置背景颜色,必须在颜色前使用前缀 bg:。否则,Jm_Console 无法区分前景颜色和背景颜色
$console->writeln('Booh!', 'white,bg:blue'); // writes white text on a blue background to stdout
表格: 可用的图形模式
光标定位
ANSI 终端支持光标定位。
$console->cursorPosition(0, 0); // positioning the cursor at upper left corner
也可以存储光标位置并在以后恢复它
$console->savecursor(); // saves the cursor position // ... $console->restorecursor(); // restores the cursor position
示例
在终端上绘制进度条
<?php require_once 'Jm/Autoloader.php'; Jm_Autoloader::singleton()->prependPath('lib/php'); $console = Jm_Console::singleton(); for($a = 0; $a < 3; $a++) { $s = rand(1, 50000); $console->savecursor(); $total = rand(1,100); for($i = 0; $i <= $total; $i++) { if($console->stdout()->assumeIsatty()) { $console->stdout()->eraseln(); $console->restorecursor(); $console->write('importing: '); progressbar($i, $total); printf(" %s/%s", $i, $total); } else { printf("importing: %s/%s", $i, $total); echo PHP_EOL; } usleep($s); } $console->writeln(); } /** * Renders the progressbar */ function progressbar($now, $total, $w=35) { $console = Jm_Console::singleton(); $console->write('[', 'white,light'); $n = floor($now * $w / $total); $console->write(str_repeat('+', $n), 'green,light'); if($n < $w) { $console->write(']', 'green,light'); $console->write(str_repeat('-', $w - $n -1), 'red,light'); } $console->write(']', 'white,light'); }
API 文档
API 文档可以在这里找到: http://metashock.de/docs/api/Jm/Console/index.xhtml



