Pretty String

style: very good analysis License: MIT

A simple way to convert strings to be human-readable!

Installation 💻

Add pretty_string to your pubspec.yaml:

dependencies:
  pretty_string: 1.0.1

Install it:

flutter pub get

Motivation

Almost every application, we need to log APIs, BLoc state transitions, ..etc to debug what is happening there. Also, we don't want to implement toString() by hand because it's quite tedious. So, many developers are finding the solution and finally stops at Equatable and Freezed. They provides good implementations for toString() but its not enough. They returns only one line which makes crazy as the model size is bigger.

See generated string made by .toPrettier() in Equatable model and Freezed model. Pretty powerful right? Remember this examples are very simple cases. In real world, you probably knows there are much complicated object that has dozens of properties which makes you crazy to see log without toPrettier().

Usage💯

  • This packages has very simple extension method named .toPrettier(). This makes every strings to be awesome to read! What only you need to do is calling .toPrettier() to an object or a string.
final object = VeryComplicatedObject(.....);
log(object.toPrettier())

final someVeryComplicatedString = '{..., ..., ...}'
log(someVeryComplicatedString.toPrettier())

Examples🌏

Equatable model

class Dog extends Equatable {
  const Dog({
    required this.hasTail,
    required this.age,
    required this.parents,
    required this.friend,
  });

  final bool hasTail;
  final int age;
  final List<Dog> parents;
  final Dog? friend;

  @override
  List<Object?> get props => [hasTail, age, parents, friend];
}

/// with toString()
Dog(true, 10, [Dog(true, 100, [], null), Dog(true, 100, [], null)], Dog(true, 100, [], null))

/// with toPrettier()
Dog(
  true,
  10,
  [
    Dog(
      true,
      100,
      [],
      null
    ),
    Dog(
      true,
      100,
      [],
      null
    )
  ],
  Dog(
    true,
    100,
    [],
    null
  )
)

Freezed model

@freezed
class Member with _$Member {
  const factory Member({
    required int id,
    required String name,
    required List<Member> friends,
  }) = _Member;

  const Member._();
}
 
/// with toString()
Member(id: 1, name: John, friends: [Member(id: 10, name: Amy, friends: []), Member(id: 30, name: Barth, friends: []), Member(id: 50, name: Irene, friends: [])])

/// with toPrettier()
Member(
  id: 1,
  name: John,
  friends: [
    Member(
      id: 10,
      name: Amy,
      friends: []
    ),
    Member(
      id: 30,
      name: Barth,
      friends: []
    ),
    Member(
      id: 50,
      name: Irene,
      friends: []
    )
  ]
)

Advanced

Custom

  • .toPrettier() has a following defaults. If you want to change the behavior, pass it as a argument.
/// Changes intent width or characters.
String indent = '  ',
/// Needed to use a special separators instead of ','
List<String> separators = const <String>[','],
/// Needed to use brackets rather than followings
Map<String, String> brackets = const {
  '{': '}',
  '[': ']',
  '(': ')',
},

Contribute🤖

  • Feel free to open pull request to improve this project!

Running Tests 🧪

To run all unit tests:

very_good test --coverage

Libraries

pretty_string