Skip to main content

Module with print and input that convert pseudo-BBCode into ANSI. It also includes a flip option that improves the .capitalize(), .lower(), .title(), and .upper() methods, and adds an invert option.

Project description

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 Key with curl:

curl -fsSL https://pablinet.github.io/apt/pablinet.gpg -o /etc/apt/trusted.gpg.d/pablinet.gpg 2> /dev/null && echo Ok! || echo "If you encounter any errors, please check the project's  \x1b[1mhttps://github.com/PabliNet/bbansi\x1b[0m"

Add APT Key with wget:

wget -O /etc/apt/trusted.gpg.d/pablinet.gpg https://pablinet.github.io/apt/pablinet.gpg  2> /dev/null && echo Ok! || echo "If you encounter any errors, please check the project's  \x1b[1mhttps://github.com/PabliNet/bbansi\x1b[0m"

Update and install:

apt update && apt install python3-bbansi

If you encounter any errors, please check the project's GitHub repository.

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 two optional boolean parameters: reset and ansi.

  • 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bbansi-0.0.2.post1.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bbansi-0.0.2.post1-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

Details for the file bbansi-0.0.2.post1.tar.gz.

File metadata

  • Download URL: bbansi-0.0.2.post1.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for bbansi-0.0.2.post1.tar.gz
Algorithm Hash digest
SHA256 624fb68da2209449cf747adc3c0bcba80ef7be5cd8c040210b3cb90e3c241352
MD5 a6ee212a5da7c82763c4e473795649c9
BLAKE2b-256 f89939437ecef9448d9bce29d495924b3e94a15b544d9020fd36117889ecb0d0

See more details on using hashes here.

File details

Details for the file bbansi-0.0.2.post1-py3-none-any.whl.

File metadata

  • Download URL: bbansi-0.0.2.post1-py3-none-any.whl
  • Upload date:
  • Size: 24.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for bbansi-0.0.2.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 d2720fb61162ef4be6d56bad9a028f290c56c3361e82a930ced7983c601152c8
MD5 593f7538e7058bae5d23dacecc2d7de7
BLAKE2b-256 c228272f03260f79b7fbd175600bfc1674c81014d58da0515ab1229c98cef6a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page