indent 1.0.0+2

  • Readme
  • Changelog
  • Installing
  • 68

indent #

pub package Build Status Coverage Status

Change indentation in multiline Dart strings while preserving the existing relative indentation.

A GIF speaks more than a thousand words:

A screencast of the example app in action

You can run the example app yourself by running cd example && pub get && webdev serve from the project root.

Usage #

For convenience, the library adds the following extension members on Dart's String class.

You can also wrap a string with the Indentation class and call methods on that - this is what the extension methods do under the hood.

unindent() #

If you found this library from a Google search, you're probably looking for the unindent() method. It's the use case this library was originally created for.

For example, this:

import 'package:indent/indent.dart';

print('''
          Hello
        there
             World!
'''.unindent());

outputs this:

  Hello
there
     World!

It gets rid of the common indentation while preserving the relative indentation. This is like Kotlin's trimIndent() or Java 12's align().

indent(int indentationLevel) #

Indents a string with the desired indentation level while preserving relative indentation.

For example, this:

import 'package:indent/indent.dart';

print('''
   Hello
World
'''.indent(2));

prints:

     Hello
  World

If the starting indentation level is higher than the desired one, the value will be unindented accordingly.

This:

import 'package:indent/indent.dart';

print('''
          Hello
       World
'''.indent(2));

also prints:

     Hello
  World

(calling indent(0) is equal to calling unindent().)

indentBy(int howMuch) #

Changes the indentation level in a string by howMuch.

A positive value for howMuch adds indentation.

For example, this:

import 'package:indent/indent.dart';

print('''
   Hello
World
'''.indentBy(4));

prints this:

       Hello
    World

When a negative value for howMuch is given, indentation is removed accordingly.

This:

import 'package:indent/indent.dart';

print('''
       Hello
    World
'''.indentBy(-4));

prints this:

   Hello
World

getIndentationLevel() #

Returns the common indentation level in a string.

For example, this:

import 'package:indent/indent.dart';

final int indentationLevel= '''
     Hello
  World
'''.getIndentationLevel();

returns 2 as the two spaces before World is the lowest common indentation in the string.

1.0.0+2 #

  • "performance improvements" - replace List.generate(indentationLevel) with a StringBuffer and a for loop. Nobody will probably notice the difference, but go ahead and enjoy the performance improvement of a couple Planck times anyway.

1.0.0+1 #

  • Somebody accidentally forgot to change the default package description. That's now fixed.

1.0.0 #

  • Initial version

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  indent: ^1.0.0+2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:indent/indent.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
36
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
68
Learn more about scoring.

We analyzed this package on Jul 14, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Maintenance suggestions

Maintain an example.

None of the files in the package's example/ directory matches known example patterns.

Common filename patterns include main.dart, example.dart, and indent.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
Dev dependencies
expected_output ^1.2.2
pedantic ^1.8.0
test ^1.6.0