-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
walid
committed
Sep 10, 2024
1 parent
def3985
commit 7654896
Showing
4 changed files
with
141 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import textwrap | ||
import hyphen | ||
import shutil | ||
|
||
def justify(text, width=None): | ||
if width is None: | ||
width = shutil.get_terminal_size().columns | ||
|
||
# Initialize the hyphenator | ||
h = hyphen.Hyphenator('en_US') | ||
|
||
# Split the text into words | ||
words = text.split() | ||
|
||
lines = [] | ||
current_line = [] | ||
current_length = 0 | ||
|
||
for word in words: | ||
if current_length + len(word) + len(current_line) <= width: | ||
current_line.append(word) | ||
current_length += len(word) | ||
else: | ||
if current_line: | ||
# Justify the current line | ||
spaces_needed = width - current_length | ||
gaps = len(current_line) - 1 | ||
if gaps > 0: | ||
space_per_gap = spaces_needed // gaps | ||
extra_spaces = spaces_needed % gaps | ||
justified_line = '' | ||
for i, w in enumerate(current_line): | ||
justified_line += w | ||
if i < gaps: | ||
justified_line += ' ' * (space_per_gap + (1 if i < extra_spaces else 0)) | ||
lines.append(justified_line) | ||
else: | ||
lines.append(current_line[0].ljust(width)) | ||
|
||
# Start a new line | ||
current_line = [word] | ||
current_length = len(word) | ||
|
||
# Check if hyphenation is needed | ||
while current_length > width: | ||
hyphenated = h.wrap(current_line[-1], width - current_length + len(current_line[-1])) | ||
if len(hyphenated) > 1: | ||
current_line[-1] = hyphenated[0] + '-' | ||
lines.append(' '.join(current_line).ljust(width)) | ||
current_line = [hyphenated[1]] | ||
current_length = len(hyphenated[1]) | ||
else: | ||
break | ||
|
||
# Add the last line without justification | ||
if current_line: | ||
lines.append(' '.join(current_line)) | ||
|
||
return '\n'.join(lines) | ||
|