textwrap 2.1.0
textwrap: ^2.1.0 copied to clipboard
Text wrapping and filling. It's a pure port of textwrap from Python.
textwrap #
A Dart library for intelligent text wrapping and filling. This is a pure port of Python's textwrap module, providing powerful text formatting utilities for console applications, documentation generation, and formatted output.
Features #
- Smart text wrapping with customizable width and indentation
- Paragraph filling that preserves formatting
- Flexible indentation for first line and subsequent lines
- Whitespace handling with configurable options
- Long word breaking for words that exceed line width
- Pure Dart implementation with no external dependencies
Installation #
Add this to your package's pubspec.yaml
file:
dependencies:
textwrap: ^2.1.0
Then run:
dart pub get
Usage #
Basic Text Wrapping #
import 'package:textwrap/textwrap.dart';
void main() {
// Simple wrapping
print(fill('Text wrapping and filling with intelligent line breaks.', width: 16));
// Output:
// Text wrapping
// and filling with
// intelligent line
// breaks.
}
Advanced options #
import 'package:textwrap/textwrap.dart';
void main() {
final text = 'This is a long paragraph that needs to be wrapped nicely.';
// Wrap with custom indentation
print(fill(text,
width: 30,
initialIndent: '>>> ',
subsequentIndent: ' '
));
// Output:
// >>> This is a long paragraph
// that needs to be wrapped
// nicely.
// Just get the wrapped lines as a list
final lines = wrap(text, width: 20);
for (final line in lines) {
print('| $line');
}
// Output:
// | This is a long
// | paragraph that needs
// | to be wrapped
// | nicely.
}
Using TextWrapper
class #
For repeated text wrapping with the same configuration, use the TextWrapper
class:
import 'package:textwrap/textwrap.dart';
void main() {
// Create a reusable wrapper with specific settings
const wrapper = TextWrapper(
width: 40,
initialIndent: ' ',
subsequentIndent: ' ',
breakLongWords: false,
);
// Apply the same formatting to multiple texts
print(wrapper.fill('First paragraph to be wrapped with consistent formatting.'));
print('');
print(wrapper.fill('Second paragraph with the same indentation and width settings.'));
// Output:
// First paragraph to be wrapped with
// consistent formatting.
//
// Second paragraph with the same
// indentation and width settings.
// Get lines as a list
final lines = wrapper.wrap('Third paragraph for line-by-line processing.');
print(lines);
// Output:
// [ Third paragraph for line-by-line, processing.]
}
Text shortening #
import 'package:textwrap/textwrap.dart';
void main() {
final longText = 'This is a very long piece of text that needs to be shortened';
// Shorten text to fit in limited space
print(shorten(longText, 30));
// Output: 'This is a very long piece ...'
// Custom placeholder
print(shorten(longText, 25, placeholder: ' [more]'));
// Output: 'This is a very [more]'
}
Contributing #
Contributions are welcome! Please feel free to submit a pull request. For major changes, please open an issue first to discuss what you would like to change.
License #
This project is licensed under the MIT License.
Features and bugs #
Please file feature requests and bugs at the issue tracker.