Passed
Push — develop ( add880...26f5dd )
by nguereza
02:36
created

Writer::dim()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 4
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Console
5
 *
6
 * Platine Console is a powerful library with support of custom
7
 * style to build command line interface applications
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Console
12
 * Copyright (c) 2017-2020 Jitendra Adhikari
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy
15
 * of this software and associated documentation files (the "Software"), to deal
16
 * in the Software without restriction, including without limitation the rights
17
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
 * copies of the Software, and to permit persons to whom the Software is
19
 * furnished to do so, subject to the following conditions:
20
 *
21
 * The above copyright notice and this permission notice shall be included in all
22
 * copies or substantial portions of the Software.
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
 * SOFTWARE.
31
 */
32
33
/**
34
 *  @file Writer.php
35
 *
36
 *  The Output Writer class
37
 *
38
 *  @package    Platine\Console\Output
39
 *  @author Platine Developers Team
40
 *  @copyright  Copyright (c) 2020
41
 *  @license    http://opensource.org/licenses/MIT  MIT License
42
 *  @link   http://www.iacademy.cf
43
 *  @version 1.0.0
44
 *  @filesource
45
 */
46
47
declare(strict_types=1);
48
49
namespace Platine\Console\Output;
50
51
/**
52
 * Class Writer
53
 * @package Platine\Console\Output
54
 */
