Last active
June 24, 2024 08:41
-
-
Save moeezm/63d24d8340860b740b4be8343cdb6a76 to your computer and use it in GitHub Desktop.
Simple code snippet searcher
This file contains 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
""" | |
Written entirely by Claude 3.5 Sonnet, I was just its manager | |
searches code snippets (or other information) in a text file (default: data.txt) | |
data.txt is formatted like | |
block 1 title | |
block 1 content line 1 | |
block 1 content line 2 | |
etc. | |
<blank line> | |
block 2 title | |
block 2 content line 1 | |
block 2 content line 2 | |
etc. | |
and so on. | |
the search is simple substring search over block titles, and you can copy the block content to your clipboard | |
If the script is run without any command line arguments, then a simple (and honestly cool) curses based searcher comes up | |
Otherwise the first command line argument is the search query | |
""" | |
import sys | |
import curses | |
import pyperclip | |
def read_file(filename): | |
blocks = [] | |
try: | |
with open(filename, 'r') as file: | |
current_block = {'title': '', 'content': []} | |
for line in file: | |
line = line.strip() | |
if not line and current_block['title']: | |
blocks.append(current_block) | |
current_block = {'title': '', 'content': []} | |
elif not current_block['title']: | |
current_block['title'] = line | |
else: | |
current_block['content'].append(line) | |
if current_block['title']: | |
blocks.append(current_block) | |
except Exception as e: | |
raise ValueError(f"Error parsing file: {str(e)}") | |
return blocks | |
def search_blocks(blocks, query): | |
for block in blocks: | |
if query.lower() in block['title'].lower(): | |
return block | |
return None | |
def copy_to_clipboard(content): | |
pyperclip.copy('\n'.join(content)) | |
def get_welcome_message(): | |
return [ | |
"Welcome to the Text Block Search Application!", | |
"", | |
"Instructions:", | |
"- Type to search for blocks by title", | |
"- Press Enter to toggle between Search and Command modes", | |
"- In Command mode, use 'q' to quit and 'c' to copy", | |
"", | |
"Start typing to begin your search..." | |
] | |
def terminal_mode(blocks, query): | |
result = search_blocks(blocks, query) | |
if result: | |
print(f"Title: {result['title']}") | |
print("Content:") | |
print('\n'.join(result['content'])) | |
user_input = input("Do you want to copy the content to clipboard? (y/n): ") | |
if user_input.lower() == 'y': | |
copy_to_clipboard(result['content']) | |
print("Content copied to clipboard.") | |
else: | |
print("No matching block found.") | |
def curses_mode(stdscr, blocks): | |
curses.curs_set(0) | |
stdscr.clear() | |
stdscr.refresh() | |
height, width = stdscr.getmaxyx() | |
search_win = curses.newwin(1, width, 0, 0) | |
content_win = curses.newwin(height - 2, width, 1, 0) | |
status_win = curses.newwin(1, width, height - 1, 0) | |
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE) | |
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK) | |
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_YELLOW) | |
search_active = True | |
search_query = "" | |
current_block = None | |
last_search_state = None | |
def update_status_bar(): | |
status_win.clear() | |
if search_active: | |
status_win.addstr(0, 0, "Search Mode | Enter: Toggle mode", curses.color_pair(2)) | |
else: | |
status_win.addstr(0, 0, "Command Mode | q: Quit, c: Copy, Enter: Toggle mode", curses.color_pair(2)) | |
status_win.refresh() | |
def display_content(): | |
content_win.clear() | |
if not search_query: | |
current_display = get_welcome_message() | |
elif current_block: | |
current_display = [f"Title: {current_block['title']}", ""] + current_block['content'] | |
else: | |
current_display = ["No matching block found."] | |
for i, line in enumerate(current_display): | |
if i < height - 2: | |
content_win.addstr(i, 0, line[:width-1]) | |
content_win.refresh() | |
def update_search_win(): | |
search_win.clear() | |
search_win.bkgd(' ', curses.color_pair(3) if search_active else curses.color_pair(2)) | |
search_win.addstr(0, 0, f"Search: {search_query}") | |
search_win.refresh() | |
# Initial draw | |
update_search_win() | |
display_content() | |
update_status_bar() | |
while True: | |
ch = stdscr.getch() | |
if ch == ord('\n'): | |
search_active = not search_active | |
elif search_active: | |
if ch in (curses.KEY_BACKSPACE, ord('\b'), 127): | |
search_query = search_query[:-1] | |
elif ch in range(32, 127): | |
search_query += chr(ch) | |
current_block = search_blocks(blocks, search_query) if search_query else None | |
elif ch == ord('q'): | |
break | |
elif ch == ord('c') and current_block: | |
copy_to_clipboard(current_block['content']) | |
status_win.clear() | |
status_win.addstr(0, 0, "Content copied to clipboard!", curses.color_pair(2)) | |
status_win.refresh() | |
curses.napms(1000) | |
current_search_state = (search_active, search_query) | |
if current_search_state != last_search_state: | |
update_search_win() | |
last_search_state = current_search_state | |
display_content() | |
update_status_bar() | |
def main(): | |
if len(sys.argv) > 2: | |
print("Usage: python script.py [search_query]") | |
sys.exit(1) | |
try: | |
blocks = read_file("data.txt") # Replace with your input file name | |
except FileNotFoundError: | |
print("Error: Input file not found.") | |
sys.exit(1) | |
except ValueError as e: | |
print(f"Error: {str(e)}") | |
sys.exit(1) | |
if len(sys.argv) == 2: | |
terminal_mode(blocks, sys.argv[1]) | |
else: | |
curses.wrapper(lambda stdscr: curses_mode(stdscr, blocks)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment