├── .github ├── FUNDING.yml └── workflows │ └── test.yml ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Colorize.php ├── ColorizeServiceProvider.php └── ColorizeString.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: jbrooksuk 2 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | runs-on: ${{ matrix.os }} 8 | strategy: 9 | fail-fast: true 10 | matrix: 11 | os: [ubuntu-latest] 12 | php: [8.0] 13 | dependency-version: [prefer-lowest, prefer-stable] 14 | 15 | name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} 16 | 17 | steps: 18 | - name: Checkout code 19 | uses: actions/checkout@v2 20 | 21 | - name: Setup PHP 22 | uses: shivammathur/setup-php@v2 23 | with: 24 | php-version: ${{ matrix.php }} 25 | extensions: mbstring, iconv 26 | coverage: none 27 | 28 | - name: Setup problem matchers 29 | run: | 30 | echo "::add-matcher::${{ runner.tool_cache }}/php.json" 31 | echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" 32 | - name: Install dependencies 33 | run: | 34 | composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction 35 | - name: Execute tests 36 | run: vendor/bin/phpunit 37 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) James Brooks james@alt-three.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Colorize 2 | 3 | A mixin for Laravel's `Stringable` to easily apply colors and styles to CLI text. 4 | 5 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/jbrooksuk/laravel-colorize?style=flat-square)](https://packagist.org/packages/jbrooksuk/laravel-colorize) 6 | ![Test](https://github.com/jbrooksuk/laravel-colorize/workflows/Test/badge.svg) 7 | [![Total Downloads](https://img.shields.io/packagist/dt/jbrooksuk/laravel-colorize?style=flat-square)](https://packagist.org/packages/jbrooksuk/laravel-colorize) 8 | 9 | ## Installation 10 | 11 | You can install the package via Composer: 12 | 13 | ``` 14 | composer require jbrooksuk/laravel-colorize 15 | ``` 16 | 17 | ## Usage 18 | 19 | ### `blink` 20 | 21 | Make the text blink. 22 | 23 | ```php 24 | Str::of('Hey Laravel')->blink(); 25 | ``` 26 | 27 | ### `bold` 28 | 29 | Make the text bold. 30 | 31 | ```php 32 | Str::of('Hey Laravel')->bold(); 33 | ``` 34 | 35 | ### `colorize` 36 | 37 | Colorize the text. Foreground, Background. 38 | 39 | ```php 40 | Str::of('Hey Laravel')->colorize('red', 'blue'); 41 | ``` 42 | 43 | ### `conceal` 44 | 45 | Make the text invisible. 46 | 47 | ```php 48 | Str::of('Hey Laravel')->conceal(); 49 | ``` 50 | 51 | ### `reverse` 52 | 53 | Swap the foreground with the background and the background with the foreground. 54 | 55 | ```php 56 | Str::of('Hey Laravel')->colorize('red', 'blue')->reverse(); 57 | ``` 58 | 59 | ### `underscore` 60 | 61 | Make the text underscored. 62 | 63 | ```php 64 | Str::of('Hey Laravel')->underscore(); 65 | ``` 66 | 67 | ## Chaining 68 | 69 | Because Laravel Colorize uses `Stringable`, all of these methods can be chained together. 70 | 71 | ```php 72 | Str::of('Hey Laravel')->colorize('red', 'yellow')->bold()->blink(); 73 | ``` 74 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jbrooksuk/laravel-colorize", 3 | "description": "A handy set of Stringable mixins for CLI text.", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "James Brooks", 9 | "email": "james@alt-three.com", 10 | "homepage": "https://james.brooks.page", 11 | "role": "Developer" 12 | } 13 | ], 14 | "require": { 15 | "php": "^7.3|^8.0", 16 | "illuminate/support": "^8.0", 17 | "symfony/console": "^5.3" 18 | }, 19 | "require-dev": { 20 | "orchestra/testbench": "^6.0", 21 | "phpunit/phpunit": "^9.3" 22 | }, 23 | "autoload": { 24 | "psr-4": { 25 | "JamesBrooks\\Colorize\\": "src" 26 | } 27 | }, 28 | "autoload-dev": { 29 | "psr-4": { 30 | "JamesBrooks\\Colorize\\Tests\\": "tests" 31 | } 32 | }, 33 | "scripts": { 34 | "test": "vendor/bin/phpunit", 35 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 36 | }, 37 | "config": { 38 | "sort-packages": true 39 | }, 40 | "extra": { 41 | "laravel": { 42 | "providers": [ 43 | "JamesBrooks\\Colorize\\ColorizeServiceProvider" 44 | ] 45 | } 46 | }, 47 | "minimum-stability": "dev", 48 | "prefer-stable": true, 49 | "funding": [ 50 | { 51 | "type": "github", 52 | "url": "https://github.com/sponsors/jbrooksuk" 53 | } 54 | ] 55 | } 56 | -------------------------------------------------------------------------------- /src/Colorize.php: -------------------------------------------------------------------------------- 1 | value); 12 | 13 | return new self($colorizeString->setForegroundColor($fgColor)->setBackgroundColor($bgColor)); 14 | }; 15 | } 16 | 17 | public function underscore() 18 | { 19 | return function () { 20 | $colorizeString = new ColorizeString($this->value); 21 | 22 | return new self($colorizeString->underscore()); 23 | }; 24 | } 25 | 26 | public function bold() 27 | { 28 | return function () { 29 | $colorizeString = new ColorizeString($this->value); 30 | 31 | return new self($colorizeString->bold()); 32 | }; 33 | } 34 | 35 | public function conceal() 36 | { 37 | return function () { 38 | $colorizeString = new ColorizeString($this->value); 39 | 40 | return new self($colorizeString->conceal()); 41 | }; 42 | } 43 | 44 | public function reverse() 45 | { 46 | return function () { 47 | $colorizeString = new ColorizeString($this->value); 48 | 49 | return new self($colorizeString->reverse()); 50 | }; 51 | } 52 | 53 | public function blink() 54 | { 55 | return function () { 56 | $colorizeString = new ColorizeString($this->value); 57 | 58 | return new self($colorizeString->blink()); 59 | }; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/ColorizeServiceProvider.php: -------------------------------------------------------------------------------- 1 | value = $value; 24 | } 25 | 26 | /** 27 | * Get the foreground color. 28 | * 29 | * @return string 30 | */ 31 | public function fgColor(): string 32 | { 33 | return $this->fgColor; 34 | } 35 | 36 | /** 37 | * Set the foreground color. 38 | * 39 | * @param string $color 40 | * @return string 41 | */ 42 | public function setForegroundColor(string $color): self 43 | { 44 | $this->fgColor = $color; 45 | 46 | return $this; 47 | } 48 | 49 | /** 50 | * Get the foreground color. 51 | * 52 | * @return string 53 | */ 54 | public function bgColor(): string 55 | { 56 | return $this->bgColor; 57 | } 58 | 59 | /** 60 | * Set the background color. 61 | * 62 | * @param string $color 63 | * @return string 64 | */ 65 | public function setBackgroundColor($color): self 66 | { 67 | $this->bgColor = $color; 68 | 69 | return $this; 70 | } 71 | 72 | /** 73 | * Set the options. 74 | * 75 | * @param array $options 76 | * @return string 77 | */ 78 | public function setOptions(array $options): self 79 | { 80 | $this->options = $options; 81 | 82 | return $this; 83 | } 84 | 85 | /** 86 | * Underscore the value. 87 | * 88 | * @return string 89 | */ 90 | public function underscore(): string 91 | { 92 | $this->options[] = 'underscore'; 93 | 94 | return (string) $this; 95 | } 96 | 97 | /** 98 | * Bold the value. 99 | * 100 | * @return string 101 | */ 102 | public function bold(): string 103 | { 104 | $this->options[] = 'bold'; 105 | 106 | return (string) $this; 107 | } 108 | 109 | /** 110 | * Conceal the value. 111 | * 112 | * @return string 113 | */ 114 | public function conceal(): string 115 | { 116 | $this->options[] = 'conceal'; 117 | 118 | return (string) $this; 119 | } 120 | 121 | /** 122 | * Blink the value. 123 | * 124 | * @return string 125 | */ 126 | public function blink(): string 127 | { 128 | $this->options[] = 'blink'; 129 | 130 | return (string) $this; 131 | } 132 | 133 | /** 134 | * Reverse the colors. 135 | * 136 | * @return string 137 | */ 138 | public function reverse(): string 139 | { 140 | $this->options[] = 'reverse'; 141 | 142 | return (string) $this; 143 | } 144 | 145 | /** 146 | * Create a new string instance of the Color. 147 | * 148 | * @return string 149 | */ 150 | public function __toString(): string 151 | { 152 | $color = new Color($this->fgColor, $this->bgColor, $this->options); 153 | 154 | return $color->apply($this->value); 155 | } 156 | } 157 | --------------------------------------------------------------------------------