Skip to content

Commit

Permalink
make linewrap still work when the terminal width has changed (ollama#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pdevine authored Dec 4, 2023
1 parent 1f126af commit 2113c9d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,17 @@ func generate(cmd *cobra.Command, opts generateOptions) error {

latest = response

if opts.WordWrap {
termWidth, _, _ = term.GetSize(int(os.Stdout.Fd()))
if opts.WordWrap && termWidth >= 10 {
for _, ch := range response.Response {
if currentLineLength+1 > termWidth-5 {
if len(wordBuffer) > termWidth-10 {
fmt.Printf("%s%c", wordBuffer, ch)
wordBuffer = ""
currentLineLength = 0
continue
}

// backtrack the length of the last word and clear to the end of the line
fmt.Printf("\x1b[%dD\x1b[K\n", len(wordBuffer))
fmt.Printf("%s%c", wordBuffer, ch)
Expand All @@ -543,7 +551,10 @@ func generate(cmd *cobra.Command, opts generateOptions) error {
}
}
} else {
fmt.Print(response.Response)
fmt.Printf("%s%s", wordBuffer, response.Response)
if len(wordBuffer) > 0 {
wordBuffer = ""
}
}

return nil
Expand Down

0 comments on commit 2113c9d

Please sign in to comment.