https://github.com/adzierzanowski/afmt
ANSI escape codes text formatter for python
https://github.com/adzierzanowski/afmt
ansi cli python terminal terminal-colors
Last synced: 6 months ago
JSON representation
ANSI escape codes text formatter for python
- Host: GitHub
- URL: https://github.com/adzierzanowski/afmt
- Owner: adzierzanowski
- License: mit
- Created: 2019-09-28T22:34:33.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-09-29T17:55:57.000Z (almost 7 years ago)
- Last Synced: 2025-11-27T17:17:50.371Z (8 months ago)
- Topics: ansi, cli, python, terminal, terminal-colors
- Language: Python
- Size: 10.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# afmt
This module provides an easy way to format text in CLI environment.
It is designed for use in **f-strings** but regular string formatting with
`format` method is also supported.
The module takes care of stripping ANSI escape sequences from piped output
and puts the sequences only if they're supported by the output device.
# Setup
`$ pip install afmt`
# Usage and examples
See the [docs](docs/index.md) for detailed information.
```python
from afmt import Formatter
f = Formatter()
# You can use inline styling
print(f'{f:bold}bold text{f:e}')
# You can define specs outside of an f-string
warning = 'bold bg(yellow) fg(black) italic'
error = 'bold bg(red) fg(white) underline'
print(f'{f:{warning}}Warning: you shouldn\'t do it{f:e}')
print(f'{f:{error}}Error: you can\'t do it{f:e}')
# You can define custom styles in the formatter instance
f.add_style('important', 'b fg(red)')
print(f'{f:important}important text{f:e}')
# Or you can pass a dict of styles in the initializer
f = Formatter(styles={
'important': 'bold fg(255,0,0)',
'unimporant': 'faint'
})
print(f'{f:important}important text{f:e}')
print(f'{f:unimportant}not so important text{f:e}')
# Move cursor to 4th row and 6th column and print text in reverse video mode
print(f'{f:reverse goto(4,6)}hello, world{f:e}')
# Make text bold and underlined and unset bold in the middle
print(f'{f:bold italic}hello,{f:!bold} world{f:e}')
```
[](https://asciinema.org/a/bDlnVRmdRpgq1cvTIRwawYswL)
# Development status
This module is in the early stage of development.
More features are coming hopefully soon.