-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from thomassamoth/develop
ansiconverter v2.0.0
- Loading branch information
Showing
6 changed files
with
280 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,168 @@ | ||
# 🌀 ANSI Converter | ||
|
||
![PyPI](https://img.shields.io/pypi/v/ansiconverter) ![PyPI - License](https://img.shields.io/pypi/l/ansiconverter) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/ansiconverter) ![PyPI - Status](https://img.shields.io/pypi/status/ansiconverter) | ||
![PyPI](https://img.shields.io/pypi/v/ansiconverter) ![PyPI - License](https://img.shields.io/pypi/l/ansiconverter) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/ansiconverter) ![PyPI - Status](https://img.shields.io/pypi/status/ansiconverter) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat)](https://makeapullrequest.com) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) | ||
|
||
Convert any colour to the ANSI format to write in colours in your terminal. | ||
Convert any colour to the [ANSI format](https://en.wikipedia.org/wiki/ANSI_escape_code) to write in colours in your terminal. | ||
|
||
## ⌨ Installation | ||
## ⌨️ Installation | ||
|
||
Run this command to install `ansiconverter`: | ||
|
||
```python | ||
```bash | ||
python -m pip install -U ansiconverter | ||
``` | ||
|
||
To install for **development**: | ||
|
||
```bash | ||
git clone https://github.com/thomassamoth/ansiconverter.git | ||
|
||
cd ansiconverter | ||
|
||
pip install -e .[dev] | ||
``` | ||
### Tests | ||
|
||
To ensure the installation of this package has been successful, you can run the [tests](https://github.com/thomassamoth/ansiconverter/tree/main/test). | ||
|
||
1. Make sure you have installed the `pytest` module. Otherwise, run: | ||
|
||
```bash | ||
pip install pytest | ||
``` | ||
|
||
2. After you downloaded the code and installed the package, run the tests by executing: | ||
|
||
```bash | ||
python -m pytest test/ | ||
``` | ||
|
||
## 💻 Usage | ||
|
||
### Converter module | ||
|
||
#### Convert any *RGB* colour to *ANSI* | ||
> ⚠️ **Warning** | ||
> Some colour combinations between background and foreground are incompatible. The result can be slightly different from what is expected. | ||
<details> | ||
<summary><strong>Convert from <i>RGB</i> colour to <i>ANSI</i></strong></summary> | ||
|
||
```python | ||
# How to print a green text on a white background | ||
from ansiconverter.converter import RGBtoANSI | ||
from ansiconverter import RGBtoANSI | ||
print(RGBtoANSI(text='Green text on a white background',foregound=[0, 255, 0], background=[255, 255, 255])) | ||
|
||
``` | ||
**Result:** | ||
|
||
<img src ="https://user-images.githubusercontent.com/25958977/190724022-a8b6e7cf-60e7-4493-9d9b-14b28be7268a.png" width=700 > | ||
|
||
#### Convert any *hexadecimal* colour to *ANSI* | ||
**Result**: | ||
<img src ="https://github.com/thomassamoth/ansiconverter/assets/25958977/76d6d253-4176-4891-adae-8df05e56d62f" height=50 > | ||
</details> | ||
|
||
> **Warning** | ||
Some colour combinations are incompatible, and the result will be [slightly different](#convert-any-rgb-colour-to-ansi) from what is expected. | ||
<details> | ||
<summary> | ||
<strong>Convert from <i>hexadecimal</i> to <i>ANSI</i></strong> | ||
</summary> | ||
|
||
```python | ||
# How to print a yellow text on a navy blue background, with hexadecimal values. | ||
from ansiconverter.converter import HEXtoANSI | ||
|
||
from ansiconverter import HEXtoANSI | ||
print(HEXtoANSI('Some yellow text on blue background','#fdf31f', '000080')) | ||
|
||
``` | ||
**Result**: | ||
|
||
<img src="https://user-images.githubusercontent.com/25958977/190716452-69a8f8df-6f2d-4a79-94c2-f601dc4b4466.png" width=700)> | ||
**Result**: | ||
<img src="https://github.com/thomassamoth/ansiconverter/assets/25958977/40954308-5be6-4e69-b8a8-c08a63224a03" height=50)> | ||
</details> | ||
|
||
### Styles module 🎨 | ||
> ℹ️ **Note** | ||
> Another little tool has been added to convert RGB to hexadecimal and vice versa. It can't be used to write in color in the terminal but could be useful for other applications. | ||
Write your text in different styles: | ||
<details> | ||
<summary> | ||
<strong>Convert from <i>hexadecimal</i> to <i>RGB</i></strong> | ||
</summary> | ||
|
||
* bold | ||
* italic | ||
* faint | ||
* underlined | ||
* bold & underlined | ||
* strikethrough | ||
* reversed | ||
```python | ||
from ansiconverter import HEXtoRGB | ||
print(HEXtoRGB("#0b38c1")) | ||
``` | ||
|
||
**Result** : | ||
```python | ||
[11, 59, 193] | ||
``` | ||
</details> | ||
|
||
|
||
from ansiconverter.styles import styles | ||
<details> | ||
<summary> | ||
<strong>Convert from <i>RGB</i> to <i>hexadecimal</i></strong> | ||
</summary> | ||
|
||
print(styles.bold("Some text in bold")) | ||
```python | ||
from ansiconverter import RGBtoHEX | ||
print(RGBtoHEX([11, 59, 193])) | ||
``` | ||
|
||
### ⚗ Combination of colours and styles | ||
**Result** : | ||
```python | ||
"#0b3bc1" | ||
``` | ||
</details> | ||
<hr/> | ||
|
||
It is possible to combine text styles with colours by doing so: | ||
### 🎨 Styles module | ||
|
||
```python | ||
from ansiconverter.converter import * | ||
from ansiconverter.styles import styles | ||
Several text styles are available as well. You can even [combine them with colours](#combination-of-colours-and-styles) | ||
|
||
print(styles.bold(HEXtoANSI('A yellow text in bold','#f6cf6c'))) | ||
| Style | Method | | ||
|------------------------------|----------------------| | ||
| bold | `bold` | | ||
| italic | `italic` | | ||
| fainted | `faint` | | ||
| underlined | `underline` | | ||
| bold & underlined | `bold_and_underline` | | ||
| strikethroughed | `strikethrough` | | ||
| reversed _(colours inverted)_ | `reverse` | | ||
|
||
``` | ||
|
||
**Result:** | ||
```python | ||
from ansiconverter import bold | ||
|
||
<img src="https://user-images.githubusercontent.com/25958977/190715961-3a3da6e1-bf9f-4011-8644-29c3efa4f263.png" width=700> | ||
print(bold("Some text in bold")) | ||
``` | ||
Replace `.bold` in the example with any method above to get the desired style. | ||
|
||
You can replace `styles.bold()` by any function mentionned [above](#styles-module) i.e. `styles.italic()`. You can even **combine** different styles! | ||
<hr/> | ||
|
||
## ➕ Additional features | ||
### Combination of colours and styles | ||
|
||
You can also use ***RGB to HEX*** converter or ***HEX to RGB*** by themselves like this: | ||
⚗️ It is possible to combine text styles with colours by doing so: | ||
|
||
``` python | ||
>>> from ansiconverter import converter | ||
```python | ||
from ansiconverter import HEXtoANSI, bold | ||
print(bold(HEXtoANSI('A yellow text in bold','#f6cf6c'))) | ||
``` | ||
|
||
>>> print(converter.HEXtoRGB("#0b38c1")) | ||
[11, 59, 193]) | ||
**Result:** | ||
<img src="https://github.com/thomassamoth/ansiconverter/assets/25958977/4936657f-a536-497e-b8da-4df1d8f53813" height=50> | ||
|
||
>>> print(converter.RGBtoHEX([11, 59, 193])) | ||
"#0b3bc1" | ||
**N.B**: the order between the style and the text format isn't important and you can switch them. | ||
|
||
```python | ||
print(bold(HEXtoANSI('A yellow text in bold','#f6cf6c'))) | ||
``` | ||
# | ||
|
||
> **Note** | ||
This repository is based on [Mark Smith](https://github.com/judy2k)'s talk available [here](https://youtu.be/GIF3LaRqgXo) and its [linked repository](https://github.com/judy2k/publishing_python_packages_talk) | ||
will work the same as | ||
|
||
```python | ||
print(HEXtoANSI(bold('A yellow text in bold'),'#f6cf6c')) | ||
``` | ||
|
||
## Note | ||
|
||
This structure of this repository is based on a talk by [Mark Smith](https://github.com/judy2k), which is available [here](https://youtu.be/GIF3LaRqgXo), and its [linked repository](https://github.com/judy2k/publishing_python_packages_talk) | ||
|
||
## License | ||
This repository is licensed under the MIT license. See the [LICENSE](LICENSE.md) file for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,22 @@ | ||
from . import converter | ||
from . import styles | ||
from .converter import Converter | ||
from .styles import Styles | ||
|
||
# Export the Styles class and styling functions | ||
bold = Styles().bold | ||
faint = Styles().faint | ||
italic = Styles().italic | ||
underline = Styles().underline | ||
bold_and_underline = Styles().bold_and_underline | ||
strikethrough = Styles().strikethrough | ||
reverse = Styles().reverse | ||
|
||
# Export the Converter methods | ||
RGBtoANSI = Converter().RGBtoANSI | ||
RGBtoHEX = Converter().RGBtoHEX | ||
HEXtoANSI = Converter().HEXtoANSI | ||
HEXtoRGB = Converter().HEXtoRGB | ||
|
||
# Are imported whrn writing from ansiconverter import * | ||
__all__ = ['Styles', 'bold', 'faint', 'italic', 'underline', 'bold_and_underline', 'strikethrough', 'reverse',] | ||
|
||
__all__.extend(['RGBtoANSI', 'RGBtoHEX', 'HEXtoANSI', 'HEXtoRGB']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.