addFillCaractere method
List<String>
addFillCaractere({
- required String primaryText,
- String fill = ' ',
- String? secondaryText,
- LoggerAlignmentState? alignment,
Adds fill characters to a string to match a maximum length, depending on the primary and secondary text and alignment. If the secondary text is not specified, it is set to an empty string.
Throws an exception if the alignment is "between" and there is no secondary text.
Returns a list of strings, where the first string is the filled space on the left side of the text, the second string is the filled space in the center, and the third string is the filled space on the right side of the text.
Implementation
List<String> addFillCaractere(
{required String primaryText,
String fill = ' ',
String? secondaryText,
LoggerAlignmentState? alignment}) {
secondaryText ??= '';
alignment ??= LoggerAlignmentState.left;
// Error if there is no secondary text in between alignment
if (alignment == LoggerAlignmentState.between && secondaryText.isEmpty) {
throw Exception("You must enter a secondary message !");
}
final fillCount =
parameters.maxLength - primaryText.length - secondaryText.length;
List<String> spaceLeft = [];
List<String> spaceRight = [];
List<String> spaceCenter = [];
if (alignment == LoggerAlignmentState.center) {
spaceLeft = List.filled(fillCount ~/ 2, fill);
spaceRight = List.filled(fillCount - spaceLeft.length, fill);
} else if (alignment == LoggerAlignmentState.left) {
if (primaryText.isNotEmpty) {
spaceLeft = [" "];
}
spaceRight =
List.filled(fillCount - spaceLeft.length - spaceCenter.length, fill);
} else if (alignment == LoggerAlignmentState.right) {
if (primaryText.isNotEmpty) {
spaceRight = [" "];
spaceCenter = [" "];
}
spaceLeft =
List.filled(fillCount - spaceCenter.length - spaceRight.length, fill);
} else if (alignment == LoggerAlignmentState.between) {
spaceLeft = [" "];
spaceRight = [" "];
spaceCenter =
List.filled(fillCount - spaceLeft.length - spaceRight.length, fill);
}
return [spaceLeft.join(), spaceCenter.join(), spaceRight.join()];
}