LCOV - code coverage report
Current view: top level - test_core-0.4.9/lib/src/util - pretty_print.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 35 0.0 %
Date: 2021-11-28 14:37:50 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright (c) 2021, the Dart project authors.  Please see the AUTHORS file
       2             : // for details. All rights reserved. Use of this source code is governed by a
       3             : // BSD-style license that can be found in the LICENSE file.
       4             : 
       5             : /// A regular expression matching terminal color codes.
       6           0 : final _colorCode = RegExp('\u001b\\[[0-9;]+m');
       7             : 
       8             : /// Returns [str] without any color codes.
       9           0 : String withoutColors(String str) => str.replaceAll(_colorCode, '');
      10             : 
      11             : /// A regular expression matching a single vowel.
      12           0 : final _vowel = RegExp('[aeiou]');
      13             : 
      14             : /// Returns [noun] with an indefinite article ("a" or "an") added, based on
      15             : /// whether its first letter is a vowel.
      16           0 : String a(String noun) => noun.startsWith(_vowel) ? 'an $noun' : 'a $noun';
      17             : 
      18             : /// Indent each line in [string] by 2 spaces.
      19           0 : String indent(String text) {
      20           0 :   var lines = text.split('\n');
      21           0 :   if (lines.length == 1) return '  $text';
      22             : 
      23           0 :   var buffer = StringBuffer();
      24             : 
      25           0 :   for (var line in lines.take(lines.length - 1)) {
      26           0 :     buffer.writeln('  $line');
      27             :   }
      28           0 :   buffer.write('  ${lines.last}');
      29           0 :   return buffer.toString();
      30             : }
      31             : 
      32             : /// Truncates [text] to fit within [maxLength].
      33             : ///
      34             : /// This will try to truncate along word boundaries and preserve words both at
      35             : /// the beginning and the end of [text].
      36           0 : String truncate(String text, int maxLength) {
      37             :   // Return the full message if it fits.
      38           0 :   if (text.length <= maxLength) return text;
      39             : 
      40             :   // If we can fit the first and last three words, do so.
      41           0 :   var words = text.split(' ');
      42           0 :   if (words.length > 1) {
      43           0 :     var i = words.length;
      44           0 :     var length = words.first.length + 4;
      45             :     do {
      46           0 :       i--;
      47           0 :       length += 1 + words[i].length;
      48           0 :     } while (length <= maxLength && i > 0);
      49           0 :     if (length > maxLength || i == 0) i++;
      50           0 :     if (i < words.length - 4) {
      51             :       // Require at least 3 words at the end.
      52           0 :       var buffer = StringBuffer();
      53           0 :       buffer.write(words.first);
      54           0 :       buffer.write(' ...');
      55           0 :       for (; i < words.length; i++) {
      56           0 :         buffer.write(' ');
      57           0 :         buffer.write(words[i]);
      58             :       }
      59           0 :       return buffer.toString();
      60             :     }
      61             :   }
      62             : 
      63             :   // Otherwise truncate to return the trailing text, but attempt to start at
      64             :   // the beginning of a word.
      65           0 :   var result = text.substring(text.length - maxLength + 4);
      66           0 :   var firstSpace = result.indexOf(' ');
      67           0 :   if (firstSpace > 0) {
      68           0 :     result = result.substring(firstSpace);
      69             :   }
      70           0 :   return '...$result';
      71             : }

Generated by: LCOV version 1.14