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 : import 'package:matcher/matcher.dart'; 6 : import 'package:term_glyph/term_glyph.dart' as glyph; 7 : 8 : /// Indent each line in [string] by [first.length] spaces. 9 : /// 10 : /// [first] is used in place of the first line's indentation. 11 0 : String indent(String text, {required String first}) { 12 0 : final prefix = ' ' * first.length; 13 0 : var lines = text.split('\n'); 14 0 : if (lines.length == 1) return '$first$text'; 15 : 16 0 : var buffer = StringBuffer('$first${lines.first}\n'); 17 : 18 : // Write out all but the first and last lines with [prefix]. 19 0 : for (var line in lines.skip(1).take(lines.length - 2)) { 20 0 : buffer.writeln('$prefix$line'); 21 : } 22 0 : buffer.write('$prefix${lines.last}'); 23 0 : return buffer.toString(); 24 : } 25 : 26 : /// Returns a pretty-printed representation of [value]. 27 : /// 28 : /// The matcher package doesn't expose its pretty-print function directly, but 29 : /// we can use it through StringDescription. 30 0 : String prettyPrint(value) => 31 0 : StringDescription().addDescriptionOf(value).toString(); 32 : 33 : /// Indents [text], and adds a bullet at the beginning. 34 0 : String addBullet(String text) => indent(text, first: '${glyph.bullet} '); 35 : 36 : /// Converts [strings] to a bulleted list. 37 0 : String bullet(Iterable<String> strings) => strings.map(addBullet).join('\n'); 38 : 39 : /// Returns [name] if [number] is 1, or the plural of [name] otherwise. 40 : /// 41 : /// By default, this just adds "s" to the end of [name] to get the plural. If 42 : /// [plural] is passed, that's used instead. 43 0 : String pluralize(String name, int number, {String? plural}) { 44 0 : if (number == 1) return name; 45 : if (plural != null) return plural; 46 0 : return '${name}s'; 47 : }