Last active
October 27, 2023 23:45
-
-
Save Curtis-64/522f496bd9a1ae803a594aa64ee90a27 to your computer and use it in GitHub Desktop.
Inline Hex and Decimal Eval
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
Inline Hex Calculator by Curtis White - Prompt Coder | |
Version: 1.1 | |
Update Notes: Now handles delete! Nicee!! | |
Features: | |
Inline calculations in real-time. | |
Supports both hexadecimal and decimal modes. | |
Debug mode for troubleshooting. | |
Resilient against typos: now handles backspace to correct mistakes. | |
How to Use: | |
Modes: | |
Decimal Mode: | |
Type .d followed by your decimal-based expression and finish it with ==. | |
Example: .d 15+1== will be replaced with 16. | |
Hexadecimal Mode: | |
Type .x followed by your hex-based expression and finish it with ==. | |
Example: .x 0xf+1== will be replaced with 0x10. | |
Keyboard Shortcuts: | |
Esc: Stops the program. | |
Backspace: Removes the last character in the buffer, allowing for corrections. | |
Developer Notes: | |
The program utilizes pynput for keyboard listening and pyautogui for simulating key presses. | |
Debug mode can be activated by setting the debug_mode flag to True. | |
Credits: | |
Main Developer: Curtis White | |
Partial Credit: OpenAI's GPT-4 for assistance | |
For more advanced features or bug fixes, feel free to contribute! | |
-------------------------------------------------------------------------------------- | |
from pynput import keyboard | |
import pyautogui | |
import re | |
# Initialize buffer to store typed characters | |
typed_buffer = "" | |
# Debug mode flag | |
debug_mode = False | |
def debug_print(msg): | |
if debug_mode: | |
print(f"Debug: {msg}") | |
# Function to handle key press events | |
def on_press(key): | |
global typed_buffer | |
try: | |
typed_buffer += key.char | |
except AttributeError: | |
if key == keyboard.Key.backspace: | |
typed_buffer = typed_buffer[:-1] | |
debug_print(f"Buffer: {typed_buffer}") | |
# Function to handle key release events | |
def on_release(key): | |
global typed_buffer | |
if key == keyboard.Key.esc: # Stop listener if 'esc' is pressed | |
return False | |
if '==' in typed_buffer: # Check for trigger sequence '==' | |
debug_print("Trigger sequence '==' detected.") | |
# Regex pattern to capture anything between '..' and '==' | |
pattern = re.compile(r'\.(.*?)==', re.S) | |
match = pattern.search(typed_buffer) | |
if match: | |
try: | |
expression_str = match.group(1) | |
mode = expression_str[0] | |
expression = expression_str[1:].strip() | |
if mode == 'x': | |
if expression.startswith("0x"): | |
expression = expression[2:] | |
if re.fullmatch(r'[a-fA-F0-9]+([\+\-\*/][a-fA-F0-9]+)*', expression): | |
expression = re.sub(r'([a-fA-F0-9]+)', r'0x\1', expression) | |
result = hex(eval(expression)) | |
else: | |
raise ValueError("Invalid hex expression") | |
elif mode == 'd': | |
result = eval(expression) | |
else: | |
raise ValueError("Invalid mode") | |
pyautogui.typewrite(f"{result} ", interval=0.01) | |
except Exception as e: | |
debug_print(f"Error: {e}") | |
pyautogui.typewrite(f"Error: {e} ", interval=0.01) | |
# Clear the buffer | |
typed_buffer = "" | |
# Setting up the listener | |
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener: | |
listener.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment