-
Notifications
You must be signed in to change notification settings - Fork 0
/
books.py
71 lines (55 loc) · 2.38 KB
/
books.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import subprocess
from blessed import Terminal
def list_supported_files(directory):
return [f for f in os.listdir(directory) if f.endswith(('.epub', '.pdf'))]
def is_japanese(text):
for part in text.split('.'):
for char in part:
if '\u3040' <= char <= '\u309F' or '\u30A0' <= char <= '\u30FF' or '\u4E00' <= char <= '\u9FFF':
return True
return False
def open_file(file):
if file.endswith('.epub') and is_japanese(file):
subprocess.run(['zathura', file])
elif file.endswith('.epub'):
subprocess.run(['epy', file])
elif file.endswith('.pdf') or is_japanese(file):
subprocess.run(['zathura', file])
def main():
term = Terminal()
directory = 'Documents/Books/' # Adjust the directory as per your setup
files = list_supported_files(directory)
with term.fullscreen(), term.cbreak(), term.hidden_cursor():
key = ''
index = 0
while key.lower() != 'q':
output = term.clear() + term.center(term.bold('Ebook Library')) + '\n\n'
# Calculate the number of rows that can fit in the terminal
max_rows = term.height - 4
# Calculate the start and end indexes for displaying files
start_index = max(0, index - max_rows // 2)
end_index = min(len(files), start_index + max_rows)
for i in range(start_index, end_index):
line = f'{i+1}. {files[i]}'
if len(line) > term.width:
# If the line is longer than the terminal width, truncate it and add ellipsis
line = line[:term.width - 3] + '…'
if i == index:
output += term.reverse(line) + '\n'
else:
output += line + '\n'
# Fill the remaining space at the bottom
output += '\n' * (term.height - len(output.split('\n')) - 1)
# Print the file list
print(output, end='', flush=True)
with term.cbreak():
key = term.inkey()
if key.name == 'KEY_DOWN':
index = min(index + 1, len(files) - 1)
elif key.name == 'KEY_UP':
index = max(index - 1, 0)
elif key == '\n':
open_file(os.path.join(directory, files[index]))
if __name__ == "__main__":
main()