Skip to content

Commit

Permalink
style text so that the current line is brighter than the previous lines
Browse files Browse the repository at this point in the history
  • Loading branch information
RNRetailer committed Sep 7, 2024
1 parent 3564892 commit 3c81cff
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
54 changes: 52 additions & 2 deletions play_audio_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from epub_conversion.utils import open_book, convert_epub_to_lines
from multiprocessing import Process, current_process
from bs4 import BeautifulSoup
from termcolor import cprint
from gtts import gTTS
import time
import os
Expand All @@ -21,6 +22,18 @@

# CODE ------------------------------------------------------------

# pretty print
def pprint(text) -> None:
cprint(text, "light_grey", "on_black", attrs=["bold"])

print = pprint

def darken_previous_line(printable_lines) -> None:
sys.stdout.write("\033[F" * len(printable_lines)) # Cursor up one line

for printable_line in printable_lines:
cprint(printable_line, "dark_grey", "on_black", attrs=["bold"])

temp_mp3_filename = tempfile.NamedTemporaryFile().name
process = None

Expand Down Expand Up @@ -90,6 +103,35 @@ def load_progress(audio_book_filename):

return progress_file_obj.get(audio_book_filename, 1)

def get_lines_to_print(line_index_human_readable, text, chunk_length=75):
printable_lines = ['--']

text = f'{line_index_human_readable}: {text}'

current_line = []
current_line_length = 0

for word in text.split(' '):
word_length = len(word)
would_be_current_line_length = current_line_length + word_length + 1

if current_line_length + word_length <= chunk_length:
current_line_length = would_be_current_line_length
current_line.append(word)
else:
printable_lines.append(' '.join(current_line))
current_line = [word]
current_line_length = len(word)

if current_line:
printable_lines.append(' '.join(current_line))

return printable_lines

def print_lines(printable_lines):
for printable_line in printable_lines:
print(printable_line)

if __name__ == '__main__':
starting_line_index_human_readable = load_progress(book_location)

Expand Down Expand Up @@ -137,23 +179,31 @@ def load_progress(audio_book_filename):
for line_index, line in enumerate(lines):
line_index_human_readable = line_index + starting_line_index_human_readable

print(f'--\n{line_index_human_readable}: {line}\n')
printable_lines = get_lines_to_print(line_index_human_readable, line)

print_lines(printable_lines)

try:
call_read_sentence(line)
except Exception as e:
print(e)

darken_previous_line(printable_lines)

save_progress(book_location, line_index_human_readable)
else:
for line_index, line in enumerate(lines):
line_index_human_readable = line_index + starting_line_index_human_readable

print(f'--\n{line_index_human_readable}: {line}\n')
printable_lines = get_lines_to_print(line_index_human_readable, line)

print_lines(printable_lines)

try:
call_read_sentence(line)
except Exception as e:
print(e)

darken_previous_line(printable_lines)

graceful_exit(None, None)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ idna==3.8
requests==2.32.3
soupsieve==2.6
striprtf==0.0.26
termcolor==2.4.0
urllib3==2.2.2
xml-cleaner==2.0.4

0 comments on commit 3c81cff

Please sign in to comment.