Badges for the terminal - Dart Edition
Tags: showdev, dart, terminal, programming
Published: 2021-09-30 12:14:28

Hey there! 👋

A while back I created a little node-js library called [`cli-badges`](https://github.com/nombrekeff/cli-badges) for creating badges that you can print to the terminal. 

Yup, those nifty little things we all know and love. The ones we add to our READMEs in order to add some more information about the project, like the version, downloads, test coverage, etc... 

**These thingies**
![Image of shields.io badges](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/isbfcervlsfw5swv6a6s.png)

They ended up looking something like this when printed to the terminal:
![Image of cli-badges output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xs1fjl8620c7gwa7zjpv.png)

Whilst not the prettiest (as per usual in the terminal), it does the job. And in certain scenarios it can make for a better experience, like displaying tests results, test coverage, or any other information we could express in a key/value manner.

## Why the port?

Like many other of my projects the reason usually is, that I was bored and wanted to code something up quickly. It has not been any different for this port. 

Another reason is, that when I initially released [`cli-badges`](https://github.com/nombrekeff/cli-badges) some other people in the community ported it to their prefered languages, like the [python version]([haideralipunjabi](https://github.com/haideralipunjabi/cli-badges)) made by [haideralipunjabi](https://github.com/haideralipunjabi).

I was bored today and as I'm recently using dart a lot (both professionally and for personal projects) I decided to port cli-badges to dart.

## The result
This is how it ended up working:
```dart
var failedBadge  = Badge(label: 'failed', message: '2', theme: BadgeTheme.red);
var successBadge = Badge(label: 'success', message: '2').green();
var skippedBadge = Badge.yellow(label: 'skipped',  message: '2');

print(
    Badge.inline([
        failedBadge, 
        skippedBadge, 
        successBadge
    ]),
);
```

The above would output something similar to the terminal:

![Example output](https://github.com/nombrekeff/cli-badges/raw/master/output-example.png)

## The repo

It would be awesome if you could check the project out and give me and the project some feedback and love. 🥰 

{% github nombrekeff/cli_badges_dart no-readme %}

## Takeaways

### Named constructors
I love named constructors, they offer a nice way of adding multiple constructors that can have different default parameters. This can make your life as a developer quite a bit easier.

In cli_badges I used named constructors for easily creating themed badges:
```dart
Badge.yellow(label: 'skipped', message: '2');
Badge.red(label: 'skipped', message: '2');
```
As opposed to this:
```dart
Badge(label: 'skipped', message: '2', theme: BadgeTheme.yellow);
Badge(label: 'skipped', message: '2', theme: BadgeTheme.red);
```


This is how it's implemented:
```dart
Badge.red({
  required this.label,
  this.message,
  this.labelWidth,
  this.messageWidth,
}) : theme = BadgeTheme.red;

Badge.green({
  required this.label,
  this.message,
  this.labelWidth,
  this.messageWidth,
}) : theme = BadgeTheme.green;
```

I added one for each of the available themes. This makes it much simpler and cleaner to create badges.


### Override `toString`
Instead of creating a new method to build the badge string, we can override `toString`. This makes our class printable, just by passing it to `print`

```dart
print(Badge(label: 'I can be printed!'));
```
As opposed to:
```dart
print(Badge(label: 'I can be printed!').makeString());
```

**This is how it's implemented:**
```dart
@override
toString() {
  var label = _getLabel();
  var message = _getMessage();

  var coloredLabel = theme.colorLabel(label);
  var coloredMessage = theme.colorMessage(message);

  return '$coloredLabel$coloredMessage';
}
```

----

That's it for this one, another quick little post! Let me end it with a question for you.

#### Can you find any use for this package? If so for what?