mirror of
https://github.com/NicolasBohn/NexQuant.git
synced 2026-08-09 21:10:56 +00:00
feat: Add line length limit to shrink_text function and settings (#715)
This commit is contained in:
+19
-5
@@ -3,19 +3,33 @@ Tools that support generating better formats.
|
||||
"""
|
||||
|
||||
|
||||
def shrink_text(text: str, context_lines: int = 200) -> str:
|
||||
def shrink_text(text: str, context_lines: int = 200, line_len: int = 5000) -> str:
|
||||
"""
|
||||
When the context is too long, hide the part in the middle.
|
||||
|
||||
text before
|
||||
... (XXXXX lines are hidden) ...
|
||||
text after
|
||||
>>> shrink_text("line1\\nline2\\nline3", context_lines=2, line_len=5)
|
||||
'line1\\n... (1 lines are hidden) ...\\nline3'
|
||||
|
||||
>>> shrink_text("short line", context_lines=2, line_len=5)
|
||||
'sh... (5 chars are hidden) ...ine'
|
||||
|
||||
>>> shrink_text("a" * 5010, context_lines=2, line_len=10)
|
||||
'aaaaa... (5000 chars are hidden) ...aaaaa'
|
||||
"""
|
||||
|
||||
lines = text.splitlines()
|
||||
total_lines = len(lines)
|
||||
|
||||
new_lines = []
|
||||
for line in lines:
|
||||
if len(line) > line_len:
|
||||
# If any line is longer than line_len, we can't shrink it
|
||||
line = f"{line[:line_len // 2]}... ({len(line) - line_len} chars are hidden) ...{line[- line_len + line_len // 2:]}"
|
||||
new_lines.append(line)
|
||||
lines = new_lines
|
||||
|
||||
if total_lines <= context_lines:
|
||||
return text
|
||||
return "\n".join(lines)
|
||||
|
||||
# Calculate how many lines to show from start and end
|
||||
half_lines = context_lines // 2
|
||||
|
||||
Reference in New Issue
Block a user