Skip to content

Commit

Permalink
Use monospaced font for Wifi QR and rjust/ljust text
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrgredowski committed Aug 27, 2023
1 parent 294ef65 commit a07e8d2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ dmypy.json
# Cython debug symbols
cython_debug/

.DS_store

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
Expand All @@ -162,3 +164,4 @@ cython_debug/

_output
_pdfs
/*.png
Binary file added _static/fonts/FiraCode-Regular.ttf
Binary file not shown.
Binary file modified _static/images/my_ssid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 25 additions & 4 deletions bucket_of_utils/qr/wifi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import enum
import os
from pathlib import Path
from typing import Annotated

Expand All @@ -24,7 +25,7 @@ class SecurityTypes(enum.StrEnum):


def get_font():
path_to_font = "_static/fonts/Lato-Regular.ttf"
path_to_font = "_static/fonts/FiraCode-Regular.ttf"
return ImageFont.truetype(path_to_font, 30)


Expand Down Expand Up @@ -87,6 +88,24 @@ def save_image(*, image: PIL.Image, image_file_name: str, directory_to_save: str
return str(path)


def get_formatted_ssid_and_password_texts(*, ssid: str, password: str):
ssid_prefix = "SSID"
password_prefix = "Password"

max_prefix_length = max(len(ssid_prefix), len(password_prefix))
ssid_prefix = ssid_prefix.rjust(max_prefix_length)
password_prefix = password_prefix.rjust(max_prefix_length)

ssid_text = f"{ssid_prefix}: {ssid}"
password_text = f"{password_prefix}: {password}"

max_text_length = max(len(ssid_text), len(password_text))
ssid_text = ssid_text.ljust(max_text_length)
password_text = password_text.ljust(max_text_length)

return ssid_text, password_text


def generate_qr_code( # noqa: PLR0913
ssid: Annotated[str, typer.Option(help="The SSID of the wifi network")],
password: Annotated[str, typer.Option(help="The password of the wifi network")],
Expand Down Expand Up @@ -121,19 +140,21 @@ def generate_qr_code( # noqa: PLR0913

font = get_font()

ssid_text = f"SSID: {ssid}"
ssid_text, password_text = get_formatted_ssid_and_password_texts(ssid=ssid, password=password)

image = make_image_fit_to_text(image, text=ssid_text, font=font)
image = add_text_to_image(image, text=ssid_text, y=20, font=font)

password_text = f"Password: {password}"

image = make_image_fit_to_text(image, text=password_text, font=font)
image = add_text_to_image(image, text=password_text, y=70, font=font)

image_file_name = f"{ssid.lower()}.png"

image = add_border_to_image(image, border=(5, 5, 5, 5), fill="black")

if str(os.environ.get("DEBUG")) == "1":
image.show()

path = save_image(
image=image,
image_file_name=image_file_name,
Expand Down

0 comments on commit a07e8d2

Please sign in to comment.