toString method Null safety

  1. @override
String toString (
  1. {int from = 0,
  2. int to = 0,
  3. bool err = false}
)
override

String representation of parsable (marks actual position)

Implementation

@override
String toString({int from = 0, int to = 0, bool err = false}) {
  final red = AnsiPen()..red();
  final redBg = AnsiPen()..red(bg: true);
  if (parsable.length > io.terminalColumns) {
    var start = parsable.length > io.terminalColumns
        ? (position - (io.terminalColumns / 2) + 3).round()
        : 0;
    var end = parsable.length > io.terminalColumns
        ? (position + (io.terminalColumns / 2) - 4).round()
        : parsable.length - 1;

    if (start < 0) {
      end += start * -1;
      start = 0;
    }
    if (end >= parsable.length) {
      start -= end - parsable.length + 1;
      end = parsable.length - 1;
    }

    String startletters = '(+$start)', startletters_;
    String endletters = '(+${parsable.length - end + 7})', endletters_;
    end -= endletters.length + startletters.length;

    do {
      endletters_ = endletters;
      startletters = '(+$end)';
      if (endletters.length - endletters_.length > 0) {
        end -= endletters.length - endletters_.length;
      }
    } while (endletters_.length != endletters.length);

    do {
      startletters_ = startletters;
      startletters = '(+$start)';
      if (startletters.length - startletters_.length > 0) {
        end -= startletters.length - startletters_.length;
      }
    } while (startletters_.length != startletters.length);

    if (start < 0) {
      end += start * -1;
      start = 0;
    }

    var pos = position - start + startletters.length + 3;
    var code = '$startletters...' +
        parsable.substring(start, end) +
        '...$endletters\n';

    var beforeSelect = code.substring(0, pos - from);
    var selected = code.substring(pos - from, pos + to + 1);
    var afterSelect = code.substring(pos + to + 1);

    var bottom = _repeatString(' ', pos - from) +
        _repeatString('^', 1 + from + to) +
        '\n';
    if (err) {
      bottom = red(bottom);
      selected = redBg(selected);
    }

    return 'position ${position + 1}/${parsable.length} (\"${actual()}\")\n\nHere:\n' +
        beforeSelect +
        selected.toString() +
        afterSelect +
        bottom.toString();
  }

  var beforeSelect = parsable.substring(0, position - from);
  var selected = parsable.substring(position - from, position + to + 1);
  var afterSelect = parsable.substring(position + to + 1);

  var bottom = _repeatString(' ', position - from) +
      _repeatString('^', 1 + from + to) +
      '\n';

  if (err) {
    bottom = red(bottom);
    selected = redBg(selected);
  }

  return 'position ${position + 1}/${parsable.length} (\"${actual()}\")\n\nHere:\n' +
      beforeSelect +
      selected.toString() +
      afterSelect +
      '\n' +
      bottom.toString();
}