├── .github ├── FUNDING.yml └── workflows │ └── test.yml ├── src ├── ColorizeServiceProvider.php ├── Colorize.php └── ColorizeString.php ├── LICENSE.md ├── composer.json └── README.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: jbrooksuk 2 | -------------------------------------------------------------------------------- /src/ColorizeServiceProvider.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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/ColorizeString.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 | --------------------------------------------------------------------------------