diff --git a/.DS_Store b/.DS_Store index af84f7d..6003272 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/src/termpdf_unstable.py b/src/termpdf_unstable.py index 88f8f24..de3284b 100755 --- a/src/termpdf_unstable.py +++ b/src/termpdf_unstable.py @@ -23,20 +23,10 @@ * You can install it using pip: pip install PyMuPDF * Make sure to include the appropriate model or adapt it for your needs. ************************************************* -* How to run the TermPDF Viewer: - -* Clone the TermPDF Viewer repository from GitHub. -* Navigate to the project directory: - -* cd TermPDF-Viewer * Install PyMuPDF library (if not already installed): pip install PyMuPDF - pip install pynput - - -* Run the TermPDF Viewer: -* python termpdf.py + pip install termcolor * The TermPDF Viewer will start, allowing you to: * Scan for PDF files in the current directory. @@ -55,65 +45,75 @@ ''' - - - import os import fitz -import curses +import re +from termcolor import colored def scan_pdf_files(): pdf_files = [file for file in os.listdir('.') if file.lower().endswith('.pdf')] return pdf_files -def display_current_page(stdscr, current_page, total_pages, doc): +def display_current_page(doc, current_page, total_pages, keyword=None): if not (0 <= current_page < total_pages): - stdscr.addstr("Invalid page number.") - stdscr.refresh() + print("Invalid page number.") return page = doc[current_page] - pdf_text = page.get_text() + print(f'Page {current_page + 1} / {total_pages}') + text = page.get_text() + + if keyword: + # Highlight occurrences of the keyword using regular expressions + highlighted_text = re.sub(rf'(?i)\b{re.escape(keyword)}\b', lambda match: colored(match.group(), 'red', attrs=['bold']), text) + print(highlighted_text) + else: + print(text) +def display_pdf(pdf_filename): try: - stdscr.addstr(f'Page {current_page + 1} / {total_pages}\n') - stdscr.addstr(pdf_text) - except curses.error: - lines = pdf_text.split('\n') - for line in lines: - stdscr.addstr(line[:curses.COLS - 1] + '\n') - - stdscr.refresh() - -def display_pdf(stdscr, pdf_filename): - doc = fitz.open(pdf_filename) - total_pages = doc.page_count - current_page = 0 - exit_viewer = False - - stdscr.clear() - stdscr.addstr("\n - Additionally, you can use the left arrow key to go back and the right arrow key to go forward.") - stdscr.addstr(" Note: Make sure to run the script in a terminal that supports arrow key input,") - stdscr.addstr(" and on some platforms, arrow keys might not work in certain terminal environments.\n") - - while not exit_viewer: - display_current_page(stdscr, current_page, total_pages, doc) - key = stdscr.getch() - - if key == curses.KEY_RIGHT: - current_page = min(current_page + 1, total_pages - 1) - elif key == curses.KEY_LEFT: - current_page = max(current_page - 1, 0) - elif key == ord('q'): - exit_viewer = True - - doc.close() - -def main(stdscr): - curses.curs_set(0) # Hide cursor - stdscr.clear() - stdscr.refresh() + doc = fitz.open(pdf_filename) + total_pages = doc.page_count + current_page = 0 + + while True: + if not (0 <= current_page < total_pages): + print("Invalid page number.") + break + + page = doc[current_page] + display_current_page(doc, current_page, total_pages) + + choice = input("Enter 'b' to go back, 'f' to go forward, 's' to search, 'q' to quit: ") + if choice == 'b': + current_page = max(current_page - 1, 0) + elif choice == 'f': + current_page = min(current_page + 1, total_pages - 1) + elif choice == 'q': + break + elif choice.lower() == 's': + keyword = input("Enter the keyword to search: ").lower() + found = False + + for page_num in range(total_pages): + text = doc[page_num].get_text().lower() + if keyword in text: + display_current_page(doc, page_num, total_pages, keyword) + found = True + + if not found: + print(f"No matches found for '{keyword}'.") + + except Exception as e: + print(f"Error: {e}") + finally: + if 'doc' in locals(): + doc.close() + + + +def main(): print("\n" "▀▀█▀▀ █▀▀ █▀▀█ █▀▄▀█ ▒█▀▀█ ▒█▀▀▄ ▒█▀▀▀   ▒█░░▒█ ░▀░ █▀▀ █░░░█ █▀▀ █▀▀█\n" "░▒█░░ █▀▀ █▄▄▀ █░▀░█ ▒█▄▄█ ▒█░▒█ ▒█▀▀▀   ░▒█▒█░ ▀█▀ █▀▀ █▄█▄█ █▀▀ █▄▄▀\n" @@ -124,24 +124,50 @@ def main(stdscr): "* Simple: TermPDF Viewer is a Python program that enables users to \n" "* view and navigate PDF files directly within the terminal.\n" "-------------------------------------------------------------------------\n" - "\n\n" "╭────────────-----────── TERMS OF USE ──────────----------───╮\n" "│ This software is licensed under the MIT License. │\n" "│ By Felipe Alfonso González - github.com/felipealfonsog │\n" "│ Computer Science Engineer - Email: f.alfonso@res-ear.ch │\n" "╰───────────────────────────────────────────────---------────╯\n" - "\n") + "-------------------------------------------------------------------------\n" + "* Prerequisites:\n" + "* Python 3.x: The program is written in Python and requires a Python 3.x interpreter to run.\n" + "* PyMuPDF: A Python binding for the MuPDF library, used to handle PDF file rendering and interaction.\n" + "* You can install it using pip: pip install PyMuPDF\n" + "* Make sure to include the appropriate model or adapt it for your needs.\n" + "-------------------------------------------------------------------------\n" + "* Important Notes:\n" + "\n" + "* Install PyMuPDF library (if not already installed):\n" + " pip install PyMuPDF\n" + " pip install termcolor\n" + "\n" + "* The TermPDF Viewer will start, allowing you to: \n" + "* Scan for PDF files in the current directory.\n" + "* Select a PDF file to view by entering its number.\n" + "* View the PDF with options to move back, forward, search, or return to the main menu.\n" + "* Quit and return to the main menu.\n" + "* To exit the TermPDF Viewer, use 'q' in the main menu.\n" + "* To search within the PDF, use 's' during viewing and enter the keyword to search.\n" + "-------------------------------------------------------------------------\n" + "* Important Notes:\n" + "* - The application has been tested on Linux and macOS.\n" + "* - For Windows, additional configurations may be required.\n" + "* - Make sure to fulfill the prerequisites before running the application.\n" + "* - For more information, please refer to the project documentation.\n" + "-------------------------------------------------------------------------\n" + ) print("Welcome to the TermPDF Viewer!") - + while True: print("\nMain Menu:") print("1. Scan for PDF files") print("2. View scanned PDF files") print("3. Quit") - + choice = input("Enter the number of your choice: ") - + if choice == '1': pdf_files = scan_pdf_files() if not pdf_files: @@ -150,7 +176,7 @@ def main(stdscr): print("Scanned PDF files:") for i, pdf_file in enumerate(pdf_files, start=1): print(f"{i}. {pdf_file}") - + elif choice == '2': pdf_files = scan_pdf_files() if not pdf_files: @@ -159,27 +185,27 @@ def main(stdscr): print("Scanned PDF files:") for i, pdf_file in enumerate(pdf_files, start=1): print(f"{i}. {pdf_file}") - + file_choice = input("Enter the number of the PDF file to view (or 'q' to go back): ") if file_choice == 'q': continue try: file_choice = int(file_choice) - 1 if 0 <= file_choice < len(pdf_files): - display_pdf(stdscr, pdf_files[file_choice]) + display_pdf(pdf_files[file_choice]) else: print("Invalid choice.") except ValueError: print("Invalid input. Please enter a valid number.") - + elif choice == '3': print("Goodbye!") break - + else: print("Invalid choice. Please enter a valid number.") if __name__ == '__main__': - curses.wrapper(main) + main()