46 lines
1.7 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/stylish.dart';
/// Converts an rgb value of given [r], [g] and [b] int values
/// to an ANSI usable color.
int rgbToAnsiCode(int r, int g, int b) =>
(((r.clamp(0, 255) / 255) * 5).toInt() * 36 +
((g.clamp(0, 255) / 255) * 5).toInt() * 6 +
((b.clamp(0, 255) / 255) * 5).toInt() +
16)
.clamp(0, 256);
/// Regular Expression pattern for all possible types of ANSI escape
/// sequences in a [String].
final RegExp ansiPattern = RegExp(
<String>[
r'[\u001B\u009B][[\]()#;?]*(?:(?:(?:[a-zA-Z\d]*(?:;[-a-zA-Z\d\/#&.:=?%@~_]*)*)?\u0007)',
r'(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-ntqry=><~]))'
].join('|'),
);
/// Formats a string if ansi escape sequences are supported.
String Function(String) format(dynamic start, dynamic end) => (String x) {
// if ((supportsAnsiColor || Stylish.forced) && Stylish.enabled) {
if (Stylish.enabled) {
return '\x1B[${start}m$x\x1B[${end}m';
} else {
return x;
}
};