-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.py
More file actions
188 lines (159 loc) · 6.04 KB
/
Copy pathbook.py
File metadata and controls
188 lines (159 loc) · 6.04 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import math
from typing import Optional, TextIO
from bookwidth import BookWidth
import uuid
class Book:
def __init__(
self,
title: str,
author: str,
pages: int,
book_width,
id: str = None
):
self.id = str(uuid.uuid4()) if id is None else id
self.title = title
self.author = author
self.pages = pages
if isinstance(book_width, int):
self.book_width = BookWidth(book_width)
else:
self.book_width = book_width
self.x_start = 6
self.prev_book: Optional['Book'] = None
self.next_book: Optional['Book'] = None
@property
def height_char(self) -> int:
"""Calculate book height based on pages."""
height = max(3, self.pages // 55)
if self.book_width == BookWidth.VERY_WIDE and height > 3:
height -= 1
return height
@property
def width_char(self) -> int:
"""Get book width based on width enum."""
width_map = {
BookWidth.SMALL: 15,
BookWidth.MEDIUM: 25,
BookWidth.WIDE: 35,
BookWidth.VERY_WIDE: 40
}
return width_map[self.book_width]
def to_dict(self) -> dict:
"""Convert book to dictionary for JSON serialization."""
return {
'id': self.id,
'title': self.title,
'author': self.author,
'pages': self.pages,
'book_width': self.book_width.value
}
@classmethod
def from_dict(cls, data: dict) -> 'Book':
"""Create book from dictionary."""
return cls(
id=data['id'],
title=data['title'],
author=data['author'],
pages=data['pages'],
book_width=BookWidth(data['book_width'])
)
def print_book(self, file: Optional[TextIO] = None):
"""Print the book visualization."""
if not self.next_book:
self._print_left_pad(file=file)
self._print_top(file=file)
self._print_middle(file=file)
if self.prev_book is None:
self._print_base(file=file)
else:
self._print_bottom(file=file)
def _print_left_pad(
self,
char: str = " ",
length: int = None,
file: TextIO = None,
):
"""Print left padding."""
pad_length = length if length is not None else self.x_start
print(char * pad_length, end="", file=file)
def _print_top(self, file: TextIO = None):
"""Print book top border."""
print("┌" + "─" * (self.width_char - 2) + "┐", file=file)
def _print_middle(self, file: TextIO = None):
"""Print book middle section with title and author."""
title_line = math.ceil(self.height_char / 2) - 2
author_line = math.ceil(self.height_char / 2) - 1
for i in range(self.height_char - 2):
self._print_left_pad(file=file)
print("│", end="", file=file)
if i == title_line:
content = self._center_text(self.title)
elif i == author_line:
content = self._center_text(self.author)
else:
content = " " * (self.width_char - 2)
print(content + "│", file=file)
def _center_text(self, text: str) -> str:
"""Center text within book width."""
max_width = self.width_char - 2
if len(text) > max_width:
text = text[:max_width - 3] + "..."
return text.center(max_width)
def _print_bottom(self, file: TextIO = None):
"""Print book bottom with connection to book below."""
if not self.prev_book:
return
prev = self.prev_book
smallest_start = min(prev.x_start, self.x_start)
self._print_left_pad(char=" ", length=smallest_start, file=file)
current_pos = smallest_start
if prev.x_start > self.x_start:
print("└", end="", file=file)
current_pos += 1
elif prev.x_start == self.x_start:
print("│", end="", file=file)
current_pos += 1
else:
print("┌", end="", file=file)
current_pos += 1
if self.x_start < prev.x_start:
for i in range(abs(prev.x_start - smallest_start - 1)):
print("─", end="", file=file)
current_pos += 1
print("┬", end="", file=file)
current_pos += 1
elif self.x_start > prev.x_start:
for i in range(abs(self.x_start - smallest_start - 1)):
print("─", end="", file=file)
current_pos += 1
print("┴", end="", file=file)
current_pos += 1
first_stop = min(prev.x_start + prev.width_char,
self.x_start + self.width_char)
while current_pos < first_stop - 1:
print("─", end="", file=file)
current_pos += 1
if self.x_start + self.width_char < prev.x_start + prev.width_char:
print("┴", end="", file=file)
current_pos += 1
elif self.x_start + self.width_char > prev.x_start + prev.width_char:
print("┬", end="", file=file)
current_pos += 1
largest_dist = max(self.x_start + self.width_char,
prev.x_start + prev.width_char)
while current_pos < largest_dist - 1:
print("─", end="", file=file)
current_pos += 1
if prev.x_start + prev.width_char > self.x_start + self.width_char:
print("┐", file=file)
elif prev.x_start + prev.width_char == self.x_start + self.width_char:
print("│", file=file)
else:
print("┘", file=file)
def _print_base(self, file: TextIO = None):
"""Print the base of the bookshelf."""
print("──────┴" + "─" * (self.width_char - 2) + "┴───────", file=file)
print("\\ " * 10, file=file)
def __str__(self) -> str:
return f"{self.title} by {self.author} ({self.pages} pages)"