displayPassword function

void displayPassword(
  1. Password password, {
  2. bool useColor = true,
})

Display on terminal result of makePassword

Implementation

void displayPassword(Password password, {bool useColor = true}) {
  ansiColorDisabled = false;
  final sb = StringBuffer();
  final pen = AnsiPen();
  //print(password.toString());

  // Without color just print password
  if (!useColor) {
    if (password.password.first.runes.length > 1) {
      password.separator = _noBreakSpace();
    }
    print(password.toString());
    return;
  }

  // With color setup color and print
  for (var items in password.password) {
    // Use runes.length instead of length otherwise some char length
    // can be egal to 2.
    // the test distinguishes the password and passphrase
    if (items.runes.length > 1) {
      sb.write(_noBreakSpace());
      pen.green();
      sb.write(pen.write(items));
    } else {
      if (items.contains(RegExp(r'[0-9]'))) {
        pen.blue();
        sb.write(pen.write(items));
      } else if (items.contains(RegExp(r'[a-z]'))) {
        pen.white();
        sb.write(pen.write(items));
      } else if (items.contains(RegExp(r'[A-Z]'))) {
        pen.white(bold: true);
        sb.write(pen.write(items));
      } else {
        pen.red();
        sb.write(pen.write(items));
      }
    }
  }
  print(sb);
}