Code Monkey home page Code Monkey logo

ansi-styles's Introduction

ansi-styles

ANSI escape codes for styling strings in the terminal

Install

$ composer require cdoco/ansi-styles

Usage

require_once 'vendor/autoload.php';

$style = new \Cdoco\AnsiStyles();

echo $style->blue->open . 'Hello Word!' . PHP_EOL . $style->blue->close;

echo $style->color->rgb(100, 200, 15) . "Hello Word!" . PHP_EOL . $style->color->close;
echo $style->color->hsl(120, 100, 60) . "Hello Word!" . PHP_EOL . $style->color->close;
echo $style->color->hex('#C0FFEE') . "Hello Word!" . PHP_EOL . $style->color->close;

echo $style->bgColor->rgb(100, 200, 15) . "Hello Word!" . PHP_EOL . $style->bgColor->close;

API

Each style has an open and close property.

Styles

Modifiers

  • reset
  • bold
  • dim
  • italic (Not widely supported)
  • underline
  • inverse
  • hidden
  • strikethrough (Not widely supported)

Colors

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • gray ("bright black")
  • redBright
  • greenBright
  • yellowBright
  • blueBright
  • magentaBright
  • cyanBright
  • whiteBright

Background colors

  • bgBlack
  • bgRed
  • bgGreen
  • bgYellow
  • bgBlue
  • bgMagenta
  • bgCyan
  • bgWhite
  • bgBlackBright
  • bgRedBright
  • bgGreenBright
  • bgYellowBright
  • bgBlueBright
  • bgMagentaBright
  • bgCyanBright
  • bgWhiteBright

Advanced usage

By default, you get a map of styles, but the styles are also available as groups. They are non-enumerable so they don't show up unless you access them explicitly. This makes it easier to expose only a subset in a higher-level module.

  • $style->modifier
  • $style->color
  • $style->bgColor
Example
echo $style->color->green->open;

To use these, call the associated conversion function with the intended output, for example:

$style->color->rgb(100, 200, 15); // RGB to 16 color ansi foreground code
$style->bgColor->rgb(100, 200, 15); // RGB to 16 color ansi background code

$style->color->hsl(120, 100, 60); // HSL to 256 color ansi foreground code
$style->bgColor->hsl(120, 100, 60); // HSL to 256 color ansi foreground code

$style->color->hex('#C0FFEE'); // Hex (RGB) to 16 million color foreground code
$style->bgColor->hex('#C0FFEE'); // Hex (RGB) to 16 million color background code

License

MIT

ansi-styles's People

Contributors

cdoco avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.