55
class Writer
56
{
57
58
    /**
59
     * The output stream
60
     * @var resource
61
     */
62
    protected $stream;
63
64
    /**
65
     * The output error stream
66
     * @var resource
67
     */
68
    protected $errorStream;
69
70
    /**
71
     * Color method to be called
72
     * @var string
73
     */
74
    protected string $method;
75
76
    /**
77
     * The color instance
78
     * @var Color
79
     */
80
    protected Color $color;
81
82
    /**
83
     * The cursor instance
84
     * @var Cursor
85
     */
86
    protected Cursor $cursor;
87
88
    /**
89
     * Create new instance
90
     * @param string|null $path the output write path
91
     * @param Color|null $color the color instance
92
     */
93
    public function __construct(?string $path = null, ?Color $color = null)
94
    {
95
        $stream = null;
96
        if ($path !== null) {
97
            $stream = fopen($path, 'w');
98
        }
99
100
        $this->stream = $stream ? $stream : STDOUT;
0 ignored issues
show
introduced by
$stream is of type null|resource, thus it always evaluated to false.
Loading history...
101
        $this->errorStream = $stream ? $stream : STDERR;
0 ignored issues
show
introduced by
$stream is of type null|resource, thus it always evaluated to false.
Loading history...
102
103
        $this->color = $color ? $color : new Color();
104
        $this->cursor = new Cursor();
105
    }
106
107
    /**
108
     * Return the color instance
109
     * @return Color
110
     */
111
    public function getColor(): Color
112
    {
113
        return $this->color;
114
    }
115
116
    /**
117
     * Return the cursor instance
118
     * @return Cursor
119
     */
120
    public function getCursor(): Cursor
121
    {
122
        return $this->cursor;
123
    }
124
125
    /**
126
     * Write the formatted text to standard output.
127
     * @param string $text
128
     * @param bool $eol
129
     * @return $this
130
     */
131
    public function write(string $text, bool $eol = false): self
132
    {
133
        $styledText = $this->color->line($text);
134
        if ($eol) {
135
            $styledText .= PHP_EOL;
136
        }
137
138
        return $this->doWrite($styledText, false);
139
    }
140
141
    /**
142
     * Write the formatted text to standard error.
143
     * @param string $text
144
     * @param bool $eol
145
     * @return $this
146
     */
147
    public function writeError(string $text, bool $eol = false): self
148
    {
149
        $styledText = $this->color->red($text);
150
        if ($eol) {
151
            $styledText .= PHP_EOL;
152
        }
153
154
        return $this->doWrite($styledText, true);
155
    }
156
157
    /**
158
     * Write the formatted text to standard output using custom style.
159
     * @param string $text
160
     * @param bool $eol
161
     * @param int|null $fg
162
     * @param int|null $bg
163
     * @param int|null $mode
164
     * @return $this
165
     */
166
    public function line(
167
        string $text,
168
        bool $eol = false,
169
        ?int $fg = Color::WHITE,
170
        ?int $bg = null,
171
        ?int $mode = null
172
    ): self {
173
        $styledText = $this->color->line($text, $fg, $bg, $mode);
174
        if ($eol) {
175
            $styledText .= PHP_EOL;
176
        }
177
178
        return $this->doWrite($styledText, false);
179
    }
180
181
    /**
182
     * Write the formatted text to standard error
183
     *  using custom style.
184
     * @param string $text
185
     * @param bool $eol
186
     * @param int|null $fg
187
     * @param int|null $bg
188
     * @param int|null $mode
189
     * @return $this
190
     */
191
    public function lineError(
192
        string $text,
193
        bool $eol = false,
194
        ?int $fg = Color::WHITE,
195
        ?int $bg = null,
196
        ?int $mode = null
197
    ): self {
198
        $styledText = $this->color->line($text, $fg, $bg, $mode);
199
        if ($eol) {
200
            $styledText .= PHP_EOL;
201
        }
202
203
        return $this->doWrite($styledText, true);
204
    }
205
206
    /**
207
     * Write the formatted text with RED color to standard output
208
     * @param string $text
209
     * @param bool $eol
210
     * @param int|null $bg
211
     * @param int|null $mode
212
     * @return $this
213
     */
214
    public function red(
215
        string $text,
216
        bool $eol = false,
217
        ?int $bg = null,
218
        ?int $mode = null
219
    ): self {
220
        $styledText = $this->color->red($text, $bg, $mode);
221
        if ($eol) {
222
            $styledText .= PHP_EOL;
223
        }
224
225
        return $this->doWrite($styledText, false);
226
    }
227
228
    /**
229
     * Write the formatted text with black color to standard output
230
     * @param string $text
231
     * @param bool $eol
232
     * @param int|null $bg
233
     * @param int|null $mode
234
     * @return $this
235
     */
236
    public function black(
237
        string $text,
238
        bool $eol = false,
239
        ?int $bg = null,
240
        ?int $mode = null
241
    ): self {
242
        $styledText = $this->color->black($text, $bg, $mode);
243
        if ($eol) {
244
            $styledText .= PHP_EOL;
245
        }
246
247
        return $this->doWrite($styledText, false);
248
    }
249
250
    /**
251
     * Write the formatted text with green color to standard output
252
     * @param string $text
253
     * @param bool $eol
254
     * @param int|null $bg
255
     * @param int|null $mode
256
     * @return $this
257
     */
258
    public function green(
259
        string $text,
260
        bool $eol = false,
261
        ?int $bg = null,
262
        ?int $mode = null
263
    ): self {
264
        $styledText = $this->color->green($text, $bg, $mode);
265
        if ($eol) {
266
            $styledText .= PHP_EOL;
267
        }
268
269
        return $this->doWrite($styledText, false);
270
    }
271
272
    /**
273
     * Write the formatted text with yellow color to standard output
274
     * @param string $text
275
     * @param bool $eol
276
     * @param int|null $bg
277
     * @param int|null $mode
278
     * @return $this
279
     */
280
    public function yellow(
281
        string $text,
282
        bool $eol = false,
283
        ?int $bg = null,
284
        ?int $mode = null
285
    ): self {
286
        $styledText = $this->color->yellow($text, $bg, $mode);
287
        if ($eol) {
288
            $styledText .= PHP_EOL;
289
        }
290
291
        return $this->doWrite($styledText, false);
292
    }
293
294
    /**
295
     * Write the formatted text with blue color to standard output
296
     * @param string $text
297
     * @param bool $eol
298
     * @param int|null $bg
299
     * @param int|null $mode
300
     * @return $this
301
     */
302
    public function blue(
303
        string $text,
304
        bool $eol = false,
305
        ?int $bg = null,
306
        ?int $mode = null
307
    ): self {
308
        $styledText = $this->color->blue($text, $bg, $mode);
309
        if ($eol) {
310
            $styledText .= PHP_EOL;
311
        }
312
313
        return $this->doWrite($styledText, false);
314
    }
315
316
    /**
317
     * Write the formatted text with purple color to standard output
318
     * @param string $text
319
     * @param bool $eol
320
     * @param int|null $bg
321
     * @param int|null $mode
322
     * @return $this
323
     */
324
    public function purple(
325
        string $text,
326
        bool $eol = false,
327
        ?int $bg = null,
328
        ?int $mode = null
329
    ): self {
330
        $styledText = $this->color->purple($text, $bg, $mode);
331
        if ($eol) {
332
            $styledText .= PHP_EOL;
333
        }
334
335
        return $this->doWrite($styledText, false);
336
    }
337
338
    /**
339
     * Write the formatted text with cyan color to standard output
340
     * @param string $text
341
     * @param bool $eol
342
     * @param int|null $bg
343
     * @param int|null $mode
344
     * @return $this
345
     */
346
    public function cyan(
347
        string $text,
348
        bool $eol = false,
349
        ?int $bg = null,
350
        ?int $mode = null
351
    ): self {
352
        $styledText = $this->color->cyan($text, $bg, $mode);
353
        if ($eol) {
354
            $styledText .= PHP_EOL;
355
        }
356
357
        return $this->doWrite($styledText, false);
358
    }
359
360
    /**
361
     * Write the formatted text with white color to standard output
362
     * @param string $text
363
     * @param bool $eol
364
     * @param int|null $bg
365
     * @param int|null $mode
366
     * @return $this
367
     */
368
    public function white(
369
        string $text,
370
        bool $eol = false,
371
        ?int $bg = null,
372
        ?int $mode = null
373
    ): self {
374
        $styledText = $this->color->white($text, $bg, $mode);
375
        if ($eol) {
376
            $styledText .= PHP_EOL;
377
        }
378
379
        return $this->doWrite($styledText, false);
380
    }
381
382
    /**
383
     * Write the formatted text with gray color to standard output
384
     * @param string $text
385
     * @param bool $eol
386
     * @param int|null $bg
387
     * @param int|null $mode
388
     * @return $this
389
     */
390
    public function gray(
391
        string $text,
392
        bool $eol = false,
393
        ?int $bg = null,
394
        ?int $mode = null
395
    ): self {
396
        $styledText = $this->color->gray($text, $bg, $mode);
397
        if ($eol) {
398
            $styledText .= PHP_EOL;
399
        }
400
401
        return $this->doWrite($styledText, false);
402
    }
403
404
    /**
405
     * Write the formatted text with dark gray color to standard output
406
     * @param string $text
407
     * @param bool $eol
408
     * @param int|null $bg
409
     * @param int|null $mode
410
     * @return $this
411
     */
412
    public function darkgray(
413
        string $text,
414
        bool $eol = false,
415
        ?int $bg = null,
416
        ?int $mode = null
417
    ): self {
418
        $styledText = $this->color->darkgray($text, $bg, $mode);
419
        if ($eol) {
420
            $styledText .= PHP_EOL;
421
        }
422
423
        return $this->doWrite($styledText, false);
424
    }
425
426
    /**
427
     * Write the formatted text with gray background color to standard output
428
     * @param string $text
429
     * @param bool $eol
430
     * @param int|null $fg
431
     * @param int|null $mode
432
     * @return $this
433
     */
434
    public function bgRed(
435
        string $text,
436
        bool $eol = false,
437
        ?int $fg = null,
438
        ?int $mode = null
439
    ): self {
440
        $styledText = $this->color->bgRed($text, $fg, $mode);
441
        if ($eol) {
442
            $styledText .= PHP_EOL;
443
        }
444
445
        return $this->doWrite($styledText, false);
446
    }
447
448
    /**
449
     * Write the formatted text with black background color to standard output
450
     * @param string $text
451
     * @param bool $eol
452
     * @param int|null $fg
453
     * @param int|null $mode
454
     * @return $this
455
     */
456
    public function bgBlack(
457
        string $text,
458
        bool $eol = false,
459
        ?int $fg = null,
460
        ?int $mode = null
461
    ): self {
462
        $styledText = $this->color->bgBlack($text, $fg, $mode);
463
        if ($eol) {
464
            $styledText .= PHP_EOL;
465
        }
466
467
        return $this->doWrite($styledText, false);
468
    }
469
470
    /**
471
     * Write the formatted text with green background color to standard output
472
     * @param string $text
473
     * @param bool $eol
474
     * @param int|null $fg
475
     * @param int|null $mode
476
     * @return $this
477
     */
478
    public function bgGreen(
479
        string $text,
480
        bool $eol = false,
481
        ?int $fg = null,
482
        ?int $mode = null
483
    ): self {
484
        $styledText = $this->color->bgGreen($text, $fg, $mode);
485
        if ($eol) {
486
            $styledText .= PHP_EOL;
487
        }
488
489
        return $this->doWrite($styledText, false);
490
    }
491
492
    /**
493
     * Write the formatted text with yellow background color to standard output
494
     * @param string $text
495
     * @param bool $eol
496
     * @param int|null $fg
497
     * @param int|null $mode
498
     * @return $this
499
     */
500
    public function bgYellow(
501
        string $text,
502
        bool $eol = false,
503
        ?int $fg = null,
504
        ?int $mode = null
505
    ): self {
506
        $styledText = $this->color->bgYellow($text, $fg, $mode);
507
        if ($eol) {
508
            $styledText .= PHP_EOL;
509
        }
510
511
        return $this->doWrite($styledText, false);
512
    }
513
514
    /**
515
     * Write the formatted text with blue background color to standard output
516
     * @param string $text
517
     * @param bool $eol
518
     * @param int|null $fg
519
     * @param int|null $mode
520
     * @return $this
521
     */
522
    public function bgBlue(
523
        string $text,
524
        bool $eol = false,
525
        ?int $fg = null,
526
        ?int $mode = null
527
    ): self {
528
        $styledText = $this->color->bgBlue($text, $fg, $mode);
529
        if ($eol) {
530
            $styledText .= PHP_EOL;
531
        }
532
533
        return $this->doWrite($styledText, false);
534
    }
535
536
    /**
537
     * Write the formatted text with purple background color to standard output
538
     * @param string $text
539
     * @param bool $eol
540
     * @param int|null $fg
541
     * @param int|null $mode
542
     * @return $this
543
     */
544
    public function bgPurple(
545
        string $text,
546
        bool $eol = false,
547
        ?int $fg = null,
548
        ?int $mode = null
549
    ): self {
550
        $styledText = $this->color->bgPurple($text, $fg, $mode);
551
        if ($eol) {
552
            $styledText .= PHP_EOL;
553
        }
554
555
        return $this->doWrite($styledText, false);
556
    }
557
558
    /**
559
     * Write the formatted text with cyan background color to standard output
560
     * @param string $text
561
     * @param bool $eol
562
     * @param int|null $fg
563
     * @param int|null $mode
564
     * @return $this
565
     */
566
    public function bgCyan(
567
        string $text,
568
        bool $eol = false,
569
        ?int $fg = null,
570
        ?int $mode = null
571
    ): self {
572
        $styledText = $this->color->bgCyan($text, $fg, $mode);
573
        if ($eol) {
574
            $styledText .= PHP_EOL;
575
        }
576
577
        return $this->doWrite($styledText, false);
578
    }
579
580
    /**
581
     * Write the formatted text with white background color to standard output
582
     * @param string $text
583
     * @param bool $eol
584
     * @param int|null $fg
585
     * @param int|null $mode
586
     * @return $this
587
     */
588
    public function bgWhite(
589
        string $text,
590
        bool $eol = false,
591
        ?int $fg = null,
592
        ?int $mode = null
593
    ): self {
594
        $styledText = $this->color->bgWhite($text, $fg, $mode);
595
        if ($eol) {
596
            $styledText .= PHP_EOL;
597
        }
598
599
        return $this->doWrite($styledText, false);
600
    }
601
602
    /**
603
     * Write the formatted text with gray background color to standard output
604
     * @param string $text
605
     * @param bool $eol
606
     * @param int|null $fg
607
     * @param int|null $mode
608
     * @return $this
609
     */
610
    public function bgGray(
611
        string $text,
612
        bool $eol = false,
613
        ?int $fg = null,
614
        ?int $mode = null
615
    ): self {
616
        $styledText = $this->color->bgGray($text, $fg, $mode);
617
        if ($eol) {
618
            $styledText .= PHP_EOL;
619
        }
620
621
        return $this->doWrite($styledText, false);
622
    }
623
624
    /**
625
     * Write the formatted text with dark gray background color to standard output
626
     * @param string $text
627
     * @param bool $eol
628
     * @param int|null $fg
629
     * @param int|null $mode
630
     * @return $this
631
     */
632
    public function bgDarkgray(
633
        string $text,
634
        bool $eol = false,
635
        ?int $fg = null,
636
        ?int $mode = null
637
    ): self {
638
        $styledText = $this->color->bgDarkgray($text, $fg, $mode);
639
        if ($eol) {
640
            $styledText .= PHP_EOL;
641
        }
642
643
        return $this->doWrite($styledText, false);
644
    }
645
646
    /**
647
     * Write the formatted bold text to standard output
648
     * @param string $text
649
     * @param bool $eol
650
     * @param int|null $fg
651
     * @param int|null $bg
652
     * @return $this
653
     */
654
    public function bold(
655
        string $text,
656
        bool $eol = false,
657
        ?int $fg = null,
658
        ?int $bg = null
659
    ): self {
660
        $styledText = $this->color->bold($text, $fg, $bg);
661
        if ($eol) {
662
            $styledText .= PHP_EOL;
663
        }
664
665
        return $this->doWrite($styledText, false);
666
    }
667
668
    /**
669
     * Write the formatted dim text to standard output
670
     * @param string $text
671
     * @param bool $eol
672
     * @param int|null $fg
673
     * @param int|null $bg
674
     * @return $this
675
     */
676
    public function dim(
677
        string $text,
678
        bool $eol = false,
679
        ?int $fg = null,
680
        ?int $bg = null
681
    ): self {
682
        $styledText = $this->color->dim($text, $fg, $bg);
683
        if ($eol) {
684
            $styledText .= PHP_EOL;
685
        }
686
687
        return $this->doWrite($styledText, false);
688
    }
689
690
    /**
691
     * Write the formatted italic text to standard output
692
     * @param string $text
693
     * @param bool $eol
694
     * @param int|null $fg
695
     * @param int|null $bg
696
     * @return $this
697
     */
698
    public function italic(
699
        string $text,
700
        bool $eol = false,
701
        ?int $fg = null,
702
        ?int $bg = null
703
    ): self {
704
        $styledText = $this->color->italic($text, $fg, $bg);
705
        if ($eol) {
706
            $styledText .= PHP_EOL;
707
        }
708
709
        return $this->doWrite($styledText, false);
710
    }
711
712
    /**
713
     * Write the formatted underline text to standard output
714
     * @param string $text
715
     * @param bool $eol
716
     * @param int|null $fg
717
     * @param int|null $bg
718
     * @return $this
719
     */
720
    public function underline(
721
        string $text,
722
        bool $eol = false,
723
        ?int $fg = null,
724
        ?int $bg = null
725
    ): self {
726
        $styledText = $this->color->underline($text, $fg, $bg);
727
        if ($eol) {
728
            $styledText .= PHP_EOL;
729
        }
730
731
        return $this->doWrite($styledText, false);
732
    }
733
734
    /**
735
     * Write the formatted blink text to standard output
736
     * @param string $text
737
     * @param bool $eol
738
     * @param int|null $fg
739
     * @param int|null $bg
740
     * @return $this
741
     */
742
    public function blink(
743
        string $text,
744
        bool $eol = false,
745
        ?int $fg = null,
746
        ?int $bg = null
747
    ): self {
748
        $styledText = $this->color->blink($text, $fg, $bg);
749
        if ($eol) {
750
            $styledText .= PHP_EOL;
751
        }
752
753
        return $this->doWrite($styledText, false);
754
    }
755
756
    /**
757
     * Write the formatted reverse text to standard output
758
     * @param string $text
759
     * @param bool $eol
760
     * @param int|null $fg
761
     * @param int|null $bg
762
     * @return $this
763
     */
764
    public function reverse(
765
        string $text,
766
        bool $eol = false,
767
        ?int $fg = null,
768
        ?int $bg = null
769
    ): self {
770
        $styledText = $this->color->reverse($text, $fg, $bg);
771
        if ($eol) {
772
            $styledText .= PHP_EOL;
773
        }
774
775
        return $this->doWrite($styledText, false);
776
    }
777
778
    /**
779
     * Write the formatted hidden text to standard output
780
     * @param string $text
781
     * @param bool $eol
782
     * @param int|null $fg
783
     * @param int|null $bg
784
     * @return $this
785
     */
786
    public function hidden(
787
        string $text,
788
        bool $eol = false,
789
        ?int $fg = null,
790
        ?int $bg = null
791
    ): self {
792
        $styledText = $this->color->hidden($text, $fg, $bg);
793
        if ($eol) {
794
            $styledText .= PHP_EOL;
795
        }
796
797
        return $this->doWrite($styledText, false);
798
    }
799
800
    /**
801
     * Write formatted text in HTML style
802
     * @see Color::colors
803
     *
804
     * @param string $text
805
     * @param bool $eol
806
     * @return $this
807
     */
808
    public function colors(string $text, bool $eol = false): self
809
    {
810
        $styledText = $this->color->colors($text);
811
        if ($eol) {
812
            $styledText .= PHP_EOL;
813
        }
814
815
        return $this->doWrite($styledText, false);
816
    }
817
818
    /**
819
     * Write the raw text to stream.
820
     * @param string $text
821
     * @param bool $error
822
     * @return $this
823
     */
824
    public function raw(string $text, bool $error = false): self
825
    {
826
        return $this->doWrite($text, $error);
827
    }
828
829
    /**
830
     * Generate table for the console
831
     * @param array<int, array<int, string>> $rows
832
     * @param array<string, string> $styles
833
     * @return self
834
     */
835
    public function table(array $rows, array $styles = []): self
836
    {
837
        $table = (new Table())->render($rows, $styles);
838
        $output = $this->color->colors($table);
839
840
        $this->write($output, true);
841
842
        return $this;
843
    }
844
845
    /**
846
     * Write EOL count times.
847
     * @param int $count
848
     * @return $this
849
     */
850
    public function eol(int $count = 1): self
851
    {
852
        return $this->doWrite(str_repeat(PHP_EOL, max($count, 1)), false);
853
    }
854
855
    /**
856
     * Write to the standard output or standard error stream.
857
     * @param string $text
858
     * @param bool $error
859
     * @return $this
860
     */
861
    protected function doWrite(string $text, bool $error = false): self
862
    {
863
        $stream = $error ? $this->errorStream : $this->stream;
864
865
        fwrite($stream, $text);
866
867
        return $this;
868
    }
869
}
870