Skip to content

Commit

Permalink
Small optimisations (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Jan 20, 2025
2 parents 422cd42 + 353b1d7 commit 81cf3de
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/em_keyboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
from __future__ import annotations

import argparse
import json
import os
import random
import re
import sys

from em_keyboard import _version
Expand Down Expand Up @@ -55,6 +52,8 @@ def try_copy_to_clipboard(text: str) -> bool:


def parse_emojis(filename: str | os.PathLike[str] = EMOJI_PATH) -> EmojiDict:
import json

return json.load(open(filename, encoding="utf-8"))


Expand All @@ -80,8 +79,7 @@ def do_find(lookup: EmojiDict, terms: tuple[str, ...]) -> list[tuple[str, str]]:

def clean_name(name: str) -> str:
"""Clean emoji name replacing specials chars by underscore"""
special_chars = "[-. ]" # square brackets are part of the regex
return re.sub(special_chars, "_", name).lower()
return name.replace("-", "_").replace(".", "_").replace(" ", "_").lower()


def cli() -> None:
Expand All @@ -101,13 +99,18 @@ def cli() -> None:
args = parser.parse_args()
no_copy = args.no_copy

if not args.name and not args.random:
sys.exit("Error: the 'name' argument is required")

# Grab the lookup dictionary.
lookup = parse_emojis()

if os.path.isfile(CUSTOM_EMOJI_PATH):
lookup.update(parse_emojis(CUSTOM_EMOJI_PATH))

if args.random:
import random

emoji, keywords = random.choice(list(lookup.items()))
name = keywords[0]
if not no_copy:
Expand All @@ -117,9 +120,6 @@ def cli() -> None:
print(f"Copied! {emoji} {name}" if copied else f"{emoji} {name}")
sys.exit(0)

if not args.name:
sys.exit("Error: the 'name' argument is required")

names = tuple(map(clean_name, args.name))

# Marker for if the given emoji isn't found.
Expand Down

0 comments on commit 81cf3de

Please sign in to comment.