Don't write word-wrap escape sequences when stdout is not a terminal#16840
Open
markymarkburt wants to merge 1 commit into
Open
Don't write word-wrap escape sequences when stdout is not a terminal#16840markymarkburt wants to merge 1 commit into
markymarkburt wants to merge 1 commit into
Conversation
displayResponse fell back to an 80-column width when term.GetSize failed, so `ollama run model "prompt" > file` still took the word-wrap path and wrote cursor-control escape sequences into the redirected file. Only word-wrap on a real terminal; otherwise emit plain text.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
ollama run <model> "<prompt>" > out.txtwrites terminal escape sequences intoout.txt. The file ends up with cursor-control codes (\x1b[<n>D,\x1b[K) instead of clean text.Fixes #16785
Cause
displayResponseasks for the terminal width and, whenterm.GetSizefails (which is what happens when stdout is a file or pipe, not a TTY), falls back to an 80-column width:So even with no real terminal, the word-wrap branch was taken and its cursor-control sequences were written into the redirected output.
Fix
Word-wrapping only makes sense on a real terminal, so only take that branch when
term.GetSizesucceeds. When stdout is redirected, fall through to the existing plain-text branch — no escape sequences.Interactive terminal behavior is unchanged (
GetSizesucceeds there and returns the real width).Test
TestDisplayResponseNoEscapesWhenNotATTYredirects stdout to a pipe (not a TTY), renders content that would wrap on a terminal, and asserts the output is the plain text with no escape sequences. It fails before this change and passes after.