summaryrefslogtreecommitdiff
path: root/lib/ansi.php
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2024-01-31 06:11:00 +0300
committerEvgeny Zinoviev <me@ch1p.io>2024-01-31 20:45:40 +0300
commitc0dc531ebefd8912819f3b6c8bda1fed3c7e750c (patch)
tree2c75aa9df182260aef09faf4befd81a4c2b9c5e2 /lib/ansi.php
parent48d688cdf7f9eae1bf11b8a6f0e5b98687c604cb (diff)
make it simple, but not simpler
Diffstat (limited to 'lib/ansi.php')
-rw-r--r--lib/ansi.php32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/ansi.php b/lib/ansi.php
new file mode 100644
index 0000000..9e0a425
--- /dev/null
+++ b/lib/ansi.php
@@ -0,0 +1,32 @@
+<?php
+
+enum AnsiColor: int {
+ case BLACK = 0;
+ case RED = 1;
+ case GREEN = 2;
+ case YELLOW = 3;
+ case BLUE = 4;
+ case MAGENTA = 5;
+ case CYAN = 6;
+ case WHITE = 7;
+}
+
+function ansi(string $text,
+ ?AnsiColor $fg = null,
+ ?AnsiColor $bg = null,
+ bool $bold = false,
+ bool $fg_bright = false,
+ bool $bg_bright = false): string {
+ $codes = [];
+ if (!is_null($fg))
+ $codes[] = $fg->value + ($fg_bright ? 90 : 30);
+ if (!is_null($bg))
+ $codes[] = $bg->value + ($bg_bright ? 100 : 40);
+ if ($bold)
+ $codes[] = 1;
+
+ if (empty($codes))
+ return $text;
+
+ return "\033[".implode(';', $codes)."m".$text."\033[0m";
+} \ No newline at end of file