-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(plugins): add FIGlet plugin (#12)
Co-authored-by: welpo <welpo@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
166 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import logging | ||
import pyfiglet # type: ignore | ||
from typing import Any | ||
|
||
|
||
def run(settings: dict[str, Any]) -> str | None: | ||
if not validate_settings(settings): | ||
return None | ||
|
||
text = str(settings.get("ascii_text")) | ||
font = settings.get("font", "standard") | ||
|
||
try: | ||
result = pyfiglet.figlet_format(text, font) | ||
except pyfiglet.FontNotFound: | ||
logging.error("Invalid font for the FIGlet plugin") | ||
return None | ||
|
||
result_lines = result.splitlines() | ||
trimmed_result_lines = trim_leading_and_trailing_empty_lines(result_lines) | ||
result = "\n".join(trimmed_result_lines) | ||
|
||
pre_text = "```text\n" | ||
post_text = "\n```" | ||
result = pre_text + result.rstrip() + post_text | ||
return str(result) | ||
|
||
|
||
def validate_settings(settings: dict[str, Any]) -> bool: | ||
if "ascii_text" not in settings: | ||
logging.error("No text provided for the FIGlet plugin") | ||
return False | ||
|
||
return True | ||
|
||
|
||
def trim_leading_and_trailing_empty_lines(lines: list[str]) -> list[str]: | ||
# Leading lines. | ||
while lines and lines[0].strip() == "": | ||
lines.pop(0) | ||
|
||
# Trailing lines. | ||
while lines and lines[-1].strip() == "": | ||
lines.pop() | ||
|
||
return lines |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import logging | ||
import pytest | ||
from doteki.plugins.figlet import run, trim_leading_and_trailing_empty_lines | ||
|
||
|
||
def test_default_font(): | ||
settings = {"ascii_text": "hola"} | ||
expected = r""" _ _ | ||
| |__ ___ | | __ _ | ||
| '_ \ / _ \| |/ _` | | ||
| | | | (_) | | (_| | | ||
|_| |_|\___/|_|\__,_|""" | ||
result = run(settings) | ||
assert expected in str(result) | ||
|
||
|
||
def test_empty_text(caplog): | ||
settings = {"font": "standard"} | ||
with caplog.at_level(logging.ERROR): | ||
result = run(settings) | ||
assert result is None | ||
assert "No text provided for the FIGlet plugin" in caplog.text | ||
|
||
|
||
def test_invalid_font_returns_none(caplog): | ||
settings = {"ascii_text": "hola", "font": "invalid_font"} | ||
with caplog.at_level(logging.ERROR): | ||
result = run(settings) | ||
assert result is None | ||
assert "Invalid font for the FIGlet plugin" in caplog.text | ||
|
||
|
||
def test_int_text(): | ||
settings = {"ascii_text": 42, "font": "standard"} | ||
expected = r""" _ _ ____ | ||
| || ||___ \ | ||
| || |_ __) | | ||
|__ _/ __/ | ||
|_||_____|""" | ||
result = run(settings) | ||
assert expected in str(result) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"lines, expected", | ||
[ | ||
# Removes leading and trailing empty lines. | ||
(["", " ", "Hello", "World", "", " "], ["Hello", "World"]), | ||
# Do not remove empty lines in the middle. | ||
(["Hello", "", " ", "World"], ["Hello", "", " ", "World"]), | ||
# Leave non-empty lines as is. | ||
(["Hello", "World"], ["Hello", "World"]), | ||
# If there are only empty lines, return an empty list. | ||
(["", " ", " "], []), | ||
# If no lines are provided, return an empty list. | ||
([], []), | ||
], | ||
) | ||
def test_trim_leading_and_trailing_empty_lines(lines, expected): | ||
assert trim_leading_and_trailing_empty_lines(lines) == expected |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# FIGlet Plugin | ||
|
||
Display text with customizable ASCII art using FIGlet fonts. | ||
|
||
## Configuration | ||
|
||
The FIGlet plugin can be configured with the following parameters: | ||
|
||
- `text`: Text to be rendered in ASCII font. | ||
- `font`: FIGlet font to use. Defaults to `standard`. | ||
|
||
## Usage | ||
|
||
Here's an example configuration: | ||
|
||
```toml title="doteki.toml" | ||
[sections.ascii_art] | ||
plugin = "figlet" | ||
ascii_text = "hello" | ||
font = "isometric1" | ||
``` | ||
|
||
This will render the following: | ||
|
||
```text | ||
___ ___ ___ ___ ___ | ||
/\__\ /\ \ /\__\ /\__\ /\ \ | ||
/:/ / /::\ \ /:/ / /:/ / /::\ \ | ||
/:/__/ /:/\:\ \ /:/ / /:/ / /:/\:\ \ | ||
/::\ \ ___ /::\~\:\ \ /:/ / /:/ / /:/ \:\ \ | ||
/:/\:\ /\__\ /:/\:\ \:\__\ /:/__/ /:/__/ /:/__/ \:\__\ | ||
\/__\:\/:/ / \:\~\:\ \/__/ \:\ \ \:\ \ \:\ \ /:/ / | ||
\::/ / \:\ \:\__\ \:\ \ \:\ \ \:\ /:/ / | ||
/:/ / \:\ \/__/ \:\ \ \:\ \ \:\/:/ / | ||
/:/ / \:\__\ \:\__\ \:\__\ \::/ / | ||
\/__/ \/__/ \/__/ \/__/ \/__/ | ||
``` | ||
|
||
## Frequently Asked Questions | ||
|
||
### Where can I find a list of available fonts? | ||
|
||
Check the [FIGlet official site](http://www.figlet.org/examples.html) to explore the available fonts. |