Skip to content

Commit

Permalink
Merge pull request #95 from felipealfonsog/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
felipealfonsog authored Oct 27, 2023
2 parents ba1a236 + dfea431 commit bd5c1b2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/Aur/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
145 changes: 90 additions & 55 deletions src/termpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -96,53 +82,100 @@ 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:
if 'doc' in locals():
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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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()


10 changes: 9 additions & 1 deletion src/termpdf_unstable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down

0 comments on commit bd5c1b2

Please sign in to comment.