https://github.com/roughike/indent
Change indentation in multiline Dart strings while preserving the existing relative indentation. Kotlin's trimIndent and trimMargin for Dart.
https://github.com/roughike/indent
Last synced: 9 months ago
JSON representation
Change indentation in multiline Dart strings while preserving the existing relative indentation. Kotlin's trimIndent and trimMargin for Dart.
- Host: GitHub
- URL: https://github.com/roughike/indent
- Owner: roughike
- License: bsd-2-clause
- Created: 2020-02-08T11:37:51.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2021-04-12T19:01:37.000Z (over 4 years ago)
- Last Synced: 2025-04-14T16:21:53.378Z (9 months ago)
- Language: Dart
- Homepage:
- Size: 1020 KB
- Stars: 17
- Watchers: 2
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# indent
[](https://pub.dartlang.org/packages/indent)
[](https://travis-ci.org/roughike/indent)
[](https://coveralls.io/github/roughike/indent?branch=master)
Change indentation in multiline Dart strings while preserving the existing relative indentation.
A GIF speaks more than a thousand words:

You can run [the example app](https://github.com/roughike/indent/tree/master/example/web) 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](https://github.com/roughike/indent/blob/master/lib/src/string_extensions.dart) 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:
```dart
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()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/trim-indent.html) or Java 12's `align()`.
### trimMargin([String marginPrefix = '|'])
Trims the leading whitespace followed by the `marginPrefix` from each line.
For example, this:
```dart
import 'package:indent/indent.dart';
print('''
| Hello
|there
| World!
'''.trimMargin());
```
outputs this:
```
Hello
there
World!
```
Behaves just like [trimMargin in Kotlin](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/trim-margin.html).
### indent(int indentationLevel)
Indents a string with the desired indentation level while preserving relative indentation.
For example, this:
```dart
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:
```dart
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:
```dart
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:
```dart
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:
```dart
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.