BBANSI
Module with custom print and input functions that convert pseudo-BBCode into ANSI escape codes. It also includes a flip function that improves the standard string methods (.capitalize(), .lower(), .title(), and .upper()) and adds an invert option, all while preserving ANSI codes.
Installation
From PyPI
pip install bbansi
From APT as root or with sudo
Add APT Repository:
echo deb https://pablinet.github.io/apt ./ > /etc/apt/sources.list.d/pablinet.list
Add APT Key with curl:
curl -fsSL https://pablinet.github.io/apt/pablinet.gpg -o /etc/apt/trusted.gpg.d/pablinet.gpg
Add APT Key with wget:
wget -O /etc/apt/trusted.gpg.d/pablinet.gpg https://pablinet.github.io/apt/pablinet.gpg
Update and install:
apt update && apt install python3-bbansi
If you encounter any errors, please check the project's GitHub repository.
Manual installation of commands from GitHub
Clone the repository and enter the directory:
git clone https://github.com/PabliNet/bbansi
cd bbansi
Make the installer executable:
chmod +x install-sh
Run it (as root or with sudo):
./install-sh
Command-line Interface (APT Version or manual installation)
The APT installation includes seven executable commands that accept BBANSI tags:
ef (emulates the echo command)
capital
capitalize
invert
lower
title
upper
For more information on the usage of these commands, use ef -h or --help.
Usage
from bbansi import flip, input, print
print (flip('[kb]wELCOME TO THIS EXAMPLE', 'invert'), reset=True)
name = input ('r [b]What is your name? ')
print (f'Your name is [171,15]{flip(name, 'capitalize')}[]')
Expected Output
Here's a preview of the output, both in its raw ANSI form and as it would appear in a terminal.
Raw Output:
'\x1b[5;1mWelcome to this example\x1b[0m'
'\x1b[1mWhat is your name?\x1b[0m pavel'
'Your name is \x1b[38;5;171;48;5;15mPavel
print function
The print function has been extended with four optional parameters: delay, wrap, reset and ansi.
-
delay: The parameter
delaymust match the pattern/[0-9]+(\.[0-9]+)?/.
When provided, the output is printed character by character with a delay ofdelayseconds between each character. -
wrap: A boolean. If set to True, it activates "word wrapper" mode by word.
-
reset: When set to True, the ANSI reset sequence
'\x1b\[0m'is automatically appended to the end of the output, ensuring that subsequent text does not retain any applied styling. -
ansi: When set to True, the ANSI escape codes are rendered explicitly in the output, similar to the behavior of the
repr()function.
input function
The only addition to this customized input function is the ability to automatically reset styles and colors by prefixing the prompt message with -r.
Example: name \= input ('-r [b]What is your name?')
Tags
| Tag open | Tag close | ANSI open | ANSI Close | Description |
|---|---|---|---|---|
[b]¹ |
[/b]¹ |
'\x1b[1m' |
'\x1b[22m' |
Bold |
[d]¹ |
[/d]¹ |
'\x1b[2m' |
'\x1b[22m' |
Dim |
[i]¹ |
[/i]¹ |
'\x1b[3m' |
'\x1b[23m' |
Italic |
[u]¹ |
[/u]¹ |
'\x1b[4m' |
'\x1b[24m' |
Underline |
[k]¹ |
[/k]¹ |
'\x1b[5m' |
'\x1b[25m' |
Blink |
[r]¹ |
[/r]¹ |
'\x1b[7m' |
'\x1b[27m' |
Reverse video |
[h]¹ |
[/h]¹ |
'\x1b[8m' |
'\x1b[8m' |
Hidden text |
[s]¹ |
[/s]¹ |
'\x1b[9m' |
'\x1b[29m' |
Strike out |
[FG,BG]² |
[-1,256]² |
'\x1b[38;5;FG;48;5;BGm' |
'\x1b[39;49m' |
Text color and bacground color |
[FG]² |
[354]² |
'\x1b[38;5;FGm' |
'\x1b[39m' |
Only text color |
[,BG]² |
[,-1]² |
'\x1b[48;5;BGm' |
'\x1b[49m' |
Only background color |
| ··· | [] |
··· | '\x1b[0m' |
All reset |
¹Multiple ANSI styles can be combined, e.g., [biuk].
²The values for FG and BG must be integers within the inclusive range of 0 to 255. Any value outside this range will reset the corresponding color.
Escapes
\\ Backslash
\a Alert BELL
\b Backspace
\c Produce no further output
\e Escape
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\0NNN Byte with octal value NNN (1 to 3 digits)
\xHH Byte with hexadecimal value HH (1 to 2 digits)
[[ Opening square bracket
]] Closing square bracket
flip function
The flip function addresses a key limitation of standard string methods like .capitalize(), .lower(), and .title(): it correctly handles ANSI styling codes and non-alphabetic characters, preserving the codes while accurately modifying the text.
Examples
Standard string methods:
>>> string = '¡hello world!'
>>> print (string.capitalize())
¡hello world! # The 'h' remains lowercase
>>> string = '\x1b[1mhello world\x1b[0m'
>>> print (repr(string.title()))
'\x1b[1Mhello World\x1b[0M' # ANSI codes are incorrectly altered
>>> print (repr(string.upper()))
'\x1b[1MHELLO WORLD\x1b[0M' # ANSI codes are affected
With the flip function:
>>> string = '¡hello world!'
>>> print (flip(string, 'capitalize')
¡Hello world! # Correctly capitalizes the first alphabetic character
>>> string = '\x1b[1mhello WOrld\x1b[0m'
>>> print (flip(string, 'capital')
'\x1b[1mHello world\x1b[0m' # Proper casing, ANSI preserved
>>> print (flip(string, 'invert')
'\x1b[1mHELLO woRLD\x1b[0m' # Inverts current case
>>> print (flip(string, 'title'))
'\x1b[1mHello World\x1b[0m' # Proper title case
>>> print (flip(string, 'upper'))
'\x1b[1mHELLO WORLD\x1b[0m' # All caps, ANSI untouched
You can also use the first letter of each option (capitalize, invert, lower, title, or upper) for a more concise syntax.
>>> string = '¡hello world!'
>>> print (flip(string, 'c')
¡Hello world! # Correctly capitalizes the first alphabetic character
>>> string = '\x1b[1mhello WOrld\x1b[0m'
>>> print (flip(string, 'c')
'\x1b[1mHello world\x1b[0m' # Proper casing, ANSI preserved
>>> print (flip(string, 'i')
'\x1b[1mHELLO woRLD\x1b[0m' # Inverts current case
>>> print (flip(string, 't'))
'\x1b[1mHello World\x1b[0m' # Proper title case
>>> print (flip(string, 'u'))
'\x1b[1mHELLO WORLD\x1b[0m' # All caps, ANSI untouched
Metadata
Release files for bbansi 0.4.2.post2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| bbansi-0.4.2.post2.tar.gz | 28.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| bbansi-0.4.2.post2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 54.8 kB
Release files / bbansi-0.4.2.post2.tar.gz
| Download URL | bbansi-0.4.2.post2.tar.gz |
|---|---|
| Size | 28.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
449bc9a6653b646719f4dce3fdee41ae9e309bbd7902f943601d07ce6300bb5c
|
|
BLAKE2b-256 checksum How to use checksums |
956ce85176f3f637e61be3e24c44b36d78260e625d68fff16b1c865b0a0bcd5e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.20
|
Release files / bbansi-0.4.2.post2-py3-none-any.whl
| Download URL | bbansi-0.4.2.post2-py3-none-any.whl |
|---|---|
| Size | 26.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d0ee8b25ddd51a6576b9cd4cebadb33b123356eb620a2952ddcf52daf641990c
|
|
BLAKE2b-256 checksum How to use checksums |
5b4cef13ced632f9ce49cae72ae318c459deaf05baeeb82add82202b575e1749
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.20
|
Security for bbansi 0.4.2.post2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.