定制 molovo/graphite 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

molovo/graphite

Composer 安装命令:

composer require molovo/graphite

包简介

A collection of helpers for building pretty command-line tools.

README 文档

README

A collection of helpers for building pretty command-line tools.

$graphite = new Molovo\Graphite\Graphite;

echo $graphite->green->underline('I believe in unicorns!');

Installation

Install using composer:

composer require molovo/graphite

Basic Usage

Once instantiated, the Graphite class becomes a re-usable string rendering tool. It comes with a number of methods for styling the foreground color, background color and style of a string using ANSI escape codes.

echo $graphite->red('A string');
// "\033[0;31mA string\033[0;m"

When accessed as a property, or as a method without arguments, these methods allow chaining of styles. When a string is passed as the first argument, it is returned with all the chained styles applied.

echo $graphite->red->yellowbg()->underline('A string');
// "\033[4;31;43mA string\033[0;m"

Global indentation can be set with the setGlobalIndentation() method. Once defined, when the render() method is invoked, the string passed to it is output with the defined indentation prepended to it, and with a newline appended.

$graphite->setGlobalIndentation(2);
echo $graphite->yellow->render('A string');
// "  \033[0;33mA string\033[0;m\n"

Available styling methods

  • black
  • blackbg
  • red
  • redbg
  • green
  • greenbg
  • yellow
  • yellowbg
  • blue
  • bluebg
  • magenta
  • magentabg
  • cyan
  • cyanbg
  • white
  • whitebg
  • gray
  • graybg
  • bold
  • italic
  • underline
  • inverse
  • strikethrough

Additional Methods

As well as the styling methods above, the following helper methods are available.

strip(string $str)

The strip() method strips all ANSI escape codes from the passed string and returns it.

$str = "\033[0;31mA string with ANSI escaping\033[0;m"
echo $graphite->strip($str);
// "A string with ANSI escaping"

repeat(string $character, int $length)

The repeat() method returns a string consisting of the defined $character, repeated $length times.

echo $graphite->repeat('+', 5);
// "+++++"

Boxes

The Box class adds a border around a string (or an array of strings, where each item is a line).

$box = new Molovo\Graphite\Box('Rainbows!');
echo $box;
// ┌─────────┐
// │Rainbows!│
// └─────────┘

Boxes can be styled in a number of ways, by passing an array of styles as a second parameter.

$box = new Box('Unicorns!', [
  'borderColor' => Graphite::WHITE, // The color of the border (and title)
  'borderStyle' => Box::SINGLE,    // The border style
  'marginX'     => 0,               // The number of characters to add to left and right
  'marginY'     => 0,               // The number of lines to add above and below
  'paddingX'    => 0,               // The number of characters to add as left and right padding
  'paddingY'    => 0,               The number of lines to add as top and bottom padding
]);

The following styles are available:

Box::SINGLE
┌──────────┐
│The string│
└──────────┘

Box::DOUBLE
╔══════════╗
║The string║
╚══════════╝

Box::ROUNDED
╭──────────╮
│The string│
╰──────────╯

Box::SINGLE_DOUBLE
╓──────────╖
║The string║
╙──────────╜

Box::DOUBLE_SINGLE
╒══════════╕
│The string│
╘══════════╛

Box::CLASSIC
+----------+
|The string|
+----------+

You can also use the Box::NO_BORDER to add margin and padding to a string without a border.

$box = new Box("Unicorns and\nrainbows", [
  'borderStyle' => Box::NO_BORDER,
  'paddingX' => 2,
  'paddingY' => 1
]);
echo $box;
//
//   Unicorns and
//   rainbows
//

You can also add titles to boxes. These are rendered within the border, in the same color.

$box = new Box('The string', ['paddingX' => 1]);
$box->setTitle('My Box');
echo $box;
// ┌ My Box ────┐
// │ The string │
// └────────────┘

Tables

$data = [
    ['First', 'Second', 'Third'],
    [1, 2, 3],
    [11, 22, 33],
];

$table = new Table($data, [
    'cellPadding'     => 1,
    'columnSeparator' => '',
    'headerColor'     => Graphite::WHITE,
    'headerSeparator' => '',
    'marginX'         => 0,
    'marginY'         => 0,
    'separatorColor'  => Graphite::WHITE,
]);
echo $table;

// First │ Second │ Third
// ══════════════════════════
// 1     │ 2      │ 3
// 11    │ 22     │ 33

molovo/graphite 适用场景与选型建议

molovo/graphite 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 819 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 03 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「cli」 「graphite」 「ansi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 molovo/graphite 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 molovo/graphite 我们能提供哪些服务?
定制开发 / 二次开发

基于 molovo/graphite 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 819
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 9
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-03-08