Last active
April 14, 2025 04:47
-
-
Save ms3056/39970326e0d5ba8cf80235e11a48a524 to your computer and use it in GitHub Desktop.
Espanso Matches
This file contains hidden or 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
# Espanso trigger to generate a secure password of a given length. | |
# Type :pz(n) where n is the desired length. The password is also copied to the clipboard. | |
matches: | |
- regex: :pz\((?P<myvar>\d*)\) | |
replace: "{{output}}" | |
vars: | |
- name: output | |
type: script | |
params: | |
args: | |
- python | |
- -c | |
- | | |
# Import necessary modules for secure password generation. | |
import secrets, string | |
# Function to generate a secure password ensuring required character types. | |
def generate_password(length: int) -> str: | |
# Ensure the password has at least 4 characters. | |
if length < 4: | |
length = 4 | |
# Define character sets for each category. | |
lower = string.ascii_lowercase | |
upper = string.ascii_uppercase | |
digits = string.digits | |
punctuation = string.punctuation | |
# Guarantee at least one character from each category. | |
password_chars = [ | |
secrets.choice(lower), | |
secrets.choice(upper), | |
secrets.choice(digits), | |
secrets.choice(punctuation) | |
] | |
# Create a combined pool of all characters. | |
all_characters = lower + upper + digits + punctuation | |
# Fill the remaining length with random choices from the combined pool. | |
for _ in range(length - 4): | |
password_chars.append(secrets.choice(all_characters)) | |
# Securely shuffle the characters to randomize the positions. | |
secrets.SystemRandom().shuffle(password_chars) | |
return ''.join(password_chars) | |
# Attempt to convert the provided argument to an integer, defaulting to 12 if not valid. | |
try: | |
length = int('{{myvar}}') if '{{myvar}}'.isdigit() else 12 | |
except ValueError: | |
length = 12 | |
length = max(length, 4) | |
# Generate the password. | |
password = generate_password(length) | |
# Try copying the password to the clipboard using pyperclip. | |
try: | |
import pyperclip | |
pyperclip.copy(password) | |
except Exception: | |
pass | |
# Output the generated password. | |
print(password) |
This file contains hidden or 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
# Alternate trigger identical to the one above, using a different trigger keyword. | |
matches: | |
- trigger: :zz | |
replace: '{{output}}' | |
vars: | |
- name: zones | |
type: script | |
params: | |
args: | |
- python | |
- -c | |
- | | |
# Output all available timezones. | |
import pytz | |
print("\n".join(pytz.all_timezones)) | |
- name: zone_choice | |
type: form | |
params: | |
layout: 'Pick a time-zone: [[zone]]' | |
fields: | |
zone: | |
type: list | |
values: '{{zones}}' | |
default: Europe/London | |
- name: output | |
type: script | |
params: | |
args: | |
- python | |
- -c | |
- | | |
# Import the necessary libraries. | |
import pytz | |
from datetime import datetime, timezone | |
# Clean up the timezone string using .strip(). | |
tz_obj = pytz.timezone('{{zone_choice.zone}}'.strip()) | |
# Create a timezone-aware datetime object for the current UTC time. | |
utc_now = datetime.now(timezone.utc) | |
# Adjust the UTC datetime to the selected timezone. | |
local_time = utc_now.astimezone(tz_obj) | |
# Output the current date and time in the selected timezone. | |
print("Current date and time in", tz_obj, "is:", local_time.strftime('%F %T')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment