Skip to content

Instantly share code, notes, and snippets.

@DaKheera47
Last active January 17, 2023 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaKheera47/e031d2a57f742805ac5218abc3a6e209 to your computer and use it in GitHub Desktop.
Save DaKheera47/e031d2a57f742805ac5218abc3a6e209 to your computer and use it in GitHub Desktop.
# dak's text editor
import pyautogui as pag
from global_hotkeys import *
import time
import pyperclip
from simpleeval import simple_eval
DELAY = 0.4
IS_ALIVE = True
pag.PAUSE = 0
# upper
# lower
# title
# mocking
# wrap with string
# evaluate expression
# split in chunks
# do nothing
def startProcess():
# delay is required as a measure to prevent `ctrl + x` mixing with the hotkey entered
time.sleep(DELAY)
pag.hotkey("ctrl", "x")
time.sleep(0.1)
data = pyperclip.paste()
# remove evaluation if it's not a valid expression
operations = {
"uppercase": {"value": data.upper(), "display": "Upper Case"},
"lowercase": {"value": data.lower(), "display": "Lower Case"},
"titlecase": {"value": data.title(), "display": "Title Case"},
"mocking": {"value": mocking(data), "display": "Mocking"},
"wrapStr": {"value": f'{data}', "display": "Wrap with String"},
}
try:
data = data.replace("x", "*").replace("^", "**")
# if the expression is valid, add it to the operations
operations["evaluateExpression"] = {
"value": simple_eval(data),
"display": "Evaluate Expression"
}
except Exception as e:
print(e)
print("Invalid expression")
# creat noop
operations["noop"] = {"value": data, "display": "Do Nothing"}
displayString = f'''
What do you want to do with the selected text?
{data[:100]}
Number of characters: {len(data)}
Number of words: {len(data.split())}
'''
# add the options to the display string
options = []
for key, value in operations.items():
options.append(value["display"])
toType = pag.confirm(text=displayString, title='Format text in place!', buttons=options)
if toType == "Wrap with String":
# ask for the character to wrap the string with
toWrap = pag.prompt(text="Enter the character to wrap the string with", title="Wrap with character")
if toWrap is None:
toWrap = ""
out = f"{toWrap}{data}{toWrap}"
elif toType == "Evaluate Expression":
rounding = 0
# if the expression results in a float, ask for the number of decimals
if isinstance(operations["evaluateExpression"]["value"], float):
rounding = pag.prompt(text="How many decimals?", title="Evaluate Expression")
# check if operations["evaluateExpression"]["value"] is not string -> if it is, it's a string
if type(operations["evaluateExpression"]["value"]) != str:
out = round(operations["evaluateExpression"]["value"], int(rounding))
else:
out = operations["evaluateExpression"]["value"]
else:
ind = options.index(toType)
out = operations[list(operations.keys())[ind]]["value"]
pyperclip.copy(out)
pag.hotkey("ctrl", "v")
def mocking(data):
outputText = ""
i = 0
for char in data:
if char.isalpha():
i += 1
if i % 2 == 0:
outputText += char.upper()
else:
outputText += char.lower()
else:
outputText += char
return outputText
def exit_application():
global IS_ALIVE
stop_checking_hotkeys()
IS_ALIVE = False
BINDINGS = [
[["control", "shift", "alt", "x"], None, startProcess],
[["control", "shift", "alt", "9"], None, exit_application],
]
# Register all of our keybindings
register_hotkeys(BINDINGS)
# Finally, start listening for keypresses
start_checking_hotkeys()
while IS_ALIVE:
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment