DEV Community

Cover image for Badges for the terminal - Python
Haider Ali Punjabi
Haider Ali Punjabi

Posted on

Badges for the terminal - Python

Hello everyone!

I've just ported the quirky node-js library cli-badges over to Python. The author of that library:

nombrekeff image

Post about the NodeJS Library

Python Port that I made:

GitHub logo haideralipunjabi / cli-badges

Quirky little python package for generating badges for your cli apps.

Quirky little python library for generating badges for your cli apps.

GitHub file size in bytes PyPI version


Inspired & Python Port of cli-badges - nombrekeff

Getting Started

Installing

As usual, you need to install from PIP:

$ pip install cli-badges

Usage

This is a simple example, using badges to display test results:

from cli_badges import badge
failedBadge = badge("failed",'2',messagebg='red')
skippedBadge = badge('skipped', '1', messagebg='yellow',messagecolor='black')
successBadge = badge('success','8', messagebg='green',messagecolor='black')
print(failedBadge, successBadge, skippedBadge)

The above would output something similar to the terminal:

output-example

You could also create a donate badge with a link (if supported):

from cli_badges import badge
donateBadge = badge('❤️ donate', 'ko-fi', messagelink='https://ko-fi.com/logginjs');
print(donateBadge)

donate-output-example.png

You can also only…

Installing

As usual, you need to install from PIP:

$ pip install cli-badges

Usage

This is a simple example, using badges to display test results:

from cli_badges import badge

failedBadge = badge("failed",'2',messagebg='red')
skippedBadge = badge('skipped', '1', messagebg='yellow',messagecolor='black')
successBadge = badge('success','8', messagebg='green',messagecolor='black')


print(failedBadge, successBadge, skippedBadge)

The above would output something similar to the terminal:

output-example

You could also create a donate badge with a link (if supported):

from cli_badges import badge

donateBadge = badge('❤️ donate', 'ko-fi', messagelink='https://ko-fi.com/logginjs');
print(donateBadge)

donate-output-example.png

You can also only show the label:

from cli_badges import badge

onlyLabel = badge('❤️ donate', '')
print(onlyLabel)

onlylabel-output-example

Example output is a mock, console output will vary slightly from terminal to terminal.

Badge Structure

A badge is conformed of a label and a message <label>:<message>. Each segment can be customized, by changing bg color, text color and style.

Available Options

Option Value Default
label String ''
message String ''
messagebg Color blue
labelbg Color dark_gray
messagecolor Color white
labelcolor Color white
labelwidth Integer label length + 2
messagewidth Integer label length + 2
labelstyles Array of Styles []
messagestyles Array of Styles []
labellink URL ''
messagelink URL ''

Colors

cli-badges uses colored internally for managing colors, you can check the list of available colors there.

Styles

cli-badges uses colored internally for managing styles, you can check the list of available styles there.

Available Styles

  • bold
  • dim
  • underlined
  • reverse
  • hidden

Links

You can output badges with a link attached to it, that can be clicked in some terminals. labellink option will add the link to the label, while messagelink option will add the link to the message.

⚠︎ cli-badges will only output link if its supported by your terminal.

See this for information on supported terminals

badge('with', 'link',  labellink='https://link.com', messagelink='https://link2.com');

withlink-output-example

Discussion (7)

Collapse
bogdaaamn profile image
Bogdan Covrig

Really nice, thank you for sharing!

I am having a hard time finding use cases for them apart from the testing results. What else did you think of?

Collapse
haideralipunjabi profile image
Haider Ali Punjabi Author

Personally, I was thinking of using them to display progress of my scripts. Other than that, any information can be displayed using these, like no. of vulnerabilities while installing an npm package, no. of files modified during git commit, the stars, forks and watchers of a git repo, etc. Anything that you might output in the form of key:value would look better using these

Collapse
bogdaaamn profile image
Bogdan Covrig

Makes sense, thank you! I might give it a spin soon

Collapse
nombrekeff profile image
Keff

Cool stuff! Thanks for the port! 🥰

Collapse
haideralipunjabi profile image
Haider Ali Punjabi Author

Used these badges in a small script that displays Github Repo Stats

Collapse
nombrekeff profile image
Keff

Nice use case!