diff --git a/src/Aur/PKGBUILD b/src/Aur/PKGBUILD index 002870d..8d2418c 100644 --- a/src/Aur/PKGBUILD +++ b/src/Aur/PKGBUILD @@ -6,7 +6,7 @@ pkgdesc="TermPDF Viewer is an open-source PDF file viewer designed to run in the arch=('x86_64') url="https://github.com/felipealfonsog/TermPDFViewer" license=('MIT') -depends=('python-pip' 'python-pymupdf') +depends=('python-pip' 'python-pymupdf' 'python-termcolor') source=("https://github.com/felipealfonsog/TermPDFViewer/archive/refs/tags/v.${pkgver}.tar.gz") sha256sums=('05d652e374019b617842c77d435f58c92da58529e033d3d29b254820c3fa03fb') diff --git a/src/termpdf.py b/src/termpdf.py index 99fa528..35de700 100755 --- a/src/termpdf.py +++ b/src/termpdf.py @@ -23,18 +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 - -* 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. @@ -53,37 +45,31 @@ ''' - - import os import fitz +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_pdf(pdf_filename): - doc = fitz.open(pdf_filename) - total_pages = doc.page_count - current_page = 0 - - while True: - page = doc[current_page] - print(f'Page {current_page + 1} / {total_pages}') - print(page.get_text()) - - choice = input("Enter 'b' to go back, 'f' to go forward, '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 +def display_current_page(doc, current_page, total_pages, keyword=None): + if not (0 <= current_page < total_pages): + print("Invalid page number.") + return + + page = doc[current_page] + 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) - doc.close() -''' - def display_pdf(pdf_filename): try: doc = fitz.open(pdf_filename) @@ -96,16 +82,36 @@ def display_pdf(pdf_filename): break page = doc[current_page] - print(f'Page {current_page + 1} / {total_pages}') - print(page.get_text()) + display_current_page(doc, current_page, total_pages) - choice = input("Enter 'b' to go back, 'f' to go forward, 'q' to quit: ") + 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(current_page, total_pages): + text = doc[page_num].get_text().lower() + if keyword in text: + display_current_page(doc, page_num, total_pages, keyword) + found = True + break + + if not found: + print(f"No matches found for '{keyword}'.") + else: + while True: + response = input("Press Enter to continue searching or press Esc to exit search: ") + if response.lower() == '': + break + elif response.lower() == 'esc': + return + except Exception as e: print(f"Error: {e}") finally: @@ -113,36 +119,63 @@ def display_pdf(pdf_filename): doc.close() + + def main(): - - print("\n") - print("▀▀█▀▀ █▀▀ █▀▀█ █▀▄▀█ ▒█▀▀█ ▒█▀▀▄ ▒█▀▀▀   ▒█░░▒█ ░▀░ █▀▀ █░░░█ █▀▀ █▀▀█") - print("░▒█░░ █▀▀ █▄▄▀ █░▀░█ ▒█▄▄█ ▒█░▒█ ▒█▀▀▀   ░▒█▒█░ ▀█▀ █▀▀ █▄█▄█ █▀▀ █▄▄▀") - print("░▒█░░ ▀▀▀ ▀░▀▀ ▀░░░▀ ▒█░░░ ▒█▄▄▀ ▒█░░░   ░░▀▄▀░ ▀▀▀ ▀▀▀ ░▀░▀░ ▀▀▀ ▀░▀▀") - print("-------------------------------------------------------------------------") - print("TermPDF Viewer - view and navigate PDF files within the terminal.") - print("-------------------------------------------------------------------------") - print("* Simple: TermPDF Viewer is a Python program that enables users to ") - print("* view and navigate PDF files directly within the terminal.") - print("-------------------------------------------------------------------------") - print("\n\n" + print("\n" + "▀▀█▀▀ █▀▀ █▀▀█ █▀▄▀█ ▒█▀▀█ ▒█▀▀▄ ▒█▀▀▀   ▒█░░▒█ ░▀░ █▀▀ █░░░█ █▀▀ █▀▀█\n" + "░▒█░░ █▀▀ █▄▄▀ █░▀░█ ▒█▄▄█ ▒█░▒█ ▒█▀▀▀   ░▒█▒█░ ▀█▀ █▀▀ █▄█▄█ █▀▀ █▄▄▀\n" + "░▒█░░ ▀▀▀ ▀░▀▀ ▀░░░▀ ▒█░░░ ▒█▄▄▀ ▒█░░░   ░░▀▄▀░ ▀▀▀ ▀▀▀ ░▀░▀░ ▀▀▀ ▀░▀▀\n" + "-------------------------------------------------------------------------\n" + "TermPDF Viewer - view and navigate PDF files within the terminal.\n" + "-------------------------------------------------------------------------\n" + "* Simple: TermPDF Viewer is a Python program that enables users to \n" + "* view and navigate PDF files directly within the terminal.\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: @@ -151,7 +184,7 @@ def main(): 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: @@ -160,7 +193,7 @@ def main(): 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 @@ -172,13 +205,15 @@ def main(): 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__': main() + + diff --git a/src/termpdf_unstable.py b/src/termpdf_unstable.py index de3284b..35de700 100755 --- a/src/termpdf_unstable.py +++ b/src/termpdf_unstable.py @@ -95,14 +95,22 @@ def display_pdf(pdf_filename): keyword = input("Enter the keyword to search: ").lower() found = False - for page_num in range(total_pages): + for page_num in range(current_page, total_pages): text = doc[page_num].get_text().lower() if keyword in text: display_current_page(doc, page_num, total_pages, keyword) found = True + break if not found: print(f"No matches found for '{keyword}'.") + else: + while True: + response = input("Press Enter to continue searching or press Esc to exit search: ") + if response.lower() == '': + break + elif response.lower() == 'esc': + return except Exception as e: print(f"Error: {e}")