format function
String
format(
- String input, {
- List<
String> ? args, - Map<
String, String> ? replace, - VariableStyle? style,
- VariableResolver? resolver,
Implementation
String format(String input,
{List<String>? args,
Map<String, String>? replace,
VariableStyle? style,
VariableResolver? resolver}) {
style ??= VariableStyle.DEFAULT;
if (Zone.current['console.format.variable_style'] != null) {
style = Zone.current['console.format.variable_style'];
}
var out = input;
var allKeys = style!.findVariables(input);
for (var id in allKeys) {
if (args != null) {
try {
var index = int.parse(id);
if (index < 0 || index > args.length - 1) {
throw RangeError.range(index, 0, args.length - 1);
}
out = style.replace(out, '${index}', args[index]);
continue;
// ignore: empty_catches
} on FormatException {}
}
if (replace != null && replace.containsKey(id)) {
out = style.replace(out, id, replace[id]!);
continue;
}
if (id.startsWith('@') || id.startsWith('color.')) {
var color = id.startsWith('@') ? id.substring(1) : id.substring(6);
if (color.isEmpty) {
throw Exception('color directive requires an argument');
}
if (_COLORS.containsKey(color)) {
out = style.replace(out, '${id}', _COLORS[color].toString());
continue;
}
if (color == 'normal' || color == 'end') {
out = style.replace(out, id, '${Console.ANSI_ESCAPE}0m');
continue;
}
}
if (id.startsWith('env.')) {
var envVariable = id.substring(4);
if (envVariable.isEmpty) {
throw Exception('Unknown Key: ${id}');
}
var value = Platform.environment[envVariable];
value ??= '';
out = style.replace(out, id, value);
continue;
}
if (id.startsWith('platform.')) {
var variable = id.substring(9);
if (variable.isEmpty) {
throw Exception('Unknown Key: ${id}');
}
var value = _resolvePlatformVariable(variable);
out = style.replace(out, id, value);
continue;
}
if (resolver != null) {
var value = resolver(id);
out = style.replace(out, id, value);
} else {
throw Exception('Unknown Key: ${id}');
}
}
return out;
}