211 lines
7.3 KiB
Dart

// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_cli_toolbox/src/stylish/utils.dart';
extension Stylish on String {
/// Status of the extension
static bool enabled = true;
/// Formats a string if ansi escape sequences are supported.
static bool forced = false;
/// Enable or disable styling
static void config({bool? enable}) {
Stylish.enabled = enable ?? Stylish.enabled;
}
// Foreground Colors
/// Format this string with ANSI to be colored black.
String black() => format(30, 39)(this);
/// Format this string with ANSI to be colored red.
String red() => format(31, 39)(this);
/// Format this string with ANSI to be colored green.
String green() => format(32, 39)(this);
/// Format this string with ANSI to be colored yellow.
String yellow() => format(33, 39)(this);
/// Format this string with ANSI to be colored blue.
String blue() => format(34, 39)(this);
/// Format this string with ANSI to be colored magenta.
String magenta() => format(35, 39)(this);
/// Format this string with ANSI to be colored cyan.
String cyan() => format(36, 39)(this);
/// Format this string with ANSI to be colored white.
String white() => format(37, 39)(this);
/// Format this string with ANSI to be colored bright black or gray or grey.
String brightBlack() => format(90, 39)(this);
/// Format this string with ANSI to be colored bright red.
String brightRed() => format(91, 39)(this);
/// Format this string with ANSI to be colored bright green.
String brightGreen() => format(92, 39)(this);
/// Format this string with ANSI to be colored bright yellow.
String brightYellow() => format(93, 39)(this);
/// Format this string with ANSI to be colored bright blue.
String brightBlue() => format(94, 39)(this);
/// Format this string with ANSI to be colored bright magenta.
String brightMagenta() => format(95, 39)(this);
/// Format this string with ANSI to be colored bright cyan.
String brightCyan() => format(96, 39)(this);
/// Format this string with ANSI to be colored bright white.
String brightWhite() => format(97, 39)(this);
// Background Colors
/// Format this string with ANSI adding a black background color.
String bgBlack() => format(40, 49)(this);
/// Format this string with ANSI adding a red background color.
String bgRed() => format(41, 49)(this);
/// Format this string with ANSI adding a green background color.
String bgGreen() => format(42, 49)(this);
/// Format this string with ANSI adding a yellow background color.
String bgYellow() => format(43, 49)(this);
/// Format this string with ANSI adding a blue background color.
String bgBlue() => format(44, 49)(this);
/// Format this string with ANSI adding a magenta background color.
String bgMagenta() => format(45, 49)(this);
/// Format this string with ANSI adding a cyan background color.
String bgCyan() => format(46, 49)(this);
/// Format this string with ANSI adding a white background color.
String bgWhite() => format(47, 49)(this);
/// Format this string with ANSI adding a bright black
/// or gray or grey background color.
String bgBrightBlack() => format(100, 49)(this);
/// Format this string with ANSI adding a bright red
/// background color.
String bgBrightRed() => format(101, 49)(this);
/// Format this string with ANSI adding a bright green
/// background color.
String bgBrightGreen() => format(102, 49)(this);
/// Format this string with ANSI adding a bright yellow
/// background color.
String bgBrightYellow() => format(103, 49)(this);
/// Format this string with ANSI adding a bright blue
/// background color.
String bgBrightBlue() => format(104, 49)(this);
/// Format this string with ANSI adding a bright magenta
/// background color.
String bgBrightMagenta() => format(105, 49)(this);
/// Format this string with ANSI adding a bright cyan
/// background color.
String bgBrightCyan() => format(106, 49)(this);
/// Format this string with ANSI adding a bright white
/// background color.
String bgBrightWhite() => format(107, 49)(this);
/// Format this string with ANSI to be colored bright black or gray or grey.
String gray() => brightBlack();
/// Format this string with ANSI to be colored bright black or gray or grey.
String grey() => brightBlack();
/// Format this string with ANSI adding a bright black
/// or gray or grey background color.
String bgGray() => bgBrightBlack();
/// Format this string with ANSI adding a bright black
/// or gray or grey background color.
String bgGrey() => bgBrightBlack();
/// Format this string with ANSI to be styled as a bold text.
String bold() => format(1, 22)(this);
/// Format this string with ANSI to make it appear as a dimmed text.
String dim() => format(2, 22)(this);
/// Format this string with ANSI to make it appear italic.
String italic() => format(3, 23)(this);
/// Format this string with ANSI adding an underline.
String underline() => format(4, 24)(this);
/// Format this string with ANSI making it appear blinking slowly.
String blink() => format(5, 25)(this);
/// Format this string with ANSI making it appear blinking rapidly.
String strobe() => format(6, 25)(this);
/// Format this string with ANSI inverting the style.
String inverse() => format(7, 27)(this);
/// Format this string with ANSI making it invisible.
String hidden() => format(8, 28)(this);
/// Format this string with ANSI adding a strike through the text.
String strikethrough() => format(9, 29)(this);
/// Format this string with ANSI adding a frame.
String frame() => format(51, 54)(this);
/// Format this string with ANSI adding a circle.
String encircle() => format(52, 54)(this);
/// Format this string with ANSI adding an overline.
String overline() => format(53, 55)(this);
/// Format this string with ANSI resetting the previous color chains.
String reset() => format(0, 0)(this);
/// If exists, all ANSI sequences will be removed from this string.
String strip() {
return replaceAll(ansiPattern, '');
}
/// Format this string with ANSI setting it's foreground color the
/// value defined as RGB parameters.
///
/// [r]/[g]/[b] parameters can be an [int] in the range of `0` to `255`.
String fg({int r = 255, int g = 255, int b = 255}) {
return format('38;5;${rgbToAnsiCode(r, g, b)}', 0)(this);
}
/// Format this string with ANSI setting it's background color the value
/// defined as RGB parameters.
///
/// [r]/[g]/[b] parameters can be an [int] in the range of `0` to `255`.
String bg({int r = 255, int g = 255, int b = 255}) {
return format('48;5;${rgbToAnsiCode(r, g, b)}', 0)(this);
}
}