indent 1.0.0 indent: ^1.0.0 copied to clipboard
A starting point for Dart libraries or applications.
indent #
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 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.