Files
NexQuant/rdagent/utils/fmt.py
T
you-n-g df5d72bcf7 fix: add stdout context length setting and improve text shrinking logic (#559)
* feat: Add stdout context length setting and improve text shrinking logic

* lint
2025-02-06 23:00:15 +08:00

29 lines
739 B
Python

"""
Tools that support generating better formats.
"""
def shrink_text(text: str, context_lines: int = 200) -> str:
"""
When the context is too long, hide the part in the middle.
text before
... (XXXXX lines are hidden) ...
text after
"""
lines = text.splitlines()
total_lines = len(lines)
if total_lines <= context_lines:
return text
# Calculate how many lines to show from start and end
half_lines = context_lines // 2
start = "\n".join(lines[:half_lines])
end = "\n".join(lines[-half_lines:])
# Count the number of lines we're hiding
hidden_lines = total_lines - half_lines * 2
return f"{start}\n... ({hidden_lines} lines are hidden) ...\n{end}"